From 81844497e621997c527ceedd722854966bd098e0 Mon Sep 17 00:00:00 2001 From: Teddy Date: Sun, 11 Aug 2013 13:05:51 +0800 Subject: fixed `next` pointer and `ext_repr` issues --- eval.cpp | 3 +-- model.cpp | 26 ++++++++++++++++++++------ model.h | 2 +- robust_test.scm | 2 ++ 4 files changed, 24 insertions(+), 9 deletions(-) diff --git a/eval.cpp b/eval.cpp index 0b9150e..6376ba9 100644 --- a/eval.cpp +++ b/eval.cpp @@ -102,7 +102,7 @@ void push(Pair * &pc, FrameObj ** &top_ptr, Environment *envt) { throw NormalError(SYN_ERR_EMPTY_COMB); *top_ptr++ = new RetAddr(pc); // Push the return address - if (!is_list(TO_PAIR(pc->car))) + if (!make_exec(TO_PAIR(pc->car))) throw TokenError(pc->car->ext_repr(), RUN_ERR_WRONG_NUM_OF_ARGS); // static_cast because of is_simple_obj() is false pc = static_cast(pc->car); // Go deeper to enter the call @@ -121,7 +121,6 @@ EvalObj *Evaluator::run_expr(Pair *prog) { { if (top_ptr == eval_stack + EVAL_STACK_SIZE) throw TokenError("Evaluation", RUN_ERR_STACK_OVERFLOW); -// for (; pc && pc->skip; pc = pc->next); if (pc) push(pc, top_ptr, envt); else diff --git a/model.cpp b/model.cpp index 85d4e04..564aa74 100644 --- a/model.cpp +++ b/model.cpp @@ -84,8 +84,13 @@ string EvalObj::ext_repr() { if (obj) { *(++top_ptr) = obj->get_repr_cons(); - if (hash.count((*top_ptr)->ori)) - *top_ptr = new ReprStr("#inf#"); + EvalObj *ptr = (*top_ptr)->ori; + if (ptr) + { + if (hash.count(ptr)) + *top_ptr = new ReprStr("#inf#"); + else hash.insert(ptr); + } } else { @@ -100,8 +105,13 @@ string EvalObj::ext_repr() { if (obj) { *(++top_ptr) = obj->get_repr_cons(); - if (hash.count((*top_ptr)->ori)) - *top_ptr = new ReprStr("#inf#"); + EvalObj *ptr = (*top_ptr)->ori; + if (ptr) + { + if (hash.count(ptr)) + *top_ptr = new ReprStr("#inf#"); + else hash.insert(ptr); + } } else *top_ptr = new ReprStr((*top_ptr)->repr); } @@ -401,13 +411,17 @@ EvalObj *PromObj::get_mem() { return mem; } void PromObj::feed_mem(EvalObj *res) { mem = res; } -bool is_list(Pair *ptr) { +bool make_exec(Pair *ptr) { if (ptr == empty_list) return true; EvalObj *nptr; for (;;) if ((nptr = ptr->cdr)->is_pair_obj()) - ptr = TO_PAIR(nptr); + { + ptr->next = TO_PAIR(nptr); + ptr = ptr->next; + } else break; + ptr->next = NULL; return ptr->cdr == empty_list; } diff --git a/model.h b/model.h index 58f45f4..955e2b4 100644 --- a/model.h +++ b/model.h @@ -464,6 +464,6 @@ class Continuation { Pair *proc_body); }; -bool is_list(Pair *ptr); +bool make_exec(Pair *ptr); #endif diff --git a/robust_test.scm b/robust_test.scm index 152e380..bc327ea 100644 --- a/robust_test.scm +++ b/robust_test.scm @@ -172,6 +172,8 @@ t src (eval src) (eval '(g 0)) +(eval (list * 2 3)) +(eval '(* 2 3)) (define f (lambda (x) (+ x x))) ;; test comments ((lambda (x y) (f 3)) 1 2) ;; first-class procedure -- cgit v1.2.3-70-g09d2