From: Elijah Cohen Date: Thu, 8 Aug 2024 21:10:27 +0000 (-0500) Subject: some cleanup, parenthesis balance in parser now X-Git-Url: https://git.eli173.com/?a=commitdiff_plain;h=7de50d5c4b6e36cef196cab141c5f3451b0b3e2d;p=klapaucius some cleanup, parenthesis balance in parser now --- diff --git a/src/builtins/core.c b/src/builtins/core.c index 9c4ebf8..98deab4 100644 --- a/src/builtins/core.c +++ b/src/builtins/core.c @@ -96,8 +96,6 @@ Sexpr* c_def(Sexpr* b, Sexpr* env) { Sexpr* args = b->value.b.args; Sexpr* val = eval(car(args), env); Sexpr* key = car(cdr(args)); - printf("val: %s\n", sprint_sexpr(val)); - printf("key: %s\n", sprint_sexpr(key)); append_to_dict(env, key, val); return val; } diff --git a/src/eval.c b/src/eval.c index b4a1618..ba3e7ca 100644 --- a/src/eval.c +++ b/src/eval.c @@ -10,7 +10,7 @@ Sexpr* apply_builtin(Sexpr* func, Sexpr* arg, Sexpr* env); Sexpr* eval(Sexpr* s, Sexpr* dict) { - printf("s: %s\n", sprint_sexpr(s)); + //printf("s: %s\n", sprint_sexpr(s)); // non-null s // generally assumes that a sexpr passed to this is well-formed (ie no inapt NULLS) // question: does a completed builtin get evaluated here? @@ -27,7 +27,6 @@ Sexpr* eval(Sexpr* s, Sexpr* dict) { if(s->type != CONS) { return s; } // from now on: type is cons - //printf("idk\n"); Sexpr* curr = s; while(curr->type == CONS) { if(cdr(curr)->type == NIL) diff --git a/src/parser.c b/src/parser.c index 55e8380..edd166a 100644 --- a/src/parser.c +++ b/src/parser.c @@ -94,6 +94,24 @@ Sexpr* vals_parse(Sexpr* tokens) { } +bool balance_checker(Sexpr* tokens) { + int balance = 0; + Sexpr* curr = tokens; + while(curr->type != NIL) { + Sexpr* h = car(curr); + if(h->type == SYM) { + if(strcmp(h->value.s, "(")==0) + balance++; + if(strcmp(h->value.s, ")")==0) + balance--; + if(balance < 0) return false; + } + curr = cdr(curr); + } + //printf("b: %d\n", balance); + return balance == 0; +} + Sexpr* cons_parse(Sexpr* tokens) { Sexpr* reversed = reverse(tokens); // takes results from previous parsing ops, aka the forward-facing? @@ -127,6 +145,10 @@ Sexpr* parse(char* s) { //printf("t: %s\n", sprint_sexpr(*tokens)); Sexpr* vals = vals_parse(tokens); //printf("v: %s\n", sprint_sexpr(vals)); + if(!balance_checker(vals)) { + printf("unbalanced parenthesis\n"); + return NULL; + } Sexpr* done = cons_parse(vals); //printf("c: %s\n", sprint_sexpr(*done)); return done; diff --git a/src/repl.c b/src/repl.c index 5329ccd..eec6619 100644 --- a/src/repl.c +++ b/src/repl.c @@ -25,15 +25,15 @@ int main(int argc, char** argv) { if(input == NULL) return 0; linenoiseHistoryAdd(input); - printf("asdf1\n"); Sexpr* in = parse(input); - printf("asdf2\n"); if(in == NULL) { printf("bad input\n"); } - printf("- -%s\n", sprint_sexpr(in)); - Sexpr* out = eval(car(in), env); - printf(" - %s\n", sprint_sexpr(out)); + else { + //printf("- -%s\n", sprint_sexpr(in)); + Sexpr* out = eval(car(in), env); + printf(" - %s\n", sprint_sexpr(out)); + } linenoiseFree(input); } return 0; diff --git a/src/sexpr.c b/src/sexpr.c index e2d4652..e68c264 100644 --- a/src/sexpr.c +++ b/src/sexpr.c @@ -110,7 +110,10 @@ Sexpr* reverse(Sexpr* s) { } char* sprint_sexpr(Sexpr* s) { - if(s == NULL) printf("SHIT IT'S NULL\n"); + if(s == NULL) { + printf("UH OH IT'S NULL\n"); + return NULL; + } // assumes not null size_t nbytes; char* out; diff --git a/src/test.c b/src/test.c index f11ee37..52ade20 100644 --- a/src/test.c +++ b/src/test.c @@ -62,10 +62,10 @@ void test_parsing() { void test_parser() { printf("parsing again...\n"); - printf("_: %s\n", sprint_sexpr(parse("457"))); + printf("_: %s\n", sprint_sexpr(parse("456"))); printf("_: %s\n", sprint_sexpr(parse("a b c d e"))); printf("_: %s\n", sprint_sexpr(parse("(a b (3 2 (5) c) d (e f) g)"))); - //printf("_: %s\n", sprint_sexpr(parse("(457"))); + printf("_: %s\n", sprint_sexpr(parse("(457"))); } void test_eq() {