From 2157fdbaca0b80e488c4f77ad5c0d04b051175cf Mon Sep 17 00:00:00 2001 From: Kimplul Date: Mon, 12 Aug 2024 18:55:46 +0300 Subject: make defer work better with generic code --- src/parser.y | 44 ++++++++------------------------------------ 1 file changed, 8 insertions(+), 36 deletions(-) (limited to 'src/parser.y') diff --git a/src/parser.y b/src/parser.y index a51eb9a..62afcac 100644 --- a/src/parser.y +++ b/src/parser.y @@ -134,7 +134,7 @@ %nterm var_init proc %nterm alias trait enum_val enums enum top unit id %nterm embed param_decl members -%nterm top_if const_if const_for defer goto assign +%nterm top_if const_if const_for goto assign %nterm construct construct_args construct_arg %nterm statelet @@ -157,6 +157,7 @@ %nterm opt_types opt_sign_decls sign_decls sign_decl sign_var_decl %nterm opt_construct_args %nterm opt_behaviours behaviours behaviour +%nterm opt_deferred_statement /* reverse lists */ %nterm rev_sign_decls; @@ -241,9 +242,6 @@ static char match_escape(char c); */ static char *strip(const char *s); -static struct ast *reverse_ast_list(struct ast *root); -static struct type *reverse_type_list(struct type *root); - %} %start input; @@ -344,9 +342,6 @@ opt_decls : decls | {$$ = NULL;} -defer - : "defer" body { $$ = gen_defer($2, src_loc(@$)); } - const_binop : const_expr "+" const_expr { $$ = gen_binop(AST_ADD, $1, $3, src_loc(@$)); @@ -480,16 +475,19 @@ statement | struct | struct_cont | for - | defer | if | const | enum | macro | ID ":" { $$ = gen_label($[ID], NULL, src_loc(@$)); } +opt_deferred_statement + : statement + | "defer" statement { $$ = gen_defer($2, src_loc(@$)); } + rev_statements - : rev_statements statement { $$ = $2; $2->n = $1; } - | statement + : rev_statements opt_deferred_statement { $$ = $2; $2->n = $1; } + | opt_deferred_statement statements : rev_statements { $$ = reverse_ast_list($1); } @@ -1002,32 +1000,6 @@ static char *strip(const char *str) } -static struct ast *reverse_ast_list(struct ast *root) -{ - struct ast *new_root = NULL; - while (root) { - struct ast *next = root->n; - root->n = new_root; - new_root = root; - root = next; - } - - return new_root; -} - -static struct type *reverse_type_list(struct type *root) -{ - struct type *new_root = NULL; - while (root) { - struct type *next = root->n; - root->n = new_root; - new_root = root; - root = next; - } - - return new_root; -} - struct parser *create_parser() { return calloc(1, sizeof(struct parser)); -- cgit v1.3