author | Alan Dipert
<alan@dipert.org> 2024-01-06 21:29:24 UTC |
committer | Alan Dipert
<alan@dipert.org> 2024-01-06 21:29:24 UTC |
parent | 34a4e3262849f9408e3b362e3b934dd58f875667 |
ast.c | +15 | -2 |
ast.h | +7 | -4 |
bbasic.y | +4 | -2 |
main.c | +2 | -0 |
diff --git a/ast.c b/ast.c index bc2c453..d05d567 100644 --- a/ast.c +++ b/ast.c @@ -1,8 +1,21 @@ #include "ast.h" -struct type_node* ast_make_string(char *s) { - struct type_node *n = malloc(sizeof(struct type_node)); +struct node_tag* ast_last_line; + +struct node_tag* ast_make_string(char *s) { + struct node_tag *n = malloc(sizeof(struct node_tag)); n->type = NODE_STRING; n->node0 = s; return n; } + +struct node_tag* ast_make_line(int line, struct node_tag* stmt) { + struct node_tag *n = malloc(sizeof(struct node_tag)); + int *i = malloc(sizeof(int)); + *i = line; + n->type = NODE_LINE; + n->node0 = i; + n->node1 = stmt; + ast_last_line = n; + return n; +} diff --git a/ast.h b/ast.h index 3f075e0..9f388ff 100644 --- a/ast.h +++ b/ast.h @@ -1,6 +1,6 @@ #include <stdlib.h> -enum TYPE_NODE { +enum NODE_TYPE { NODE_LINE, NODE_PRINT, NODE_IF, @@ -18,10 +18,13 @@ enum TYPE_NODE { NODE_ID }; -struct type_node { - enum TYPE_NODE type; +struct node_tag { + enum NODE_TYPE type; void *node0; void *node1; }; -struct type_node* ast_make_string(char *s); +struct node_tag* ast_make_string(char *s); +struct node_tag* ast_make_line(int linum, struct node_tag* stmt); + +extern struct node_tag* ast_last_line; diff --git a/bbasic.y b/bbasic.y index b9123b0..77c73c5 100644 --- a/bbasic.y +++ b/bbasic.y @@ -23,7 +23,7 @@ void yyerror(const char *s) { %token <str> STRING %token PRINT IF THEN GOTO LT LTE GT GTE EQ -%type <node> expression +%type <node> line expression statement %define parse.error verbose @@ -32,7 +32,9 @@ void yyerror(const char *s) { %% line: - NUMBER_INTEGER statement + NUMBER_INTEGER statement { + $$ = ast_make_line($1, $2); + } ; statement: diff --git a/main.c b/main.c index 6633c0f..4f0583a 100644 --- a/main.c +++ b/main.c @@ -1,5 +1,6 @@ #include <stdio.h> #include "bbasic.tab.h" +#include "ast.h" extern int yyparse(); @@ -8,6 +9,7 @@ int main() { while (1) { if(!yyparse()) { printf("It parsed :-)\n"); + printf("linum: %d\n", *(int *)ast_last_line->node0); } } return 0;