commit c78f9c88cbc5a4bb51e6bc2f5d3c8e2f2b51d478
parent 6b3e0070bde0711ce26791070704d0392b323ede
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Wed, 15 Jun 2022 16:34:22 -0400
cc: add "else" support in if statement
Diffstat:
4 files changed, 11 insertions(+), 4 deletions(-)
diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs
@@ -225,7 +225,6 @@ ASTIDCNT wordtbl astdatatbl ( node -- node )
_nextt dup S" )" s= if 2drop exit then
',' expectChar _nextt again ;
-\ Parse a LValue
\ Parse an assignment statement. It consists of a '=' char with an lvalue on
\ the left and an expression on the right. The left part can have LValue ops
\ applied to it.
@@ -248,7 +247,10 @@ create statementnames 6 c, ," return" 2 c, ," if" 0 c,
_nextt '(' expectChar
_nextt parseExpression ( sn ifn expr ) over addnode
_nextt ')' expectChar
- parseStatements ;
+ dup parseStatements ( snode ifnode )
+ _nextt dup S" else" s= if ( sn ifn tok )
+ drop parseStatements else
+ to nexttputback drop then ;
: _ ( parentnode -- ) \ parseStatements
_nextt '{' expectChar AST_STATEMENTS newnode _nextt
diff --git a/fs/cc/gen.fs b/fs/cc/gen.fs
@@ -99,7 +99,10 @@ ASTIDCNT wordtbl gentbl ( node -- )
:w ( If )
firstchild ?dup not if _err then dup gennode ( exprnode )
operand?>result vmjz, swap ( jump_addr exprnode )
- nextsibling ?dup not if _err then gennode ( jump_addr ) vmjmp! ;
+ nextsibling ?dup not if _err then dup gennode ( jump_addr condnode )
+ nextsibling ?dup if ( jump_addr elsenode )
+ vmjnz, ( ja1 enode ja2 ) rot vmjmp! ( enode ja2 )
+ swap gennode ( ja2 ) then ( jump_addr ) vmjmp! ;
'w _err ( unused )
:w ( FunCall )
\ pass arguments
diff --git a/fs/tests/cc/cc.fs b/fs/tests/cc/cc.fs
@@ -15,6 +15,6 @@ funcall 42 #eq
42 plusone 43 #eq
ptrget 42 #eq
ptrset 54 #eq
-12 condif 12 #eq
+12 condif 13 #eq
42 condif 142 #eq
testend
diff --git a/fs/tests/cc/test.c b/fs/tests/cc/test.c
@@ -40,6 +40,8 @@ int ptrset() {
int condif(int x) {
if (x == 42) {
x = x+100;
+ } else {
+ x = x+1;
}
return x;
}