diff options
author | Preston Pan <preston@nullring.xyz> | 2023-01-04 18:22:24 -0800 |
---|---|---|
committer | Preston Pan <preston@nullring.xyz> | 2023-01-04 18:22:24 -0800 |
commit | 6fe77e2f20f045b89ed10c3952f8f088e9bd3d6c (patch) | |
tree | 37abb7d1b129a0a46bbff940aa1b728d4d4f0dbd /src | |
parent | f1e455d2fa84067edda695cbba5ddb9e5b77235e (diff) |
fully functional parser (collisions might be a problem)
Diffstat (limited to 'src')
-rw-r--r-- | src/hash_table.c | 4 | ||||
-rw-r--r-- | src/main.c | 30 | ||||
-rw-r--r-- | src/parser.c | 14 | ||||
-rw-r--r-- | src/visitor.c | 5 |
4 files changed, 38 insertions, 15 deletions
diff --git a/src/hash_table.c b/src/hash_table.c index a6524ac..99508e9 100644 --- a/src/hash_table.c +++ b/src/hash_table.c @@ -2,6 +2,7 @@ #include "./include/ast.h" #include "./include/macros.h" #include <stdbool.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> @@ -33,12 +34,14 @@ sl_list_t *init_sl_list() { return l; } +/* TODO: fix segfault bug */ void sl_list_add(sl_list_t *l, char *key, ast_t *value) { sl_node_t *cur = l->head; bool modified = false; if (l->head == NULL) { l->head = init_sl_node(key, value); l->size++; + return; } for (int i = 0; i < l->size - 1; i++) { @@ -49,6 +52,7 @@ void sl_list_add(sl_list_t *l, char *key, ast_t *value) { } cur = cur->next; } + if (strcmp(cur->value->key, key) == 0) { cur->value->value = value; modified = true; @@ -63,20 +63,34 @@ int main(int argc, char **argv) { /* printf("%s\n", world->string_value); */ /* printf("%d\n", fal->bool_value); */ - /* TODO: TEST PARSING FUNCTIONS */ - lexer_t *lexer = init_lexer("(lambda (x) (y))"); - printf("why is code not working, part 1\n"); - parser_t *parser = init_parser(lexer); - printf("why is code not working, part 2\n"); - ast_t *root = parse_all(parser); + /* DONE: TEST PARSING FUNCTIONS */ + /* lexer_t *lexer = init_lexer("(lambda (x) (y))"); */ + /* parser_t *parser = init_parser(lexer); */ + /* ast_t *root = parse_all(parser); */ + /* ast_t *func = root->subnodes[0]; */ + /* ast_t *args = func->car; */ + /* ast_t *expr = func->cdr; */ + /* printf("%d\t%s\t\%s\n", func->type, args->car->string_value, */ + /* expr->car->string_value); */ - ast_t *func = root->subnodes[0]; - printf("%d\n", func->type); /* DONE: TEST PARSING QUOTE */ /* lexer_t *lexer = init_lexer("'(hello)"); */ /* parser_t *parser = init_parser(lexer); */ /* ast_t *root = parse_all(parser); */ /* ast_t *quote = root->subnodes[0]; */ /* printf("%s\n", quote->cdr->car->car->string_value); */ + + /* DONE: TEST PARSING AND STORING BINDINGS */ + /* lexer_t *lexer = init_lexer("(bind x \"hello world\")"); */ + /* parser_t *parser = init_parser(lexer); */ + /* ast_t *root = parse_all(parser); */ + /* if (hash_table_exists(parser->symbol_table, "x")) { */ + /* printf("YES!\n"); */ + /* ast_t *str = hash_table_get(parser->symbol_table, "x"); */ + /* printf("%s\n", str->string_value); */ + /* } */ + + /* TODO: TEST HASH TABLE COLLISIONS */ + /* TODO: TEST LIST EVALUATION */ return 0; } diff --git a/src/parser.c b/src/parser.c index 62e1c5f..16be8e1 100644 --- a/src/parser.c +++ b/src/parser.c @@ -127,15 +127,18 @@ ast_t *parse_function(parser_t *parser) { void parse_bind(parser_t *parser) { parser_move(parser); - parser_eat(parser, TOKEN_ID); - + if (parser->tokens[parser->i]->type != TOKEN_ID) + parser_error(parser); token_t *t = parser->tokens[parser->i]; char *name = t->value; parser_move(parser); ast_t *expr = parse_expr(parser); /* unevaluated expr will be evaluated when hash table transfers to visitor JIT */ - hash_table_add(parser->symbol_table, name, expr); + + parser_move(parser); + /* if (parser->tokens[parser->i]->type != TOKEN_RPAREN) */ + /* parser_error(parser); */ } ast_t *parse_list(parser_t *parser) { @@ -201,8 +204,11 @@ ast_t *parse_expr(parser_t *parser) { return parse_symbol(parser); else if (t->type == TOKEN_LPAREN) return parse_list(parser); - else + else { + printf("DEBUG\n"); + printf("%d", t->type); parser_error(parser); + } return NULL; } diff --git a/src/visitor.c b/src/visitor.c index a6fc040..96eb191 100644 --- a/src/visitor.c +++ b/src/visitor.c @@ -68,11 +68,10 @@ 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(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->symbol_table, e->string_value)) { ast_t *unevaled = hash_table_get(v->symbol_table, e->string_value); |