From 5e16df071dfd41659b296dfde067749ef216a47c Mon Sep 17 00:00:00 2001 From: Teddy Date: Sat, 17 Aug 2013 11:24:42 +0800 Subject: more doc --- TODO.rst | 2 +- builtin.cpp | 2 +- builtin.h | 6 +- model.cpp | 4 +- types.cpp | 314 ++++++++++++++++++++++++++++++------------------------------ types.h | 141 +++++++++++++++++++-------- 6 files changed, 265 insertions(+), 204 deletions(-) diff --git a/TODO.rst b/TODO.rst index 23082bc..04b9fc1 100644 --- a/TODO.rst +++ b/TODO.rst @@ -3,7 +3,7 @@ - model --- done - eval --- done - builtin --- almost - - types + - types --- done - gc - consts - parser diff --git a/builtin.cpp b/builtin.cpp index 0481024..bbc2f93 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -590,7 +590,7 @@ Pair *SpecialOptForce::call(Pair *_args, Environment * &lenvt, { gc.attach(static_cast(*(++top_ptr))); top_ptr++; - nexp = cont->state = prom->get_entry(); + nexp = cont->state = prom->get_exp(); nexp->next = NULL; gc.expose(_args); return nexp; diff --git a/builtin.h b/builtin.h index e6beb4a..67b07a7 100644 --- a/builtin.h +++ b/builtin.h @@ -17,10 +17,10 @@ class SpecialOptIf: public SpecialOptObj {/*{{{*/ public: /** Construct a `if` operator */ SpecialOptIf(); - /** Prevent and from being evaluated */ + /** Prevent \ and \ from being evaluated */ void prepare(Pair *pc); /** When it's invoked at the first time, it will determined which of - * and should be evaluated. Then when it's + * \ and \ should be evaluated. Then when it's * invoked again, it will tell the system the corresponding result.*/ Pair *call(Pair *args, Environment * &envt, Continuation * &cont, EvalObj ** &top_ptr, Pair *pc); @@ -138,7 +138,7 @@ class SpecialOptApply: public SpecialOptObj {/*{{{*/ SpecialOptApply(); /** Nothing special */ void prepare(Pair *pc); - /** Provoke the with args */ + /** Provoke the \ with args */ Pair *call(Pair *args, Environment * &envt, Continuation * &cont, EvalObj ** &top_ptr, Pair *pc); diff --git a/model.cpp b/model.cpp index 2e28795..e454c72 100644 --- a/model.cpp +++ b/model.cpp @@ -100,9 +100,9 @@ string EvalObj::ext_repr() { *top_ptr++ = this->get_repr_cons(); EvalObj *obj; hash.insert(this); - while (!(*repr_stack)->done) + while (!(*repr_stack)->prim) { - if ((*(top_ptr - 1))->done) + if ((*(top_ptr - 1))->prim) { top_ptr -= 2; obj = (*top_ptr)->next((*(top_ptr + 1))->repr); diff --git a/types.cpp b/types.cpp index aef780f..331dc4b 100644 --- a/types.cpp +++ b/types.cpp @@ -43,20 +43,20 @@ ReprCons *Pair::get_repr_cons() { SymObj::SymObj(const string &str) : - EvalObj(CLS_SIM_OBJ | CLS_SYM_OBJ), val(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(int otype) : - Container(otype | CLS_SIM_OBJ | CLS_OPT_OBJ, true) {} +Container(otype | CLS_SIM_OBJ | CLS_OPT_OBJ, true) {} void OptObj::gc_decrement() {} void OptObj::gc_trigger(EvalObj ** &tail) {} ProcObj::ProcObj(Pair *_body, Environment *_envt, EvalObj *_params) : - OptObj(CLS_CONTAINER), body(_body), params(_params), envt(_envt) { +OptObj(CLS_CONTAINER), body(_body), params(_params), envt(_envt) { gc.attach(body); gc.attach(params); gc.attach(envt); @@ -71,7 +71,6 @@ ProcObj::~ProcObj() { Pair *ProcObj::call(Pair *_args, Environment * &lenvt, Continuation * &cont, EvalObj ** &top_ptr, Pair *pc) { // Create a new continuation - // static_cast see `call` invocation in eval.cpp Pair *ret_addr = cont->pc; if (cont->state) { @@ -80,16 +79,17 @@ Pair *ProcObj::call(Pair *_args, Environment * &lenvt, { gc.expose(*top_ptr); *top_ptr++ = gc.attach(TO_PAIR(_args->cdr)->car); - EXIT_CURRENT_EXEC(lenvt, cont, _args); // exit cont and envt + EXIT_CURRENT_EXEC(lenvt, cont, _args); // exit cont and envt return ret_addr->next; } else { - if (nexp->cdr == empty_list && !nexp->car->is_simple_obj()) // tail recursion opt + // tail recursion opt + if (nexp->cdr == empty_list && !nexp->car->is_simple_obj()) { cont->tail = true; cont->state = NULL; - top_ptr++; // revert the cont + top_ptr++; // revert the cont } else { @@ -116,11 +116,13 @@ Pair *ProcObj::call(Pair *_args, Environment * &lenvt, if ((nptr = args->cdr) != empty_list) args = TO_PAIR(nptr); else break; - lenvt->add_binding(static_cast(TO_PAIR(ppar)->car), args->car); + lenvt->add_binding(static_cast(TO_PAIR(ppar)->car), + args->car); } + // (... . var_n) if (ppar->is_sym_obj()) - lenvt->add_binding(static_cast(ppar), args->cdr); // (... . var_n) + lenvt->add_binding(static_cast(ppar), args->cdr); else if (args->cdr != empty_list || ppar != empty_list) throw TokenError("", RUN_ERR_WRONG_NUM_OF_ARGS); @@ -128,7 +130,8 @@ Pair *ProcObj::call(Pair *_args, Environment * &lenvt, top_ptr++; cont->state = body; gc.expose(_args); - return cont->state; // Move pc to the proc entry point + // Move pc to the proc entry point + return cont->state; } } @@ -172,15 +175,15 @@ BoolObj *BoolObj::from_string(string repr) { } NumObj::NumObj(NumLvl _level, bool _exactness) : - EvalObj(CLS_SIM_OBJ | CLS_NUM_OBJ), exactness(_exactness), level(_level) {} +EvalObj(CLS_SIM_OBJ | CLS_NUM_OBJ), exactness(_exactness), level(_level) {} - bool NumObj::is_exact() { return exactness; } +bool NumObj::is_exact() { return exactness; } - StrObj::StrObj(string _str) : EvalObj(CLS_SIM_OBJ | CLS_STR_OBJ), str(_str) {} +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) {} @@ -204,7 +207,7 @@ ReprCons *CharObj::get_repr_cons() { } VecObj::VecObj(size_t size, EvalObj *fill) : - Container(CLS_SIM_OBJ | CLS_VECT_OBJ) { +Container(CLS_SIM_OBJ | CLS_VECT_OBJ) { vec.resize(size); for (size_t i = 0; i < size; i++) { @@ -294,24 +297,24 @@ bool StrObj::eq(StrObj *r) { } BuiltinProcObj::BuiltinProcObj(BuiltinProc f, string _name) : - OptObj(), handler(f), name(_name) {} +OptObj(), handler(f), name(_name) {} - Pair *BuiltinProcObj::call(Pair *args, Environment * &lenvt, - Continuation * &cont, EvalObj ** &top_ptr, Pair *pc) { +Pair *BuiltinProcObj::call(Pair *args, Environment * &lenvt, + Continuation * &cont, EvalObj ** &top_ptr, Pair *pc) { - Pair *ret_addr = cont->pc; - gc.expose(*top_ptr); - *top_ptr++ = gc.attach(handler(TO_PAIR(args->cdr), name)); - EXIT_CURRENT_EXEC(lenvt, cont, args); - return ret_addr->next; // Move to the next instruction - } + Pair *ret_addr = cont->pc; + gc.expose(*top_ptr); + *top_ptr++ = gc.attach(handler(TO_PAIR(args->cdr), name)); + EXIT_CURRENT_EXEC(lenvt, cont, args); + return ret_addr->next; // Move to the next instruction +} ReprCons *BuiltinProcObj::get_repr_cons() { return new ReprStr("#"); } Environment::Environment(Environment *_prev_envt) : - Container(), prev_envt(_prev_envt) { +Container(), prev_envt(_prev_envt) { gc.attach(prev_envt); } @@ -392,16 +395,14 @@ EvalObj *Environment::get_obj(EvalObj *obj) { throw TokenError(name, RUN_ERR_UNBOUND_VAR); } -Environment *Environment::get_prev() { - return prev_envt; +Continuation::Continuation(Environment *_envt, Pair *_pc, + Continuation *_prev_cont ) : +Container(), prev_cont(_prev_cont), envt(_envt), pc(_pc), +state(NULL), prog(NULL), tail(false) { + gc.attach(prev_cont); + gc.attach(envt); } -Continuation::Continuation(Environment *_envt, Pair *_pc, Continuation *_prev_cont ) : - Container(), prev_cont(_prev_cont), envt(_envt), pc(_pc), state(NULL), prog(NULL), tail(false) { - gc.attach(prev_cont); - gc.attach(envt); - } - Continuation::~Continuation() { gc.expose(prev_cont); gc.expose(envt); @@ -421,7 +422,7 @@ ReprCons *Continuation::get_repr_cons() { return new ReprStr("#"); } -ReprCons::ReprCons(bool _done, EvalObj *_ori) : ori(_ori), done(_done) {} +ReprCons::ReprCons(bool _prim, EvalObj *_ori) : ori(_ori), prim(_prim) {} ReprStr::ReprStr(string _repr) : ReprCons(true) { repr = _repr; } EvalObj *ReprStr::next(const string &prev) { fprintf(stderr, "Oops in ReprStr::next\n"); @@ -429,85 +430,85 @@ EvalObj *ReprStr::next(const string &prev) { } PairReprCons::PairReprCons(Pair *_ptr, EvalObj *_ori) : - ReprCons(false, _ori), state(0), ptr(_ptr) {} +ReprCons(false, _ori), state(0), ptr(_ptr) {} - EvalObj *PairReprCons::next(const string &prev) { - repr += prev; - EvalObj *res; - if (state == 0) - { - state = 1; - res = TO_PAIR(ptr)->car; - if (res->is_pair_obj()) - repr += "("; - return res; - } - else if (state == 1) - { - state = 2; - if (TO_PAIR(ptr)->car->is_pair_obj()) - repr += ")"; - ptr = TO_PAIR(ptr)->cdr; - if (ptr == empty_list) - return NULL; - repr += " "; - if (ptr->is_simple_obj()) - repr += ". "; - return ptr; - } - else - { +EvalObj *PairReprCons::next(const string &prev) { + repr += prev; + EvalObj *res; + if (state == 0) + { + state = 1; + res = TO_PAIR(ptr)->car; + if (res->is_pair_obj()) + repr += "("; + return res; + } + else if (state == 1) + { + state = 2; + if (TO_PAIR(ptr)->car->is_pair_obj()) + repr += ")"; + ptr = TO_PAIR(ptr)->cdr; + if (ptr == empty_list) return NULL; - } + repr += " "; + if (ptr->is_simple_obj()) + repr += ". "; + return ptr; + } + else + { + return NULL; } +} VectReprCons::VectReprCons(VecObj *_ptr, EvalObj *_ori) : - ReprCons(false, _ori), ptr(_ptr), idx(0) { repr = "#("; } +ReprCons(false, _ori), ptr(_ptr), idx(0) { repr = "#("; } - EvalObj *VectReprCons::next(const string &prev) { - repr += prev; +EvalObj *VectReprCons::next(const string &prev) { + repr += prev; - if (idx && ptr->get(idx - 1)->is_pair_obj()) - repr += ")"; + if (idx && ptr->get(idx - 1)->is_pair_obj()) + repr += ")"; - if (idx == ptr->get_size()) - { - repr += ")"; - return NULL; - } - else - { - if (idx) repr += " "; - EvalObj *res = ptr->get(idx++); - if (res->is_pair_obj()) - repr += "("; - return res; - } + if (idx == ptr->get_size()) + { + repr += ")"; + return NULL; + } + else + { + if (idx) repr += " "; + EvalObj *res = ptr->get(idx++); + if (res->is_pair_obj()) + repr += "("; + return res; } +} PromObj::PromObj(EvalObj *exp) : - Container(CLS_SIM_OBJ | CLS_PROM_OBJ), - entry(new Pair(exp, empty_list)), mem(NULL) { - gc.attach(entry); - entry->next = NULL; - } +Container(CLS_SIM_OBJ | CLS_PROM_OBJ), +exp(new Pair(exp, empty_list)), mem(NULL) { + gc.attach(exp); + exp->next = NULL; +} PromObj::~PromObj() { - gc.expose(entry); + gc.expose(exp); gc.expose(mem); } void PromObj::gc_decrement() { - GC_CYC_DEC(entry); + GC_CYC_DEC(exp); GC_CYC_DEC(mem); } void PromObj::gc_trigger(EvalObj ** &tail) { - GC_CYC_TRIGGER(entry); + GC_CYC_TRIGGER(exp); GC_CYC_TRIGGER(mem); } -Pair *PromObj::get_entry() { return entry; } +Pair *PromObj::get_exp() { return exp; } ReprCons *PromObj::get_repr_cons() { return new ReprStr("#"); } @@ -572,71 +573,71 @@ bool is_zero(double x) { InexactNumObj::InexactNumObj(NumLvl level) : NumObj(level, false) {} CompNumObj::CompNumObj(double _real, double _imag) : - InexactNumObj(NUM_LVL_COMP), real(_real), imag(_imag) {} - - CompNumObj *CompNumObj::from_string(string repr) { - // spos: the position of the last sign - // ipos: the position of i - long long spos = -1, ipos = -1; - size_t len = repr.length(); - bool sign = false; - for (size_t i = 0; i < len; i++) - if (repr[i] == '+' || repr[i] == '-') - { - spos = i; - sign = repr[i] == '-'; - } - else if (repr[i] == 'i' || repr[i] == 'I') - ipos = i; - - if (spos == -1 || ipos == -1 || !(spos < ipos)) - return NULL; +InexactNumObj(NUM_LVL_COMP), real(_real), imag(_imag) {} - double real = 0, imag = 1; - IntNumObj *int_ptr; - RatNumObj *rat_ptr; - RealNumObj *real_ptr; - if (spos > 0) +CompNumObj *CompNumObj::from_string(string repr) { + // spos: the position of the last sign + // ipos: the position of i + long long spos = -1, ipos = -1; + size_t len = repr.length(); + bool sign = false; + for (size_t i = 0; i < len; i++) + if (repr[i] == '+' || repr[i] == '-') { - string real_str = repr.substr(0, spos); - if ((int_ptr = IntNumObj::from_string(real_str))) + spos = i; + sign = repr[i] == '-'; + } + else if (repr[i] == 'i' || repr[i] == 'I') + ipos = i; + + if (spos == -1 || ipos == -1 || !(spos < ipos)) + return NULL; + + double real = 0, imag = 1; + IntNumObj *int_ptr; + RatNumObj *rat_ptr; + RealNumObj *real_ptr; + if (spos > 0) + { + string real_str = repr.substr(0, spos); + if ((int_ptr = IntNumObj::from_string(real_str))) #ifndef GMP_SUPPORT - real = int_ptr->val; + real = int_ptr->val; #else - real = int_ptr->val.get_d(); + real = int_ptr->val.get_d(); #endif - else if ((rat_ptr = RatNumObj::from_string(real_str))) + else if ((rat_ptr = RatNumObj::from_string(real_str))) #ifndef GMP_SUPPORT - real = rat_ptr->a / double(rat_ptr->b); + 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; - else return NULL; - } - if (ipos > spos + 1) - { - string imag_str = repr.substr(spos + 1, ipos - spos - 1); - if ((int_ptr = IntNumObj::from_string(imag_str))) + else if ((real_ptr = RealNumObj::from_string(real_str))) + real = real_ptr->real; + else return NULL; + } + if (ipos > spos + 1) + { + string imag_str = repr.substr(spos + 1, ipos - spos - 1); + if ((int_ptr = IntNumObj::from_string(imag_str))) #ifndef GMP_SUPPORT - imag = int_ptr->val; + imag = int_ptr->val; #else - imag = int_ptr->val.get_d(); + imag = int_ptr->val.get_d(); #endif - else if ((rat_ptr = RatNumObj::from_string(imag_str))) + else if ((rat_ptr = RatNumObj::from_string(imag_str))) #ifndef GMP_SUPPORT - imag = rat_ptr->a / double(rat_ptr->b); + imag = rat_ptr->a / double(rat_ptr->b); #else - imag = rat_ptr->val.get_d(); + imag = rat_ptr->val.get_d(); #endif - else if ((real_ptr = RealNumObj::from_string(imag_str))) - imag = real_ptr->real; - else return NULL; - } - if (sign) imag = -imag; - return new CompNumObj(real, imag); + else if ((real_ptr = RealNumObj::from_string(imag_str))) + imag = real_ptr->real; + else return NULL; } + if (sign) imag = -imag; + return new CompNumObj(real, imag); +} NumObj *CompNumObj::clone() const { return new CompNumObj(*this); @@ -744,7 +745,8 @@ ReprCons *CompNumObj::get_repr_cons() { #undef C #undef D -RealNumObj::RealNumObj(double _real) : InexactNumObj(NUM_LVL_REAL), real(_real) {} +RealNumObj::RealNumObj(double _real) : +InexactNumObj(NUM_LVL_REAL), real(_real) {} NumObj *RealNumObj::clone() const { return new RealNumObj(*this); @@ -833,14 +835,14 @@ ExactNumObj::ExactNumObj(NumLvl level) : NumObj(level, true) {} #ifndef GMP_SUPPORT RatNumObj::RatNumObj(int _a, int _b) : - ExactNumObj(NUM_LVL_RAT), a(_a), b(_b) { - if (b == 0) - throw NormalError(RUN_ERR_NUMERIC_OVERFLOW); - if (b < 0) a = -a, b = -b; - int g = gcd(a, b); - a /= g; - b /= g; - } +ExactNumObj(NUM_LVL_RAT), a(_a), b(_b) { + if (b == 0) + throw NormalError(RUN_ERR_NUMERIC_OVERFLOW); + if (b < 0) a = -a, b = -b; + int g = gcd(a, b); + a /= g; + b /= g; +} RatNumObj *RatNumObj::from_string(string repr) { int a, b; @@ -858,7 +860,7 @@ RatNumObj *RatNumObj::from_string(string repr) { } #else RatNumObj::RatNumObj(mpq_class _val) : ExactNumObj(NUM_LVL_RAT), val(_val) { - val.canonicalize(); + val.canonicalize(); } NumObj *RatNumObj::clone() const { @@ -881,7 +883,7 @@ RatNumObj *RatNumObj::from_string(string repr) { } RatNumObj::RatNumObj(const RatNumObj &ori) : - ExactNumObj(NUM_LVL_RAT), val(ori.val.get_mpq_t()) {} +ExactNumObj(NUM_LVL_RAT), val(ori.val.get_mpq_t()) {} #endif @@ -1060,7 +1062,7 @@ IntNumObj *IntNumObj::from_string(string repr) { } int IntNumObj::get_i() { return val.get_si(); } IntNumObj::IntNumObj(const IntNumObj &ori) : - ExactNumObj(NUM_LVL_INT), val(ori.val.get_mpz_t()) {} +ExactNumObj(NUM_LVL_INT), val(ori.val.get_mpz_t()) {} #endif diff --git a/types.h b/types.h index 147efc3..b66f36b 100644 --- a/types.h +++ b/types.h @@ -42,11 +42,12 @@ class PairReprCons; class Pair : public Container {/*{{{*/ public: EvalObj *car; /**< car (as in Scheme) */ - EvalObj *cdr; /**< cdr (as in Scheme) */ - Pair* next; /**< The next branch in effect */ + EvalObj *cdr; /**< cdr (as in Scheme) */ + /** The next branch in effect (make sense only in evaluation) */ + Pair* next; - Pair(EvalObj *car, EvalObj *cdr); /**< Create a Pair (car . cdr) */ - ~Pair(); + Pair(EvalObj *car, EvalObj *cdr); /**< Create a Pair (car . cdr) */ + ~Pair(); /**< The destructor */ ReprCons *get_repr_cons(); void gc_decrement(); void gc_trigger(EvalObj ** &tail); @@ -57,40 +58,62 @@ class Pair : public Container {/*{{{*/ */ class EmptyList: public Pair {/*{{{*/ public: - EmptyList(); + EmptyList(); /**< The constructor */ ReprCons *get_repr_cons(); };/*}}}*/ +/** @class ReprCons + * The abstraction class to represent a representation construction, which is + * used as stack frame in `ext_repr`. + */ class ReprCons {/*{{{*/ public: - EvalObj *ori; - bool done; + EvalObj *ori; /**< Reflexive pointer to the obj */ + bool prim; /**< true if no further expansion is needed */ + /** The finally generated external represenation of the EvalObj */ string repr; - ReprCons(bool done, EvalObj *ori = NULL); + /** The constructor */ + ReprCons(bool prim, EvalObj *ori = NULL); + /** This function is called to get the next component in a complex + * EvalObj + * @param prev Feed the string form of the previous component */ virtual EvalObj *next(const string &prev) = 0; };/*}}}*/ +/** @class ReprStr + * Used to store the generated external representation. + */ class ReprStr : public ReprCons {/*{{{*/ public: + /** The constructor */ ReprStr(string repr); EvalObj *next(const string &prev); };/*}}}*/ +/** @class PairReprCons + * The ReprCons implementation of Pair + */ class PairReprCons : public ReprCons {/*{{{*/ private: int state; EvalObj *ptr; public: + /** The constructor */ PairReprCons(Pair *ptr, EvalObj *ori); EvalObj *next(const string &prev); };/*}}}*/ class VecObj; + +/** @class VectReprCons + * The ReprCons implementation of VecObj + */ class VectReprCons : public ReprCons {/*{{{*/ private: VecObj *ptr; size_t idx; public: + /** The constructor */ VectReprCons(VecObj *ptr, EvalObj *ori); EvalObj *next(const string &prev); };/*}}}*/ @@ -110,13 +133,13 @@ class UnspecObj: public EvalObj {/*{{{*/ */ class SymObj: public EvalObj {/*{{{*/ public: + /** Storage implementation: string */ string val; - + /** The constructor */ SymObj(const string &); ReprCons *get_repr_cons(); };/*}}}*/ -// Everything is cons class Environment; class Continuation; @@ -126,6 +149,7 @@ class Continuation; class OptObj: public Container {/*{{{*/ public: + /** The constructor */ OptObj(int otype = 0); /** * The function is called when an operation is needed. @@ -133,6 +157,7 @@ class OptObj: public Container {/*{{{*/ * @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) + * @param pc Pointing to the entry of the call in AST * @return New value for pc register */ virtual Pair *call(Pair *args, Environment * &envt, @@ -149,7 +174,7 @@ class ProcObj: public OptObj {/*{{{*/ public: /** The procedure body, a list of expressions to be evaluated */ Pair *body; - /** The arguments: | var1 ... | var1 var2 ... . varn */ + /** The arguments: \ | var_1 ... | var_1 var_2 ... . var_n */ EvalObj *params; /** Pointer to the environment */ Environment *envt; @@ -170,8 +195,10 @@ class ProcObj: public OptObj {/*{{{*/ */ class SpecialOptObj: public OptObj {/*{{{*/ protected: + /** The name of this operator */ string name; public: + /** The constructor */ SpecialOptObj(string name); ReprCons *get_repr_cons(); };/*}}}*/ @@ -214,7 +241,6 @@ class BoolObj: public EvalObj {/*{{{*/ /** @class NumObj * The top level abstract of numbers */ - class NumObj: public EvalObj {/*{{{*/ protected: /** True if the number is of exact value */ @@ -229,20 +255,23 @@ class NumObj: public EvalObj {/*{{{*/ * Construct a general Numeric object */ NumObj(NumLvl level, bool _exactness); + /** Deep-copy a NumObj */ virtual NumObj *clone() const = 0; + /** @return true if it's an exact numeric value */ bool is_exact(); + /** Upper convert a NumObj `r` to the same type as `this` */ virtual NumObj *convert(NumObj *r) = 0; - virtual void add(NumObj *r) = 0; - virtual void sub(NumObj *r) = 0; - virtual void mul(NumObj *r) = 0; - virtual void div(NumObj *r) = 0; - virtual void abs(); - - virtual bool lt(NumObj *r); - virtual bool gt(NumObj *r); - virtual bool le(NumObj *r); - virtual bool ge(NumObj *r); - virtual bool eq(NumObj *r) = 0; + virtual void add(NumObj *r) = 0; /**< Addition */ + virtual void sub(NumObj *r) = 0; /**< Substraction */ + virtual void mul(NumObj *r) = 0; /**< Multiplication */ + virtual void div(NumObj *r) = 0; /**< Division */ + virtual void abs(); /**< Absolute function */ + + virtual bool lt(NumObj *r); /**< "<" implementation of numbers */ + virtual bool gt(NumObj *r); /**< ">" implementation of numbers */ + virtual bool le(NumObj *r); /**< "<=" implementation of numbers */ + virtual bool ge(NumObj *r); /**< ">=" implementation of numbers */ + virtual bool eq(NumObj *r) = 0; /**< "=" implementation of numbers */ };/*}}}*/ /** @class StrObj @@ -250,6 +279,7 @@ class NumObj: public EvalObj {/*{{{*/ */ class StrObj: public EvalObj {/*{{{*/ public: + /** Storage implementation: C++ string */ string str; /** Construct a string object */ @@ -258,11 +288,13 @@ class StrObj: public EvalObj {/*{{{*/ * @return NULL if failed */ static StrObj *from_string(string repr); - bool lt(StrObj *r); - bool gt(StrObj *r); - bool le(StrObj *r); - bool ge(StrObj *r); - bool eq(StrObj *r); + + bool lt(StrObj *r); /**< "<" implementation of strings */ + bool gt(StrObj *r); /**< ">" implementation of strings */ + bool le(StrObj *r); /**< "<=" implementation of strings */ + bool ge(StrObj *r); /**< ">=" implementation of strings */ + bool eq(StrObj *r); /**< "=" implementation of strings */ + ReprCons *get_repr_cons(); };/*}}}*/ @@ -271,8 +303,8 @@ class StrObj: public EvalObj {/*{{{*/ */ class CharObj: public EvalObj {/*{{{*/ public: + /** Storage implementation */ char ch; - /** Construct a string object */ CharObj(char ch); /** Try to construct an CharObj object @@ -289,10 +321,12 @@ class CharObj: public EvalObj {/*{{{*/ */ class VecObj: public Container {/*{{{*/ public: + /** Storage implementation: C++ STL vector */ EvalObjVec vec; /** Construct a vector object */ VecObj(size_t size = 0, EvalObj *fill = NULL); ~VecObj(); + /** Get the actual size of this vector */ size_t get_size(); /** Add a new element to the rear */ void push_back(EvalObj *new_elem); @@ -300,6 +334,7 @@ class VecObj: public Container {/*{{{*/ void fill(EvalObj *obj); /** Replace an existing element in the vector */ void set(size_t idx, EvalObj *obj); + /** Get the pointer to an element by index */ EvalObj *get(size_t idx); ReprCons *get_repr_cons(); @@ -313,13 +348,20 @@ class VecObj: public Container {/*{{{*/ */ class PromObj: public Container {/*{{{*/ private: - Pair *entry; + /** The delayed expression */ + Pair *exp; + /** The memorized result */ EvalObj *mem; public: + /** Construct with a delayed expression */ PromObj(EvalObj *exp); + /** The destructor */ ~PromObj(); - Pair *get_entry(); + /** Get the delayed expression */ + Pair *get_exp(); + /** Extract the memorized result */ EvalObj *get_mem(); + /** Provide with the result to let the PromObj remember */ void feed_mem(EvalObj *res); ReprCons *get_repr_cons(); @@ -343,6 +385,8 @@ class Environment : public Container{/*{{{*/ ~Environment(); /** Add a binding entry which binds sym_obj to eval_obj * @param def true to force the assignment + * @param sym_obj SymbolObj which provides with the identifier + * @param eval_obj EvalObj to which the identifier is binding * @return when def is set to false, this return value is true iff. the * assignment carried out successfully */ @@ -353,7 +397,6 @@ class Environment : public Container{/*{{{*/ * */ EvalObj *get_obj(EvalObj *obj); ReprCons *get_repr_cons(); - Environment *get_prev(); void gc_decrement(); void gc_trigger(EvalObj ** &tail); @@ -388,6 +431,7 @@ class Continuation : public Container {/*{{{*/ */ class InexactNumObj: public NumObj {/*{{{*/ public: + /** Construct an InexactNumObj */ InexactNumObj(NumLvl level); };/*}}}*/ @@ -396,7 +440,10 @@ class InexactNumObj: public NumObj {/*{{{*/ */ class CompNumObj: public InexactNumObj {/*{{{*/ public: - double real, imag; + /** Storage implementation: real part */ + double real; + /** Storage implementation: imaginary part */ + double imag; /** Construct a complex number */ CompNumObj(double _real, double _imag); @@ -421,6 +468,7 @@ class CompNumObj: public InexactNumObj {/*{{{*/ */ class RealNumObj: public InexactNumObj {/*{{{*/ public: + /** Storage implementation: real part */ double real; /** Construct a real number */ RealNumObj(double _real); @@ -453,7 +501,10 @@ class IntNumObj; */ class ExactNumObj: public NumObj {/*{{{*/ public: + /** Construct an ExactNumObj */ ExactNumObj(NumLvl level); + /** Convert an exact number to an integer. An exception will be + * throwed if it fails. */ virtual IntNumObj *to_int() = 0; };/*}}}*/ @@ -463,10 +514,14 @@ class ExactNumObj: public NumObj {/*{{{*/ class RatNumObj: public ExactNumObj {/*{{{*/ public: #ifndef GMP_SUPPORT - int a, b; + /** Storage implementation of numerator: C-integer */ + int a; + /** Storage implementation of denominator: C-integer */ + int b; /** Construct a rational number */ RatNumObj(int _a, int _b); #else + /** Storage implementation: GMP Rational */ mpq_class val; RatNumObj(mpq_class val); RatNumObj(const RatNumObj &ori); @@ -500,19 +555,22 @@ class RatNumObj: public ExactNumObj {/*{{{*/ class IntNumObj: public ExactNumObj {/*{{{*/ public: #ifndef GMP_SUPPORT + /** Storage implementation: C-integer */ int val; /** Construct a integer */ IntNumObj(int val); - int get_i(); #else + /** Storage implementation: GMP integer */ mpz_class val; /** Construct a integer */ IntNumObj(mpz_class val); - int get_i(); /** Copy constructor */ IntNumObj *to_int(); IntNumObj(const IntNumObj &ori); #endif + /** Get the C-format integer representation of the storage + * implementation value */ + int get_i(); NumObj *clone() const; /** Try to construct an IntNumObj object * @return NULL if failed @@ -526,11 +584,12 @@ class IntNumObj: public ExactNumObj {/*{{{*/ void mul(NumObj *r); void div(NumObj *r); void abs(); - void mod(NumObj *r); - void rem(NumObj *r); - void quo(NumObj *r); - void gcd(NumObj *r); - void lcm(NumObj *r); + + void mod(NumObj *r); /**< Modulo */ + void rem(NumObj *r); /**< Remainder */ + void quo(NumObj *r); /**< Quotient */ + void gcd(NumObj *r); /**< The greatest common divisor */ + void lcm(NumObj *r); /**< The least common divisor */ bool lt(NumObj *r); bool gt(NumObj *r); -- cgit v1.2.3