Permalink
Browse files

quote is whole-function now

  • Loading branch information...
1 parent 7e7ff7e commit 219f02be96a7344e4fc1cc02db739cca0301e747 Y.Y committed Jun 20, 2012
Showing with 39 additions and 9 deletions.
  1. +5 −0 src/builtin.cc
  2. +31 −8 src/eval.cc
  3. +2 −0 src/eval.hh
  4. +1 −1 test/eval_test.cc
View
@@ -4,6 +4,7 @@
#include "number.hh"
#include "function.hh"
#include "lisp_ptr.hh"
+#include "eval.hh"
using namespace std;
@@ -146,6 +147,10 @@ static struct Entry {
Function func;
}
builtin_func[] = {
+ {"quote", Function{
+ vm_op_quote,
+ Function::Calling::whole_function, {1, false}}},
+
{"+", Function{
plus_2,
Function::Calling::function, {2, true}}},
View
@@ -24,11 +24,11 @@ Symbol* to_varname(Lisp_ptr p){
return nullptr;
}
- if(var->to_keyword() != Keyword::not_keyword){
- fprintf(stderr, "eval error: variable's name is Keyword (%s)!\n",
- var->name().c_str());
- return nullptr;
- }
+ // if(var->to_keyword() != Keyword::not_keyword){
+ // fprintf(stderr, "eval error: variable's name is Keyword (%s)!\n",
+ // var->name().c_str());
+ // return nullptr;
+ // }
return var;
}
@@ -656,6 +656,30 @@ void vm_op_quasiquote(){
} // namespace
+/*
+ stack = (args, arg_bottom)
+ ----
+ ret = arg[0]
+*/
+void vm_op_quote(){
+ auto args = VM.stack().top();
+ VM.stack().pop();
+
+ if(VM.stack().top().tag() != Ptr_tag::vm_op){
+ fprintf(stderr, "eval error: stack corrupted -- no bottom found!\n");
+ VM.return_value() = {};
+ return;
+ }
+
+ auto c = args.get<Cons*>();
+
+ VM.return_value() = c->car();
+
+ if(c->cdr()){
+ fprintf(stderr, "eval warning: quote has two or more args. ignored.\n");
+ }
+}
+
void eval(){
while(!VM.code().empty()){
auto p = VM.code().top();
@@ -702,9 +726,7 @@ void eval(){
switch(k){
case Keyword::not_keyword: break;
- case Keyword::quote:
- VM.return_value() = r.get<Cons*>()->car();
- break;
+ case Keyword::quote: goto call;
case Keyword::lambda: eval_lambda(r); break;
case Keyword::if_: eval_if(r); break;
case Keyword::set_: eval_set("set!", r, vm_op_set); break;
@@ -747,6 +769,7 @@ void eval(){
break;
}
}
+ call:
// procedure/macro call?
VM.code().push(Lisp_ptr(vm_op_call));
VM.code().push(first);
View
@@ -4,6 +4,8 @@
#include "lisp_ptr.hh"
#include "vm.hh"
+void vm_op_quote();
+
void eval();
#endif // EVAL_HH
View
@@ -120,7 +120,7 @@ int main(){
check(read_eql, "(define x 1)", "1");
check(read_eql, "x", "1");
check(read_eql, "(+ x x)", "2");
- check(test_undef, "(define else 1)");
+ //check(test_undef, "(define else 1)");
check(read_eql, "(define else_ 1)", "1");
check(read_eql, "else_", "1");

0 comments on commit 219f02b

Please sign in to comment.