summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPreston Pan <preston@nullring.xyz>2023-01-06 11:14:52 -0800
committerPreston Pan <preston@nullring.xyz>2023-01-06 11:14:52 -0800
commit30f12dfc1f32688f377913c52b131aa78d7830b5 (patch)
treeb59a2822cb6edb062b9965e6dc89b718d65b596b
parente98f89c84ec0492a36e364ac32987e17296b33df (diff)
fixed stack bug; found recursion bug
-rw-r--r--doc/main.nxs2
-rw-r--r--src/include/stack.h2
-rw-r--r--src/main.c22
-rw-r--r--src/stack.c8
-rw-r--r--src/visitor.c4
5 files changed, 28 insertions, 10 deletions
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;