engine: resources, lang: funcs, parser: Add panic magic

It's valuable to check your runtime values and to shut down the entire
engine in case something doesn't match. This patch adds some magic
plumbing to support a "panic" mechanism.

A new "panic" statement gets transparently converted into a panic
function and panic resource. The former errors if the input is not
empty. The latter must be present to consume the value, but doesn't
actually do anything.
This commit is contained in:
James Shubin
2023-11-28 13:49:31 -05:00
parent 64e6e686e0
commit 2cbce963b7
12 changed files with 328 additions and 17 deletions

View File

@@ -231,6 +231,11 @@
}
return BOOL
}
/panic/ {
yylex.pos(lval) // our pos
lval.str = yylex.Text()
return PANIC_IDENTIFIER
}
/"(\\.|[^"])*"/
{ // This matches any number of the bracketed patterns
// that are surrounded by the two quotes on each side.

View File

@@ -92,6 +92,7 @@ func init() {
%token CLASS_IDENTIFIER INCLUDE_IDENTIFIER
%token IMPORT_IDENTIFIER AS_IDENTIFIER
%token COMMENT ERROR
%token PANIC_IDENTIFIER
// precedence table
// "Operator precedence is determined by the line ordering of the declarations;
@@ -169,6 +170,11 @@ stmt:
posLast(yylex, yyDollar) // our pos
$$.stmt = $1.stmt
}
| panic
{
posLast(yylex, yyDollar) // our pos
$$.stmt = $1.stmt
}
| resource
{
posLast(yylex, yyDollar) // our pos
@@ -919,6 +925,22 @@ bind:
}
}
;
panic:
// panic("some error")
PANIC_IDENTIFIER OPEN_PAREN call_args CLOSE_PAREN
{
posLast(yylex, yyDollar) // our pos
call := &ast.ExprCall{
Name: $1.str,
Args: $3.exprs,
//Var: false, // default
}
$$.stmt = &ast.StmtBind{
Ident: interfaces.PanicVarName, // make up a placeholder var
Value: call,
}
}
;
/* TODO: do we want to include this?
// resource bind
rbind: