diff --git a/examples/project.carp b/examples/project.carp index 79dfcc61..1e31259e 100644 --- a/examples/project.carp +++ b/examples/project.carp @@ -5,6 +5,15 @@ ;;(reset! log-deps-when-baking-ast true) ;;(reset! log-redefining-struct true) +(def carp-core-macros (open "../lisp/core_macros.carp")) +(def carp-core (read (open "../lisp/core.carp"))) + + + + + + + ;;(eb '(do (def x "yeah") (println x))) ;;(eb '(do (def f (fn [z] (* z z))) (f 3))) ;;(eb '(map (fn [x] (* x x)) (list 1 2 3))) @@ -48,33 +57,17 @@ ;; (time (println (str "fibb 26: " (eb '(fibb 26))))) ;;(time (println (str "fibb 20: " (eb '(fibb 20))))) -(eb '(def make-counter (fn [start] - (let [value start] - (fn [] - (do (reset! value (inc value)) - value)))))) -(eb '(def c1 (make-counter 10))) -;;(eb '(def c2 (make-counter 10))) +;; (eb '(def make-counter (fn [start] +;; (let [value start] +;; (fn [] +;; (do (reset! value (inc value)) +;; value)))))) -(eb '(println (str "c1: " (c1)))) +;; (eb '(def c1 (make-counter 10))) +;; (eb '(def c2 (make-counter 10))) +;; (eb '(println (str "c1: " (c1)))) +;; (eb '(println (str "c1: " (c1)))) ;; (eb '(println (str "c1: " (c1)))) ;; (eb '(println (str "c2: " (c2)))) - -;; (eb '(def f (fn [a] (list a a a)))) -;; (eb '(def l (f 5))) - -;; (eb '(def make-f (fn [a] -;; (let [x a] -;; (fn [] (reset! x (inc x)) -;; x))))) - -;; (eb '(def f1 (make-f 100))) - -;; (eb '(f1)) - -;; (eb '(def f (fn [x] (fn [] (* x 123))))) -;; (eb '(def x (f 3))) - -;; (eb '(println (str "x: " (x)))) diff --git a/src/bytecode.c b/src/bytecode.c index 4d5a8f67..757e9be1 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -12,13 +12,13 @@ #define HEAD_EQ(str) (form->car->tag == 'Y' && strcmp(form->car->s, (str)) == 0) +// 'a' push lambda // 'c' call // 'd' def // 'e' discard // 'i' jump if false // 'j' jump (no matter what) // 'l' push -// 'm' push lambda // 'n' not // 'o' do // 'p' push nil @@ -43,7 +43,7 @@ void add_lambda(Obj *bytecodeObj, int *position, Obj *form) { Obj *literals = bytecodeObj->bytecode_literals; char new_literal_index = literals->count; obj_array_mut_append(literals, form); - bytecodeObj->bytecode[*position] = 'm'; + bytecodeObj->bytecode[*position] = 'a'; bytecodeObj->bytecode[*position + 1] = new_literal_index + 65; *position += 2; } @@ -133,6 +133,15 @@ void add_while(Process *process, Obj *env, Obj *bytecodeObj, int *position, Obj *position += 1; } +void add_match(Process *process, Obj *env, Obj *bytecodeObj, int *position, Obj *form) { + Obj *literals = bytecodeObj->bytecode_literals; + char new_literal_index = literals->count; + obj_array_mut_append(literals, form); + bytecodeObj->bytecode[*position] = 'm'; + bytecodeObj->bytecode[*position + 1] = new_literal_index + 65; + *position += 2; +} + void add_do(Process *process, Obj *env, Obj *bytecodeObj, int *position, Obj *form) { Obj *p = form->cdr; while(p && p->car) { @@ -232,6 +241,9 @@ void visit_form(Process *process, Obj *env, Obj *bytecodeObj, int *position, Obj else if(HEAD_EQ("while")) { add_while(process, env, bytecodeObj, position, form); } + else if(HEAD_EQ("match")) { + add_match(process, env, bytecodeObj, position, form); + } else if(HEAD_EQ("do")) { add_do(process, env, bytecodeObj, position, form); } @@ -387,7 +399,7 @@ Obj *bytecode_eval_internal(Process *process, Obj *bytecodeObj, int steps) { stack_push(process, literal); process->frames[process->frame].p += 2; break; - case 'm': + case 'a': i = bytecode[p + 1] - 65; literal = literals_array[i]; Obj *lambda = obj_new_lambda(literal->cdr->car, form_to_bytecode(process, @@ -489,7 +501,7 @@ Obj *bytecode_eval_internal(Process *process, Obj *bytecodeObj, int steps) { function = stack_pop(process); arg_count = bytecode[p + 1] - 65; - printf("will call %s with %d args.\n", obj_to_string(process, function)->s, arg_count); + //printf("will call %s with %d args.\n", obj_to_string(process, function)->s, arg_count); Obj **args = NULL; if(arg_count > 0) { @@ -497,7 +509,6 @@ Obj *bytecode_eval_internal(Process *process, Obj *bytecodeObj, int steps) { } for(int i = 0; i < arg_count; i++) { Obj *arg = stack_pop(process); - printf("popped %s\n", obj_to_string(process, arg)->s); args[arg_count - i - 1] = arg; //shadow_stack_push(process, arg); }