aboutsummaryrefslogtreecommitdiff
path: root/model.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'model.cpp')
-rw-r--r--model.cpp116
1 files changed, 20 insertions, 96 deletions
diff --git a/model.cpp b/model.cpp
index 29f9e68..b652d47 100644
--- a/model.cpp
+++ b/model.cpp
@@ -7,14 +7,10 @@ FrameObj::FrameObj(ClassType _ftype) : ftype(_ftype) {}
EmptyList *empty_list = new EmptyList();
-EmptyList::EmptyList() : Cons(NULL, NULL) {}
+EmptyList::EmptyList() : Pair(NULL, NULL) {}
string EmptyList::ext_repr() { return string("()"); }
-#ifdef DEBUG
-string EmptyList::_debug_repr() { return ext_repr(); }
-#endif
-
bool FrameObj::is_ret_addr() {
return ftype & CLS_RET_ADDR;
}
@@ -23,9 +19,9 @@ bool FrameObj::is_parse_bracket() {
return ftype & CLS_PAR_BRA;
}
-EvalObj::EvalObj(ClassType _otype) : FrameObj(CLS_EVAL_OBJ), otype(_otype) {}
+EvalObj::EvalObj(int _otype) : FrameObj(CLS_EVAL_OBJ), otype(_otype) {}
-void EvalObj::prepare(Cons *pc) {}
+void EvalObj::prepare(Pair *pc) {}
bool EvalObj::is_simple_obj() {
return otype & CLS_SIM_OBJ;
@@ -56,95 +52,43 @@ ClassType EvalObj::get_otype() {
return otype;
}
-#ifdef DEBUG
-string EvalObj::_debug_repr() {
- return ext_repr();
-}
-void EvalObj::_debug_print() {
- printf("mem: 0x%llX\n%s\n\n", (unsigned long long)this,
- _debug_repr().c_str());
-}
-#endif
-
bool EvalObj::is_true() {
return true;
}
-Cons::Cons(EvalObj *_car, EvalObj *_cdr) :
- EvalObj(CLS_CONS_OBJ), car(_car), cdr(_cdr), skip(false),
- next(NULL) {}
-
-string Cons::ext_repr() {
- string res = "(";
- EvalObj *ptr = this;
- for (;ptr != empty_list && ptr->is_cons_obj();
- ptr = TO_CONS(ptr)->cdr)
- res += TO_CONS(ptr)->car->ext_repr() + " ";
- if (ptr == empty_list)
- res[res.length() - 1] = ')';
- else
- res += ". " + ptr->ext_repr() + ")";
- return res;
-}
-
-#ifdef DEBUG
-string Cons::_debug_repr() { return ext_repr(); }
-
-void Cons::_debug_print() {
- printf("mem: 0x%llX (0x%llX . 0x%llX) | 0x%llX\n%s\n",
- (unsigned long long)this,
- (unsigned long long)car,
- (unsigned long long)cdr,
- (unsigned long long)next,
- ("car: " + car -> ext_repr() + "\n" + \
- "cdr: " + cdr -> ext_repr() + "\n").c_str());
+string EvalObj::ext_repr() {
}
-#endif
-RetAddr::RetAddr(Cons *_addr) : FrameObj(CLS_RET_ADDR), addr(_addr) {}
+Pair::Pair(EvalObj *_car, EvalObj *_cdr) :
+ EvalObj(CLS_CONS_OBJ), car(_car), cdr(_cdr), skip(false),
+ next(NULL) {}
-#ifdef DEBUG
-string RetAddr::_debug_repr() { return string("#<Return Address>"); }
-#endif
+RetAddr::RetAddr(Pair *_addr) : FrameObj(CLS_RET_ADDR), addr(_addr) {}
ParseBracket::ParseBracket(unsigned char _btype) :
FrameObj(CLS_SIM_OBJ | CLS_PAR_BRA), btype(_btype) {}
-#ifdef DEBUG
-string ParseBracket::_debug_repr() {
- return string("#<Bracket>");
-}
-#endif
-
UnspecObj::UnspecObj() : EvalObj(CLS_SIM_OBJ) {}
string UnspecObj::ext_repr() { return string("#<Unspecified>"); }
-#ifdef DEBUG
-string UnspecObj::_debug_repr() { return ext_repr(); }
-#endif
-
SymObj::SymObj(const string &str) :
EvalObj(CLS_SIM_OBJ | CLS_SYM_OBJ), val(str) {}
string SymObj::ext_repr() { return val; }
-#ifdef DEBUG
-string SymObj::_debug_repr() { return "#<Symbol: " + val + ">"; }
-#endif
-
OptObj::OptObj() : EvalObj(CLS_SIM_OBJ | CLS_OPT_OBJ) {}
-ProcObj::ProcObj(ASTList *_body,
+ProcObj::ProcObj(Pair *_body,
Environment *_envt,
EvalObj *_params) :
OptObj(), body(_body), envt(_envt), params(_params) {}
-Cons *ProcObj::call(ArgList *args, Environment * &genvt,
+Pair *ProcObj::call(ArgList *args, Environment * &genvt,
Continuation * &cont, FrameObj ** &top_ptr) {
// Create a new continuation
// static_cast see `call` invocation in eval.cpp
- Cons *ret_addr = static_cast<RetAddr*>(*top_ptr)->addr;
+ Pair *ret_addr = static_cast<RetAddr*>(*top_ptr)->addr;
Continuation *_cont = new Continuation(genvt, ret_addr, cont, body);
// Create local env and recall the closure
Environment *_envt = new Environment(envt);
@@ -152,12 +96,12 @@ Cons *ProcObj::call(ArgList *args, Environment * &genvt,
EvalObj *ppar, *nptr;
for (ppar = params;
ppar->is_cons_obj();
- ppar = TO_CONS(ppar)->cdr)
+ ppar = TO_PAIR(ppar)->cdr)
{
if ((nptr = args->cdr) != empty_list)
- args = TO_CONS(nptr);
+ args = TO_PAIR(nptr);
else break;
- _envt->add_binding(static_cast<SymObj*>(TO_CONS(ppar)->car), args->car);
+ _envt->add_binding(static_cast<SymObj*>(TO_PAIR(ppar)->car), args->car);
}
if (ppar->is_sym_obj())
@@ -173,10 +117,6 @@ Cons *ProcObj::call(ArgList *args, Environment * &genvt,
string ProcObj::ext_repr() { return string("#<Procedure>"); }
-#ifdef DEBUG
-string ProcObj::_debug_repr() { return ext_repr(); }
-#endif
-
SpecialOptObj::SpecialOptObj(string _name) : OptObj(), name(_name) {}
BoolObj::BoolObj(bool _val) : EvalObj(CLS_SIM_OBJ | CLS_BOOL_OBJ), val(_val) {}
@@ -225,7 +165,7 @@ string CharObj::ext_repr() {
return "#\\" + val;
}
-VecObj::VecObj() : EvalObj(CLS_SIM_OBJ) {}
+VecObj::VecObj() : EvalObj(CLS_SIM_OBJ | CLS_VECT_OBJ) {}
void VecObj::resize(int new_size) {
vec.resize(new_size);
@@ -235,18 +175,6 @@ void VecObj::push_back(EvalObj *new_elem) {
vec.push_back(new_elem);
}
-string VecObj::ext_repr() {
- string res = "#(";
- for (EvalObjVec::iterator it = vec.begin(); it != vec.end(); it++)
- res += (*it)->ext_repr() + " ";
- if (vec.begin() == vec.end())
- res += ')';
- else
- res[res.length() - 1] = ')';
- return res;
-}
-
-
StrObj *StrObj::from_string(string repr) {
size_t len = repr.length();
if (repr[0] == '\"' && repr[len - 1] == '\"')
@@ -257,11 +185,11 @@ StrObj *StrObj::from_string(string repr) {
BuiltinProcObj::BuiltinProcObj(BuiltinProc f, string _name) :
OptObj(), handler(f), name(_name) {}
-Cons *BuiltinProcObj::call(ArgList *args, Environment * &envt,
+Pair *BuiltinProcObj::call(ArgList *args, Environment * &envt,
Continuation * &cont, FrameObj ** &top_ptr) {
- Cons *ret_addr = static_cast<RetAddr*>(*top_ptr)->addr;
- *top_ptr++ = handler(TO_CONS(args->cdr), name);
+ Pair *ret_addr = static_cast<RetAddr*>(*top_ptr)->addr;
+ *top_ptr++ = handler(TO_PAIR(args->cdr), name);
return ret_addr->next; // Move to the next instruction
}
@@ -269,10 +197,6 @@ string BuiltinProcObj::ext_repr() {
return "#<Builtin Procedure: " + name + ">";
}
-#ifdef DEBUG
-string BuiltinProcObj::_debug_repr() { return ext_repr(); }
-#endif
-
Environment::Environment(Environment *_prev_envt) : prev_envt(_prev_envt) {}
bool Environment::add_binding(SymObj *sym_obj, EvalObj *eval_obj, bool def) {
@@ -296,8 +220,8 @@ EvalObj *Environment::get_obj(EvalObj *obj) {
throw TokenError(name, RUN_ERR_UNBOUND_VAR);
}
-Continuation::Continuation(Environment *_envt, Cons *_pc,
+Continuation::Continuation(Environment *_envt, Pair *_pc,
Continuation *_prev_cont,
- ASTList *_proc_body) :
+ Pair *_proc_body) :
envt(_envt), pc(_pc), prev_cont(_prev_cont),
proc_body(_proc_body) {}