commit 8875a29ca72770d70422b8723737aad5428afdb1
parent dbe8de208e985ad151b10245a3de1c8bf80a4746
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Fri, 4 Nov 2022 10:25:46 -0400
cc: code consolidation
Diffstat:
3 files changed, 18 insertions(+), 20 deletions(-)
diff --git a/fs/cc/ast.fs b/fs/cc/ast.fs
@@ -316,7 +316,7 @@ alias noop parseFactor ( tok -- node ) \ forward declaration
\ can be an expression or a typecast
nextt dup parseType if ( tok type )
\ TODO: actually process the typecast
- nip parseType* nip ')' expectChar nextt parseExpression
+ nip parseDeclarator drop nextt ')' expectChar nextt parseExpression
else ( tok ) parseExpression nextt ')' expectChar then
endof
'"' of isChar?^ ( )
diff --git a/fs/cc/type.fs b/fs/cc/type.fs
@@ -156,12 +156,11 @@ create symbols 0 , 0 c, \ this is a dict link
: cctypes$ 0 localdefs ! 0 symbols ! _arena :reset ;
-: printType* ( type -- ) type*lvl begin ?dup while '*' emit 1- repeat ;
-
: printtype ( type -- )
dup ctype? if ctype' CType :. else
dup typeunsigned? if ." unsigned " then
- dup >> >> 3 and typenames slistiter stype printType* then ;
+ dup >> >> 3 and typenames slistiter stype
+ type*lvl begin ?dup while '*' emit 1- repeat then ;
current to _printtype
: typesize ( type -- size-in-bytes )
@@ -189,9 +188,6 @@ current to _typesize
drop 4
endcase ;
-: parseType* ( type -- type tok )
- begin nextt dup '*' isChar? while drop type*lvl+ repeat ;
-
alias _err parseType ( tok -- type? f ) \ forward declaration
alias _err parseDeclarator ( type -- ctype ) \ forward declaration
diff --git a/fs/tests/cc/type.fs b/fs/tests/cc/type.fs
@@ -3,26 +3,19 @@
testbegin
\ Tests for CC types
+TYPE_VOID typesize 0 #eq
+TYPE_VOID* typesize 4 #eq
+TYPE_CHAR typesize 1 #eq
+TYPE_CHAR* typesize 4 #eq
+
\ with parseType, we systematically "peek" 1 token in stdin, which is why we
\ always end our test input with "STOP".
-: _parse nextt parseType # parseType* S" STOP" #s= ;
+: _parse nextt parseType # nextt S" STOP" #s= ;
current with-stdin< int STOP TYPE_INT #eq
current with-stdin< unsigned int STOP TYPE_UINT #eq
current with-stdin< char STOP TYPE_CHAR #eq
current with-stdin< short STOP TYPE_SHORT #eq
current with-stdin< void STOP TYPE_VOID #eq
-current with-stdin< int* STOP TYPE_INT* #eq
-current with-stdin< unsigned int * STOP TYPE_UINT* #eq
-current with-stdin< unsigned short*** STOP ( type )
-dup type*lvl 3 #eq dup typeunsigned? #
-type*lvl- type*lvl- type*lvl- typesize 2 #eq
-
-TYPE_VOID typesize 0 #eq
-TYPE_VOID* typesize 4 #eq
-TYPE_CHAR typesize 1 #eq
-TYPE_CHAR* typesize 4 #eq
-
-: _parse nextt parseType # nextt S" STOP" #s= ;
current with-stdin<
struct Struct1 { unsigned int foo; short *bar; char baz[2]; } STOP
( type ) dup typesize 10 #eq
@@ -43,6 +36,15 @@ current with-stdin< int *foo STOP
dup CType type TYPE_INT* #eq
CType name S" foo" #s=
+current with-stdin< unsigned int *foo STOP
+dup CType type TYPE_UINT* #eq
+CType name S" foo" #s=
+
+current with-stdin< unsigned short ***foo STOP
+CType type
+dup type*lvl 3 #eq dup typeunsigned? #
+type*lvl- type*lvl- type*lvl- typesize 2 #eq
+
current with-stdin< int *foo[42] STOP
dup CType type TYPE_INT* #eq
dup CType nbelem 42 #eq