started working on 'match'

This commit is contained in:
Erik Svedäng 2016-06-14 12:13:08 +02:00
parent b631a185ef
commit c06cd76d76
2 changed files with 34 additions and 30 deletions

View File

@ -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))))

View File

@ -12,13 +12,13 @@
#define HEAD_EQ(str) (form->car->tag == 'Y' && strcmp(form->car->s, (str)) == 0)
// 'a' push lambda <literal>
// 'c' call <argcount>
// 'd' def <literal>
// 'e' discard
// 'i' jump if false
// 'j' jump (no matter what)
// 'l' push <literal>
// 'm' push lambda <literal>
// '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);
}