diff options
| author | Kimplul <kimi.h.kuparinen@gmail.com> | 2024-08-11 19:33:23 +0300 |
|---|---|---|
| committer | Kimplul <kimi.h.kuparinen@gmail.com> | 2024-08-11 19:34:23 +0300 |
| commit | 3065334aec0f357c88eab144628a798db4a3e5ea (patch) | |
| tree | 93e819215c6599103f28408c3b7afe8487b4685e /src | |
| parent | 0bee8234a52c9399544364e8d26b124cb487ce7c (diff) | |
| download | ek-3065334aec0f357c88eab144628a798db4a3e5ea.tar.gz ek-3065334aec0f357c88eab144628a798db4a3e5ea.zip | |
parse struct continuation
+ Similar in idea to impl in Rust
Diffstat (limited to 'src')
| -rw-r--r-- | src/ast.c | 1 | ||||
| -rw-r--r-- | src/parser.y | 62 |
2 files changed, 51 insertions, 12 deletions
@@ -244,6 +244,7 @@ void ast_dump(int depth, struct ast *n) DUMP(AST_ALIAS_DEF); DUMP(AST_TRAIT_DEF); DUMP(AST_STRUCT_DEF); + DUMP(AST_STRUCT_CONT_DEF); DUMP(AST_IF); DUMP(AST_BLOCK); DUMP(AST_IMPORT); diff --git a/src/parser.y b/src/parser.y index 6c567a9..06599f2 100644 --- a/src/parser.y +++ b/src/parser.y @@ -140,7 +140,7 @@ %nterm <type> types type opt_type -%nterm <node> tagged_struct expr_if +%nterm <node> struct struct_cont expr_if /* constant operations */ %nterm <node> const_expr const_unop const_binop @@ -155,6 +155,7 @@ /* optional stuff */ %nterm <node> opt_exprs proc_decl member opt_members %nterm <type> opt_types opt_sign_decls sign_decls sign_decl sign_var_decl +%nterm <node> opt_behaviours behaviours behaviour /* reverse lists */ %nterm <type> rev_sign_decls; @@ -250,8 +251,7 @@ id : ID {$$ = gen_id($1, src_loc(@$));} var - : var_decl - | var_init + : var_init embed : "embed" "(" STRING ")" { $$ = gen_embed(strip($3), src_loc(@$)); } @@ -476,7 +476,8 @@ statement | while | do_while | body - | tagged_struct + | struct + | struct_cont | for | defer | if @@ -731,11 +732,14 @@ proc ast_set_flags($$, AST_FLAG_EXTERN | AST_FLAG_NOMANGLE); } -member - : var_decl ";" - | type_expand ";" +behaviour + : type_expand ";" | proc_decl ";" | proc + +member + : var_decl ";" + | behaviour ; members @@ -746,12 +750,23 @@ opt_members : members | {$$ = NULL;} +behaviours + : behaviours behaviour { + $$ = $1; $1->n = $2; + ast_set_flags($$, AST_FLAG_MEMBER); + } + | behaviour { $$ = $1; ast_set_flags($$, AST_FLAG_MEMBER); } + +opt_behaviours + : behaviours + | {$$ = NULL;} + macro_expand : APPLY "(" opt_exprs ")" { $$ = gen_macro_expand($1, $3, src_loc(@$)); } -tagged_struct +struct : "typedef" ID "[" opt_type_params "]" "{" opt_members "}" { $$ = gen_struct($2, $4, $7, src_loc(@$)); } @@ -759,6 +774,25 @@ tagged_struct $$ = gen_struct($2, NULL, $4, src_loc(@$)); } +struct_cont + : "continue" "[" opt_type_params "]" + APPLY "[" opt_types "]" "{" opt_behaviours "}" { + /* full form */ + $$ = gen_struct_cont($5, $3, $7, $10, src_loc(@$)); + } + | "continue" "[" opt_type_params "]" ID "{" opt_behaviours "}" { + /* abbrev 1 */ + $$ = gen_struct_cont($5, $3, NULL, $7, src_loc(@$)); + } + | "continue" APPLY "[" opt_types "]" "{" opt_behaviours "}" { + /* abbrev 2 */ + $$ = gen_struct_cont($2, NULL, $4, $7, src_loc(@$)); + } + | "continue" ID "{" opt_behaviours "}" { + /* abbrev 3 */ + $$ = gen_struct_cont($2, NULL, NULL, $4, src_loc(@$)); + } + alias : "typedef" ID type { $$ = gen_alias($2, $3, src_loc(@$)); @@ -771,7 +805,9 @@ type_param } type_params - : type_param "," type_params { $$ = $1; $1->n = $3; } + : type_param "," type_params { + $$ = $1; $1->n = $3; /** @todo this could be left recursive */ + } | type_param opt_type_params @@ -779,7 +815,7 @@ opt_type_params | { $$ = NULL; } trait - : "define" ID "[" opt_type_params "]" "{" opt_members "}" { + : "define" ID "[" opt_type_params "]" "{" opt_behaviours "}" { $$ = gen_trait($2, $4, $7, src_loc(@$)); } @@ -824,14 +860,16 @@ top_if top : enum | proc - | tagged_struct + | struct + | struct_cont { $$ = $1; } | macro { $$ = $1; } | top_if { $$ = $1; ast_set_flags($$, AST_FLAG_CONST); } | import { $$ = $1; } | alias { $$ = $1; } | trait { $$ = $1; } | "pub" enum { $$ = $2; ast_set_flags($2, AST_FLAG_PUBLIC); } - | "pub" tagged_struct { $$ = $2; ast_set_flags($2, AST_FLAG_PUBLIC); } + | "pub" struct { $$ = $2; ast_set_flags($2, AST_FLAG_PUBLIC); } + | "pub" struct_cont { $$ = $2; ast_set_flags($2, AST_FLAG_PUBLIC); } | "pub" proc { $$ = $2; ast_set_flags($2, AST_FLAG_PUBLIC); } | "pub" macro { $$ = $2; ast_set_flags($2, AST_FLAG_PUBLIC); } | "pub" import { $$ = $2; ast_set_flags($2, AST_FLAG_PUBLIC); } |
