diff --git a/source/debug.h b/source/debug.h index eb3711d..c84f01f 100644 --- a/source/debug.h +++ b/source/debug.h @@ -19,6 +19,8 @@ void debug_printf(const char * const fmt, ...) { static void dump_variable(void * data) { symbol_t * variable = (symbol_t*)data; + if (variable->symbol_type != VARIABLE) { return; } + if (variable->elements == 1) { printf("{ .name = '%s', .value = '%ld' }\n", variable->name, @@ -36,9 +38,32 @@ void dump_variable(void * data) { static void debug_dump_variables(void) { + puts("# Variables:"); tommy_hashtable_foreach(&symbol_table, dump_variable); } +static +void dump_function(void * data) { + symbol_t * function = (symbol_t*)data; + if (function->symbol_type != FUNCTION) { return; } + + printf("{ .name = '%s' }\n", + function->name + ); +} + +static +void debug_dump_functions(void) { + puts("# Functions:"); + tommy_hashtable_foreach(&symbol_table, dump_function); +} + +static +void debug_dump_symbols(void) { + debug_dump_variables(); + debug_dump_functions(); +} + static void debug_token_dump(void) { extern unsigned int * token_array; diff --git a/source/eaxhla.c b/source/eaxhla.c index bd72161..f79c409 100644 --- a/source/eaxhla.c +++ b/source/eaxhla.c @@ -25,6 +25,10 @@ int has_encountered_error = 0; int is_program_found = 0; char * scope = NULL; +void empty_out_scope(void) { + free(scope); + scope = NULL; +} char * yyfilename = ""; @@ -35,14 +39,14 @@ int eaxhla_init(void) { } static -void free_variable(void * data) { +void free_symbol(void * data) { symbol_t * variable = (symbol_t*)data; free(variable->name); free(variable); } int eaxhla_deinit(void) { - tommy_hashtable_foreach(&symbol_table, free_variable); + tommy_hashtable_foreach(&symbol_table, free_symbol); tommy_hashtable_done(&symbol_table); return 0; } @@ -63,6 +67,7 @@ void add_variable(symbol_t variable) { return; } variable._id = symbol_id++; + variable.symbol_type = VARIABLE; // XXX this is cursed symbol_t * heap_variable = malloc(sizeof(variable)); memcpy(heap_variable, &variable, sizeof(variable)); @@ -77,6 +82,7 @@ void add_variable(symbol_t variable) { void add_procedure(symbol_t procedure) { procedure._id = symbol_id++; + procedure.symbol_type = FUNCTION; // XXX this is cursed symbol_t * heap_procedure = malloc(sizeof(procedure)); memcpy(heap_procedure, &procedure, sizeof(procedure)); diff --git a/source/eaxhla.h b/source/eaxhla.h index 52bd7c0..eaaf923 100644 --- a/source/eaxhla.h +++ b/source/eaxhla.h @@ -46,6 +46,7 @@ extern int is_program_found; extern int has_encountered_error; extern char * scope; +extern void empty_out_scope(void); // Used for error reporting extern char * yyfilename; diff --git a/source/eaxhla.l b/source/eaxhla.l index 4f7c7b1..fd19fc6 100644 --- a/source/eaxhla.l +++ b/source/eaxhla.l @@ -199,8 +199,8 @@ xor { return ITXOR; } /* --- Instrunctions end here --- */ <IN_END>{ -program { BEGIN INITIAL; free(scope); scope = NULL; return END_PROGRAM; } -procedure { BEGIN INITIAL; free(scope); scope = NULL; return END_PROCEDURE; } +program { BEGIN INITIAL; return END_PROGRAM; } +procedure { BEGIN INITIAL; return END_PROCEDURE; } repeat { BEGIN INITIAL; return END_REPEAT; } if { BEGIN INITIAL; return END_IF; } machine { BEGIN INITIAL; return END_MACHINE; } diff --git a/source/eaxhla.y b/source/eaxhla.y index e0cc6d3..f31efad 100644 --- a/source/eaxhla.y +++ b/source/eaxhla.y @@ -33,12 +33,6 @@ cpuregister_t regval; } -/* XXX NOTE: while naming, if my name collided with something in assembler.h i just - * added a 'T' prefix. this is obviously horrible and should be fixed, - * but that will require careful discussion with xolatile. - * for the time being its fine - */ - %token MYBEGIN %token PROGRAM END_PROGRAM @@ -99,14 +93,16 @@ %token EXIT BREAK %% -hla: %empty // { issue_warning("empty file, noting to be done."); } +hla: %empty // | library hla | declaration hla // tmp | program hla | function hla ; -program: program_head declaration_section MYBEGIN code END_PROGRAM { scope = NULL; } +program: program_head declaration_section MYBEGIN code END_PROGRAM { + empty_out_scope(); + } ; program_head: program_specifier PROGRAM IDENTIFIER { @@ -115,7 +111,7 @@ program_head: program_specifier PROGRAM IDENTIFIER { YYERROR; } is_program_found = 1; - scope = $3; + scope = $3; // XXX IF WE START USING THE REFERENCE OF $3 THIS WILL DOUBLE FREE }; program_specifier: %empty @@ -128,14 +124,15 @@ system_specifier: UNIX { system_type = UNIX; } // XXX: end procedure thing function: function_head declaration_section MYBEGIN code END_PROCEDURE { - scope = NULL; append_instructions(RETN); + empty_out_scope(); } ; function_head: function_specifier PROCEDURE IDENTIFIER { - scope = $3; + scope = strdup($3); symbol_t procedure; + procedure.name = $3; add_procedure(procedure); } ; diff --git a/source/main.c b/source/main.c index a534736..55a8030 100644 --- a/source/main.c +++ b/source/main.c @@ -44,7 +44,7 @@ signed main(int argc, char * argv[]) { yyparse(); - debug_dump_variables(); + debug_dump_symbols(); if (!has_encountered_error) { compile(); diff --git a/test/simple_procedure.eax b/test/simple_procedure.eax index 9655f2c..03f7c5a 100644 --- a/test/simple_procedure.eax +++ b/test/simple_procedure.eax @@ -1,4 +1,4 @@ -fastcall procedure heyo +fast procedure heyo s8 <> h = "Heyo world!\n" begin nop mov rax 1 @@ -8,7 +8,7 @@ begin nop syscall end procedure -fastcall procedure cyaa +fast procedure cyaa s8 <> c = "Cyaa world!\n" begin nop mov rax 1