commit 0ec09a0bb39be905edc36b3c8cd8a3ea2485d7dd
parent cfaeeaab9bbeb78382b6ce65526c002f6e4a75c8
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Mon, 13 Jun 2022 10:36:23 -0400
cc: laying out the ground work for pointers
Diffstat:
3 files changed, 19 insertions(+), 9 deletions(-)
diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs
@@ -8,9 +8,10 @@
\ 0 - Negate
\ 1 ~ Complement
\ 2 ! Not
+\ 3 & Reference
-3 value UOPSCNT
-create uopssyms ," -~!?"
+4 value UOPSCNT
+create uopssyms ," -~!&?"
: uopid ( tok -- opid? f )
c@+ 1 = if c@ uopssyms UOPSCNT [c]? dup 0< if drop 0 else 1 then
@@ -40,7 +41,7 @@ create bopsprectbl 1 c, 1 c, 0 c, 0 c, 2 c, 2 c, 2 c, 2 c,
\ AST node types
15 value ASTIDCNT
-0 value AST_DECLARE \ data1=name
+0 value AST_DECLARE \ data1=name data2='*' levels
1 value AST_UNIT
2 value AST_FUNCTION \ data1=name data2=MAP_FUNCTION
3 value AST_RETURN
@@ -74,7 +75,7 @@ create astidnames 7 c, ," declare" 4 c, ," unit" 8 c, ," function"
: _i _[ dup data1 .x _] ;
ASTIDCNT wordtbl astdatatbl ( node -- node )
-'w _s ( Declare )
+:w ( Declare ) _[ dup data1 stype spc> dup data2 .x1 _] ;
'w noop ( Unit )
'w _s ( Function )
'w noop ( Return )
@@ -185,17 +186,20 @@ ASTIDCNT wordtbl astdatatbl ( node -- node )
'=' expectChar AST_ASSIGN newnode ( anode )
_nextt parseExpression read; ( anode expr ) swap addnode ;
+: parseDeclare ( parentnode -- dnode )
+ 0 begin ( pnode *lvl )
+ _nextt dup S" *" s= if drop 1+ 0 else 1 then until ( pnode *lvl tok )
+ expectIdent rot ( *lvl name pnode ) AST_DECLARE newnode ( *lvl name dnode )
+ swap , swap , ( dnode ) ;
+
: parseDeclarationList ( stmtsnode -- )
- _nextt expectIdent
- swap AST_DECLARE newnode swap ,
- _nextt parseAssign ;
+ parseDeclare _nextt parseAssign ;
: parseArgSpecs ( funcnode -- )
_nextt '(' expectChar AST_ARGSPECS newnode _nextt ( argsnode tok )
dup S" )" s= if 2drop exit then
begin ( argsnode tok )
- expectType drop dup _nextt expectIdent
- swap AST_DECLARE newnode drop ,
+ expectType drop dup parseDeclare drop
_nextt dup S" )" s= if 2drop exit then
',' expectChar _nextt again ;
diff --git a/fs/cc/gen.fs b/fs/cc/gen.fs
@@ -15,6 +15,7 @@ UOPSCNT wordtbl uopgentbl ( -- )
eax eax test,
eax 0 i32 mov,
al setz, ;
+'w noop ( & )
\ In binary Ops, the result is in EAX and the source operand is EBX.
BOPSCNT wordtbl bopgentblmiddle ( node -- node )
diff --git a/fs/tests/cc/test.c b/fs/tests/cc/test.c
@@ -26,3 +26,8 @@ int adder(int a, int b) {
int plusone(int x) {
return adder(x, 1);
}
+int pointers() {
+ int a = 42;
+ int *b = &a;
+ return a;
+}