aboutsummaryrefslogtreecommitdiff
path: root/src/parser.y
diff options
context:
space:
mode:
Diffstat (limited to 'src/parser.y')
-rw-r--r--src/parser.y62
1 files changed, 50 insertions, 12 deletions
diff --git a/src/parser.y b/src/parser.y
index 751aaa1..68725a8 100644
--- a/src/parser.y
+++ b/src/parser.y
@@ -72,10 +72,14 @@
%token RBRACKET "]"
%token IF "if"
%token ELSE "else"
+%token NIL "nil"
+%token OWN "own"
%token MUT "mut"
+%token ERROR "error"
%token DOT "."
%token SCOPE "::"
%token FATARROW "=>"
+%token ERRARROW "!>"
%right "[" "]"
/* precedence */
@@ -94,10 +98,10 @@
%left "::"
%nterm <node> top unit proc call closure var expr statement body
-%nterm <node> vars exprs statements closures trailing_closure
-%nterm <node> opt_vars opt_exprs opt_statements opt_trailing_closure
+%nterm <node> vars exprs statements closures err trailing_closure
+%nterm <node> opt_vars opt_exprs opt_statements opt_trailing_closure opt_err opt_error
%nterm <node> rev_vars rev_exprs rev_closures rev_statements
-%nterm <node> let if unop binop construct
+%nterm <node> let if nil own unop binop construct
%nterm <type> type rev_types types opt_types
@@ -299,7 +303,9 @@ opt_exprs
| { $$ = NULL; }
closure
- : "=>" opt_vars body { $$ = gen_closure($[opt_vars], $[body], src_loc(@$)); }
+ : "=>" opt_vars body {
+ $$ = gen_closure($[opt_vars], $[body], src_loc(@$));
+ }
| "&" "=>" opt_vars body {
$$ = gen_closure($[opt_vars], $[body], src_loc(@$));
ast_set_flags($$, AST_FLAG_NOMOVES);
@@ -330,20 +336,29 @@ closures
}
}
+err
+ : "!>" ID body {
+ $$ = gen_err_branch($2, $3, src_loc(@$));
+ }
+
+opt_err
+ : err
+ | { $$ = NULL; }
+
call
- : expr "(" opt_exprs ")" ";" {
- $$ = gen_call($1, $[opt_exprs], src_loc(@$));
+ : expr "(" opt_exprs ")" ";" opt_err {
+ $$ = gen_call($1, $[opt_exprs], $[opt_err], src_loc(@$));
}
- | expr "(" opt_exprs ")" "=>" opt_vars ";" {
+ | expr "(" opt_exprs ")" "=>" opt_vars ";" opt_err {
/* 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], src_loc(@$));
+ $$ = gen_call($[expr], $[opt_exprs], $[opt_err], src_loc(@$));
}
- | expr "(" opt_exprs ")" closures {
+ | expr "(" opt_exprs ")" closures opt_err {
ast_append(&$[opt_exprs], $[closures]);
- $$ = gen_call($[expr], $[opt_exprs], src_loc(@$));
+ $$ = gen_call($[expr], $[opt_exprs], $[opt_err], src_loc(@$));
}
let
@@ -354,11 +369,32 @@ if
| "if" expr body "else" body { $$ = gen_if($2, $3, $5, src_loc(@$)); }
| "if" expr body "else" if { $$ = gen_if($2, $3, $5, src_loc(@$)); }
+nil
+ : "nil" ID body "=>" var {
+ $$ = 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
- | body
| ";" { $$ = gen_empty(src_loc(@$)); }
rev_statements
@@ -373,7 +409,9 @@ opt_statements
| { $$ = NULL; }
body
- : "{" opt_statements "}" { $$ = gen_block($2, src_loc(@$)); }
+ : "{" opt_statements opt_error "}" {
+ $$ = gen_block($2, $3, src_loc(@$));
+ }
top
: proc