This commit is contained in:
anon 2024-07-16 19:40:34 +02:00
parent 0d7ab59c1a
commit 6d4d82ccc2
7 changed files with 47 additions and 18 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -44,7 +44,7 @@ signed main(int argc, char * argv[]) {
yyparse();
debug_dump_variables();
debug_dump_symbols();
if (!has_encountered_error) {
compile();

View File

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