duskos

dusk os fork
git clone git://git.alexwennerberg.com/duskos
Log | Files | Refs | README | LICENSE

commit 6ed422509bc46b0f7781b650ca46440fbb05b5c7
parent f00717bee6e64408dfab512e75b90b2cccd49291
Author: Virgil Dupras <hsoft@hardcoded.net>
Date:   Thu,  8 Dec 2022 16:29:22 -0500

comp/c: allow multi-declarator statements in struct typedefs

Diffstat:
Mfs/asm/uxntal.c | 10+++-------
Mfs/comp/c/pgen.fs | 30++++++++++++++++--------------
Mfs/emul/uxn/vm.c | 7++-----
Mfs/tests/comp/c/test.c | 2+-
4 files changed, 22 insertions(+), 27 deletions(-)

diff --git a/fs/asm/uxntal.c b/fs/asm/uxntal.c @@ -19,7 +19,6 @@ struct MacroLine { struct Macro { Macro *next; - // TODO: allow multiple decls per line char name[LINESZ]; MacroLine *items; }; @@ -27,21 +26,18 @@ struct Macro { struct Label { Label *next; char name[LINESZ]; - Uint16 addr; - Uint16 refs; + Uint16 addr, refs; }; struct Reference { Reference *next; - char name[LINESZ]; - char rune; + char name[LINESZ], rune; Uint16 addr; }; struct Program { Uint8 data[LENGTH]; - unsigned int ptr; - unsigned int length; + unsigned int ptr, length; Label *labels; Macro *macros; Reference *refs; diff --git a/fs/comp/c/pgen.fs b/fs/comp/c/pgen.fs @@ -170,6 +170,20 @@ alias noop parseExpression ( tok -- ) \ forward declaration over to CType type ( ctype ) then ; current to parseDeclarator +: _parseStruct ( -- ctype ) + nextt dup isIdent? if nextt else NULLSTR swap then + '{' expectChar ( name ) TYPE_VOID CType :new ( res ) + dup CType :struct! dup addTypedef + 0 >r dup begin ( res prev ) \ V1=offset + '}' readChar? not while ( res prev tok ) + parseType _assert parseDeclarator begin ( res prev new ) + tuck swap to CType nexttype ( res new ) + V1 over to CType offset + dup typesize to+ V1 + ';' readChar? not while ( res prev tok ) + ',' expectChar dup CType type parseDeclarator repeat ( res prev ) + repeat ( res prev ) rdrop drop ; + \ parse a type from stream, starting with "tok". This only parses the "type" \ part without the "*" part or the name part. The result can be a "base" type \ (type < $100) or a CType if the type is a struct, union or enum. @@ -177,23 +191,11 @@ current to parseDeclarator dup S" typedef" s= if drop nextt parseType _assert parseDeclarator ( ctype ) dup addTypedef 1 exit then - dup S" struct" s= if - drop nextt dup isIdent? if nextt else NULLSTR swap then - '{' expectChar ( name ) TYPE_VOID CType :new ( res ) - dup CType :struct! dup addTypedef - 0 >r dup begin ( res prev ) \ V1=offset - '}' readChar? not while ( res prev tok ) - parseType _assert parseDeclarator ( res prev new ) - tuck swap to CType nexttype ( res new ) - V1 over to CType offset - dup typesize to+ V1 read; - repeat ( res prev ) rdrop drop 1 - else + dup S" struct" s= if drop _parseStruct 1 else dup S" unsigned" s= if drop $10 nextt else $00 swap then ( type tok ) dup typenames sfind dup 0>= if ( type tok idx ) nip << << or 1 - else drop nip findTypedef ( type-or-0 ) ?dup bool then - then ; + else drop nip findTypedef ( type-or-0 ) ?dup bool then then ; current to parseType alias noop parseFactor ( tok -- ) \ forward declaration diff --git a/fs/emul/uxn/vm.c b/fs/emul/uxn/vm.c @@ -7,14 +7,11 @@ */ struct Stack { - // TODO: allow single line decl - unsigned char ptr; - unsigned char dat[$ff]; + unsigned char ptr, dat[$ff]; }; struct Device { - unsigned char port; - unsigned char dat[$10]; + unsigned char port, dat[$10]; unsigned char (*dei)(Device*, unsigned char); void (*deo)(Device*, unsigned char); }; diff --git a/fs/tests/comp/c/test.c b/fs/tests/comp/c/test.c @@ -256,7 +256,7 @@ struct MyStruct { int foo; short bar; MyType baz[2]; - int array[2]; + int array[2], another; }; MyType structget(MyStruct *s) {