From c54fefd17fb54029bbb62c363197c808b0fecc6b Mon Sep 17 00:00:00 2001 From: Teddy Date: Mon, 5 Aug 2013 09:41:19 +0800 Subject: added list support --- builtin.cpp | 4 ++++ builtin.h | 1 + eval.cpp | 1 + model.cpp | 11 ++++++++++- 4 files changed, 16 insertions(+), 1 deletion(-) 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; diff --git a/builtin.h b/builtin.h index 8d2a6a0..997c0c6 100644 --- a/builtin.h +++ b/builtin.h @@ -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 diff --git a/eval.cpp b/eval.cpp index de06209..56bd06c 100644 --- a/eval.cpp +++ b/eval.cpp @@ -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()); diff --git a/model.cpp b/model.cpp index fab17d6..83d5b79 100644 --- a/model.cpp +++ b/model.cpp @@ -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 -- cgit v1.2.3-70-g09d2