From c4b15b98bbba3a95014c877ed6bc86906c88f368 Mon Sep 17 00:00:00 2001 From: pommicket Date: Tue, 18 Jan 2022 16:29:48 -0500 Subject: start typedefs --- 05/idents.b | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 05/main.b | 12 ++++++------ 05/main.c | 7 +------ 05/parse.b | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 12 deletions(-) create mode 100644 05/idents.b (limited to '05') diff --git a/05/idents.b b/05/idents.b new file mode 100644 index 0000000..1606b95 --- /dev/null +++ b/05/idents.b @@ -0,0 +1,51 @@ +; an "identifier list" is a list of identifiers and 64-bit values associated with them +; the values should be non-zero because 0 is returned for undefined identifiers. + +function ident_list_create + argument nbytes + local list + list = malloc(nbytes) + *1list = 255 + return list + +function ident_list_add + argument list + argument ident + argument value + + list = memchr(list, 255) + list = strcpy(list, ident) + list += 1 + *8list = value ; UNALIGNED + list += 8 + *1list = 255 + + return + +; return the value associated with this identifier, or 0 if none is +function ident_list_lookup + argument list + argument ident + local b + :ilist_lookup_loop + if *1list == 255 goto return_0 + b = str_equals(list, ident) + list = memchr(list, 0) + list += 1 + if b == 0 goto ilist_lookup_loop + return *8list ; UNALIGNED + +function ident_list_print + argument list + :ilist_print_loop + if *1list == 255 goto ilist_print_loop_end + puts(list) + putc(':) + putc(32) + list = memchr(list, 0) + list += 1 + putn(*8list) + list += 8 + goto ilist_print_loop + :ilist_print_loop_end + return diff --git a/05/main.b b/05/main.b index 4632338..c512e6d 100644 --- a/05/main.b +++ b/05/main.b @@ -95,14 +95,16 @@ global powers_of_10 global types global types_bytes_used +; ident list of type IDs +global typedefs #include util.b +#include idents.b #include constants.b #include preprocess.b #include tokenize.b #include parse.b - function main argument argv2 argument argv1 @@ -118,6 +120,8 @@ function main local i fill_in_powers_of_10() + typedefs = ident_list_create(100000) + dat_banned_objmacros = 255 dat_banned_fmacros = 255 @@ -170,11 +174,7 @@ function main print_tokens(tokens, p) ; NOTE: do NOT free pptokens as identifiers still reference them. - ast = malloc(56000000) - p -= 16 - parse_expression(tokens, p, ast) - print_expression(ast) - putc(10) + parse_tokens(tokens) exit(0) diff --git a/05/main.c b/05/main.c index 313487e..23ca650 100644 --- a/05/main.c +++ b/05/main.c @@ -1,6 +1 @@ -/* +*"hello" */ -/* *"hello"+3 */ -/* 3+4+5 */ -/* 3+=4+=5*=6>>=7 */ -/* "hello"+=7 */ -5*(4<<8)-2-"hello"[3/4] +typedef int x; diff --git a/05/parse.b b/05/parse.b index dc61d0c..9ddf364 100644 --- a/05/parse.b +++ b/05/parse.b @@ -1,3 +1,37 @@ +function parse_tokens + argument tokens + local token + local ident + local type + + token = tokens + :parse_tokens_loop + if *1token == TOKEN_EOF goto parse_tokens_eof + if *1token == KEYWORD_TYPEDEF goto parse_typedef + + byte 0xcc ; not implemented + + :parse_typedef + token += 16 + parse_type_and_ident(&token, &ident, &type) + puts(ident) + putc(10) + print_type(type) + putc(10) + exit(0) + :parse_tokens_eof + return + +; parse things like `int x` or `int f(void, int, char *)` +; advances *p_token and sets *p_ident to a pointer to the identifier (or 0 if this is just a type) +; and *p_typeid to the type ID +function parse_type_and_ident + argument p_token + argument p_ident + argument p_typeid + local token + byte 0xcc ; aah + ; how many bytes does it take to encode this type? function type_length argument type -- cgit v1.2.3