From 957da9056c36a5eea15c6058701f7465b31f64a8 Mon Sep 17 00:00:00 2001 From: Kimplul Date: Sun, 30 Mar 2025 22:36:53 +0300 Subject: WIP: rewrite C++ backend to be C + C allows for a bit more control, and we can manually handle closure contexts. For example `examples/fib.fwd` now works for effectively any `n`, pretty cool. + Fairly slow Fibonacci, I must admit. Initial profiling indicates it's mainly due to branch mispredictions, but I'll have to look into this a bit deeper. + The code is a bit hacked together, for now I'm more interested in getting things working, I'll worry about making things pretty later. + For testing, there's also initial support for modules, just so I can print stuff to the terminal + This commit is way too big, lol --- src/parser.y | 52 ++++++++++++---------------------------------------- 1 file changed, 12 insertions(+), 40 deletions(-) (limited to 'src/parser.y') diff --git a/src/parser.y b/src/parser.y index 7b2c651..6ec2eb2 100644 --- a/src/parser.y +++ b/src/parser.y @@ -73,7 +73,6 @@ %token IF "if" %token ELSE "else" %token NIL "nil" -%token OWN "own" %token PUB "pub" %token MUT "mut" %token CONTINUE "continue" @@ -82,7 +81,6 @@ %token DOT "." %token SCOPE "::" %token FATARROW "=>" -%token ERRARROW "!>" %right "[" "]" /* precedence */ @@ -101,10 +99,10 @@ %left "::" %nterm top unit proc proc_decl call closure var expr statement body -%nterm vars exprs statements closures err trailing_closure -%nterm opt_vars opt_exprs opt_statements opt_trailing_closure opt_err opt_error +%nterm vars exprs statements closures trailing_closure +%nterm opt_vars opt_exprs opt_statements opt_trailing_closure %nterm rev_vars rev_exprs rev_closures rev_statements -%nterm let if nil own unop binop construct import +%nterm let if nil unop binop construct import %nterm struct struct_cont trait %nterm type_param type_params opt_type_params @@ -346,29 +344,20 @@ closures } } -err - : "!>" ID body { - $$ = gen_err_branch($2, $3, src_loc(@$)); - } - -opt_err - : err - | { $$ = NULL; } - call - : expr "(" opt_exprs ")" ";" opt_err { - $$ = gen_call($1, $[opt_exprs], $[opt_err], src_loc(@$)); + : expr "(" opt_exprs ")" ";" { + $$ = gen_call($1, $[opt_exprs], src_loc(@$)); } - | expr "(" opt_exprs ")" "=>" opt_vars ";" opt_err { + | expr "(" opt_exprs ")" "=>" opt_vars ";" { /* the rest of the function body is our closure, gets fixed up * later */ struct ast *closure = gen_closure($[opt_vars], NULL, src_loc(@$)); ast_append(&$[opt_exprs], closure); - $$ = gen_call($[expr], $[opt_exprs], $[opt_err], src_loc(@$)); + $$ = gen_call($[expr], $[opt_exprs], src_loc(@$)); } - | expr "(" opt_exprs ")" closures opt_err { + | expr "(" opt_exprs ")" closures { ast_append(&$[opt_exprs], $[closures]); - $$ = gen_call($[expr], $[opt_exprs], $[opt_err], src_loc(@$)); + $$ = gen_call($[expr], $[opt_exprs], src_loc(@$)); } let @@ -384,26 +373,11 @@ nil $$ = gen_nil($2, $3, $5, src_loc(@$)); } -own - : "own" ID body { - $$ = gen_own($2, $3, src_loc(@$)); - } - -opt_error - : "error" STRING { - $$ = gen_error($2, NULL, src_loc(@$)); - } - | "error" ID { - $$ = gen_error(NULL, gen_id($2, src_loc(@$)), src_loc(@$)); - } - | { $$ = NULL; } - statement : call | body | let | nil - | own | if | ";" { $$ = gen_empty(src_loc(@$)); } @@ -419,8 +393,8 @@ opt_statements | { $$ = NULL; } body - : "{" opt_statements opt_error "}" { - $$ = gen_block($2, $3, src_loc(@$)); + : "{" opt_statements "}" { + $$ = gen_block($2, src_loc(@$)); } behaviour @@ -481,18 +455,16 @@ trait import : "import" STRING { - $$ = gen_import($2, src_loc(@$)); + $$ = gen_import(strip($2), src_loc(@$)); } top : proc - | proc_decl | struct | struct_cont | import | trait | "pub" proc { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); } - | "pub" proc_decl { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); } | "pub" struct { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); } | "pub" struct_cont { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); } | "pub" import { $$ = $2; ast_set_flags($$, AST_FLAG_PUBLIC); } -- cgit v1.2.3