From b2c539fadfa7ea3eea2e5f7c0c37b5f5f1370c5a Mon Sep 17 00:00:00 2001 From: Preston Pan Date: Tue, 3 Jan 2023 09:45:40 -0800 Subject: actually evaluates and prints something now! --- src/ast.c | 7 +++++++ src/hash_table.c | 12 +++++------ src/include/.ast.h.swp | Bin 12288 -> 0 bytes src/include/.visitor.h.swp | Bin 0 -> 12288 bytes src/include/ast.h | 9 +++++++- src/include/parser.h | 2 +- src/include/print.h | 6 ++++++ src/include/visitor.h | 2 +- src/main.c | 29 ++++++++++++++++++++------ src/parser.c | 5 +++-- src/print.c | 50 +++++++++++++++++++++++++++++++++++++++++++++ src/visitor.c | 24 ++++++++++++++++++++++ 12 files changed, 129 insertions(+), 17 deletions(-) delete mode 100644 src/include/.ast.h.swp create mode 100644 src/include/.visitor.h.swp create mode 100644 src/print.c (limited to 'src') diff --git a/src/ast.c b/src/ast.c index a2a7bb5..e1a8e6c 100644 --- a/src/ast.c +++ b/src/ast.c @@ -57,3 +57,10 @@ ast_t *init_ast_function(ast_t *car, ast_t *cdr) { a->cdr = cdr; return a; } + +ast_t *init_ast_root(ast_t **subnodes, int size) { + ast_t *a = init_ast(AST_ROOT); + a->subnodes = subnodes; + a->root_size = size; + return a; +} diff --git a/src/hash_table.c b/src/hash_table.c index 3ca6dd0..a6524ac 100644 --- a/src/hash_table.c +++ b/src/hash_table.c @@ -70,7 +70,7 @@ ast_t *sl_list_get(sl_list_t *l, char *key) { return NULL; } -bool sl_list_exists(sl_list_l, char *key) { +bool sl_list_exists(sl_list_t *l, char *key) { if (sl_list_get(l, key) != NULL) return true; return false; @@ -111,17 +111,17 @@ ast_t *hash_table_get(hash_table_t *h, char *key) { return sl_list_get(l, key); } +bool hash_table_exists(hash_table_t *h, char *key) { + sl_list_t *l = h->buckets[hash(key, h->size)]; + return sl_list_exists(l, key); +} + void hash_table_free(hash_table_t *h) { for (int i = 0; i < h->size; i++) sl_list_free(h->buckets[i]); free(h); } -bool hash_table_exists(hash_table_t *h, char *key) { - sl_list_t *l = h->buckets[hash(key, h->size)]; - return sl_list_exists(l, key); -} - /* DJB2 HASH FUNCTION */ unsigned long hash(char *key, int size) { unsigned long hash = 5381; diff --git a/src/include/.ast.h.swp b/src/include/.ast.h.swp deleted file mode 100644 index 31e16ab..0000000 Binary files a/src/include/.ast.h.swp and /dev/null differ diff --git a/src/include/.visitor.h.swp b/src/include/.visitor.h.swp new file mode 100644 index 0000000..febe477 Binary files /dev/null and b/src/include/.visitor.h.swp differ diff --git a/src/include/ast.h b/src/include/ast.h index ca07c9d..a5095e1 100644 --- a/src/include/ast.h +++ b/src/include/ast.h @@ -4,6 +4,8 @@ typedef struct AST_STRUCT { enum { + /* root node */ + AST_ROOT, /* complex types */ AST_PAIR, AST_SYMBOL, @@ -14,9 +16,12 @@ typedef struct AST_STRUCT { AST_FLOAT, AST_FUNCTION, } type; + /* we need to know the amount of expressions in the root node */ + int root_size; + struct AST_STRUCT **subnodes; /* For functions, the car will be a list of variables, and the cdr will be the - * expression */ + * expression. */ int argument_number; /* number of arguments that function accepts. Used for speeding up stuff. */ struct AST_STRUCT *car; @@ -43,4 +48,6 @@ ast_t *init_ast_bool(bool value); ast_t *init_ast_symbol(char *value); ast_t *init_ast_function(ast_t *car, ast_t *cdr); + +ast_t *init_ast_root(ast_t **subnodes, int size); #endif diff --git a/src/include/parser.h b/src/include/parser.h index 64ae792..7b26696 100644 --- a/src/include/parser.h +++ b/src/include/parser.h @@ -44,5 +44,5 @@ void parse_bind(parser_t *parser); ast_t *parse_expr(parser_t *parser); -ast_t *read_in(char *s); +ast_t *parse_all(parser_t *parser); #endif diff --git a/src/include/print.h b/src/include/print.h index 9dbecbd..866f63e 100644 --- a/src/include/print.h +++ b/src/include/print.h @@ -12,5 +12,11 @@ void print_float(ast_t *f); void print_func(ast_t *f); +void print_list(ast_t *f); + +void print_symbol(ast_t *s); + +void print_pair(ast_t *p); + void print(ast_t *res); #endif diff --git a/src/include/visitor.h b/src/include/visitor.h index 87f2853..6af052b 100644 --- a/src/include/visitor.h +++ b/src/include/visitor.h @@ -10,7 +10,7 @@ typedef struct { ast_t *root; } visitor_t; -void eval_error(visitor_t *v); +void eval_error(visitor_t *v, ast_t *e); visitor_t *init_visitor(ast_t *root); diff --git a/src/main.c b/src/main.c index 08cadbc..7da03dc 100644 --- a/src/main.c +++ b/src/main.c @@ -1,6 +1,12 @@ #define _GNU_SOURCE +#include "./include/ast.h" +#include "./include/hash_table.h" #include "./include/lexer.h" +#include "./include/parser.h" +#include "./include/print.h" +#include "./include/stack.h" #include "./include/token.h" +#include "./include/visitor.h" #include #include #include @@ -8,12 +14,12 @@ int main(int argc, char **argv) { /* Test Lexer */ - lexer_t *lexer = init_lexer("'(fasd \"aaaaaaaa\" 4)"); - token_t *t = lexer_collect_next(lexer); - while (t != NULL) { - printf("%d: %s\n", t->type, t->value); - t = lexer_collect_next(lexer); - } + /* lexer_t *lexer = init_lexer("'(fasd \"aaaaaaaa\" 4)"); */ + /* token_t *t = lexer_collect_next(lexer); */ + /* while (t != NULL) { */ + /* printf("%d: %s\n", t->type, t->value); */ + /* t = lexer_collect_next(lexer); */ + /* } */ /* printf("Welcome to the NXS REPL.\n"); */ /* char *buf = malloc(2); */ @@ -35,5 +41,16 @@ int main(int argc, char **argv) { /* } */ /* } */ + /* TEST PARSER, VISITOR, PRINTER */ + lexer_t *lexer = init_lexer("\"hello world\""); + parser_t *parser = init_parser(lexer); + + ast_t *root = parse_expr(parser); + + visitor_t *v = init_visitor(root); + + ast_t *res = eval_expr(v, root); + + print(res); return 0; } diff --git a/src/parser.c b/src/parser.c index da2cf1d..be08ac6 100644 --- a/src/parser.c +++ b/src/parser.c @@ -52,9 +52,9 @@ void parser_eat(parser_t *parser, int type) { ast_t *parse_bool(parser_t *parser) { token_t *t = parser->tokens[parser->i]; parser_move(parser); - if (strcmp("T", t->value) == 0) { + if (strcmp("T", t->value) == 0) return init_ast_bool(true); - } else + else return init_ast_bool(false); } @@ -181,4 +181,5 @@ ast_t *parse_expr(parser_t *parser) { return NULL; } +ast_t *parse_all(parser_t *parser) {} void parser_error(parser_t *parser) { exit(1); } diff --git a/src/print.c b/src/print.c new file mode 100644 index 0000000..e653571 --- /dev/null +++ b/src/print.c @@ -0,0 +1,50 @@ +#include "./include/print.h" +#include "./include/ast.h" +#include + +void print_string(ast_t *str) { printf("=> %s\n", str->string_value); } + +void print_int(ast_t *i) { printf("=> %d\n", i->int_value); } + +void print_bool(ast_t *b) { + if (b->bool_value) + printf("=> T\n"); + else + printf("=> F\n"); +} + +void print_float(ast_t *f) { printf("=> %f\n", f->float_value); } + +void print_symbol(ast_t *s) { printf("=> %s\n", s->string_value); } + +void print_func(ast_t *f) {} + +void print_pair(ast_t *p) {} +void print(ast_t *res) { + switch (res->type) { + case AST_STRING: + print_string(res); + break; + case AST_INT: + print_int(res); + break; + case AST_BOOL: + print_bool(res); + break; + case AST_FLOAT: + print_float(res); + break; + case AST_FUNCTION: + print_func(res); + break; + case AST_SYMBOL: + print_symbol(res); + break; + case AST_PAIR: + print_pair(res); + break; + case AST_ROOT: + printf("Yeah, let's not do that\n"); + break; + } +} diff --git a/src/visitor.c b/src/visitor.c index 25296c1..98a7d92 100644 --- a/src/visitor.c +++ b/src/visitor.c @@ -2,7 +2,9 @@ #include "./include/hash_table.h" #include "./include/macros.h" #include "./include/stack.h" +#include #include +#include visitor_t *init_visitor(ast_t *root) { visitor_t *v = (visitor_t *)malloc(sizeof(visitor_t)); @@ -23,3 +25,25 @@ bool is_self_evaluating(ast_t *e) { return true; return false; } + +/* Special symbols: car, cdr, quote, *, /, +, -, %, inc, dec, >, <, >=, <=, /=, + * =, equal (for strings), input */ +ast_t *eval_symbol(visitor_t *v, ast_t *e) {} + +ast_t *eval_list(visitor_t *v, ast_t *e) {} + +ast_t *eval_expr(visitor_t *v, ast_t *e) { + if (is_self_evaluating(e)) + return e; + else if (e->type == AST_PAIR) + return eval_list(v, e); + else if (e->type == AST_SYMBOL) + return eval_symbol(v, e); + else { + eval_error(v, e); + } +} + +ast_t *eval(visitor_t *v) {} + +void eval_error(visitor_t *v, ast_t *e) { exit(1); } -- cgit