commit b440fd5d0ec09320cc6e346861934e65383b7a06
parent 8feddf080119b066cfd8556ad1832b572ca4327e
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Fri, 24 Mar 2023 22:28:21 -0400
halcc: forbreak() forcontinue()
Diffstat:
3 files changed, 41 insertions(+), 2 deletions(-)
diff --git a/fs/comp/c/fgen.fs b/fs/comp/c/fgen.fs
@@ -55,11 +55,15 @@ alias noop parseStatement ( tok -- ) \ forward declaration
: _pspush
read( nextt parseExpression Result :>W$ 0 PSP+) !, PS+ read) read; ;
+: _break [compile] ahead _breaks :push read; ;
+
+: _continue [compile] ahead _conts :push read; ;
+
10 stringlist statementnames
"{" "return" "if" "for" "pspush" "break" "continue" "while" "do" "switch"
10 wordtbl statementhandler ( -- )
'w parseStatements 'w _return 'w _if 'w _for
-'w _pspush 'w _err 'w _err 'w _err
+'w _pspush 'w _break 'w _continue 'w _err
'w _err 'w _err
0 value _laststmtid
diff --git a/fs/tests/comp/c/cc.fs b/fs/tests/comp/c/cc.fs
@@ -44,7 +44,6 @@ capture helloworld S" Hello World!" #s=
create expected ," Null terminated\0"
nullstr expected 16 []= #
5 forsum 10 #eq
-testend \s
1 multret 1 #eq
42 multret 32 #eq
1234 \ test that void funcs with args don't mess with the PS underneath it.
@@ -54,6 +53,7 @@ testend \s
forbreak 10 #eq
forcontinue 9 #eq
forempty \ no crash
+testend \s
-1 0 lts #
-1 0 ltu not #
0 boolnot 1 #eq
diff --git a/fs/tests/comp/c/test2.c b/fs/tests/comp/c/test2.c
@@ -180,3 +180,38 @@ int forsum(int n) {
}
return r;
}
+unsigned int multret(unsigned int x) {
+ if (x < 10) {
+ return x;
+ } else {
+ return x-10;
+ }
+}
+void multretvoid(unsigned int x) {
+ if (x == 42) {
+ stype("Answer to the universe");
+ return;
+ }
+ stype("Nope");
+}
+int forbreak() {
+ int i, j;
+ for (i=0; i<100; i++) {
+ if (i==10) break;
+ // the presence of a for() after the break doesn't break "break".
+ for (j=0; j<1; j++) {}
+ }
+ return i;
+}
+int forcontinue() {
+ int i, j=0;
+ for (i=0; i<10; i++) {
+ if (i==5) continue;
+ ++j;
+ }
+ return j;
+}
+// the first and last element of for() can be empty
+void forempty() {
+ for (;1;) return;
+}