i tried
This commit is contained in:
parent
0d7ab59c1a
commit
6d4d82ccc2
@ -19,6 +19,8 @@ void debug_printf(const char * const fmt, ...) {
|
|||||||
static
|
static
|
||||||
void dump_variable(void * data) {
|
void dump_variable(void * data) {
|
||||||
symbol_t * variable = (symbol_t*)data;
|
symbol_t * variable = (symbol_t*)data;
|
||||||
|
if (variable->symbol_type != VARIABLE) { return; }
|
||||||
|
|
||||||
if (variable->elements == 1) {
|
if (variable->elements == 1) {
|
||||||
printf("{ .name = '%s', .value = '%ld' }\n",
|
printf("{ .name = '%s', .value = '%ld' }\n",
|
||||||
variable->name,
|
variable->name,
|
||||||
@ -36,9 +38,32 @@ void dump_variable(void * data) {
|
|||||||
|
|
||||||
static
|
static
|
||||||
void debug_dump_variables(void) {
|
void debug_dump_variables(void) {
|
||||||
|
puts("# Variables:");
|
||||||
tommy_hashtable_foreach(&symbol_table, dump_variable);
|
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
|
static
|
||||||
void debug_token_dump(void) {
|
void debug_token_dump(void) {
|
||||||
extern unsigned int * token_array;
|
extern unsigned int * token_array;
|
||||||
|
@ -25,6 +25,10 @@ int has_encountered_error = 0;
|
|||||||
int is_program_found = 0;
|
int is_program_found = 0;
|
||||||
|
|
||||||
char * scope = NULL;
|
char * scope = NULL;
|
||||||
|
void empty_out_scope(void) {
|
||||||
|
free(scope);
|
||||||
|
scope = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
char * yyfilename = "";
|
char * yyfilename = "";
|
||||||
|
|
||||||
@ -35,14 +39,14 @@ int eaxhla_init(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static
|
static
|
||||||
void free_variable(void * data) {
|
void free_symbol(void * data) {
|
||||||
symbol_t * variable = (symbol_t*)data;
|
symbol_t * variable = (symbol_t*)data;
|
||||||
free(variable->name);
|
free(variable->name);
|
||||||
free(variable);
|
free(variable);
|
||||||
}
|
}
|
||||||
|
|
||||||
int eaxhla_deinit(void) {
|
int eaxhla_deinit(void) {
|
||||||
tommy_hashtable_foreach(&symbol_table, free_variable);
|
tommy_hashtable_foreach(&symbol_table, free_symbol);
|
||||||
tommy_hashtable_done(&symbol_table);
|
tommy_hashtable_done(&symbol_table);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -63,6 +67,7 @@ void add_variable(symbol_t variable) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
variable._id = symbol_id++;
|
variable._id = symbol_id++;
|
||||||
|
variable.symbol_type = VARIABLE;
|
||||||
// XXX this is cursed
|
// XXX this is cursed
|
||||||
symbol_t * heap_variable = malloc(sizeof(variable));
|
symbol_t * heap_variable = malloc(sizeof(variable));
|
||||||
memcpy(heap_variable, &variable, sizeof(variable));
|
memcpy(heap_variable, &variable, sizeof(variable));
|
||||||
@ -77,6 +82,7 @@ void add_variable(symbol_t variable) {
|
|||||||
|
|
||||||
void add_procedure(symbol_t procedure) {
|
void add_procedure(symbol_t procedure) {
|
||||||
procedure._id = symbol_id++;
|
procedure._id = symbol_id++;
|
||||||
|
procedure.symbol_type = FUNCTION;
|
||||||
// XXX this is cursed
|
// XXX this is cursed
|
||||||
symbol_t * heap_procedure = malloc(sizeof(procedure));
|
symbol_t * heap_procedure = malloc(sizeof(procedure));
|
||||||
memcpy(heap_procedure, &procedure, sizeof(procedure));
|
memcpy(heap_procedure, &procedure, sizeof(procedure));
|
||||||
|
@ -46,6 +46,7 @@ extern int is_program_found;
|
|||||||
extern int has_encountered_error;
|
extern int has_encountered_error;
|
||||||
|
|
||||||
extern char * scope;
|
extern char * scope;
|
||||||
|
extern void empty_out_scope(void);
|
||||||
|
|
||||||
// Used for error reporting
|
// Used for error reporting
|
||||||
extern char * yyfilename;
|
extern char * yyfilename;
|
||||||
|
@ -199,8 +199,8 @@ xor { return ITXOR; }
|
|||||||
/* --- Instrunctions end here --- */
|
/* --- Instrunctions end here --- */
|
||||||
|
|
||||||
<IN_END>{
|
<IN_END>{
|
||||||
program { BEGIN INITIAL; free(scope); scope = NULL; return END_PROGRAM; }
|
program { BEGIN INITIAL; return END_PROGRAM; }
|
||||||
procedure { BEGIN INITIAL; free(scope); scope = NULL; return END_PROCEDURE; }
|
procedure { BEGIN INITIAL; return END_PROCEDURE; }
|
||||||
repeat { BEGIN INITIAL; return END_REPEAT; }
|
repeat { BEGIN INITIAL; return END_REPEAT; }
|
||||||
if { BEGIN INITIAL; return END_IF; }
|
if { BEGIN INITIAL; return END_IF; }
|
||||||
machine { BEGIN INITIAL; return END_MACHINE; }
|
machine { BEGIN INITIAL; return END_MACHINE; }
|
||||||
|
@ -33,12 +33,6 @@
|
|||||||
cpuregister_t regval;
|
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 MYBEGIN
|
||||||
|
|
||||||
%token PROGRAM END_PROGRAM
|
%token PROGRAM END_PROGRAM
|
||||||
@ -99,14 +93,16 @@
|
|||||||
%token EXIT BREAK
|
%token EXIT BREAK
|
||||||
%%
|
%%
|
||||||
|
|
||||||
hla: %empty // { issue_warning("empty file, noting to be done."); }
|
hla: %empty
|
||||||
// | library hla
|
// | library hla
|
||||||
| declaration hla // tmp
|
| declaration hla // tmp
|
||||||
| program hla
|
| program hla
|
||||||
| function 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 {
|
program_head: program_specifier PROGRAM IDENTIFIER {
|
||||||
@ -115,7 +111,7 @@ program_head: program_specifier PROGRAM IDENTIFIER {
|
|||||||
YYERROR;
|
YYERROR;
|
||||||
}
|
}
|
||||||
is_program_found = 1;
|
is_program_found = 1;
|
||||||
scope = $3;
|
scope = $3; // XXX IF WE START USING THE REFERENCE OF $3 THIS WILL DOUBLE FREE
|
||||||
};
|
};
|
||||||
|
|
||||||
program_specifier: %empty
|
program_specifier: %empty
|
||||||
@ -128,14 +124,15 @@ system_specifier: UNIX { system_type = UNIX; }
|
|||||||
|
|
||||||
// XXX: end procedure thing
|
// XXX: end procedure thing
|
||||||
function: function_head declaration_section MYBEGIN code END_PROCEDURE {
|
function: function_head declaration_section MYBEGIN code END_PROCEDURE {
|
||||||
scope = NULL;
|
|
||||||
append_instructions(RETN);
|
append_instructions(RETN);
|
||||||
|
empty_out_scope();
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
|
||||||
function_head: function_specifier PROCEDURE IDENTIFIER {
|
function_head: function_specifier PROCEDURE IDENTIFIER {
|
||||||
scope = $3;
|
scope = strdup($3);
|
||||||
symbol_t procedure;
|
symbol_t procedure;
|
||||||
|
procedure.name = $3;
|
||||||
add_procedure(procedure);
|
add_procedure(procedure);
|
||||||
}
|
}
|
||||||
;
|
;
|
||||||
|
@ -44,7 +44,7 @@ signed main(int argc, char * argv[]) {
|
|||||||
|
|
||||||
yyparse();
|
yyparse();
|
||||||
|
|
||||||
debug_dump_variables();
|
debug_dump_symbols();
|
||||||
|
|
||||||
if (!has_encountered_error) {
|
if (!has_encountered_error) {
|
||||||
compile();
|
compile();
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
fastcall procedure heyo
|
fast procedure heyo
|
||||||
s8 <> h = "Heyo world!\n"
|
s8 <> h = "Heyo world!\n"
|
||||||
begin
|
begin
|
||||||
nop mov rax 1
|
nop mov rax 1
|
||||||
@ -8,7 +8,7 @@ begin
|
|||||||
nop syscall
|
nop syscall
|
||||||
end procedure
|
end procedure
|
||||||
|
|
||||||
fastcall procedure cyaa
|
fast procedure cyaa
|
||||||
s8 <> c = "Cyaa world!\n"
|
s8 <> c = "Cyaa world!\n"
|
||||||
begin
|
begin
|
||||||
nop mov rax 1
|
nop mov rax 1
|
||||||
|
Loading…
x
Reference in New Issue
Block a user