diff options
author | Teddy <[email protected]> | 2013-08-05 09:41:19 +0800 |
---|---|---|
committer | Teddy <[email protected]> | 2013-08-05 09:41:19 +0800 |
commit | c54fefd17fb54029bbb62c363197c808b0fecc6b (patch) | |
tree | deaaa87177698b093fd45dc1ff12955b1abbe678 | |
parent | 645549b8a42844fc5a8042a4808c8ebf5050d7da (diff) |
added list support
-rw-r--r-- | builtin.cpp | 4 | ||||
-rw-r--r-- | builtin.h | 1 | ||||
-rw-r--r-- | eval.cpp | 1 | ||||
-rw-r--r-- | model.cpp | 11 |
4 files changed, 16 insertions, 1 deletions
diff --git a/builtin.cpp b/builtin.cpp index f1b518f..17c5e94 100644 --- a/builtin.cpp +++ b/builtin.cpp @@ -296,6 +296,10 @@ EvalObj *builtin_cdr(ArgList *args) { return TO_CONS(args->car)->cdr; } +EvalObj *builtin_list(ArgList *args) { + return args; +} + EvalObj *builtin_plus(ArgList *args) { // TODO: type conversion and proper arithmetic int res = 0; @@ -136,5 +136,6 @@ EvalObj *builtin_display(ArgList *); EvalObj *builtin_cons(ArgList *); EvalObj *builtin_car(ArgList *); EvalObj *builtin_cdr(ArgList *); +EvalObj *builtin_list(ArgList *); #endif @@ -24,6 +24,7 @@ void Evaluator::add_builtin_routines() { ADD_ENTRY("cons", new BuiltinProcObj(builtin_cons, "cons")); ADD_ENTRY("car", new BuiltinProcObj(builtin_car, "car")); ADD_ENTRY("cdr", new BuiltinProcObj(builtin_cdr, "cdr")); + ADD_ENTRY("list", new BuiltinProcObj(builtin_list, "list")); ADD_ENTRY("if", new SpecialOptIf()); ADD_ENTRY("lambda", new SpecialOptLambda()); ADD_ENTRY("define", new SpecialOptDefine()); @@ -55,7 +55,16 @@ Cons::Cons(EvalObj *_car, EvalObj *_cdr) : next(NULL) {} string Cons::ext_repr() { - return "(" + car->ext_repr() + " . " + cdr->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 |