From 30f12dfc1f32688f377913c52b131aa78d7830b5 Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Fri, 6 Jan 2023 11:14:52 -0800 Subject: fixed stack bug; found recursion bug --- doc/main.nxs | 2 ++ src/include/stack.h | 2 ++ src/main.c | 22 ++++++++++++++-------- src/stack.c | 8 +++++++- src/visitor.c | 4 +++- 5 files changed, 28 insertions(+), 10 deletions(-) create mode 100644 doc/main.nxs diff --git a/doc/main.nxs b/doc/main.nxs new file mode 100644 index 0000000..e09c304 --- /dev/null +++ b/doc/main.nxs @@ -0,0 +1,2 @@ +(bind factorial (lambda (n) + (if (= n 0) 1 (* n (factorial (- n 1)))))) diff --git a/src/include/stack.h b/src/include/stack.h index 5a58f0b..6b0ed9a 100644 --- a/src/include/stack.h +++ b/src/include/stack.h @@ -13,4 +13,6 @@ void stack_push(stack_t *s, hash_table_t *h); hash_table_t *stack_peek(stack_t *s); hash_table_t *stack_pop(stack_t *s); + +bool is_empty(stack_t *s); #endif diff --git a/src/main.c b/src/main.c index ff28829..b111ec5 100644 --- a/src/main.c +++ b/src/main.c @@ -78,14 +78,20 @@ int main(int argc, char **argv) { /* print(res); */ /* DONE: TEST NON-BUILTIN FUNCTIONS (stack frame) */ - lexer_t *lexer = init_lexer("(bind my_var 3) (bind hello (lambda (x y) (+ x " - "y))) (hello (+ my_var 4) 4) (+ my_var 4)"); - parser_t *parser = init_parser(lexer); - visitor_t *visitor = init_visitor(parser); - ast_t *root = eval(visitor); - print_root(root); - /* ast_t *res = root->subnodes[0]; */ - /* print(res); */ + /* lexer_t *lexer = init_lexer("(if (= (+ 3 2) 5) 1 2)"); */ + /* parser_t *parser = init_parser(lexer); */ + /* visitor_t *visitor = init_visitor(parser); */ + /* ast_t *root = eval(visitor); */ + /* print_root(root); */ + + /* TODO: TEST RECURSION */ + /* lexer_t *lexer = init_lexer("(bind factorial (lambda (x) (if (<= x 0) 1 (* + * x" */ + /* "(factorial (- x 1)))))) (factorial 3)"); */ + /* parser_t *parser = init_parser(lexer); */ + /* visitor_t *visitor = init_visitor(parser); */ + /* ast_t *root = eval(visitor); */ + /* print_root(root); */ /* lexer_t *lexer = init_lexer("((lambda (x y) (+ x y)) (+ 3 4) 4) (+ 3 * 4)"); */ diff --git a/src/stack.c b/src/stack.c index 2438792..ebfa640 100644 --- a/src/stack.c +++ b/src/stack.c @@ -26,7 +26,7 @@ void stack_push(stack_t *s, hash_table_t *h) { } /* fix heap buffer overflow */ hash_table_t *stack_peek(stack_t *s) { - if (s->cur == -1) + if (is_empty(s)) return NULL; return s->stack[s->cur]; } @@ -37,3 +37,9 @@ hash_table_t *stack_pop(stack_t *s) { s->cur--; return h; } + +bool is_empty(stack_t *s) { + if (s->cur == -1) + return true; + return false; +} diff --git a/src/visitor.c b/src/visitor.c index 736ce03..5e58952 100644 --- a/src/visitor.c +++ b/src/visitor.c @@ -44,7 +44,7 @@ bool is_built_in(ast_t *e) { /* Comparison functions */ if (strcmp(cmp, "<") == 0 || strcmp(cmp, ">") == 0 || strcmp(cmp, "=") == 0 || strcmp(cmp, "<=") == 0 || strcmp(cmp, ">=") == 0 || - strcmp(cmp, "eq") == 0) + strcmp(cmp, "eq") == 0 || strcmp(cmp, "if") == 0) return true; /* Type-checking */ @@ -67,6 +67,8 @@ bool is_built_in(ast_t *e) { * =, equal (for strings), input */ ast_t *eval_symbol(visitor_t *v, ast_t *e) { /* hash_table_t *lmao = stack_peek(v->stack_frame); */ + printf("symbol\n"); + printf("%s\n", e->string_value); hash_table_t *h = stack_peek(v->stack_frame); if (is_built_in(e)) return e; -- cgit