Skip to content

Commit 87992e9

Browse files
Refactor: Decouple Bulti-In methods to a match pattern.
1 parent fc6fa86 commit 87992e9

2 files changed

Lines changed: 29 additions & 82 deletions

File tree

compiler/src/modules/vm/ops.rs

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -121,47 +121,7 @@ impl<'a> VM<'a> {
121121
HeapObj::BigInt(b) => b.to_decimal(),
122122
HeapObj::Type(name) => format!("<class '{}'>", name),
123123
HeapObj::Func(i, _, _) => format!("<function {}>", i),
124-
HeapObj::BoundMethod(_, id) => match id {
125-
BuiltinMethodId::ListAppend => "<built-in method append>".into(),
126-
BuiltinMethodId::DictKeys => "<built-in method keys>".into(),
127-
BuiltinMethodId::DictValues => "<built-in method values>".into(),
128-
BuiltinMethodId::DictItems => "<built-in method items>".into(),
129-
BuiltinMethodId::StrUpper => "<built-in method upper>".into(),
130-
BuiltinMethodId::StrLower => "<built-in method lower>".into(),
131-
BuiltinMethodId::StrStrip => "<built-in method strip>".into(),
132-
BuiltinMethodId::StrSplit => "<built-in method split>".into(),
133-
BuiltinMethodId::StrJoin => "<built-in method join>".into(),
134-
BuiltinMethodId::StrReplace => "<built-in method replace>".into(),
135-
BuiltinMethodId::StrStartswith => "<built-in method startswith>".into(),
136-
BuiltinMethodId::StrEndswith => "<built-in method endswith>".into(),
137-
BuiltinMethodId::StrFind => "<built-in method find>".into(),
138-
BuiltinMethodId::StrCount => "<built-in method count>".into(),
139-
BuiltinMethodId::ListSort => "<built-in method sort>".into(),
140-
BuiltinMethodId::ListReverse => "<built-in method reverse>".into(),
141-
BuiltinMethodId::ListPop => "<built-in method pop>".into(),
142-
BuiltinMethodId::ListInsert => "<built-in method insert>".into(),
143-
BuiltinMethodId::ListRemove => "<built-in method remove>".into(),
144-
BuiltinMethodId::ListIndex => "<built-in method index>".into(),
145-
BuiltinMethodId::ListCount => "<built-in method count>".into(),
146-
BuiltinMethodId::DictGet => "<built-in method get>".into(),
147-
BuiltinMethodId::DictUpdate => "<built-in method update>".into(),
148-
BuiltinMethodId::DictPop => "<built-in method pop>".into(),
149-
BuiltinMethodId::DictSetDefault => "<built-in method setdefault>".into(),
150-
BuiltinMethodId::StrLstrip => "<built-in method lstrip>".into(),
151-
BuiltinMethodId::StrRstrip => "<built-in method rstrip>".into(),
152-
BuiltinMethodId::StrIsDigit => "<built-in method isdigit>".into(),
153-
BuiltinMethodId::StrIsAlpha => "<built-in method isalpha>".into(),
154-
BuiltinMethodId::StrIsAlnum => "<built-in method isalnum>".into(),
155-
BuiltinMethodId::StrCapitalize => "<built-in method capitalize>".into(),
156-
BuiltinMethodId::StrTitle => "<built-in method title>".into(),
157-
BuiltinMethodId::StrCenter => "<built-in method center>".into(),
158-
BuiltinMethodId::StrZfill => "<built-in method zfill>".into(),
159-
BuiltinMethodId::ListExtend => "<built-in method extend>".into(),
160-
BuiltinMethodId::ListClear => "<built-in method clear>".into(),
161-
BuiltinMethodId::ListCopy => "<built-in method copy>".into(),
162-
},
163-
HeapObj::Slice(s, e, st) => format!("slice({}, {}, {})",
164-
self.display(*s), self.display(*e), self.display(*st)),
124+
HeapObj::Slice(s, e, st) => format!("slice({}, {}, {})", self.display(*s), self.display(*e), self.display(*st)),
165125
HeapObj::Range(s, e, st) => if *st == 1 {
166126
format!("range({}, {})", s, e)
167127
} else {
@@ -173,9 +133,8 @@ impl<'a> VM<'a> {
173133
} else {
174134
format!("({})", self.join_reprs(t.iter()))
175135
},
176-
HeapObj::Dict(d) => format!("{{{}}}", d.borrow().iter()
177-
.map(|(k, v)| format!("{}: {}", self.repr(k), self.repr(v)))
178-
.collect::<Vec<_>>().join(", ")),
136+
HeapObj::Dict(d) => format!("{{{}}}", d.borrow().iter().map(|(k, v)| format!("{}: {}", self.repr(k), self.repr(v))).collect::<Vec<_>>().join(", ")),
137+
HeapObj::BoundMethod(_, id) => format!("<built-in method {}>", id.name()),
179138
HeapObj::Set(s) => {
180139
let mut items: Vec<Val> = s.borrow().iter().cloned().collect();
181140
if items.is_empty() { return "set()".into(); }

compiler/src/modules/vm/types.rs

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -464,46 +464,34 @@ pub enum HeapObj {
464464
}
465465

466466
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
467-
pub enum BuiltinMethodId {
468-
ListAppend,
469-
DictKeys,
470-
DictValues,
471-
DictItems,
472-
StrUpper,
473-
StrLower,
474-
StrStrip,
475-
StrSplit,
476-
StrJoin,
477-
StrReplace,
478-
StrStartswith,
479-
StrEndswith,
480-
StrFind,
481-
StrCount,
482-
ListSort,
483-
ListReverse,
484-
ListPop,
485-
ListInsert,
486-
ListRemove,
487-
ListIndex,
488-
ListCount,
489-
DictGet,
490-
DictUpdate,
491-
DictPop,
492-
DictSetDefault,
493-
StrLstrip,
494-
StrRstrip,
495-
StrIsDigit,
496-
StrIsAlpha,
497-
StrIsAlnum,
498-
StrCapitalize,
499-
StrTitle,
500-
StrCenter,
501-
StrZfill,
502-
ListExtend,
503-
ListClear,
504-
ListCopy,
467+
pub enum BuiltinMethodId {
468+
ListAppend, DictKeys, DictValues, DictItems, StrUpper, StrLower, StrStrip,
469+
StrSplit, StrJoin, StrReplace, StrStartswith, StrEndswith, StrFind, StrCount,
470+
ListSort, ListReverse, ListPop, ListInsert, ListRemove, ListIndex, ListCount,
471+
DictGet, DictUpdate, DictPop, DictSetDefault, StrLstrip, StrRstrip,
472+
StrIsDigit, StrIsAlpha, StrIsAlnum, StrCapitalize, StrTitle, StrCenter,
473+
StrZfill, ListExtend, ListClear, ListCopy,
505474
}
506475

476+
impl BuiltinMethodId {
477+
const METHOD_NAMES: &'static [&'static str] = &[
478+
"append", "keys", "values", "items",
479+
"upper", "lower", "strip", "split", "join", "replace",
480+
"startswith", "endswith", "find", "count",
481+
"sort", "reverse", "pop", "insert", "remove", "index", "count",
482+
"get", "update", "pop", "setdefault",
483+
"lstrip", "rstrip", "isdigit", "isalpha", "isalnum",
484+
"capitalize", "title", "center", "zfill",
485+
"extend", "clear", "copy",
486+
];
487+
488+
#[inline]
489+
pub fn name(self) -> &'static str {
490+
Self::METHOD_NAMES[self as usize]
491+
}
492+
}
493+
494+
507495
/*
508496
DictMap
509497
Insertion-ordered dict backed by Vec with HashMap index for O(1) lookup.

0 commit comments

Comments
 (0)