From 44f5dd57f8001964b8cbe9e5d857de6332f3c1c3 Mon Sep 17 00:00:00 2001 From: Elijah Cohen Date: Tue, 29 Oct 2024 21:36:24 -0500 Subject: [PATCH] added a few demos, testing infrastructure, major fix for quoting though the fix is far from complete, still need to figure out how to actually manage it properly --- demos.kl | 17 ++++++----------- src/builtins.c | 11 +++++++++-- src/repl.c | 2 +- src/test.c | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 14 deletions(-) diff --git a/demos.kl b/demos.kl index f26bb66..237a576 100644 --- a/demos.kl +++ b/demos.kl @@ -1,25 +1,20 @@ +(def CC (B (C B (C B cdr)) (B (Phi cons) (C B car)))) + (def D (B B)) (def fac (Z (D S (C (eq 0) 1) (D S * (C B (C - 1)))))) -(def rh (B B S (S (B (eq 0) car) cdr) (C B (Phi cons (B (C - 1) car) (Phi cons car cdr))))) - -(def range (S (C (eq 0) nil) (B (Z rh) (C cons nil)))) - -(def fph (B (S (Phi (eq nil) cdr car)) (C B (Phi cons (Phi + car (B car cdr)) (B cdr cdr))))) +(def range (S (C (eq 0) nil) (B (Z (B B S (S (B (eq 0) car) cdr) (C B (Phi cons (B (C - 1) car) (Phi cons car cdr))))) (C cons nil)))) (def reverse (B (Z (B (S (S (B (eq nil) cdr) car)) (C B (Phi cons (Phi cons (B car cdr) car) (B cdr cdr))))) (cons nil))) -(def maphelp (B (S (S (B (eq nil) (B cdr cdr)) (B car cdr)) (C B (S (B cons car) (S (cons (Phi cons (S car (B car (B cdr cdr))) (B car cdr))) (B cdr (B cdr cdr)))))))) - -(def nth-bad (B B B (Z (B (S (S (B (eq 1) car) (B car cdr))) (C B (Phi cons (C (B - car) 1) (B cdr cdr))))) cons)) - (def nth (B (B car) (B (C I cdr) (C - 1)))) (def list (rest I)) -(def maphelp (B (S (S (B (eq nil) (3 cdr)) (B car cdr))) (C B (Phi cons car (Phi cons (Phi cons (S car (B car (3 cdr))) (B car cdr)) (4 cdr)))))) +(def len (B (Z (B (S (S (B (eq nil) cdr) car)) (C B (CC (+ 1) cdr)))) (cons 0))) +(def map (B (B reverse) (B (B (Z (B (S (S (B (eq nil) (2 cdr)) (B car cdr))) (C B (S (B cons car) (Phi cons (Phi cons (S car (B (B car cdr) cdr)) (B car cdr)) (3 cdr))))))) (C (B B cons) (cons nil))))) -(def CC (B (C B (C B cdr)) (B (Phi cons) (C B car)))) +(def fold (B (S (C (C (eq nil) nil) (Z (B (S (S (B (eq nil) (B cdr cdr)) (B car cdr))) (C B (S (B cons car) (Phi cons (S (S car (B car cdr)) (B car (2 cdr))) (3 cdr)))))))) cons)) \ No newline at end of file diff --git a/src/builtins.c b/src/builtins.c index 2874876..5239e8e 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -54,8 +54,15 @@ Sexpr* dispatch_others(Sexpr* b, Sexpr* rest, Sexpr* env) { return b; } else if(b->type == QUOTE) { - // what even is this? - return b; + // okay I am using this to cheat my quoting problems with lists and such... + // temporary fix hopefully + // also need to return to this for the other types... + if((b->value.q->type == CONS) || + (b->value.q->type == SYM)) { + return clone(b); + } + // i might need to free something here? i'll come back later for it + return cons(clone(b->value.q), rest); } else if(b->type == UINT) { uint64_t anx = (CORE_PREFIX << 8) | CORE_APPLYN; diff --git a/src/repl.c b/src/repl.c index 0654c30..9bc64d8 100644 --- a/src/repl.c +++ b/src/repl.c @@ -54,7 +54,7 @@ int main(int argc, char** argv) { // add to histfile here f = fopen(histfile, "a"); if(f) { - fprintf(f, "%s", input); + fprintf(f, "\n%s", input); fclose(f); } // diff --git a/src/test.c b/src/test.c index e17fd40..7e64cf1 100644 --- a/src/test.c +++ b/src/test.c @@ -9,6 +9,28 @@ #include "eval.h" #include "builtins.h" +void assert_eq(Sexpr* env, char* a, char* b) { + Sexpr* ap = parse(a); + Sexpr* av = eval(ap, env); + Sexpr* bp = parse(b); + Sexpr* bv = eval(bp, env); + Sexpr* iseq = equal(av, bv); + char* ao = sprint_sexpr(av); + char* bo = sprint_sexpr(bv); + if(iseq->type == T) { + printf("\033[32mpassed\033[0m\n"); + } + else { + printf("\033[1m\033[31mfailed\033[0m\n"); + } + sexpr_free(av); + sexpr_free(bv); + sexpr_free(iseq); + free(ao); + free(bo); + return; +} + void run_eval_test(char* str) { printf("<- %s\n", str); Sexpr* env = init_dict(); @@ -316,9 +338,19 @@ void eval_tests() { } +void many_asserts() { + Sexpr* env = init_dict(); + env = load_env(env); + + assert_eq(env, "(+ 4 4)", "8"); + assert_eq(env, "(* 3 4)", "8"); + + sexpr_free(env); +} void run_tests(){ eval_tests(); + many_asserts(); //memtest_eval(); //mem_testing(); //mem_parser(); -- 2.39.2