From 2c9f05a38ef64cbaeeae6429cd0e245c27944660 Mon Sep 17 00:00:00 2001 From: Teddy Date: Thu, 8 Aug 2013 18:27:03 +0800 Subject: rectified some names --- builtin.cpp | 200 ++++++++++++++++++++++++++++++------------------------------ builtin.h | 30 ++++----- eval.cpp | 24 +++----- eval.h | 2 +- main.cpp | 12 +--- model.cpp | 116 ++++++----------------------------- model.h | 98 +++++++++++------------------ parser.cpp | 20 +++--- parser.h | 4 +- 9 files changed, 196 insertions(+), 310 deletions(-) diff --git a/builtin.cpp b/builtin.cpp index 491d1ae..6579e98 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -19,7 +19,7 @@ static const int NUM_LVL_INT = 3; #define ARGS_EXACTLY_TWO \ if (args == empty_list || \ args->cdr == empty_list || \ - TO_CONS(args->cdr)->cdr != empty_list) \ + TO_PAIR(args->cdr)->cdr != empty_list) \ throw TokenError(name, RUN_ERR_WRONG_NUM_OF_ARGS) #define ARGS_EXACTLY_ONE \ @@ -31,12 +31,12 @@ static const int NUM_LVL_INT = 3; if (args == empty_list) \ throw TokenError(name, RUN_ERR_WRONG_NUM_OF_ARGS) -bool is_list(Cons *ptr) { +bool is_list(Pair *ptr) { if (ptr == empty_list) return true; EvalObj *nptr; for (;;) if ((nptr = ptr->cdr)->is_cons_obj()) - ptr = TO_CONS(nptr); + ptr = TO_PAIR(nptr); else break; return ptr->cdr == empty_list; } @@ -539,13 +539,13 @@ string IntNumObj::ext_repr() { SpecialOptIf::SpecialOptIf() : SpecialOptObj("if") {} -void SpecialOptIf::prepare(Cons *pc) { +void SpecialOptIf::prepare(Pair *pc) { #define IF_EXP_ERR \ throw TokenError(name, RUN_ERR_WRONG_NUM_OF_ARGS) state = 0; // Prepared if (pc->cdr->is_cons_obj()) - pc = TO_CONS(pc->cdr); + pc = TO_PAIR(pc->cdr); else IF_EXP_ERR; // if (pc == empty_list) @@ -554,7 +554,7 @@ void SpecialOptIf::prepare(Cons *pc) { pc->skip = false; if (pc->cdr->is_cons_obj()) - pc = TO_CONS(pc->cdr); + pc = TO_PAIR(pc->cdr); else IF_EXP_ERR; // if (pc == empty_list) @@ -565,8 +565,8 @@ void SpecialOptIf::prepare(Cons *pc) { { if (pc->cdr->is_cons_obj()) { - TO_CONS(pc->cdr)->skip = true; - if (TO_CONS(pc->cdr)->cdr != empty_list) + TO_PAIR(pc->cdr)->skip = true; + if (TO_PAIR(pc->cdr)->cdr != empty_list) IF_EXP_ERR; } else @@ -574,41 +574,41 @@ void SpecialOptIf::prepare(Cons *pc) { } } -void SpecialOptIf::pre_call(ArgList *args, Cons *pc, +void SpecialOptIf::pre_call(ArgList *args, Pair *pc, Environment *envt) { // static_cast because it's a call invocation - pc = TO_CONS(TO_CONS(pc->car)->cdr); + pc = TO_PAIR(TO_PAIR(pc->car)->cdr); // Condition evaluated and the decision is made state = 1; - if (TO_CONS(args->cdr)->car->is_true()) + if (TO_PAIR(args->cdr)->car->is_true()) { pc->skip = true; - pc = TO_CONS(pc->cdr); + pc = TO_PAIR(pc->cdr); pc->skip = false; if (pc->cdr != empty_list) - TO_CONS(pc->cdr)->skip = true; // Eval the former + TO_PAIR(pc->cdr)->skip = true; // Eval the former } else { pc->skip = true; - pc = TO_CONS(pc->cdr); + pc = TO_PAIR(pc->cdr); pc->skip = true; if (pc->cdr != empty_list) - TO_CONS(pc->cdr)->skip = false; //Eval the latter + TO_PAIR(pc->cdr)->skip = false; //Eval the latter } } -EvalObj *SpecialOptIf::post_call(ArgList *args, Cons *pc, +EvalObj *SpecialOptIf::post_call(ArgList *args, Pair *pc, Environment *envt) { // Value already evaluated, so just return it - return TO_CONS(args->cdr)->car; + return TO_PAIR(args->cdr)->car; } -Cons *SpecialOptIf::call(ArgList *args, Environment * &envt, +Pair *SpecialOptIf::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { - Cons *ret_addr = static_cast(*top_ptr)->addr; + Pair *ret_addr = static_cast(*top_ptr)->addr; if (state) { *top_ptr++ = post_call(args, ret_addr, envt); @@ -620,7 +620,7 @@ Cons *SpecialOptIf::call(ArgList *args, Environment * &envt, top_ptr += 2; // Undo pop and invoke again // static_cast because it's a call invocation - return TO_CONS(ret_addr->car)->next; + return TO_PAIR(ret_addr->car)->next; } } @@ -631,11 +631,11 @@ SpecialOptLambda::SpecialOptLambda() : SpecialOptObj("lambda") {} do \ { \ EvalObj *nptr; \ - Cons *ptr; \ + Pair *ptr; \ for (ptr = pc;;) \ { \ if ((nptr = ptr->cdr)->is_cons_obj()) \ - ptr = TO_CONS(nptr); \ + ptr = TO_PAIR(nptr); \ else break; \ ptr->skip = flag; \ } \ @@ -656,11 +656,11 @@ do \ { \ if (p == empty_list) break; \ EvalObj *nptr; \ - Cons *ptr; \ - for (ptr = TO_CONS(p);;) \ + Pair *ptr; \ + for (ptr = TO_PAIR(p);;) \ { \ if ((nptr = ptr->cdr)->is_cons_obj()) \ - ptr = TO_CONS(nptr); \ + ptr = TO_PAIR(nptr); \ else break; \ CHECK_SYMBOL(ptr->car); \ } \ @@ -670,26 +670,26 @@ do \ while (0) -void SpecialOptLambda::prepare(Cons *pc) { +void SpecialOptLambda::prepare(Pair *pc) { // Do not evaluate anything FILL_MARKS(pc, true); } -Cons *SpecialOptLambda::call(ArgList *args, Environment * &envt, +Pair *SpecialOptLambda::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { - Cons *ret_addr = static_cast(*top_ptr)->addr; - Cons *pc = static_cast(ret_addr->car); + Pair *ret_addr = static_cast(*top_ptr)->addr; + Pair *pc = static_cast(ret_addr->car); // TODO: remove the following two lines? if (pc->cdr == empty_list) throw TokenError(name, SYN_ERR_EMPTY_PARA_LIST); - if (TO_CONS(pc->cdr)->cdr == empty_list) + if (TO_PAIR(pc->cdr)->cdr == empty_list) throw TokenError(name, SYN_ERR_MISS_OR_EXTRA_EXP); // Clear the flag to avoid side-effects (e.g. proc calling) FILL_MARKS(pc, false); - pc = TO_CONS(pc->cdr); // Now pointintg to params + pc = TO_PAIR(pc->cdr); // Now pointintg to params if (pc->car->is_simple_obj()) CHECK_SYMBOL(pc->car); else @@ -698,8 +698,8 @@ Cons *SpecialOptLambda::call(ArgList *args, Environment * &envt, // store a list of expressions inside - ASTList *body = TO_CONS(pc->cdr); // Truncate the expression list - for (Cons *ptr = body; ptr != empty_list; ptr = TO_CONS(ptr->cdr)) + Pair *body = TO_PAIR(pc->cdr); // Truncate the expression list + for (Pair *ptr = body; ptr != empty_list; ptr = TO_PAIR(ptr->cdr)) ptr->next = NULL; // Make each expression an orphan *top_ptr++ = new ProcObj(body, envt, params); @@ -710,40 +710,40 @@ string SpecialOptLambda::ext_repr() { return string("#"); SpecialOptDefine::SpecialOptDefine() : SpecialOptObj("define") {} -void SpecialOptDefine::prepare(Cons *pc) { +void SpecialOptDefine::prepare(Pair *pc) { if (!pc->cdr->is_cons_obj()) throw TokenError(name, RUN_ERR_WRONG_NUM_OF_ARGS); - if (TO_CONS(pc->cdr)->car->is_simple_obj()) // Simple value assignment + if (TO_PAIR(pc->cdr)->car->is_simple_obj()) // Simple value assignment { - pc = TO_CONS(pc->cdr); + pc = TO_PAIR(pc->cdr); if (!pc->cdr->is_cons_obj()) throw TokenError(name, RUN_ERR_WRONG_NUM_OF_ARGS); pc->skip = true; // Skip the identifier - TO_CONS(pc->cdr)->skip = false; + TO_PAIR(pc->cdr)->skip = false; } // Procedure definition else FILL_MARKS(pc, true); // Skip all parts } -Cons *SpecialOptDefine::call(ArgList *args, Environment * &envt, +Pair *SpecialOptDefine::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { - Cons *ret_addr = static_cast(*top_ptr)->addr; - Cons *pc = static_cast(ret_addr->car); + Pair *ret_addr = static_cast(*top_ptr)->addr; + Pair *pc = static_cast(ret_addr->car); EvalObj *obj; SymObj *id; - EvalObj *first = TO_CONS(pc->cdr)->car; + EvalObj *first = TO_PAIR(pc->cdr)->car; if (first->is_simple_obj()) { if (!first->is_sym_obj()) throw TokenError(first->ext_repr(), SYN_ERR_NOT_AN_ID); ARGS_EXACTLY_TWO; id = static_cast(first); - obj = TO_CONS(args->cdr)->car; + obj = TO_PAIR(args->cdr)->car; } else { // static_cast because of is_simple_obj() is false - Cons *plst = static_cast(first); + Pair *plst = static_cast(first); if (plst == empty_list) throw TokenError(name, SYN_ERR_EMPTY_PARA_LIST); @@ -758,12 +758,12 @@ Cons *SpecialOptDefine::call(ArgList *args, Environment * &envt, // Clear the flag to avoid side-effects (e.g. proc calling) FILL_MARKS(pc, false); - ASTList *body = TO_CONS(TO_CONS(pc->cdr)->cdr); // Truncate the expression list + Pair *body = TO_PAIR(TO_PAIR(pc->cdr)->cdr); // Truncate the expression list if (body == empty_list) throw TokenError(name, SYN_ERR_MISS_OR_EXTRA_EXP); - for (Cons *ptr = body; ptr != empty_list; ptr = TO_CONS(ptr->cdr)) + for (Pair *ptr = body; ptr != empty_list; ptr = TO_PAIR(ptr->cdr)) ptr->next = NULL; // Make each expression a orphan obj = new ProcObj(body, envt, params); @@ -775,27 +775,27 @@ Cons *SpecialOptDefine::call(ArgList *args, Environment * &envt, string SpecialOptDefine::ext_repr() { return string("#"); } -void SpecialOptSet::prepare(Cons *pc) { +void SpecialOptSet::prepare(Pair *pc) { if (!pc->cdr->is_cons_obj()) throw TokenError(name, RUN_ERR_WRONG_NUM_OF_ARGS); - pc = TO_CONS(pc->cdr); + pc = TO_PAIR(pc->cdr); pc->skip = true; // Skip the identifier if (!pc->cdr->is_cons_obj()) throw TokenError(name, RUN_ERR_WRONG_NUM_OF_ARGS); - pc = TO_CONS(pc->cdr); + pc = TO_PAIR(pc->cdr); pc->skip = false; } -Cons *SpecialOptSet::call(ArgList *args, Environment * &envt, +Pair *SpecialOptSet::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { - Cons *ret_addr = static_cast(*top_ptr)->addr; - Cons *pc = static_cast(ret_addr->car); - EvalObj *first = TO_CONS(pc->cdr)->car; + Pair *ret_addr = static_cast(*top_ptr)->addr; + Pair *pc = static_cast(ret_addr->car); + EvalObj *first = TO_PAIR(pc->cdr)->car; if (!first->is_sym_obj()) throw TokenError(first->ext_repr(), SYN_ERR_NOT_AN_ID); @@ -803,7 +803,7 @@ Cons *SpecialOptSet::call(ArgList *args, Environment * &envt, SymObj *id = static_cast(first); - bool flag = envt->add_binding(id, TO_CONS(args->cdr)->car, false); + bool flag = envt->add_binding(id, TO_PAIR(args->cdr)->car, false); if (!flag) throw TokenError(id->ext_repr(), RUN_ERR_UNBOUND_VAR); *top_ptr++ = new UnspecObj(); return ret_addr->next; @@ -815,18 +815,18 @@ string SpecialOptSet::ext_repr() { return string("#"); } SpecialOptQuote::SpecialOptQuote() : SpecialOptObj("quote") {} -void SpecialOptQuote::prepare(Cons *pc) { +void SpecialOptQuote::prepare(Pair *pc) { // Do not evaluate anything FILL_MARKS(pc, true); } -Cons *SpecialOptQuote::call(ArgList *args, Environment * &envt, +Pair *SpecialOptQuote::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { - Cons *ret_addr = static_cast(*top_ptr)->addr; - Cons *pc = static_cast(ret_addr->car); + Pair *ret_addr = static_cast(*top_ptr)->addr; + Pair *pc = static_cast(ret_addr->car); // revert FILL_MARKS(pc, false); - *top_ptr++ = TO_CONS(pc->cdr)->car; + *top_ptr++ = TO_PAIR(pc->cdr)->car; return ret_addr->next; } @@ -834,27 +834,27 @@ string SpecialOptQuote::ext_repr() { return string("#"); } SpecialOptEval::SpecialOptEval() : SpecialOptObj("eval") {} -void SpecialOptEval::prepare(Cons *pc) { +void SpecialOptEval::prepare(Pair *pc) { state = 0; } -Cons *SpecialOptEval::call(ArgList *args, Environment * &envt, +Pair *SpecialOptEval::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { if (args->cdr == empty_list || - TO_CONS(args->cdr)->cdr != empty_list) + TO_PAIR(args->cdr)->cdr != empty_list) throw TokenError(name, RUN_ERR_WRONG_NUM_OF_ARGS); - Cons *ret_addr = static_cast(*top_ptr)->addr; - Cons *pc = static_cast(ret_addr->car); + Pair *ret_addr = static_cast(*top_ptr)->addr; + Pair *pc = static_cast(ret_addr->car); if (state) { - *top_ptr++ = TO_CONS(args->cdr)->car; + *top_ptr++ = TO_PAIR(args->cdr)->car; return ret_addr->next; // Move to the next instruction } else { state = 1; top_ptr += 2; - return TO_CONS(args->cdr); + return TO_PAIR(args->cdr); } } @@ -862,7 +862,7 @@ string SpecialOptEval::ext_repr() { return string("#"); } BUILTIN_PROC_DEF(make_pair) { ARGS_EXACTLY_TWO; - return new Cons(args->car, TO_CONS(args->cdr)->car); + return new Pair(args->car, TO_PAIR(args->cdr)->car); } BUILTIN_PROC_DEF(pair_car) { @@ -870,7 +870,7 @@ BUILTIN_PROC_DEF(pair_car) { if (!args->car->is_cons_obj()) throw TokenError("pair", RUN_ERR_WRONG_TYPE); - return TO_CONS(args->car)->car; + return TO_PAIR(args->car)->car; } BUILTIN_PROC_DEF(pair_cdr) { @@ -878,7 +878,7 @@ BUILTIN_PROC_DEF(pair_cdr) { if (!args->car->is_cons_obj()) throw TokenError("pair", RUN_ERR_WRONG_TYPE); - return TO_CONS(args->car)->cdr; + return TO_PAIR(args->car)->cdr; } BUILTIN_PROC_DEF(make_list) { @@ -890,7 +890,7 @@ BUILTIN_PROC_DEF(make_list) { BUILTIN_PROC_DEF(num_add) { ARGS_AT_LEAST_ONE; NumObj *res = new IntNumObj(0), *opr; // the most accurate type - for (;args != empty_list; args = TO_CONS(args->cdr)) + for (;args != empty_list; args = TO_PAIR(args->cdr)) { if (!args->car->is_num_obj()) // not a number throw TokenError("a number", RUN_ERR_WRONG_TYPE); @@ -911,8 +911,8 @@ BUILTIN_PROC_DEF(num_sub) { throw TokenError("a number", RUN_ERR_WRONG_TYPE); NumObj *res = static_cast(args->car), *opr; - args = TO_CONS(args->cdr); - for (; args != empty_list; args = TO_CONS(args->cdr)) + args = TO_PAIR(args->cdr); + for (; args != empty_list; args = TO_PAIR(args->cdr)) { if (!args->car->is_num_obj()) // not a number throw TokenError("a number", RUN_ERR_WRONG_TYPE); @@ -932,7 +932,7 @@ BUILTIN_PROC_DEF(num_sub) { BUILTIN_PROC_DEF(num_mul) { ARGS_AT_LEAST_ONE; NumObj *res = new IntNumObj(1), *opr; // the most accurate type - for (;args != empty_list; args = TO_CONS(args->cdr)) + for (;args != empty_list; args = TO_PAIR(args->cdr)) { if (!args->car->is_num_obj()) // not a number throw TokenError("a number", RUN_ERR_WRONG_TYPE); @@ -953,8 +953,8 @@ BUILTIN_PROC_DEF(num_div) { throw TokenError("a number", RUN_ERR_WRONG_TYPE); NumObj *res = static_cast(args->car), *opr; - args = TO_CONS(args->cdr); - for (; args != empty_list; args = TO_CONS(args->cdr)) + args = TO_PAIR(args->cdr); + for (; args != empty_list; args = TO_PAIR(args->cdr)) { if (!args->car->is_num_obj()) // not a number throw TokenError("a number", RUN_ERR_WRONG_TYPE); @@ -978,7 +978,7 @@ BUILTIN_PROC_DEF(num_lt) { throw TokenError("a number", RUN_ERR_WRONG_TYPE); NumObj *last = static_cast(args->car), *opr; - for (; args != empty_list; args = TO_CONS(args->cdr), last = opr) + for (; args != empty_list; args = TO_PAIR(args->cdr), last = opr) { if (!args->car->is_num_obj()) // not a number throw TokenError("a number", RUN_ERR_WRONG_TYPE); @@ -1002,7 +1002,7 @@ BUILTIN_PROC_DEF(num_gt) { throw TokenError("a number", RUN_ERR_WRONG_TYPE); NumObj *last = static_cast(args->car), *opr; - for (; args != empty_list; args = TO_CONS(args->cdr), last = opr) + for (; args != empty_list; args = TO_PAIR(args->cdr), last = opr) { if (!args->car->is_num_obj()) // not a number throw TokenError("a number", RUN_ERR_WRONG_TYPE); @@ -1026,7 +1026,7 @@ BUILTIN_PROC_DEF(num_eq) { throw TokenError("a number", RUN_ERR_WRONG_TYPE); NumObj *last = static_cast(args->car), *opr; - for (; args != empty_list; args = TO_CONS(args->cdr), last = opr) + for (; args != empty_list; args = TO_PAIR(args->cdr), last = opr) { if (!args->car->is_num_obj()) // not a number throw TokenError("a number", RUN_ERR_WRONG_TYPE); @@ -1061,7 +1061,7 @@ BUILTIN_PROC_DEF(pair_set_car) { ARGS_EXACTLY_TWO; if (!args->car->is_cons_obj()) throw TokenError("pair", RUN_ERR_WRONG_TYPE); - TO_CONS(args->car)->car = TO_CONS(args->cdr)->car; + TO_PAIR(args->car)->car = TO_PAIR(args->cdr)->car; return new UnspecObj(); } @@ -1069,7 +1069,7 @@ BUILTIN_PROC_DEF(pair_set_cdr) { ARGS_EXACTLY_TWO; if (!args->car->is_cons_obj()) throw TokenError("pair", RUN_ERR_WRONG_TYPE); - TO_CONS(args->car)->cdr = TO_CONS(args->cdr)->car; + TO_PAIR(args->car)->cdr = TO_PAIR(args->cdr)->car; return new UnspecObj(); } @@ -1084,12 +1084,12 @@ BUILTIN_PROC_DEF(is_list) { return new BoolObj(true); if (!args->car->is_cons_obj()) return new BoolObj(false); - args = TO_CONS(args->car); + args = TO_PAIR(args->car); EvalObj *nptr; for (;;) { if ((nptr = args->cdr)->is_cons_obj()) - args = TO_CONS(nptr); + args = TO_PAIR(nptr); else break; } return new BoolObj(args->cdr == empty_list); @@ -1117,11 +1117,11 @@ BUILTIN_PROC_DEF(length) { throw TokenError("a list", RUN_ERR_WRONG_TYPE); int num = 0; EvalObj *nptr; - for (args = TO_CONS(args->car);;) + for (args = TO_PAIR(args->car);;) { num++; if ((nptr = args->cdr)->is_cons_obj()) - args = TO_CONS(nptr); + args = TO_PAIR(nptr); else break; } @@ -1130,43 +1130,43 @@ BUILTIN_PROC_DEF(length) { return new IntNumObj(mpz_class(num)); } -Cons *copy_list(Cons *src, EvalObj * &tail) { +Pair *copy_list(Pair *src, EvalObj * &tail) { if (src == empty_list) throw NormalError(INT_ERR); EvalObj* nptr; - Cons head(NULL, NULL); + Pair head(NULL, NULL); tail = &head; for (;;) { - TO_CONS(tail)->cdr = new Cons(*src); - tail = TO_CONS(TO_CONS(tail)->cdr); + TO_PAIR(tail)->cdr = new Pair(*src); + tail = TO_PAIR(TO_PAIR(tail)->cdr); if ((nptr = src->cdr)->is_cons_obj()) - src = TO_CONS(nptr); + src = TO_PAIR(nptr); else break; } - return TO_CONS(head.cdr); + return TO_PAIR(head.cdr); } BUILTIN_PROC_DEF(append) { EvalObj *tail = empty_list, *head = tail; - for (; args != empty_list; args = TO_CONS(args->cdr)) + for (; args != empty_list; args = TO_PAIR(args->cdr)) { if (tail == empty_list) { head = args->car; if (head->is_cons_obj()) - head = copy_list(TO_CONS(head), tail); + head = copy_list(TO_PAIR(head), tail); else tail = head; } else { if (tail->is_cons_obj()) { - Cons *prev = TO_CONS(tail); + Pair *prev = TO_PAIR(tail); if (prev->cdr != empty_list) throw TokenError("empty list", RUN_ERR_WRONG_TYPE); if (args->car->is_cons_obj()) - prev->cdr = copy_list(TO_CONS(args->car), tail); + prev->cdr = copy_list(TO_PAIR(args->car), tail); else prev->cdr = args->car; } @@ -1179,11 +1179,11 @@ BUILTIN_PROC_DEF(append) { BUILTIN_PROC_DEF(reverse) { ARGS_EXACTLY_ONE; - Cons *tail = empty_list; + Pair *tail = empty_list; EvalObj *ptr; for (ptr = args->car; - ptr->is_cons_obj(); ptr = TO_CONS(ptr)->cdr) - tail = new Cons(TO_CONS(ptr)->car, tail); + ptr->is_cons_obj(); ptr = TO_PAIR(ptr)->cdr) + tail = new Pair(TO_PAIR(ptr)->car, tail); if (ptr != empty_list) throw TokenError("a list", RUN_ERR_WRONG_TYPE); return tail; @@ -1191,7 +1191,7 @@ BUILTIN_PROC_DEF(reverse) { BUILTIN_PROC_DEF(list_tail) { ARGS_EXACTLY_TWO; - EvalObj *sec = TO_CONS(args->cdr)->car; + EvalObj *sec = TO_PAIR(args->cdr)->car; if (!sec->is_num_obj() || static_cast(sec)->level != NUM_LVL_INT) throw TokenError("an exact integer", RUN_ERR_WRONG_TYPE); @@ -1200,13 +1200,13 @@ BUILTIN_PROC_DEF(list_tail) { throw TokenError("a non-negative integer", RUN_ERR_WRONG_TYPE); EvalObj *ptr; for (i = 0, ptr = args->car; - ptr->is_cons_obj(); ptr = TO_CONS(ptr)->cdr, i++) + ptr->is_cons_obj(); ptr = TO_PAIR(ptr)->cdr, i++) if (i == k) break; if (i != k) throw TokenError("a pair", RUN_ERR_WRONG_TYPE); EvalObj *tail; if (ptr->is_cons_obj()) - return copy_list(TO_CONS(ptr), tail); + return copy_list(TO_PAIR(ptr), tail); else return ptr; } @@ -1214,7 +1214,7 @@ BUILTIN_PROC_DEF(list_tail) { BUILTIN_PROC_DEF(is_eqv) { ARGS_EXACTLY_TWO; EvalObj *obj1 = args->car; - EvalObj *obj2 = TO_CONS(args->cdr)->car; + EvalObj *obj2 = TO_PAIR(args->cdr)->car; ClassType otype = obj1->get_otype(); if (otype != obj2->get_otype()) return new BoolObj(false); diff --git a/builtin.h b/builtin.h index 11b0d92..589e7a9 100644 --- a/builtin.h +++ b/builtin.h @@ -7,7 +7,7 @@ using std::string; -bool is_list(Cons *ptr); +bool is_list(Pair *ptr); /** @class InexactNumObj * Inexact number implementation (using doubles) @@ -152,18 +152,18 @@ class SpecialOptIf: public SpecialOptObj { * The evaluator will call this after the exp is evaluated. * And this function tells the evaluator which of and * should be evaluted. */ - void pre_call(ArgList *args, Cons *pc, + void pre_call(ArgList *args, Pair *pc, Environment *envt); /** The system will call this again after the desired result is * evaluated, so just return it to let the evaluator know the it's the * answer. */ - EvalObj *post_call(ArgList *args, Cons *pc, + EvalObj *post_call(ArgList *args, Pair *pc, Environment *envt); public: SpecialOptIf(); - void prepare(Cons *pc); - Cons *call(ArgList *args, Environment * &envt, + void prepare(Pair *pc); + Pair *call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr); string ext_repr(); }; @@ -174,8 +174,8 @@ class SpecialOptIf: public SpecialOptObj { class SpecialOptLambda: public SpecialOptObj { public: SpecialOptLambda(); - void prepare(Cons *pc); - Cons *call(ArgList *args, Environment * &envt, + void prepare(Pair *pc); + Pair *call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr); string ext_repr(); @@ -187,8 +187,8 @@ class SpecialOptLambda: public SpecialOptObj { class SpecialOptDefine: public SpecialOptObj { public: SpecialOptDefine(); - void prepare(Cons *pc); - Cons *call(ArgList *args, Environment * &envt, + void prepare(Pair *pc); + Pair *call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr); string ext_repr(); }; @@ -199,8 +199,8 @@ class SpecialOptDefine: public SpecialOptObj { class SpecialOptSet: public SpecialOptObj { public: SpecialOptSet(); - void prepare(Cons *pc); - Cons *call(ArgList *args, Environment * &envt, + void prepare(Pair *pc); + Pair *call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr); string ext_repr(); }; @@ -211,8 +211,8 @@ class SpecialOptSet: public SpecialOptObj { class SpecialOptQuote: public SpecialOptObj { public: SpecialOptQuote(); - void prepare(Cons *pc); - Cons *call(ArgList *args, Environment * &envt, + void prepare(Pair *pc); + Pair *call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr); string ext_repr(); @@ -226,8 +226,8 @@ class SpecialOptEval: public SpecialOptObj { unsigned char state; /**< 0 for prepared, 1 for pre_called */ public: SpecialOptEval(); - void prepare(Cons *pc); - Cons *call(ArgList *args, Environment * &envt, + void prepare(Pair *pc); + Pair *call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr); string ext_repr(); diff --git a/eval.cpp b/eval.cpp index 08f14f4..98d3d5a 100644 --- a/eval.cpp +++ b/eval.cpp @@ -4,7 +4,7 @@ #include "consts.h" #include -extern Cons *empty_list; +extern Pair *empty_list; const int EVAL_STACK_SIZE = 65536; FrameObj *eval_stack[EVAL_STACK_SIZE]; @@ -63,7 +63,7 @@ Evaluator::Evaluator() { add_builtin_routines(); } -void push(Cons * &pc, FrameObj ** &top_ptr, Environment *envt) { +void push(Pair * &pc, FrameObj ** &top_ptr, Environment *envt) { if (pc->car->is_simple_obj()) // Not an opt invocation { *top_ptr = envt->get_obj(pc->car); // Objectify the symbol @@ -78,22 +78,16 @@ void push(Cons * &pc, FrameObj ** &top_ptr, Environment *envt) { throw NormalError(SYN_ERR_EMPTY_COMB); *top_ptr++ = new RetAddr(pc); // Push the return address - if (!is_list(TO_CONS(pc->car))) + if (!is_list(TO_PAIR(pc->car))) throw TokenError(pc->car->ext_repr(), RUN_ERR_WRONG_NUM_OF_ARGS); // static_cast because of is_simple_obj() is false - pc = static_cast(pc->car); // Go deeper to enter the call + pc = static_cast(pc->car); // Go deeper to enter the call } } -void stack_print(FrameObj **top_ptr) { - for (FrameObj **ptr = eval_stack; ptr != top_ptr; ptr++) - printf("%s\n", (*ptr)->_debug_repr().c_str()); - puts(""); -} - -EvalObj *Evaluator::run_expr(Cons *prog) { +EvalObj *Evaluator::run_expr(Pair *prog) { FrameObj **top_ptr = eval_stack; - Cons *pc = prog; + Pair *pc = prog; Continuation *cont = NULL; // envt is this->envt push(pc, top_ptr, envt); @@ -105,14 +99,14 @@ EvalObj *Evaluator::run_expr(Cons *prog) { push(pc, top_ptr, envt); else { - Cons *args = empty_list; + Pair *args = empty_list; while (!(*(--top_ptr))->is_ret_addr()) - args = new Cons(static_cast(*top_ptr), args); + args = new Pair(static_cast(*top_ptr), args); //< static_cast because the while condition RetAddr *ret_addr = static_cast(*top_ptr); if (!ret_addr->addr) { - Cons *nexp = TO_CONS(cont->proc_body->cdr); + Pair *nexp = TO_PAIR(cont->proc_body->cdr); cont->proc_body = nexp; if (nexp == empty_list) { diff --git a/eval.h b/eval.h index 78ede95..1c8d14f 100644 --- a/eval.h +++ b/eval.h @@ -11,7 +11,7 @@ class Evaluator { void add_builtin_routines(); /**< Add builtin routines to the env */ public: Evaluator(); - EvalObj *run_expr(Cons *prog); /**< Interpret a program */ + EvalObj *run_expr(Pair *prog); /**< Interpret a program */ }; #endif diff --git a/main.cpp b/main.cpp index c07b873..810537d 100644 --- a/main.cpp +++ b/main.cpp @@ -6,14 +6,7 @@ #include #ifdef DEBUG -extern Cons *empty_list; -void tree_print(Cons *ptr) { - ptr->_debug_print(); - if (ptr->car->is_cons_obj()) - tree_print(TO_CONS(ptr->car)); - if (ptr->cdr->is_cons_obj()) - tree_print(TO_CONS(ptr->cdr)); -} +extern Pair *empty_list; #endif int main() { @@ -28,9 +21,8 @@ int main() { fprintf(stderr, "Sonsi> "); try { - Cons *tree = ast->absorb(tk); + Pair *tree = ast->absorb(tk); if (!tree) break; - //tree_print(tree); fprintf(stderr, "Ret> $%d = %s\n", rcnt++, eval->run_expr(tree)->ext_repr().c_str()); } 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("#"); } -#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("#"); -} -#endif - UnspecObj::UnspecObj() : EvalObj(CLS_SIM_OBJ) {} string UnspecObj::ext_repr() { return string("#"); } -#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 "#"; } -#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(*top_ptr)->addr; + Pair *ret_addr = static_cast(*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(TO_CONS(ppar)->car), args->car); + _envt->add_binding(static_cast(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("#"); } -#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(*top_ptr)->addr; - *top_ptr++ = handler(TO_CONS(args->cdr), name); + Pair *ret_addr = static_cast(*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 "#"; } -#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) {} diff --git a/model.h b/model.h index 9afbf53..32a1785 100644 --- a/model.h +++ b/model.h @@ -27,10 +27,11 @@ const int CLS_NUM_OBJ = 1 << 4; const int CLS_BOOL_OBJ = 1 << 5; const int CLS_CHAR_OBJ = 1 << 6; const int CLS_STR_OBJ = 1 << 7; +const int CLS_VECT_OBJ = 1 << 8; -#define TO_CONS(ptr) \ - (static_cast(ptr)) +#define TO_PAIR(ptr) \ + (static_cast(ptr)) /** @class FrameObj * Objects that can be held in the evaluation stack @@ -60,14 +61,10 @@ class FrameObj { * @return true for yes */ bool is_parse_bracket(); - -#ifdef DEBUG - virtual string _debug_repr() = 0; -#endif }; -class Cons; +class Pair; /** @class EvalObj * Objects that represents a value in evaluation */ @@ -77,7 +74,7 @@ class EvalObj : public FrameObj { * Report the type of the EvalObj, which can avoid the use of * dynamic_cast to improve efficiency. See the constructor for detail */ - ClassType otype; + int otype; public: /** * Construct an EvalObj @@ -85,78 +82,72 @@ class EvalObj : public FrameObj { * construction, CLS_SIM_OBJ for a simple object), which defaults to * CLS_SIM_OBJ */ - EvalObj(ClassType otype = CLS_SIM_OBJ); + EvalObj(int otype = CLS_SIM_OBJ); /** Check if the object is a simple object (instead of a call * invocation) - * @return true if the object is not a construction (Cons) + * @return true if the object is not a construction (Pair) * */ bool is_simple_obj(); /** Check if the object is a symobl */ bool is_sym_obj(); /** Check if the object is an operator */ bool is_opt_obj(); - /** Check if the object is a Cons */ + /** Check if the object is a Pair */ bool is_cons_obj(); /** Check if the object is a number */ bool is_num_obj(); /** Check if the object is a boolean */ bool is_bool_obj(); ClassType get_otype(); - virtual void prepare(Cons *pc); + virtual void prepare(Pair *pc); /** Any EvalObj has its external representation */ - virtual string ext_repr() = 0; + virtual string ext_repr(); /** Always true for all EvalObjs except BoolObj */ virtual bool is_true(); -#ifdef DEBUG - virtual string _debug_repr(); - virtual void _debug_print(); -#endif }; -/** @class Cons +/** @class Pair * Pair construct, which can be used to represent a list, or further * more, a syntax tree * (car . cdr) in Scheme */ -class Cons : public EvalObj { +class Pair : public EvalObj { public: EvalObj *car; /**< car (as in Scheme) */ EvalObj *cdr; /**< cdr (as in Scheme) */ bool skip; /**< Wether to skip the current branch */ - Cons* next; /**< The next branch in effect */ + Pair* next; /**< The next branch in effect */ - Cons(EvalObj *car, EvalObj *cdr); /**< Create a Cons (car . cdr) */ -#ifdef DEBUG - void _debug_print(); - string _debug_repr(); -#endif - string ext_repr(); + Pair(EvalObj *car, EvalObj *cdr); /**< Create a Pair (car . cdr) */ }; /** @class EmptyList * The empty list (special situation of a list) */ -class EmptyList: public Cons { +class EmptyList: public Pair { public: EmptyList(); -#ifdef DEBUG - string _debug_repr(); -#endif string ext_repr(); }; /** @class RetAddr - * Tracking the caller's Cons pointer + * Tracking the caller's Pair pointer */ class RetAddr : public FrameObj { public: - Cons* addr; /**< The return address */ + Pair* addr; /**< The return address */ /** Constructs a return address object which refers to the node addr in * the AST */ - RetAddr(Cons *addr); -#ifdef DEBUG - string _debug_repr(); -#endif + RetAddr(Pair *addr); +}; + +class ReprConstructor : public FrameObj { + virtual EvalObj *next(); +}; + +class ExtRepr : public FrameObj { + public: + string repr; }; /** @class ParseBracket @@ -167,9 +158,6 @@ class ParseBracket : public FrameObj { unsigned char btype; /**< The type of the bracket */ /** Construct a ParseBracket object */ ParseBracket(unsigned char btype); -#ifdef DEBUG - string _debug_repr(); -#endif }; /** @class UnspecObj @@ -178,9 +166,6 @@ class ParseBracket : public FrameObj { class UnspecObj: public EvalObj { public: UnspecObj(); -#ifdef DEBUG - string _debug_repr(); -#endif string ext_repr(); }; @@ -199,9 +184,7 @@ class SymObj: public EvalObj { }; // Everything is cons -typedef Cons ASTList; -typedef Cons SymbolList; -typedef Cons ArgList; +typedef Pair ArgList; class Environment; class Continuation; @@ -219,7 +202,7 @@ class OptObj: public EvalObj { * @param top_ptr Pointing to the top of the stack (may be modified) * @return New value for pc register */ - virtual Cons *call(ArgList *args, Environment * &envt, + virtual Pair *call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) = 0; }; @@ -229,19 +212,16 @@ class OptObj: public EvalObj { class ProcObj: public OptObj { public: /** The procedure body, a list of expressions to be evaluated */ - ASTList *body; + Pair *body; /** The arguments: | var1 ... | var1 var2 ... . varn */ EvalObj *params; /** Pointer to the environment */ Environment *envt; /** Conctructs a ProcObj */ - ProcObj(ASTList *body, Environment *envt, EvalObj *params); - Cons *call(ArgList *args, Environment * &envt, + ProcObj(Pair *body, Environment *envt, EvalObj *params); + Pair *call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr); -#ifdef DEBUG - string _debug_repr(); -#endif string ext_repr(); }; @@ -271,11 +251,8 @@ class BuiltinProcObj: public OptObj { * @param name the name of this built-in procedure */ BuiltinProcObj(BuiltinProc proc, string name); - Cons *call(ArgList *args, Environment * &envt, + Pair *call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr); -#ifdef DEBUG - string _debug_repr(); -#endif string ext_repr(); }; @@ -371,7 +348,6 @@ class VecObj: public EvalObj { void resize(int new_size); /** Add a new element to the rear */ void push_back(EvalObj *new_elem); - string ext_repr(); }; typedef map Str2EvalObj; @@ -411,15 +387,15 @@ class Continuation { /** Linking the previous continuation on the chain */ Continuation *prev_cont; Environment *envt; /**< The saved envt */ - Cons *pc; /**< The saved pc */ + Pair *pc; /**< The saved pc */ /** Pointing to the current expression that is being evaluated. * When its value goes to empty_list, the call is accomplished. */ - ASTList *proc_body; + Pair *proc_body; /** Create a continuation */ - Continuation(Environment *envt, Cons *pc, Continuation *prev_cont, - ASTList *proc_body); + Continuation(Environment *envt, Pair *pc, Continuation *prev_cont, + Pair *proc_body); }; #endif diff --git a/parser.cpp b/parser.cpp index 194443e..b6486cc 100644 --- a/parser.cpp +++ b/parser.cpp @@ -10,7 +10,7 @@ using std::stringstream; static char buff[TOKEN_BUFF_SIZE]; static FrameObj *parse_stack[PARSE_STACK_SIZE]; -extern Cons *empty_list; +extern Pair *empty_list; Tokenizor::Tokenizor() : stream(stdin), buff_ptr(buff), escaping(false) {} void Tokenizor::set_stream(FILE *_stream) { @@ -145,7 +145,7 @@ EvalObj *ASTGenerator::to_obj(const string &str) { #define IS_BRAKET(ptr) \ ((ptr)->is_parse_bracket()) -Cons *ASTGenerator::absorb(Tokenizor *tk) { +Pair *ASTGenerator::absorb(Tokenizor *tk) { FrameObj **top_ptr = parse_stack; for (;;) { @@ -157,8 +157,8 @@ Cons *ASTGenerator::absorb(Tokenizor *tk) { if (bptr->btype == 2) { top_ptr -= 2; - Cons *lst_cdr = new Cons(TO_EVAL(*(top_ptr + 1)), empty_list); - Cons *lst = new Cons(new SymObj("quote"), lst_cdr); + Pair *lst_cdr = new Pair(TO_EVAL(*(top_ptr + 1)), empty_list); + Pair *lst = new Pair(new SymObj("quote"), lst_cdr); lst->next = lst_cdr; lst_cdr->next = NULL; *top_ptr++ = lst; @@ -166,7 +166,7 @@ Cons *ASTGenerator::absorb(Tokenizor *tk) { } if (top_ptr > parse_stack && !IS_BRAKET(*parse_stack)) - return new Cons(TO_EVAL(*(top_ptr - 1)), empty_list); + return new Pair(TO_EVAL(*(top_ptr - 1)), empty_list); string token; if (!tk->get_token(token)) return NULL; if (token == "(") // a list @@ -188,15 +188,15 @@ Cons *ASTGenerator::absorb(Tokenizor *tk) { { if (improper || lst == empty_list || - TO_CONS(lst)->cdr != empty_list) + TO_PAIR(lst)->cdr != empty_list) throw NormalError(PAR_ERR_IMPROPER_PAIR); improper = true; - lst = TO_CONS(lst)->car; + lst = TO_PAIR(lst)->car; } else { - Cons *_lst = new Cons(obj, lst); // Collect the list - _lst->next = lst->is_cons_obj() ? TO_CONS(lst) : NULL; + Pair *_lst = new Pair(obj, lst); // Collect the list + _lst->next = lst->is_cons_obj() ? TO_PAIR(lst) : NULL; lst = _lst; } } @@ -208,7 +208,7 @@ Cons *ASTGenerator::absorb(Tokenizor *tk) { { if (improper) throw NormalError(PAR_ERR_IMPROPER_VECT); VecObj *vec = new VecObj(); - for (Cons *ptr = TO_CONS(lst); ptr != empty_list; ptr = TO_CONS(ptr->cdr)) + for (Pair *ptr = TO_PAIR(lst); ptr != empty_list; ptr = TO_PAIR(ptr->cdr)) vec->push_back(ptr->car); *top_ptr++ = vec; } diff --git a/parser.h b/parser.h index 11a03a5..e44abbc 100644 --- a/parser.h +++ b/parser.h @@ -31,7 +31,7 @@ class Tokenizor { /** @class ASTGenerator * Read the tokens and build up an Abstract Syntax Tree (which is in effect a - * Cons) + * Pair) */ class ASTGenerator { private: @@ -43,7 +43,7 @@ class ASTGenerator { * @param tk pointer to a Tokenizor * @return Abstract Syntax Tree */ - Cons *absorb(Tokenizor *tk); + Pair *absorb(Tokenizor *tk); }; #endif -- cgit v1.2.3-70-g09d2