commit 29e4eff849ad3bc03f17cb356d01e6e0b8d43bf0
parent fa79542264d6ea6676f200275b0f24c3db596c34
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Mon, 13 Jun 2022 09:18:54 -0400
cc: allow variable assignment statements
Diffstat:
4 files changed, 11 insertions(+), 7 deletions(-)
diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs
@@ -47,7 +47,7 @@ create bopsprectbl 1 c, 1 c, 0 c, 0 c, 2 c, 2 c, 2 c, 2 c,
4 value AST_CONSTANT \ data1=value
5 value AST_STATEMENTS
6 value AST_ARGSPECS
-\ 7 = unused
+7 value AST_LVALUE \ data1=varname
8 value AST_UNARYOP \ data1=uopid
\ 9 = unused
10 value AST_BINARYOP \ data1=bopid
@@ -58,7 +58,7 @@ create bopsprectbl 1 c, 1 c, 0 c, 0 c, 2 c, 2 c, 2 c, 2 c,
create astidnames 7 c, ," declare" 4 c, ," unit" 8 c, ," function"
6 c, ," return" 8 c, ," constant" 5 c, ," stmts"
- 4 c, ," args" 4 c, ," expr" 7 c, ," unaryop"
+ 4 c, ," args" 6 c, ," lvalue" 7 c, ," unaryop"
1 c, ," _" 5 c, ," binop" 6 c, ," assign"
1 c, ," _" 3 c, ," var" 4 c, ," call"
0 c,
@@ -81,7 +81,7 @@ ASTIDCNT wordtbl astdatatbl ( node -- node )
'w _i ( Constant )
'w noop ( Statements )
'w noop ( ArgSpecs )
-'w noop ( Unused )
+'w _s ( LValue )
:w ( UnaryOp ) _[ dup data1 uopchar emit _] ;
'w noop ( Unused )
:w ( BinaryOp ) _[ dup data1 boptoken stype _] ;
@@ -209,8 +209,11 @@ ASTIDCNT wordtbl astdatatbl ( node -- node )
drop AST_RETURN createnode ( snode rnode ) 2dup swap addnode
_nextt parseExpression read; ( snode rnode expr )
swap addnode ( snode )
- else
- expectType drop dup parseDeclarationList then
+ else ( snode tok )
+ dup isType? if drop dup parseDeclarationList else ( snode tok )
+ expectIdent over AST_LVALUE newnode ( snode ident lvnode )
+ swap , _nextt parseAssign then
+ then ( snode )
_nextt again ;
: parseFunction ( unitnode tok -- )
diff --git a/fs/cc/gen.fs b/fs/cc/gen.fs
@@ -84,7 +84,7 @@ ASTIDCNT wordtbl gentbl ( node -- )
:w ( Constant ) eax data1 i32 mov, ;
:w ( Statements ) genchildren ;
'w genchildren ( ArgSpecs )
-'w genchildren ( Expression )
+'w genchildren ( Lvalue )
:w ( UnaryOp ) dup genchildren data1 uopgentbl swap wexec ;
'w genchildren ( Factor )
:w ( BinaryOp )
diff --git a/fs/tests/cc/cc.fs b/fs/tests/cc/cc.fs
@@ -9,7 +9,7 @@ neg -42 #eq
bwnot $ffffffd5 #eq
exprbinops 7 #eq
boolops 0 #eq
-variables 42 #eq
+variables 82 #eq
funcall 42 #eq
2 3 adder 5 #eq
42 plusone 43 #eq
diff --git a/fs/tests/cc/test.c b/fs/tests/cc/test.c
@@ -14,6 +14,7 @@ int boolops() {
int variables() {
int foo = 40;
int bar = 2;
+ bar = foo + bar;
return foo + bar;
}
int funcall() {