diff options
author | Teddy <[email protected]> | 2013-08-05 23:31:48 +0800 |
---|---|---|
committer | Teddy <[email protected]> | 2013-08-05 23:31:48 +0800 |
commit | 2ec8f26f31d13bf3c3bbda24ea5e1cace6a819a2 (patch) | |
tree | f623aaa220efa2993161997729dce9ff03d936ad | |
parent | 5dab7df6830018c5c28ebcc7cc3b242ccad2736b (diff) |
added support for string
-rw-r--r-- | TODO.rst | 5 | ||||
-rw-r--r-- | builtin.h | 4 | ||||
-rw-r--r-- | model.cpp | 15 | ||||
-rw-r--r-- | model.h | 21 | ||||
-rw-r--r-- | parser.cpp | 13 |
5 files changed, 43 insertions, 15 deletions
@@ -1,5 +1,4 @@ -- More sophisticated type system - - Number system && proper arithmetic - - Cons support +- More sophisticated parser +- GMP - Quotation - Add macro support @@ -25,7 +25,7 @@ class CompNumObj: public InexactNumObj { /** Construct a complex number */ CompNumObj(double _real, double _imag); - /** Try to construct an RealNumObj object + /** Try to construct an CompNumObj object * @return NULL if failed */ static CompNumObj *from_string(string repr); @@ -85,7 +85,7 @@ class RatNumObj: public ExactNumObj { int a, b; /** Construct a rational number */ RatNumObj(int _a, int _b); - /** Try to construct an RealNumObj object + /** Try to construct an RatNumObj object * @return NULL if failed */ static RatNumObj *from_string(string repr); @@ -94,7 +94,7 @@ RetAddr::RetAddr(Cons *_addr) : FrameObj(CLS_RET_ADDR), addr(_addr) {} string RetAddr::_debug_repr() { return string("#<Return Address>"); } #endif -UnspecObj::UnspecObj() : EvalObj() {} +UnspecObj::UnspecObj() : EvalObj(CLS_SIM_OBJ) {} string UnspecObj::ext_repr() { return string("#<Unspecified>"); } @@ -150,7 +150,7 @@ string ProcObj::_debug_repr() { return ext_repr(); } SpecialOptObj::SpecialOptObj() : OptObj() {} -BoolObj::BoolObj(bool _val) : EvalObj(), val(_val) {} +BoolObj::BoolObj(bool _val) : EvalObj(CLS_SIM_OBJ), val(_val) {} bool BoolObj::is_true() { return val; } @@ -161,6 +161,17 @@ NumObj::NumObj(NumLvl _level, bool _exactness) : bool NumObj::is_exact() { return exactness; } +StrObj::StrObj(string _str) : EvalObj(CLS_SIM_OBJ), str(_str) {} + +string StrObj::ext_repr() { return str; } + +StrObj *StrObj::from_string(string repr) { + int len = repr.length(); + if (repr[0] == '\"' && repr[len - 1] == '\"') + return new StrObj(repr.substr(1, len - 2)); + return NULL; +} + BuiltinProcObj::BuiltinProcObj(BuiltinProc f, string _name) : OptObj(), handler(f), name(_name) {} @@ -259,6 +259,10 @@ class BoolObj: public EvalObj { BoolObj(bool); /**< Converts a C bool value to a BoolObj*/ bool is_true(); /**< Override EvalObj `is_true()` */ string ext_repr(); + /** Try to construct an BoolObj object + * @return NULL if failed + */ + static BoolObj *from_string(string repr); }; /** @class NumObj @@ -290,6 +294,23 @@ class NumObj: public EvalObj { virtual bool eq(NumObj *r) = 0; }; +/** @class StrObj + * String support + */ +class StrObj: public EvalObj { + public: + string str; + + /** Construct a string object */ + StrObj(string str); + /** Try to construct an StrObj object + * @return NULL if failed + */ + static StrObj *from_string(string repr); + string ext_repr(); +}; + + typedef map<string, EvalObj*> Str2EvalObj; /** @class Environment * The environment of current evaluation, i.e. the local variable binding @@ -105,14 +105,11 @@ ASTGenerator::ASTGenerator() {} EvalObj *ASTGenerator::to_obj(const string &str) { EvalObj *res = NULL; - if ((res = IntNumObj::from_string(str))) - return res; - if ((res = RatNumObj::from_string(str))) - return res; - if ((res = RealNumObj::from_string(str))) - return res; - if ((res = CompNumObj::from_string(str))) - return res; + if ((res = IntNumObj::from_string(str))) return res; + if ((res = RatNumObj::from_string(str))) return res; + if ((res = RealNumObj::from_string(str))) return res; + if ((res = CompNumObj::from_string(str))) return res; + if ((res = StrObj::from_string(str))) return res; return new SymObj(str); } Cons *ASTGenerator::absorb(Tokenizor *tk) { |