aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHimbeer <himbeer@disroot.org>2024-10-01 18:55:39 +0200
committerHimbeer <himbeer@disroot.org>2024-10-01 19:04:36 +0200
commit002ed247934384de40337fff32b39b3dcf4a5458 (patch)
tree0ad944c209bfed21d5026baea409419e03f45979
parent7c974011b2d2812550de0c1af49fa83b18ea3f81 (diff)
Attach (optional) labels to blocks rather than loops
This makes it possible to return values from any block by breaking out of it, enabling if statements to yield expressions when combined with commit ce68792c848caee2f184e7a6392d9f1e958da1a1.
-rw-r--r--doc/grammar.txt6
-rw-r--r--include/parse.h2
2 files changed, 4 insertions, 4 deletions
diff --git a/doc/grammar.txt b/doc/grammar.txt
index 4df88d7..0a86a68 100644
--- a/doc/grammar.txt
+++ b/doc/grammar.txt
@@ -14,7 +14,7 @@ externfunc := "extern" "func" NAME "(" ( type ), ")" [ type ] ";"
constant := [ COMMENT ]
"const" NAME "=" ( NUMBER | bool | STRING* | type ) ";"
-block := "{" command* "}"
+block := [ ":" NAME ] "{" command* "}"
command := block | statement
@@ -30,11 +30,11 @@ if := "if" expression block elseif* else*
elseif := "else" "if" expression block
else := "else" block
-for := "for" [ ":" NAME ] [ assignment ";" ] expression [ ";" assignment ] block
+for := "for" [ assignment ";" ] expression [ ";" assignment ] block
return := "return" expression
-break := "break" ":" NAME
+break := "break" ":" NAME [ expression ]
continue := "continue" ":" NAME
declaration := "let" [ "mut" ] NAME "=" expression ";"
diff --git a/include/parse.h b/include/parse.h
index ff5e3f1..21584cc 100644
--- a/include/parse.h
+++ b/include/parse.h
@@ -19,6 +19,7 @@ struct ast_param {
};
struct ast_block {
+ const char *label;
struct ast_cmd *cmds;
int cmdsz, cmdlen;
};
@@ -92,7 +93,6 @@ struct ast_if {
};
struct ast_for {
- const char *label;
struct ast_assign *init, *action;
struct ast_expr cond;
struct ast_block blk;