From 2ec8f26f31d13bf3c3bbda24ea5e1cace6a819a2 Mon Sep 17 00:00:00 2001 From: Teddy Date: Mon, 5 Aug 2013 23:31:48 +0800 Subject: added support for string --- TODO.rst | 5 ++--- builtin.h | 4 ++-- model.cpp | 15 +++++++++++++-- model.h | 21 +++++++++++++++++++++ parser.cpp | 13 +++++-------- 5 files changed, 43 insertions(+), 15 deletions(-) diff --git a/TODO.rst b/TODO.rst index d853b36..69a9c43 100644 --- a/TODO.rst +++ b/TODO.rst @@ -1,5 +1,4 @@ -- More sophisticated type system - - Number system && proper arithmetic - - Cons support +- More sophisticated parser +- GMP - Quotation - Add macro support diff --git a/builtin.h b/builtin.h index ab52d5a..322e82e 100644 --- a/builtin.h +++ b/builtin.h @@ -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); diff --git a/model.cpp b/model.cpp index 3703f1d..4f0e1c5 100644 --- a/model.cpp +++ b/model.cpp @@ -94,7 +94,7 @@ RetAddr::RetAddr(Cons *_addr) : FrameObj(CLS_RET_ADDR), addr(_addr) {} string RetAddr::_debug_repr() { return string("#"); } #endif -UnspecObj::UnspecObj() : EvalObj() {} +UnspecObj::UnspecObj() : EvalObj(CLS_SIM_OBJ) {} string UnspecObj::ext_repr() { return string("#"); } @@ -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) {} diff --git a/model.h b/model.h index d402b2b..f883032 100644 --- a/model.h +++ b/model.h @@ -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 Str2EvalObj; /** @class Environment * The environment of current evaluation, i.e. the local variable binding diff --git a/parser.cpp b/parser.cpp index cb8695f..07ca066 100644 --- a/parser.cpp +++ b/parser.cpp @@ -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) { -- cgit v1.2.3