commit 779a4db6315a7839bc05897e012b37d30a34b98a
parent daf1246ca14dd25b3fdaacde68b0142b6d8fa87e
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Fri, 30 Sep 2022 07:48:41 -0400
cc: code consolidation
Diffstat:
3 files changed, 35 insertions(+), 39 deletions(-)
diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs
@@ -10,6 +10,16 @@
: _err ( -- ) abort" ast error" ;
: _assert ( f -- ) not if _err then ;
+0 value curunit \ points to current Unit, the beginning of the AST
+
+\ Symbols are anything that an Ident node can refer to: function or variable.
+\ This dictionary below contain global symbols of the current unit
+create symbols 0 , 0 c, \ this is a dict link
+
+: addSymbol ( node name -- ) symbols swap entry , ;
+: findSymbol ( name -- node-or-0 ) symbols find dup if @ then ;
+: ccast$ 0 symbols ! ;
+
\ Unary operators
7 const UOPSCNT
UOPSCNT stringlist UOPTlist "-" "~" "!" "&" "*" "++" "--"
@@ -69,40 +79,21 @@ create bopsprectbl 1 c, 1 c, 0 c, 0 c, 2 c, 2 c, 3 c, 3 c, 3 c, 3 c,
extends Node struct[ ASTNode
]struct
-\ It's important that name field in named nodes have the same offset, so we
-\ can't just have "name" being a pointer to the addr "after other fields". This
-\ is why we have this "storename" scheme.
-extends ASTNode struct[ NamedNode
- sfield name
- : :new ( id -- node ) ASTNode :new 0 ( name ) , ;
- : :storename ( name self -- ) here swap to name s, ;
-]struct
-
-0 value curunit \ points to current Unit, the beginning of the AST
-extends ASTNode struct[ Unit
- : :find ( name -- fnode-or-0 )
- curunit firstchild begin ( name node )
- ?dup while ( name node )
- over over NamedNode name s= if ( name node )
- nip exit then
- nextsibling repeat ( name ) drop 0 ;
-]struct
-
0 const STORAGE_SF \ Stack frame (or struct offset)
1 const STORAGE_PS \ Parameter Stack
2 const STORAGE_MEM \ Fixed address in memory
-extends NamedNode struct[ Declare
+extends ASTNode struct[ Declare
sfield ctype
\ for variables and args, "address" is a frame offset
sfield address
sfield storage
: :new ( ctype -- node )
- AST_DECLARE NamedNode :new ( ctype node )
- swap ( ctype ) , 0 ( address ) , STORAGE_SF ,
- dup ctype CType name over to name ;
+ AST_DECLARE ASTNode :new ( ctype node )
+ swap ( ctype ) , 0 ( address ) , STORAGE_SF , ;
+ : :name ctype CType name ;
: :isarg? ( dnode -- f ) storage STORAGE_PS = ;
: :isglobal? ( dnode -- f ) storage STORAGE_MEM = ;
@@ -137,7 +128,7 @@ extends ASTNode struct[ Declarations
Node firstchild begin ( name node )
?dup while ( name node )
dup Node id AST_DECLARE = if
- over over Declare name s= if ( name node )
+ over over Declare :name s= if ( name node )
nip exit then then
Node nextsibling repeat ( name ) drop 0 ;
@@ -148,13 +139,14 @@ extends ASTNode struct[ FuncSig
]struct
0 value curstatic \ is current definition "static"?
-extends NamedNode struct[ Function
+extends ASTNode struct[ Function
sfield address
sfield flags \ b0=static
+ SZ &+ name
: :new ( name -- node )
- AST_FUNCTION NamedNode :new ( name node )
- 0 ( address ) , curstatic ( flags ) , tuck :storename ;
+ AST_FUNCTION ASTNode :new ( name node )
+ 0 ( address ) , curstatic ( flags ) , swap s, ;
: :sig ( self -- anode ) firstchild dup id AST_FUNCSIG = _assert ;
: :stmts ( self -- snode ) :sig nextsibling dup id AST_STATEMENTS = _assert ;
@@ -172,14 +164,15 @@ extends ASTNode struct[ Constant
\ Result of the last "identfind" call
0 value lastidentfound
-extends NamedNode struct[ Ident
- : :new ( name -- node ) AST_IDENT NamedNode :new tuck :storename ;
+extends ASTNode struct[ Ident
+ SZ &+ name
+
+ : :new ( name -- node ) AST_IDENT ASTNode :new swap s, ;
: :finddecl ( self -- dnode-or-fnode-or-0 )
dup name dup rot AST_FUNCTION swap Node :findparent
( name name fnode ) Function :finddecl ?dup not if ( name )
- Unit :find else nip then dup to lastidentfound ;
-
+ findSymbol else nip then dup to lastidentfound ;
]struct
extends ASTNode struct[ Op
@@ -232,9 +225,10 @@ extends ASTNode struct[ StrLit
: :new AST_STRLIT ASTNode :new ;
]struct
-extends NamedNode struct[ Arrow
- : :new AST_ARROW NamedNode :new ;
- : :parse ( -- node ) :new nextt over :storename ;
+extends ASTNode struct[ Arrow
+ SZ &+ name
+
+ : :new AST_ARROW ASTNode :new swap s, ;
]struct
ASTIDCNT stringlist astidnames
@@ -347,10 +341,10 @@ alias noop parseFactor ( tok -- node ) \ forward declaration
nextt dup ',' isChar? if drop else to nexttputback then
repeat ( tok ) drop parsePostfixOp
endof
- S" ->" of s= ( inode ) Arrow :parse tuck Node :add parsePostfixOp endof
+ S" ->" of s= ( inode ) nextt Arrow :new tuck Node :add parsePostfixOp endof
'.' of isChar?^ ( inode )
UnaryOp :new& tuck Node :add
- Arrow :parse tuck Node :add parsePostfixOp
+ nextt Arrow :new tuck Node :add parsePostfixOp
endof
r@ popid if ( inode opid )
PostfixOp :new ( inode opnode )
@@ -512,11 +506,13 @@ current to parseStatements
\ returntype, name and '(' have already been parsed, parse the rest
: parseFuncDef ( unitnode type name -- )
Function :new ( unode type fnode )
+ dup dup Function name addSymbol
swap parseFuncArgs over Node :add ( unode fnode )
dup rot Node :add ( fnode ) parseStatements ;
: parseGlobalDecl ( unitnode ctype -- )
Declare :new ( unode dnode ) dup rot Node :add ( dnode )
+ dup dup Declare :name addSymbol
STORAGE_MEM over to Declare storage
nextt parseDeclareInit read; ;
diff --git a/fs/cc/cc.fs b/fs/cc/cc.fs
@@ -11,13 +11,13 @@ require sys/xhere.fs
\ Compiles input coming from the stdin alias and writes the
\ result to here. Aborts on error.
: cc1, ( -- )
- cctypes$ xhere$ xhere[
+ cctypes$ ccast$ xhere$ xhere[
parseast _debug if curunit printast nl> then
curunit trnode _debug if curunit printast nl> then
]xhere curunit gennode ;
: :cfunc
- cctypes$ xhere$ xhere[ newparseunit nextt parseUnit ]xhere
+ cctypes$ ccast$ xhere$ xhere[ newparseunit nextt parseUnit ]xhere
curunit Node firstchild dup _assert ( node )
_debug if dup printast nl> then
dup trnode _debug if dup printast nl> then gennode ;
diff --git a/fs/cc/type.fs b/fs/cc/type.fs
@@ -94,7 +94,7 @@ struct[ CType
then rdrop ;
]struct
-\ Typedefs a dictionary entries in the "typedefs" dict, which contains a 4b
+\ Typedefs are dictionary entries in the "typedefs" dict, which contains a 4b
\ value representing the type it aliases.
create typedefs 0 , 0 c, \ this is a dict link