start typedefs

This commit is contained in:
pommicket 2022-01-18 16:29:48 -05:00
parent 857d5552fc
commit c4b15b98bb
4 changed files with 92 additions and 12 deletions

51
05/idents.b Normal file
View file

@ -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

View file

@ -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)

View file

@ -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;

View file

@ -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