author | Alan Dipert
<alan@dipert.org> 2024-04-27 00:15:13 UTC |
committer | Alan Dipert
<alan@dipert.org> 2024-04-27 00:15:13 UTC |
parent | e9a0ab3e288fa30f8decec50769597c4a863e2b0 |
main.c | +11 | -3 |
parse.c | +10 | -5 |
parse.h | +7 | -0 |
usbasic.l | +1 | -0 |
usbasic.y | +4 | -1 |
diff --git a/main.c b/main.c index d584258..a4d0c2f 100644 --- a/main.c +++ b/main.c @@ -29,9 +29,17 @@ void read_loop() { struct node_tag **lines = vector_create(); while (1) { if ((line = read_line_stdin("🦄 "))) { - printf("It parsed 👍\n"); - printf("linum: %g\n", line->data.line.linum); - lines_insert(&lines, line); + switch (line->type) { + case NODE_COMMAND_RUN: + printf("RUN command entered\n"); + break; + case NODE_NUMBERED_LINE: + printf("linum: %g\n", line->data.line.linum); + lines_insert(&lines, line); + break; + default: + /* die */ + } lines_print(lines); } } diff --git a/parse.c b/parse.c index 3afff7b..1f01f9f 100644 --- a/parse.c +++ b/parse.c @@ -10,7 +10,7 @@ #include <gc.h> #include <stdio.h> -struct node_tag *ast_last_numbered_line; +struct node_tag *ast_last_read; static struct node_tag *ast_alloc(enum NODE_TYPE node_type) { struct node_tag *new_node = GC_malloc(sizeof(struct node_tag)); @@ -28,7 +28,13 @@ struct node_tag *ast_make_numbered_line(double line, struct node_tag *stmt) { struct node_tag *new_node = ast_alloc(NODE_NUMBERED_LINE); new_node->data.line.linum = line; new_node->data.line.stmt = stmt; - ast_last_numbered_line = new_node; + ast_last_read = new_node; + return new_node; +} + +struct node_tag *ast_make_command_run() { + struct node_tag *new_node = ast_alloc(NODE_COMMAND_RUN); + ast_last_read = new_node; return new_node; } @@ -62,7 +68,7 @@ struct node_tag *read_line(char *line) { yy_scan_string(line); yyparse_failed = yyparse(); yylex_destroy(); - return yyparse_failed ? NULL : ast_last_numbered_line; + return yyparse_failed ? NULL : ast_last_read; } struct node_tag *read_line_stdin(char *prompt) { @@ -72,8 +78,7 @@ struct node_tag *read_line_stdin(char *prompt) { if (strlen(line) == 0) return NULL; read_node = read_line(line); - if (read_node != NULL) - add_history(line); + add_history(line); free(line); return read_node; } diff --git a/parse.h b/parse.h index 8e63377..f5f6413 100644 --- a/parse.h +++ b/parse.h @@ -12,6 +12,7 @@ #include <readline/readline.h> enum NODE_TYPE { + NODE_COMMAND_RUN, NODE_NUMBERED_LINE, NODE_PRINT, NODE_IF, @@ -34,6 +35,10 @@ struct node_line_data { struct node_tag *stmt; }; +struct node_command_data { + /* arguments, if any */ +}; + struct node_string_data { char *str; }; @@ -59,6 +64,7 @@ struct node_tag { enum NODE_TYPE type; union { struct node_line_data line; + struct node_command_data command; struct node_string_data string; struct node_print_data print; struct node_iff_data iff; @@ -69,6 +75,7 @@ struct node_tag { struct node_tag *ast_make_string(char *s); struct node_tag *ast_make_numbered_line(double linum, struct node_tag *stmt); +struct node_tag *ast_make_command_run(); struct node_tag *ast_make_print(struct node_tag *expr); struct node_tag *ast_make_if(struct node_tag *pred, struct node_tag *stmt); struct node_tag *ast_make_id(char *name); diff --git a/usbasic.l b/usbasic.l index 821c755..9602add 100644 --- a/usbasic.l +++ b/usbasic.l @@ -21,6 +21,7 @@ [ \t]+ ; /* Ignore whitespaces */ \n { return 0; } +"run" return RUN; "print" return PRINT; "if" return IF; "then" return THEN; diff --git a/usbasic.y b/usbasic.y index 97b7453..0b53465 100644 --- a/usbasic.y +++ b/usbasic.y @@ -28,7 +28,7 @@ void yyerror(const char *s) { %token <number> NUMBER %token <id> IDENTIFIER %token <str> STRING -%token PRINT IF THEN GOTO LT LTE GT GTE EQ +%token RUN PRINT IF THEN GOTO LT LTE GT GTE EQ %type <node> line %type <node> expression @@ -44,6 +44,9 @@ line: NUMBER statement { $$ = ast_make_numbered_line($1, $2); } + | RUN { + $$ = ast_make_command_run(); + } ; statement: