commit d39087289be5f7ab6db8089a7734e7b08f7fde6e
parent 324a907fe1873d4033a1cbac1d91aa33cde9a5b3
Author: Virgil Dupras <hsoft@hardcoded.net>
Date: Mon, 14 Nov 2022 07:16:17 -0500
cc: avoid infinite recursion in printtype
Printing struct sub-elements by default leads to infinite recursion if a member
of the struct references the struct itself. We avoid that by not printing struct
sub-element in printtype and move that functionality to CType :.struct
Diffstat:
3 files changed, 10 insertions(+), 8 deletions(-)
diff --git a/fs/cc/type.fs b/fs/cc/type.fs
@@ -108,16 +108,16 @@ struct[ CType
: :find ( name self -- ctype-or-0 ) llnext dup if _ then nip ;
: :find# :find dup _assert ;
- : _( case of :struct? ." {" endof
- of :funcsig? ." (" endof
- r@ nexttype if ." , " then endcase ;
- : _) case of :struct? ." }" endof of :funcsig? ." )" endof endcase ;
- : :. ( self -- ) >r
+ : _.children begin nexttype ?dup while dup _printtype ." , " repeat ;
+ : :. ( self -- ) >r \ print without children
r@ offset if '+' emit r@ offset .x? spc> then
r@ :struct? if ." struct" else r@ type _printtype then
r@ name c@ if spc> r@ name stype then
r@ nbelem if '[' emit r@ nbelem . ']' emit then
- r@ _( r@ nexttype ?dup if :. then r@ _) rdrop ;
+ r@ :funcsig? if '(' emit r@ _.children ')' emit then rdrop ;
+ \ Because of possibilities of infinite recursion, structs are not fully
+ \ expanded in vanilla :.
+ : :.struct dup :struct? _assert dup :. ." {" _.children '}' emit ;
: :export ( self -- )
dup :struct? _assert \ we can only export structs
diff --git a/fs/tests/app/all.fs b/fs/tests/app/all.fs
@@ -1 +1,2 @@
f<< /tests/app/cos/all.fs
+f<< /tests/app/uxn/all.fs
diff --git a/fs/tests/cc/type.fs b/fs/tests/cc/type.fs
@@ -8,6 +8,7 @@ TYPE_VOID* typesize 4 #eq
TYPE_CHAR typesize 1 #eq
TYPE_CHAR* typesize 4 #eq
+: printstruct CType :.struct ;
\ with parseType, we systematically "peek" 1 token in stdin, which is why we
\ always end our test input with "STOP".
: _parse nextt parseType # nextt S" STOP" #s= ;
@@ -24,8 +25,8 @@ S" bar" over CType :find
\ once defined, parseType will find the struct in typedefs
current with-stdin< Struct1 STOP over #eq
-capture printtype
-S" struct Struct1 {unsigned int foo, +04 short* bar, +08 char baz[2]}" #s=
+capture printstruct
+S" struct Struct1 {unsigned int foo, +04 short* bar, +08 char baz[2], }" #s=
\ Anonymous structs work too
current with-stdin< struct { int foo; } STOP typesize 4 #eq