diff options
-rw-r--r-- | builtin.cpp | 128 | ||||
-rw-r--r-- | builtin.h | 30 | ||||
-rw-r--r-- | eval.cpp | 6 | ||||
-rw-r--r-- | eval.h | 2 | ||||
-rw-r--r-- | main.cpp | 2 | ||||
-rw-r--r-- | model.cpp | 84 | ||||
-rw-r--r-- | model.h | 48 | ||||
-rw-r--r-- | parser.cpp | 26 | ||||
-rw-r--r-- | parser.h | 8 |
9 files changed, 167 insertions, 167 deletions
diff --git a/builtin.cpp b/builtin.cpp index a5d8b15..610cdec 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -19,7 +19,7 @@ static const int NUM_LVL_INT = 3; if (args == empty_list || \ args->cdr == empty_list || \ TO_PAIR(args->cdr)->cdr != empty_list) \ - throw TokenError(name, RUN_ERR_WRONG_NUM_OF_ARGS) + throw TokenError(name, RUN_ERR_WRONG_NUM_OF_ARGS) #define ARGS_EXACTLY_ONE \ if (args == empty_list || \ @@ -46,7 +46,7 @@ string int_to_str(int val) { double str_to_double(string repr, bool &flag) { const char *nptr = repr.c_str(); - char *endptr; + char *endptr; double val = strtod(nptr, &endptr); if (endptr == nptr || endptr != nptr + repr.length()) { @@ -59,7 +59,7 @@ double str_to_double(string repr, bool &flag) { int str_to_int(string repr, bool &flag) { const char *nptr = repr.c_str(); - char *endptr; + char *endptr; int val = strtol(nptr, &endptr, 10); if (endptr == nptr || endptr != nptr + repr.length()) { @@ -95,10 +95,10 @@ CompNumObj::CompNumObj(double _real, double _imag) : spos = i; sign = repr[i] == '-'; } - else if (repr[i] == 'i' || repr[i] == 'I') + else if (repr[i] == 'i' || repr[i] == 'I') ipos = i; - if (spos == -1 || ipos == -1 || !(spos < ipos)) + if (spos == -1 || ipos == -1 || !(spos < ipos)) return NULL; double real = 0, imag = 1; @@ -118,7 +118,7 @@ CompNumObj::CompNumObj(double _real, double _imag) : #ifndef GMP_SUPPORT real = rat_ptr->a / double(rat_ptr->b); #else - real = rat_ptr->val.get_d(); + real = rat_ptr->val.get_d(); #endif else if ((real_ptr = RealNumObj::from_string(real_str))) real = real_ptr->real; @@ -148,11 +148,11 @@ CompNumObj::CompNumObj(double _real, double _imag) : } CompNumObj *CompNumObj::convert(NumObj *obj) { - switch (obj->level) + switch (obj->level) { - case NUM_LVL_COMP : + case NUM_LVL_COMP : return static_cast<CompNumObj*>(obj); break; - case NUM_LVL_REAL : + case NUM_LVL_REAL : return new CompNumObj(static_cast<RealNumObj*>(obj)->real, 0); break; case NUM_LVL_RAT : @@ -238,7 +238,7 @@ RealNumObj *RealNumObj::from_string(string repr) { RealNumObj *RealNumObj::convert(NumObj *obj) { switch (obj->level) { - case NUM_LVL_REAL: + case NUM_LVL_REAL: return static_cast<RealNumObj*>(obj); break; case NUM_LVL_RAT: { @@ -296,7 +296,7 @@ ReprCons *RealNumObj::get_repr_cons() { ExactNumObj::ExactNumObj(NumLvl level) : NumObj(level, true) {} #ifndef GMP_SUPPORT -RatNumObj::RatNumObj(int _a, int _b) : +RatNumObj::RatNumObj(int _a, int _b) : ExactNumObj(NUM_LVL_RAT), a(_a), b(_b) { int g = gcd(a, b); a /= g; @@ -318,13 +318,13 @@ RatNumObj *RatNumObj::from_string(string repr) { return new RatNumObj(a, b); } #else -RatNumObj::RatNumObj(mpq_class _val) : +RatNumObj::RatNumObj(mpq_class _val) : ExactNumObj(NUM_LVL_RAT), val(_val) { val.canonicalize(); } RatNumObj *RatNumObj::from_string(string repr) { - try + try { mpq_class ret(repr, 10); return new RatNumObj(ret); @@ -347,7 +347,7 @@ RatNumObj *RatNumObj::convert(NumObj *obj) { return new RatNumObj(static_cast<IntNumObj*>(obj)->val, 1); #else return new RatNumObj(mpq_class( - static_cast<IntNumObj*>(obj)->val, + static_cast<IntNumObj*>(obj)->val, mpz_class(1))); #endif } @@ -463,7 +463,7 @@ int IntNumObj::get_i() { return val; } #else IntNumObj::IntNumObj(mpz_class _val) : ExactNumObj(NUM_LVL_INT), val(_val) {} IntNumObj *IntNumObj::from_string(string repr) { - try + try { mpz_class ret(repr, 10); return new IntNumObj(ret); @@ -538,7 +538,7 @@ void SpecialOptIf::prepare(Pair *pc) { if (pc->cdr->is_pair_obj()) first = TO_PAIR(pc->cdr); - else + else IF_EXP_ERR; if (first->cdr->is_pair_obj()) @@ -565,7 +565,7 @@ void SpecialOptIf::pre_call(ArgList *args, Pair *pc, Environment *envt) { // prepare has guaranteed ... pc = TO_PAIR(pc->car); - Pair *first = TO_PAIR(pc->cdr); + Pair *first = TO_PAIR(pc->cdr); Pair *second = TO_PAIR(first->cdr); Pair *third = TO_PAIR(second->cdr); @@ -590,10 +590,10 @@ EvalObj *SpecialOptIf::post_call(ArgList *args, Pair *pc, return TO_PAIR(args->cdr)->car; } -Pair *SpecialOptIf::call(ArgList *args, Environment * &envt, +Pair *SpecialOptIf::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { Pair *ret_addr = static_cast<RetAddr*>(*top_ptr)->addr; - if (state) + if (state) { *top_ptr++ = post_call(args, ret_addr, envt); return ret_addr->next; // Move to the next instruction @@ -608,8 +608,8 @@ Pair *SpecialOptIf::call(ArgList *args, Environment * &envt, } } -ReprCons *SpecialOptIf::get_repr_cons() { - return new ReprStr("#<Builtin Macro: if>"); +ReprCons *SpecialOptIf::get_repr_cons() { + return new ReprStr("#<Builtin Macro: if>"); } SpecialOptLambda::SpecialOptLambda() : SpecialOptObj("lambda") {} @@ -661,7 +661,7 @@ void SpecialOptLambda::prepare(Pair *pc) { pc->next = NULL; } -Pair *SpecialOptLambda::call(ArgList *args, Environment * &envt, +Pair *SpecialOptLambda::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { Pair *ret_addr = static_cast<RetAddr*>(*top_ptr)->addr; @@ -673,10 +673,10 @@ Pair *SpecialOptLambda::call(ArgList *args, Environment * &envt, // <body> is expected if (first->cdr == empty_list) throw TokenError(name, SYN_ERR_MISS_OR_EXTRA_EXP); - + // Restore the next pointer pc->next = TO_PAIR(pc->cdr); // CHECK_COM made it always okay - + if (first->car->is_simple_obj()) CHECK_SYMBOL(first->car); else @@ -692,8 +692,8 @@ Pair *SpecialOptLambda::call(ArgList *args, Environment * &envt, return ret_addr->next; // Move to the next instruction } -ReprCons *SpecialOptLambda::get_repr_cons() { - return new ReprStr("#<Builtin Macro: lambda>"); +ReprCons *SpecialOptLambda::get_repr_cons() { + return new ReprStr("#<Builtin Macro: lambda>"); } SpecialOptDefine::SpecialOptDefine() : SpecialOptObj("define") {} @@ -712,14 +712,14 @@ void SpecialOptDefine::prepare(Pair *pc) { pc->next = second; // Skip the identifier second->next = NULL; } // Procedure definition - else + else { CHECK_COM(pc); pc->next = NULL; // Skip all parts } } -Pair *SpecialOptDefine::call(ArgList *args, Environment * &envt, +Pair *SpecialOptDefine::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { Pair *ret_addr = static_cast<RetAddr*>(*top_ptr)->addr; Pair *pc = static_cast<Pair*>(ret_addr->car); @@ -748,7 +748,7 @@ Pair *SpecialOptDefine::call(ArgList *args, Environment * &envt, id = static_cast<SymObj*>(plst->car); EvalObj *params = plst->cdr; - + // Restore the next pointer pc->next = TO_PAIR(pc->cdr); @@ -767,8 +767,8 @@ Pair *SpecialOptDefine::call(ArgList *args, Environment * &envt, return ret_addr->next; } -ReprCons *SpecialOptDefine::get_repr_cons() { - return new ReprStr("#<Builtin Macro: define>"); +ReprCons *SpecialOptDefine::get_repr_cons() { + return new ReprStr("#<Builtin Macro: define>"); } void SpecialOptSet::prepare(Pair *pc) { @@ -781,14 +781,14 @@ void SpecialOptSet::prepare(Pair *pc) { throw TokenError(name, RUN_ERR_WRONG_NUM_OF_ARGS); second = TO_PAIR(pc->cdr); - if (second->cdr != empty_list) + if (second->cdr != empty_list) throw TokenError(name, RUN_ERR_WRONG_NUM_OF_ARGS); pc->next = second; second->next = NULL; } -Pair *SpecialOptSet::call(ArgList *args, Environment * &envt, +Pair *SpecialOptSet::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { Pair *ret_addr = static_cast<RetAddr*>(*top_ptr)->addr; Pair *pc = static_cast<Pair*>(ret_addr->car); @@ -807,8 +807,8 @@ Pair *SpecialOptSet::call(ArgList *args, Environment * &envt, SpecialOptSet::SpecialOptSet() : SpecialOptObj("set!") {} -ReprCons *SpecialOptSet::get_repr_cons() { - return new ReprStr("#<Builtin Macro: set!>"); +ReprCons *SpecialOptSet::get_repr_cons() { + return new ReprStr("#<Builtin Macro: set!>"); } SpecialOptQuote::SpecialOptQuote() : SpecialOptObj("quote") {} @@ -819,7 +819,7 @@ void SpecialOptQuote::prepare(Pair *pc) { pc->next = NULL; } -Pair *SpecialOptQuote::call(ArgList *args, Environment * &envt, +Pair *SpecialOptQuote::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { Pair *ret_addr = static_cast<RetAddr*>(*top_ptr)->addr; Pair *pc = static_cast<Pair*>(ret_addr->car); @@ -829,8 +829,8 @@ Pair *SpecialOptQuote::call(ArgList *args, Environment * &envt, return ret_addr->next; } -ReprCons *SpecialOptQuote::get_repr_cons() { - return new ReprStr("#<Builtin Macro: quote>"); +ReprCons *SpecialOptQuote::get_repr_cons() { + return new ReprStr("#<Builtin Macro: quote>"); } SpecialOptEval::SpecialOptEval() : SpecialOptObj("eval") {} @@ -839,7 +839,7 @@ void SpecialOptEval::prepare(Pair *pc) { state = 0; } -Pair *SpecialOptEval::call(ArgList *args, Environment * &envt, +Pair *SpecialOptEval::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { if (args->cdr == empty_list || TO_PAIR(args->cdr)->cdr != empty_list) @@ -858,8 +858,8 @@ Pair *SpecialOptEval::call(ArgList *args, Environment * &envt, } } -ReprCons *SpecialOptEval::get_repr_cons() { - return new ReprStr("#<Builtin Macro: eval>"); +ReprCons *SpecialOptEval::get_repr_cons() { + return new ReprStr("#<Builtin Macro: eval>"); } SpecialOptAnd::SpecialOptAnd() : SpecialOptObj("and") {} @@ -873,7 +873,7 @@ void SpecialOptAnd::prepare(Pair *pc) { } } -Pair *SpecialOptAnd::call(ArgList *args, Environment * &envt, +Pair *SpecialOptAnd::call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) { Pair *ret_addr = static_cast<RetAddr*>(*top_ptr)->addr; Pair *pc = static_cast<Pair*>(ret_addr->car); @@ -906,8 +906,8 @@ Pair *SpecialOptAnd::call(ArgList *args, Environment * &envt, throw NormalError(INT_ERR); } -ReprCons *SpecialOptAnd::get_repr_cons() { - return new ReprStr("#<Builtin Macro: and>"); +ReprCons *SpecialOptAnd::get_repr_cons() { + return new ReprStr("#<Builtin Macro: and>"); } BUILTIN_PROC_DEF(make_pair) { @@ -951,7 +951,7 @@ BUILTIN_PROC_DEF(num_add) { _res = opr->convert(_res); res = _res->add(opr); } - return res; + return res; } BUILTIN_PROC_DEF(num_sub) { @@ -974,7 +974,7 @@ BUILTIN_PROC_DEF(num_sub) { _res = opr->convert(_res); res = _res->sub(opr); } - return res; + return res; } @@ -993,7 +993,7 @@ BUILTIN_PROC_DEF(num_mul) { _res = opr->convert(_res); res = _res->mul(opr); } - return res; + return res; } BUILTIN_PROC_DEF(num_div) { @@ -1016,7 +1016,7 @@ BUILTIN_PROC_DEF(num_div) { _res = opr->convert(_res); res = _res->div(opr); } - return res; + return res; } BUILTIN_PROC_DEF(num_lt) { @@ -1026,7 +1026,7 @@ BUILTIN_PROC_DEF(num_lt) { if (!args->car->is_num_obj()) throw TokenError("a number", RUN_ERR_WRONG_TYPE); - NumObj *last = static_cast<NumObj*>(args->car), *opr; + NumObj *last = static_cast<NumObj*>(args->car), *opr; for (; args != empty_list; args = TO_PAIR(args->cdr), last = opr) { if (!args->car->is_num_obj()) // not a number @@ -1050,7 +1050,7 @@ BUILTIN_PROC_DEF(num_gt) { if (!args->car->is_num_obj()) throw TokenError("a number", RUN_ERR_WRONG_TYPE); - NumObj *last = static_cast<NumObj*>(args->car), *opr; + NumObj *last = static_cast<NumObj*>(args->car), *opr; for (; args != empty_list; args = TO_PAIR(args->cdr), last = opr) { if (!args->car->is_num_obj()) // not a number @@ -1074,7 +1074,7 @@ BUILTIN_PROC_DEF(num_eq) { if (!args->car->is_num_obj()) throw TokenError("a number", RUN_ERR_WRONG_TYPE); - NumObj *last = static_cast<NumObj*>(args->car), *opr; + NumObj *last = static_cast<NumObj*>(args->car), *opr; for (; args != empty_list; args = TO_PAIR(args->cdr), last = opr) { if (!args->car->is_num_obj()) // not a number @@ -1148,14 +1148,14 @@ BUILTIN_PROC_DEF(num_is_exact) { ARGS_EXACTLY_ONE; if (!args->car->is_num_obj()) throw TokenError("a number", RUN_ERR_WRONG_TYPE); - return new BoolObj(static_cast<NumObj*>(args->car)->is_exact()); + return new BoolObj(static_cast<NumObj*>(args->car)->is_exact()); } BUILTIN_PROC_DEF(num_is_inexact) { ARGS_EXACTLY_ONE; if (!args->car->is_num_obj()) throw TokenError("a number", RUN_ERR_WRONG_TYPE); - return new BoolObj(!static_cast<NumObj*>(args->car)->is_exact()); + return new BoolObj(!static_cast<NumObj*>(args->car)->is_exact()); } BUILTIN_PROC_DEF(length) { @@ -1171,7 +1171,7 @@ BUILTIN_PROC_DEF(length) { num++; if ((nptr = args->cdr)->is_pair_obj()) args = TO_PAIR(nptr); - else + else break; } if (args->cdr != empty_list) @@ -1207,7 +1207,7 @@ BUILTIN_PROC_DEF(append) { head = copy_list(TO_PAIR(head), tail); else tail = head; } - else + else { if (tail->is_pair_obj()) { @@ -1230,7 +1230,7 @@ BUILTIN_PROC_DEF(reverse) { ARGS_EXACTLY_ONE; Pair *tail = empty_list; EvalObj *ptr; - for (ptr = args->car; + for (ptr = args->car; ptr->is_pair_obj(); ptr = TO_PAIR(ptr)->cdr) tail = new Pair(TO_PAIR(ptr)->car, tail); if (ptr != empty_list) @@ -1241,7 +1241,7 @@ BUILTIN_PROC_DEF(reverse) { BUILTIN_PROC_DEF(list_tail) { ARGS_EXACTLY_TWO; EvalObj *sec = TO_PAIR(args->cdr)->car; - if (!sec->is_num_obj() || + if (!sec->is_num_obj() || static_cast<NumObj*>(sec)->level != NUM_LVL_INT) throw TokenError("an exact integer", RUN_ERR_WRONG_TYPE); int i, k = static_cast<IntNumObj*>(sec)->get_i(); @@ -1269,7 +1269,7 @@ BUILTIN_PROC_DEF(is_eqv) { if (otype != obj2->get_otype()) return new BoolObj(false); if (otype & CLS_BOOL_OBJ) return new BoolObj( - static_cast<BoolObj*>(obj1)->val == + static_cast<BoolObj*>(obj1)->val == static_cast<BoolObj*>(obj2)->val); if (otype & CLS_SYM_OBJ) return new BoolObj( @@ -1312,7 +1312,7 @@ do { \ throw NormalError(RUN_ERR_QUEUE_OVERFLOW); \ } while (0) - + static EvalObj *q1[EQUAL_QUEUE_SIZE], *q2[EQUAL_QUEUE_SIZE]; ARGS_EXACTLY_TWO; @@ -1353,8 +1353,8 @@ do { \ VecObj *vb = static_cast<VecObj*>(b); if (va->get_size() != vb->get_size()) return new BoolObj(false); - for (EvalObjVec::iterator - it = va->vec.begin(); + for (EvalObjVec::iterator + it = va->vec.begin(); it != va->vec.end(); it++) { *r1 = TO_PAIR(a)->car; @@ -1362,8 +1362,8 @@ do { \ CHK1; } - for (EvalObjVec::iterator - it = vb->vec.begin(); + for (EvalObjVec::iterator + it = vb->vec.begin(); it != vb->vec.end(); it++) { *r2 = TO_PAIR(b)->car; @@ -1373,7 +1373,7 @@ do { \ } else if (otype & CLS_BOOL_OBJ) { - if (static_cast<BoolObj*>(a)->val != + if (static_cast<BoolObj*>(a)->val != static_cast<BoolObj*>(b)->val) return new BoolObj(false); } @@ -26,7 +26,7 @@ class CompNumObj: public InexactNumObj { /** Construct a complex number */ CompNumObj(double _real, double _imag); - /** Try to construct an CompNumObj object + /** Try to construct an CompNumObj object * @return NULL if failed */ static CompNumObj *from_string(string repr); @@ -51,7 +51,7 @@ class RealNumObj: public InexactNumObj { double real; /** Construct a real number */ RealNumObj(double _real); - /** Try to construct an RealNumObj object + /** Try to construct an RealNumObj object * @return NULL if failed */ static RealNumObj *from_string(string repr); @@ -91,7 +91,7 @@ class RatNumObj: public ExactNumObj { mpq_class val; RatNumObj(mpq_class val); #endif - /** Try to construct an RatNumObj object + /** Try to construct an RatNumObj object * @return NULL if failed */ static RatNumObj *from_string(string repr); @@ -124,7 +124,7 @@ class IntNumObj: public ExactNumObj { IntNumObj(mpz_class val); int get_i(); #endif - /** Try to construct an IntNumObj object + /** Try to construct an IntNumObj object * @return NULL if failed */ static IntNumObj *from_string(string repr); @@ -148,22 +148,22 @@ class IntNumObj: public ExactNumObj { class SpecialOptIf: public SpecialOptObj { private: unsigned char state; /**< 0 for prepared, 1 for pre_called */ - /** + /** * The evaluator will call this after the <condition> exp is evaluated. * And this function tells the evaluator which of <consequence> and * <alternative> should be evaluted. */ - void pre_call(ArgList *args, Pair *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, Pair *pc, + EvalObj *post_call(ArgList *args, Pair *pc, Environment *envt); public: SpecialOptIf(); void prepare(Pair *pc); - Pair *call(ArgList *args, Environment * &envt, + Pair *call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr); ReprCons *get_repr_cons(); }; @@ -175,7 +175,7 @@ class SpecialOptLambda: public SpecialOptObj { public: SpecialOptLambda(); void prepare(Pair *pc); - Pair *call(ArgList *args, Environment * &envt, + Pair *call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr); ReprCons *get_repr_cons(); @@ -186,9 +186,9 @@ class SpecialOptLambda: public SpecialOptObj { */ class SpecialOptDefine: public SpecialOptObj { public: - SpecialOptDefine(); + SpecialOptDefine(); void prepare(Pair *pc); - Pair *call(ArgList *args, Environment * &envt, + Pair *call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr); ReprCons *get_repr_cons(); }; @@ -200,7 +200,7 @@ class SpecialOptSet: public SpecialOptObj { public: SpecialOptSet(); void prepare(Pair *pc); - Pair *call(ArgList *args, Environment * &envt, + Pair *call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr); ReprCons *get_repr_cons(); }; @@ -212,7 +212,7 @@ class SpecialOptQuote: public SpecialOptObj { public: SpecialOptQuote(); void prepare(Pair *pc); - Pair *call(ArgList *args, Environment * &envt, + Pair *call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr); ReprCons *get_repr_cons(); @@ -227,7 +227,7 @@ class SpecialOptEval: public SpecialOptObj { public: SpecialOptEval(); void prepare(Pair *pc); - Pair *call(ArgList *args, Environment * &envt, + Pair *call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr); ReprCons *get_repr_cons(); @@ -242,7 +242,7 @@ class SpecialOptAnd: public SpecialOptObj { public: SpecialOptAnd(); void prepare(Pair *pc); - Pair *call(ArgList *args, Environment * &envt, + Pair *call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr); ReprCons *get_repr_cons(); @@ -78,7 +78,7 @@ void push(Pair * &pc, FrameObj ** &top_ptr, Environment *envt) { *top_ptr++ = new RetAddr(pc); // Push the return address if (!is_list(TO_PAIR(pc->car))) - throw TokenError(pc->car->ext_repr(), RUN_ERR_WRONG_NUM_OF_ARGS); + throw TokenError(pc->car->ext_repr(), RUN_ERR_WRONG_NUM_OF_ARGS); // static_cast because of is_simple_obj() is false pc = static_cast<Pair*>(pc->car); // Go deeper to enter the call envt->get_obj(pc->car)->prepare(pc); @@ -90,7 +90,7 @@ EvalObj *Evaluator::run_expr(Pair *prog) { Pair *pc = prog; Continuation *cont = NULL; // envt is this->envt - push(pc, top_ptr, envt); + push(pc, top_ptr, envt); while((*eval_stack)->is_ret_addr()) { @@ -120,7 +120,7 @@ EvalObj *Evaluator::run_expr(Pair *prog) { else pc = nexp; top_ptr++; } - else + else { EvalObj *opt = args->car; if (opt->is_opt_obj()) @@ -3,7 +3,7 @@ #include "model.h" const int EVAL_STACK_SIZE = 262144; -/** @class Evaluator +/** @class Evaluator * A runtime platform of interpreting */ class Evaluator { @@ -19,7 +19,7 @@ int main() { { Pair *tree = ast->absorb(tk); if (!tree) break; - fprintf(stderr, "Ret> $%d = %s\n", rcnt++, + fprintf(stderr, "Ret> $%d = %s\n", rcnt++, eval->run_expr(tree)->ext_repr().c_str()); } catch (GeneralError &e) @@ -14,11 +14,11 @@ EmptyList::EmptyList() : Pair(NULL, NULL) {} ReprCons *EmptyList::get_repr_cons() { return new ReprStr("()"); } -bool FrameObj::is_ret_addr() { +bool FrameObj::is_ret_addr() { return ftype & CLS_RET_ADDR; } -bool FrameObj::is_parse_bracket() { +bool FrameObj::is_parse_bracket() { return ftype & CLS_PAR_BRA; } @@ -61,9 +61,9 @@ bool EvalObj::is_true() { string EvalObj::ext_repr() { hash.clear(); - // TODO: Performance improvement + // TODO: Performance improvement // (from possibly O(n^2logn) to strictly O(nlogn)) - ReprCons **top_ptr = repr_stack; + ReprCons **top_ptr = repr_stack; *top_ptr++ = this->get_repr_cons(); EvalObj *obj; hash.insert(this); @@ -105,8 +105,8 @@ string EvalObj::ext_repr() { return res; } -Pair::Pair(EvalObj *_car, EvalObj *_cdr) : - EvalObj(CLS_PAIR_OBJ), car(_car), cdr(_cdr), +Pair::Pair(EvalObj *_car, EvalObj *_cdr) : + EvalObj(CLS_PAIR_OBJ), car(_car), cdr(_cdr), next(NULL) {} ReprCons *Pair::get_repr_cons() { @@ -115,26 +115,26 @@ Pair::Pair(EvalObj *_car, EvalObj *_cdr) : RetAddr::RetAddr(Pair *_addr) : FrameObj(CLS_RET_ADDR), addr(_addr) {} -ParseBracket::ParseBracket(unsigned char _btype) : +ParseBracket::ParseBracket(unsigned char _btype) : FrameObj(CLS_SIM_OBJ | CLS_PAR_BRA), btype(_btype) {} UnspecObj::UnspecObj() : EvalObj(CLS_SIM_OBJ) {} - ReprCons *UnspecObj::get_repr_cons() { + ReprCons *UnspecObj::get_repr_cons() { return new ReprStr("#<Unspecified>"); } -SymObj::SymObj(const string &str) : +SymObj::SymObj(const string &str) : EvalObj(CLS_SIM_OBJ | CLS_SYM_OBJ), val(str) {} - ReprCons *SymObj::get_repr_cons() { - return new ReprStr(val); + ReprCons *SymObj::get_repr_cons() { + return new ReprStr(val); } OptObj::OptObj() : EvalObj(CLS_SIM_OBJ | CLS_OPT_OBJ) {} -ProcObj::ProcObj(Pair *_body, - Environment *_envt, +ProcObj::ProcObj(Pair *_body, + Environment *_envt, EvalObj *_params) : OptObj(), body(_body), params(_params), envt(_envt) {} @@ -145,11 +145,11 @@ ProcObj::ProcObj(Pair *_body, 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); + Environment *_envt = new Environment(envt); // static_cast<SymObj*> because the params is already checked EvalObj *ppar, *nptr; for (ppar = params; - ppar->is_pair_obj(); + ppar->is_pair_obj(); ppar = TO_PAIR(ppar)->cdr) { if ((nptr = args->cdr) != empty_list) @@ -164,13 +164,13 @@ ProcObj::ProcObj(Pair *_body, throw TokenError("", RUN_ERR_WRONG_NUM_OF_ARGS); genvt = _envt; - cont = _cont; + cont = _cont; *top_ptr++ = new RetAddr(NULL); // Mark the entrance of a cont return body; // Move pc to the proc entry point } -ReprCons *ProcObj::get_repr_cons() { - return new ReprStr("#<Procedure>"); +ReprCons *ProcObj::get_repr_cons() { + return new ReprStr("#<Procedure>"); } SpecialOptObj::SpecialOptObj(string _name) : OptObj(), name(_name) {} @@ -179,12 +179,12 @@ BoolObj::BoolObj(bool _val) : EvalObj(CLS_SIM_OBJ | CLS_BOOL_OBJ), val(_val) {} bool BoolObj::is_true() { return val; } -ReprCons *BoolObj::get_repr_cons() { - return new ReprStr(val ? "#t" : "#f"); +ReprCons *BoolObj::get_repr_cons() { + return new ReprStr(val ? "#t" : "#f"); } BoolObj *BoolObj::from_string(string repr) { - if (repr.length() != 2 || repr[0] != '#') + if (repr.length() != 2 || repr[0] != '#') return NULL; if (repr[1] == 't') return new BoolObj(true); @@ -193,15 +193,15 @@ BoolObj *BoolObj::from_string(string repr) { return NULL; } -NumObj::NumObj(NumLvl _level, bool _exactness) : +NumObj::NumObj(NumLvl _level, bool _exactness) : EvalObj(CLS_SIM_OBJ | CLS_NUM_OBJ), exactness(_exactness), level(_level) {} bool NumObj::is_exact() { return exactness; } StrObj::StrObj(string _str) : EvalObj(CLS_SIM_OBJ | CLS_STR_OBJ), str(_str) {} - ReprCons *StrObj::get_repr_cons() { - return new ReprStr(str); + ReprCons *StrObj::get_repr_cons() { + return new ReprStr(str); } CharObj::CharObj(char _ch) : EvalObj(CLS_SIM_OBJ | CLS_CHAR_OBJ), ch(_ch) {} @@ -231,7 +231,7 @@ EvalObj *VecObj::get_obj(int idx) { return vec[idx]; } -size_t VecObj::get_size() { +size_t VecObj::get_size() { return vec.end() - vec.begin(); } @@ -265,7 +265,7 @@ BuiltinProcObj::BuiltinProcObj(BuiltinProc f, string _name) : return ret_addr->next; // Move to the next instruction } -ReprCons *BuiltinProcObj::get_repr_cons() { +ReprCons *BuiltinProcObj::get_repr_cons() { return new ReprStr("#<Builtin Procedure: " + name + ">"); } @@ -292,10 +292,10 @@ EvalObj *Environment::get_obj(EvalObj *obj) { throw TokenError(name, RUN_ERR_UNBOUND_VAR); } -Continuation::Continuation(Environment *_envt, Pair *_pc, - Continuation *_prev_cont, - Pair *_proc_body) : - prev_cont(_prev_cont), envt(_envt), pc(_pc), +Continuation::Continuation(Environment *_envt, Pair *_pc, + Continuation *_prev_cont, + Pair *_proc_body) : + prev_cont(_prev_cont), envt(_envt), pc(_pc), proc_body(_proc_body) {} ReprCons::ReprCons(bool _done, EvalObj *_ori) : ori(_ori), done(_done) {} @@ -304,7 +304,7 @@ Continuation::Continuation(Environment *_envt, Pair *_pc, throw NormalError(INT_ERR); } -PairReprCons::PairReprCons(Pair *_ptr, EvalObj *_ori) : +PairReprCons::PairReprCons(Pair *_ptr, EvalObj *_ori) : ReprCons(false, _ori), state(0), ptr(_ptr) {} EvalObj *PairReprCons::next(const string &prev) { @@ -314,14 +314,14 @@ PairReprCons::PairReprCons(Pair *_ptr, EvalObj *_ori) : { state = 1; res = TO_PAIR(ptr)->car; - if (res->is_pair_obj()) + if (res->is_pair_obj()) repr += "("; return res; } else if (state == 1) - { + { state = 2; - if (TO_PAIR(ptr)->car->is_pair_obj()) + if (TO_PAIR(ptr)->car->is_pair_obj()) repr += ")"; ptr = TO_PAIR(ptr)->cdr; if (ptr == empty_list) @@ -331,7 +331,7 @@ PairReprCons::PairReprCons(Pair *_ptr, EvalObj *_ori) : repr += ". "; return ptr; } - else + else { return NULL; } @@ -351,7 +351,7 @@ VectReprCons::VectReprCons(VecObj *_ptr, EvalObj *_ori) : repr += ")"; return NULL; } - else + else { if (idx) repr += " "; EvalObj *res = ptr->get_obj(idx++); @@ -363,11 +363,11 @@ VectReprCons::VectReprCons(VecObj *_ptr, EvalObj *_ori) : bool is_list(Pair *ptr) { if (ptr == empty_list) return true; - EvalObj *nptr; - for (;;) - if ((nptr = ptr->cdr)->is_pair_obj()) - ptr = TO_PAIR(nptr); - else break; + EvalObj *nptr; + for (;;) + if ((nptr = ptr->cdr)->is_pair_obj()) + ptr = TO_PAIR(nptr); + else break; return ptr->cdr == empty_list; -} +} @@ -14,8 +14,8 @@ using std::vector; using std::set; // the range of unsigned char is enough for these types -typedef unsigned char ClassType; -typedef unsigned char NumLvl; +typedef unsigned char ClassType; +typedef unsigned char NumLvl; const int CLS_RET_ADDR = 1 << 0; const int CLS_EVAL_OBJ = 1 << 1; @@ -49,7 +49,7 @@ class FrameObj { * Report the type of the FrameObj, which can avoid the use of * dynamic_cast to improve efficiency. See the constructor for detail */ - ClassType ftype; + ClassType ftype; public: /** * Construct an EvalObj @@ -92,7 +92,7 @@ class EvalObj : public FrameObj { */ EvalObj(int otype = CLS_SIM_OBJ); /** Check if the object is a simple object (instead of a call - * invocation) + * invocation) * @return true if the object is not a construction (Pair) * */ bool is_simple_obj(); @@ -111,7 +111,7 @@ class EvalObj : public FrameObj { /** Any EvalObj has its external representation */ string ext_repr(); /** Always true for all EvalObjs except BoolObj */ - virtual bool is_true(); + virtual bool is_true(); virtual ReprCons *get_repr_cons() = 0; }; @@ -158,7 +158,7 @@ class ReprCons { EvalObj *ori; bool done; string repr; - ReprCons(bool done, EvalObj *ori = NULL); + ReprCons(bool done, EvalObj *ori = NULL); virtual EvalObj *next(const string &prev) = 0; }; @@ -234,9 +234,9 @@ class OptObj: public EvalObj { * @param envt The current environment (may be modified) * @param cont The current continuation (may be modified) * @param top_ptr Pointing to the top of the stack (may be modified) - * @return New value for pc register + * @return New value for pc register */ - virtual Pair *call(ArgList *args, Environment * &envt, + virtual Pair *call(ArgList *args, Environment * &envt, Continuation * &cont, FrameObj ** &top_ptr) = 0; }; @@ -246,7 +246,7 @@ class OptObj: public EvalObj { class ProcObj: public OptObj { public: /** The procedure body, a list of expressions to be evaluated */ - Pair *body; + Pair *body; /** The arguments: <list> | var1 ... | var1 var2 ... . varn */ EvalObj *params; /** Pointer to the environment */ @@ -276,7 +276,7 @@ typedef EvalObj* (*BuiltinProc)(ArgList *, const string &); class BuiltinProcObj: public OptObj { private: /** The function that tackle the inputs in effect */ - BuiltinProc handler; + BuiltinProc handler; string name; public: /** @@ -295,11 +295,11 @@ class BuiltinProcObj: public OptObj { */ class BoolObj: public EvalObj { public: - bool val; /**< true for \#t, false for \#f */ + bool val; /**< true for \#t, false for \#f */ BoolObj(bool); /**< Converts a C bool value to a BoolObj*/ bool is_true(); /**< Override EvalObj `is_true()` */ ReprCons *get_repr_cons(); - /** Try to construct an BoolObj object + /** Try to construct an BoolObj object * @return NULL if failed */ static BoolObj *from_string(string repr); @@ -319,7 +319,7 @@ class NumObj: public EvalObj { */ NumLvl level; - /** + /** * Construct a general Numeric object */ NumObj(NumLvl level, bool _exactness); @@ -340,10 +340,10 @@ class NumObj: public EvalObj { class StrObj: public EvalObj { public: string str; - + /** Construct a string object */ StrObj(string str); - /** Try to construct an StrObj object + /** Try to construct an StrObj object * @return NULL if failed */ static StrObj *from_string(string repr); @@ -356,10 +356,10 @@ class StrObj: public EvalObj { class CharObj: public EvalObj { public: char ch; - + /** Construct a string object */ CharObj(char ch); - /** Try to construct an CharObj object + /** Try to construct an CharObj object * @return NULL if failed */ static CharObj *from_string(string repr); @@ -388,7 +388,7 @@ class VecObj: public EvalObj { typedef map<string, EvalObj*> Str2EvalObj; /** @class Environment - * The environment of current evaluation, i.e. the local variable binding + * The environment of current evaluation, i.e. the local variable binding */ class Environment { private: @@ -400,14 +400,14 @@ class Environment { * @param prev_envt the outer environment */ Environment(Environment *prev_envt); - /** Add a binding entry which binds sym_obj to eval_obj + /** Add a binding entry which binds sym_obj to eval_obj * @param def true to force the assignment * @return when def is set to false, this return value is true iff. the * assignment carried out successfully */ - bool add_binding(SymObj *sym_obj, EvalObj *eval_obj, bool def = true); + bool add_binding(SymObj *sym_obj, EvalObj *eval_obj, bool def = true); /** Extract the corresponding EvalObj if obj is a SymObj, or just - * simply return obj as it is + * simply return obj as it is * @param obj the object as request * */ EvalObj *get_obj(EvalObj *obj); @@ -421,7 +421,7 @@ class Environment { class Continuation { public: /** Linking the previous continuation on the chain */ - Continuation *prev_cont; + Continuation *prev_cont; Environment *envt; /**< The saved envt */ Pair *pc; /**< The saved pc */ /** Pointing to the current expression that is being evaluated. @@ -430,10 +430,10 @@ class Continuation { Pair *proc_body; /** Create a continuation */ - Continuation(Environment *envt, Pair *pc, Continuation *prev_cont, + Continuation(Environment *envt, Pair *pc, Continuation *prev_cont, Pair *proc_body); }; -bool is_list(); +bool is_list(Pair *ptr); #endif @@ -24,7 +24,7 @@ void Tokenizor::set_stream(FILE *_stream) { #define IS_SLASH(ch) \ ((ch) == '\\') #define IS_BRACKET(ch) \ - ((ch) == '(' || (ch) == ')') + ((ch) == '(' || (ch) == ')') #define IS_SPACE(ch) \ ((ch) == ' ' || (ch) == '\t' || IS_NEWLINE(ch)) #define IS_COMMENT(ch) \ @@ -66,7 +66,7 @@ bool Tokenizor::get_token(string &ret) { case 't': *buff_ptr++ = '\t'; break; default: { buff_ptr = buff; - throw TokenError(string("") + ch, + throw TokenError(string("") + ch, PAR_ERR_ILLEGAL_CHAR_IN_ESC); } } @@ -96,23 +96,23 @@ bool Tokenizor::get_token(string &ret) { flag = true; } } - else if (IS_QUOTE(ch)) + else if (IS_QUOTE(ch)) { // in a double-quote which is being enclosed *buff_ptr++ = '\"'; POP; - return true; // prevent duplicate quote sign + return true; // prevent duplicate quote sign } } } - if (in_quote || !IS_SPACE(ch)) + if (in_quote || !IS_SPACE(ch)) { if (in_quote && IS_SLASH(ch)) escaping = true; - else + else *buff_ptr++ = ch; } - if (flag) + if (flag) { str_to_lower(ret); return true; @@ -120,7 +120,7 @@ bool Tokenizor::get_token(string &ret) { } } if (buff_ptr != buff) POP; - return false; // can't read more + return false; // can't read more } ASTGenerator::ASTGenerator() {} @@ -152,7 +152,7 @@ Pair *ASTGenerator::absorb(Tokenizor *tk) { if (top_ptr == parse_stack + PARSE_STACK_SIZE) throw TokenError("Parser", RUN_ERR_STACK_OVERFLOW); - if (top_ptr - parse_stack > 1 && + if (top_ptr - parse_stack > 1 && !IS_BRAKET(*(top_ptr - 1)) && IS_BRAKET(*(top_ptr - 2))) { @@ -173,7 +173,7 @@ Pair *ASTGenerator::absorb(Tokenizor *tk) { string token; if (!tk->get_token(token)) return NULL; if (token == "(") // a list - *top_ptr++ = new ParseBracket(0); + *top_ptr++ = new ParseBracket(0); else if (token == "#(") // a vector *top_ptr++ = new ParseBracket(1); else if (token == "\'") // syntatic sugar for quote @@ -189,8 +189,8 @@ Pair *ASTGenerator::absorb(Tokenizor *tk) { EvalObj *obj = TO_EVAL(*top_ptr); if (obj->is_sym_obj() && static_cast<SymObj*>(obj)->val == ".") { - if (improper || - lst == empty_list || + if (improper || + lst == empty_list || TO_PAIR(lst)->cdr != empty_list) throw NormalError(PAR_ERR_IMPROPER_PAIR); improper = true; @@ -203,7 +203,7 @@ Pair *ASTGenerator::absorb(Tokenizor *tk) { lst = _lst; } } - + ParseBracket *bptr = TO_BRACKET(*top_ptr); if (bptr->btype == 0) *top_ptr++ = lst; @@ -9,7 +9,7 @@ using std::string; const int TOKEN_BUFF_SIZE = 65536; const int PARSE_STACK_SIZE = 262144; -/** @class Tokenizor +/** @class Tokenizor * Break down the input string stream into tokens */ class Tokenizor { @@ -22,7 +22,7 @@ class Tokenizor { /** Set the stream to be read from (without setting this, the default * would be stdin) */ void set_stream(FILE *stream); - /** Extract the next token + /** Extract the next token * @param ret the extracted token * @return false if nothing can be read further * */ @@ -37,13 +37,13 @@ class ASTGenerator { private: /** Convert the string to an internal object */ static EvalObj* to_obj(const string &); - public: + public: ASTGenerator(); /** Read tokens from Tokenizor tk, then return a AST * @param tk pointer to a Tokenizor * @return Abstract Syntax Tree */ - Pair *absorb(Tokenizor *tk); + Pair *absorb(Tokenizor *tk); }; #endif |