aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTeddy <[email protected]>2013-08-05 09:41:19 +0800
committerTeddy <[email protected]>2013-08-05 09:41:19 +0800
commitc54fefd17fb54029bbb62c363197c808b0fecc6b (patch)
treedeaaa87177698b093fd45dc1ff12955b1abbe678
parent645549b8a42844fc5a8042a4808c8ebf5050d7da (diff)
added list support
-rw-r--r--builtin.cpp4
-rw-r--r--builtin.h1
-rw-r--r--eval.cpp1
-rw-r--r--model.cpp11
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;
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