From 77ce4095c01aa82952ae6a4093e8bb948e5bd7be Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Mon, 11 May 2020 23:20:54 -0400 Subject: [PATCH] Fix compiling varadic params, demo method syntax --- bf.kp | 16 ++++++++++++++-- k_prime.krak | 24 +++++++++++------------- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/bf.kp b/bf.kp index 3db7ff5..1db4201 100644 --- a/bf.kp +++ b/bf.kp @@ -143,10 +143,12 @@ (if (= method_fn nil) (println "no method " method) (apply method_fn object arguments))))) - +; method call syntax +(add_grammer_rule 'form [ 'form "\\." 'atom 'optional_WS "\\(" 'optional_WS 'space_forms 'optional_WS "\\)" ] (fn* (xs) `(method-call ~(nth xs 0) '~(nth xs 2) ,(nth xs 6)))) (def! actual_obj (with-meta [0] [ 'inc (fn* (o) (set-nth! o 0 (+ (nth o 0) 1))) 'dec (fn* (o) (set-nth! o 0 (- (nth o 0) 1))) + 'set (fn* (o n) (set-nth! o 0 n)) 'get (fn* (o) (nth o 0)) ])) @@ -175,11 +177,21 @@ (println "actual_obj" actual_obj) (method-call actual_obj 'inc) (println "actual_obj" actual_obj) - (println (method-call actual_obj 'get)) + (println "with get: " (method-call actual_obj 'get)) (println "actual_obj" actual_obj) (method-call actual_obj 'dec) (method-call actual_obj 'dec) (println "actual_obj" actual_obj) + (println "setting old style 654") + (method-call actual_obj 'set 654) + (println "actual_obj" actual_obj) + (println "Ok, doing with new method call syntax") + actual_obj.inc() + (println "actual_obj" actual_obj) + (println "setting new style 1337") + actual_obj.set(1337) + (println "actual_obj" actual_obj) + (println "with get " actual_obj.get()) a))) ))))) (do diff --git a/k_prime.krak b/k_prime.krak index 60e9464..aa5bc94 100644 --- a/k_prime.krak +++ b/k_prime.krak @@ -1312,9 +1312,7 @@ fun main(argc: int, argv: **char): int { } env->set(str("eval-read-string"), make_builtin_function(str("eval-read-string"), ERS)); // reader macros - /*rep(grammer, env, str("(add_grammer_rule (quote atom) [\"'\" (quote form)] (fn* (xs) (quasiquote (quote (unquote (nth xs 1))))))")) //'*/ rep(grammer, env, str("(add_grammer_rule (quote atom) (vector \"'\" (quote form)) (fn* (xs) (quasiquote (quote (unquote (nth xs 1))))))")) //' - /*rep(grammer, env, str("(add_grammer_rule 'form (vector \"\\\\[\\\\]\") (fn* (xs) '(vector)))")) //'*/ rep(grammer, env, str("(add_grammer_rule 'form (vector \"\\\\[\" 'optional_WS \"\\\\]\") (fn* (xs) '(vector)))")) //' rep(grammer, env, str("(add_grammer_rule 'form (vector \"\\\\[\" 'optional_WS 'space_forms 'optional_WS \"\\\\]\") (fn* (xs) (quasiquote (vector (splice-unquote (nth xs 2))))))")) //' // now we can use ' for the rest @@ -1360,7 +1358,7 @@ fun main(argc: int, argv: **char): int { size_t current_exception_value; void error(char* message) {fprintf(stderr, "%s", message); exit(1);} void check_num_params(size_t a, size_t b, char* function) { if (a!=b) {fprintf(stderr, "%s: expected num params to be %d\n", function, b); exit(1);}} - void check_num_params_at_least(size_t a, size_t b, char* function) { if (a!=b) {fprintf(stderr, "%s: expected num params to be at least %d\n", function, b); exit(1);}} + void check_num_params_at_least(size_t a, size_t b, char* function) { if (a, env: *Env, ast: MalValue): set { match (ast.internal) { MalValue_int::Vector(l) { - println("Find closed vars list") + /*println("Find closed vars list")*/ if (l.get().size == 0) { return set() } else if (l.get()[0].is_symbol("def!")) { - println("Find closed vars in def!") + /*println("Find closed vars in def!")*/ defined.add(l.get()[1].get_symbol_text()) /*return find_closed_vars(defined, env, l[2])*/ var to_ret = find_closed_vars(defined, env, l.get()[2]) - println("end Find closed vars in def!") + /*println("end Find closed vars in def!")*/ return to_ret } else if (l.get()[0].is_symbol("let*")) { var bindings = l.get()[1].get_vector_rc() @@ -1663,11 +1661,11 @@ fun find_closed_vars(defined: set, env: *Env, ast: MalValue): set { } return to_ret } else if (l.get()[0].is_symbol("fn*")) { - println("Find closed vars fn*") + /*println("Find closed vars fn*")*/ var f = EVAL(env, ast) /*return find_closed_vars(defined, env, get_value(f))*/ var to_ret = find_closed_vars(defined, env, get_value(f)) - println("end find closed vars fn*") + /*println("end find closed vars fn*")*/ return to_ret } else if (l.get()[0].is_symbol("quote")) { return set() @@ -1684,7 +1682,7 @@ fun find_closed_vars(defined: set, env: *Env, ast: MalValue): set { } return to_ret } - println("end list") + /*println("end list")*/ } MalValue_int::Symbol(s) { if !defined.contains(s) { @@ -1721,10 +1719,10 @@ fun find_closed_vars(defined: set, env: *Env, ast: MalValue): set { for (var i = 0; i < f.parameters.size; i++;) { new_env->set(f.parameters[i], malNil()) } - println("Find closed vars going inside function:\n" + new_env->to_string()) + /*println("Find closed vars going inside function:\n" + new_env->to_string())*/ /*return find_closed_vars(defined.union(from_vector(f.parameters)), new_env, *f.body)*/ var to_ret = find_closed_vars(defined.union(from_vector(f.parameters)), new_env, *f.body) - println("coming out of function") + /*println("coming out of function")*/ return to_ret } } @@ -1828,7 +1826,7 @@ fun compile_value(top_decs: *str, top_defs: *str, main_init: *str, defs: *str, e function += f.parameters[i] + "_raw[0] = 0x2F;\n" function += f.parameters[i] + "_raw[1] = (num-" + (f.parameters.size-1) + ");\n" function += str() + "for (int i = 0; i < (num-" + (f.parameters.size-1) + "); i++) {\n" - function += f.parameters[i] + "_raw[i] = args[i+" + (f.parameters.size-1) + "];\n" + function += f.parameters[i] + "_raw[i+2] = args[i+" + (f.parameters.size-1) + "];\n" function += "}\n" function += "size_t " + f.parameters[i] + " = ((((size_t)" + f.parameters[i] + "_raw)<<3)|0x2);\n" } else { @@ -1868,7 +1866,7 @@ fun compile_value(top_decs: *str, top_defs: *str, main_init: *str, defs: *str, e var c_legal_s = c_legal(s) var e = env->find(s); if e != null() && e->outer == null() { - println(s + " found in outer-est scope!") + /*println(s + " found in outer-est scope!")*/ var v = e->get(s) e->remove(s) var x = str()