summaryrefslogtreecommitdiff
path: root/src/visitor.c
diff options
context:
space:
mode:
authorPreston Pan <preston@nullring.xyz>2023-01-03 13:19:58 -0800
committerPreston Pan <preston@nullring.xyz>2023-01-03 13:19:58 -0800
commit3c35be76e515098431643155b66e61a1c64816ae (patch)
tree4a11454acce1965c52e511dd44dba2e1dd73ee67 /src/visitor.c
parentb2c539fadfa7ea3eea2e5f7c0c37b5f5f1370c5a (diff)
done the parser in theory
Diffstat (limited to 'src/visitor.c')
-rw-r--r--src/visitor.c28
1 files changed, 24 insertions, 4 deletions
diff --git a/src/visitor.c b/src/visitor.c
index 98a7d92..6eb29a2 100644
--- a/src/visitor.c
+++ b/src/visitor.c
@@ -1,18 +1,20 @@
#include "./include/visitor.h"
#include "./include/hash_table.h"
#include "./include/macros.h"
+#include "./include/parser.h"
#include "./include/stack.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
-visitor_t *init_visitor(ast_t *root) {
+visitor_t *init_visitor(parser_t *p) {
visitor_t *v = (visitor_t *)malloc(sizeof(visitor_t));
if (v == NULL)
die("malloc on visitor");
v->stack_frame = init_stack(512);
- v->symbol_table = init_hash_table(10000);
- v->root = root;
+ v->symbol_table = p->symbol_table;
+ v->eval_table = init_hash_table(10000);
+ v->root = parse_all(p);
return v;
}
@@ -26,9 +28,27 @@ bool is_self_evaluating(ast_t *e) {
return false;
}
+bool is_built_in(ast_t *e) {}
/* Special symbols: car, cdr, quote, *, /, +, -, %, inc, dec, >, <, >=, <=, /=,
* =, equal (for strings), input */
-ast_t *eval_symbol(visitor_t *v, ast_t *e) {}
+ast_t *eval_symbol(visitor_t *v, ast_t *e) {
+ if (is_built_in(e))
+ return e;
+
+ else if (hash_table_exists(stack_peek(v->stack_frame), e->string_value))
+ return hash_table_get(stack_peek(v->stack_frame), e->string_value);
+
+ else if (hash_table_exists(v->eval_table, e->string_value))
+ return hash_table_get(v->eval_table, e->string_value);
+
+ else if (hash_table_exists(v->symbol_table, e->string_value)) {
+ ast_t *unevaled = hash_table_get(v->symbol_table, e->string_value);
+ ast_t *eval = eval_expr(v, unevaled);
+ hash_table_add(v->eval_table, e->string_value, eval);
+ return eval;
+ } else
+ return e;
+}
ast_t *eval_list(visitor_t *v, ast_t *e) {}