diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/include/print.h | 2 | ||||
-rw-r--r-- | src/main.c | 14 | ||||
-rw-r--r-- | src/parser.c | 4 | ||||
-rw-r--r-- | src/print.c | 8 | ||||
-rw-r--r-- | src/visitor.c | 43 |
5 files changed, 55 insertions, 16 deletions
diff --git a/src/include/print.h b/src/include/print.h index 866f63e..e272fa4 100644 --- a/src/include/print.h +++ b/src/include/print.h @@ -19,4 +19,6 @@ void print_symbol(ast_t *s); void print_pair(ast_t *p); void print(ast_t *res); + +void print_root(ast_t *root); #endif @@ -40,17 +40,11 @@ int main(int argc, char **argv) { /* printf("lmao\n"); */ /* } */ /* } */ - /* TEST PARSER, VISITOR, PRINTER */ - lexer_t *lexer = init_lexer("34.4"); + 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); + visitor_t *visitor = init_visitor(parser); + ast_t *root = eval(visitor); + print_root(root); return 0; } diff --git a/src/parser.c b/src/parser.c index 7f174a5..ac4d709 100644 --- a/src/parser.c +++ b/src/parser.c @@ -14,7 +14,7 @@ parser_t *init_parser(lexer_t *lexer) { p->i = 0; p->tokens = malloc(sizeof(token_t *)); - p->symbol_table = init_hash_table(10000); + p->symbol_table = init_hash_table(1000); p->finished = false; if (p->tokens == NULL) die("malloc on p->tokens"); @@ -117,6 +117,8 @@ ast_t *parse_function(parser_t *parser) { ast_t *car = parse_function_args(parser); ast_t *cdr = parse_expr(parser); /* a function can contain a single expression */ + if (cdr == NULL) + parser_error(parser); parser_eat(parser, TOKEN_RPAREN); return init_ast_function(car, cdr); } diff --git a/src/print.c b/src/print.c index e653571..49b6ec9 100644 --- a/src/print.c +++ b/src/print.c @@ -17,9 +17,10 @@ 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_func(ast_t *f) { print_pair(f->cdr); } void print_pair(ast_t *p) {} + void print(ast_t *res) { switch (res->type) { case AST_STRING: @@ -48,3 +49,8 @@ void print(ast_t *res) { break; } } + +void print_root(ast_t *root) { + for (int i = 0; i < root->root_size; i++) + print(root->subnodes[i]); +} diff --git a/src/visitor.c b/src/visitor.c index 1e14c2e..3c28245 100644 --- a/src/visitor.c +++ b/src/visitor.c @@ -13,7 +13,7 @@ visitor_t *init_visitor(parser_t *p) { die("malloc on visitor"); v->stack_frame = init_stack(512); v->symbol_table = p->symbol_table; - v->eval_table = init_hash_table(10000); + v->eval_table = init_hash_table(1000); v->root = parse_all(p); return v; } @@ -28,7 +28,39 @@ bool is_self_evaluating(ast_t *e) { return false; } -bool is_built_in(ast_t *e) {} +bool is_built_in(ast_t *e) { + char *cmp = e->string_value; + /* Basic mathematics */ + if (strcmp(cmp, "*") == 0 || strcmp(cmp, "+") == 0 || strcmp(cmp, "-") == 0 || + strcmp(cmp, "/") == 0 || strcmp(cmp, "%") == 0) + return true; + + /* Some string and list operations */ + if (strcmp(cmp, "concat") == 0 || strcmp(cmp, "len") == 0 || + strcmp(cmp, "car") == 0 || strcmp(cmp, "cdr") == 0 || + strcmp(cmp, "cons") == 0) + return true; + + if (strcmp(cmp, "<") == 0 || strcmp(cmp, ">") == 0 || strcmp(cmp, "=") == 0 || + strcmp(cmp, "<=") == 0 || strcmp(cmp, ">=") == 0 || + strcmp(cmp, "eq") == 0) + return true; + + /* Type-checking */ + if (strcmp(cmp, "bool?") == 0 || strcmp(cmp, "int?") == 0 || + strcmp(cmp, "symbol?") == 0 || strcmp(cmp, "float?") == 0 || + strcmp(cmp, "string?") == 0 || strcmp(cmp, "pair?") == 0 || + strcmp(cmp, "func?") == 0) + return true; + + /* Type conversions */ + if (strcmp(cmp, "atoi") == 0 || strcmp(cmp, "itof") == 0 || + strcmp(cmp, "ftoi") == 0 || strcmp(cmp, "itoa") == 0 || + strcmp(cmp, "atof") == 0 || strcmp(cmp, "ftoa") == 0) + 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) { @@ -67,11 +99,14 @@ ast_t *eval_expr(visitor_t *v, ast_t *e) { ast_t *eval(visitor_t *v) { ast_t *cur; ast_t *root; - ast_t **eval_nodes; - int j = 0; + ast_t **eval_nodes = malloc(sizeof(ast_t *)); for (int i = 0; i < v->root->root_size; i++) { cur = eval_expr(v, v->root->subnodes[i]); + eval_nodes = realloc(eval_nodes, (i + 1) * sizeof(ast_t *)); + eval_nodes[i] = cur; } + root = init_ast_root(eval_nodes, v->root->root_size); + return root; } void eval_error(visitor_t *v, ast_t *e) { exit(1); } |