typedefs mostly working

This commit is contained in:
pommicket 2022-01-24 14:06:52 -05:00
parent 46635e5748
commit 5167a60347
3 changed files with 64 additions and 21 deletions

View file

@ -4,4 +4,5 @@
} (*x)(void); } (*x)(void);
*/ */
typedef long int unsigned Foo[sizeof"hello"+sizeof(double[sizeof(int) * sizeof 3])]; typedef long int unsigned Foo[sizeof"hello"+sizeof(double[sizeof(int) * sizeof 3])];
typedef int (*x)(Foo);
/* */ /* */

View file

@ -40,11 +40,27 @@ function parse_tokens
:parse_typedef :parse_typedef
token += 16 token += 16
type = parse_type(&token, &ident) type = parse_type(&token, &ident)
if ident == 0 goto typedef_no_ident
if *1token != SYMBOL_SEMICOLON goto typedef_no_semicolon
puts(ident) puts(ident)
putc(10) putc(':)
putc(32)
print_type(type) print_type(type)
putc(10) putc(10)
exit(0)
ident_list_add(typedefs, ident, type)
token += 16 ; skip semicolon
goto parse_tokens_loop
:typedef_no_ident
token_error(tokens, .str_typedef_no_ident)
:str_typedef_no_ident
string No identifier in typedef declaration.
byte 0
:typedef_no_semicolon
token_error(tokens, .str_typedef_no_semicolon)
:str_typedef_no_semicolon
string typedef does not end with a semicolon.
byte 0
:parse_tokens_eof :parse_tokens_eof
return return
@ -237,24 +253,24 @@ function parse_type_to
suffix_end = token suffix_end = token
#define TYPEDEBUG ; #define TYPEDEBUG ;
TYPEDEBUG putc('B) TYPEDEBUG putc('B)
TYPEDEBUG putc('a) TYPEDEBUG putc('a)
TYPEDEBUG putc('s) TYPEDEBUG putc('s)
TYPEDEBUG putc(':) TYPEDEBUG putc(':)
TYPEDEBUG putc(32) TYPEDEBUG putc(32)
TYPEDEBUG print_tokens(*8p_token, base_type_end) TYPEDEBUG print_tokens(*8p_token, base_type_end)
TYPEDEBUG putc('P) TYPEDEBUG putc('P)
TYPEDEBUG putc('r) TYPEDEBUG putc('r)
TYPEDEBUG putc('e) TYPEDEBUG putc('e)
TYPEDEBUG putc(':) TYPEDEBUG putc(':)
TYPEDEBUG putc(32) TYPEDEBUG putc(32)
TYPEDEBUG print_tokens(prefix, prefix_end) TYPEDEBUG print_tokens(prefix, prefix_end)
TYPEDEBUG putc('S) TYPEDEBUG putc('S)
TYPEDEBUG putc('u) TYPEDEBUG putc('u)
TYPEDEBUG putc('f) TYPEDEBUG putc('f)
TYPEDEBUG putc(':) TYPEDEBUG putc(':)
TYPEDEBUG putc(32) TYPEDEBUG putc(32)
TYPEDEBUG print_tokens(suffix, suffix_end) TYPEDEBUG print_tokens(suffix, suffix_end)
; main loop for parsing types ; main loop for parsing types
:parse_type_loop :parse_type_loop
@ -264,7 +280,7 @@ function parse_type_to
if *1p == SYMBOL_TIMES goto parse_pointer_type if *1p == SYMBOL_TIMES goto parse_pointer_type
if suffix == suffix_end goto parse_base_type if suffix == suffix_end goto parse_base_type
if *1suffix == SYMBOL_RPAREN goto parse_type_remove_parentheses if *1suffix == SYMBOL_RPAREN goto parse_type_remove_parentheses
goto bad_type
:parse_pointer_type :parse_pointer_type
*1out = TYPE_POINTER *1out = TYPE_POINTER
@ -440,7 +456,9 @@ function parse_type_to
:base_type_typedef :base_type_typedef
p = prefix + 8 p = prefix + 8
c = ident_list_lookup(typedefs, *8p) c = ident_list_lookup(typedefs, *8p)
if c == 0 goto bad_type
n = type_length(c) n = type_length(c)
c += types
out = memcpy(out, c, n) out = memcpy(out, c, n)
goto base_type_done goto base_type_done

View file

@ -372,6 +372,13 @@ function puts
fputs(1, s) fputs(1, s)
return return
function putsln
argument s
fputs(1, s)
fputc(1, 10)
return
function print_separator function print_separator
fputs(1, .str_separator) fputs(1, .str_separator)
return return
@ -455,6 +462,18 @@ function putn_signed
fputn_signed(1, n) fputn_signed(1, n)
return return
function putnln
argument n
fputn(1, n)
fputc(1, 10)
return
function putnln_signed
argument n
fputn_signed(1, n)
fputc(1, 10)
return
function fputc function fputc
argument fd argument fd
@ -466,6 +485,11 @@ function putc
argument c argument c
fputc(1, c) fputc(1, c)
return return
function putcln
argument c
fputc(1, c)
fputc(1, 10)
return
; returns 0 at end of file ; returns 0 at end of file
function fgetc function fgetc