Skip to content
Snippets Groups Projects
Commit 8d898e37 authored by Michal Štěpánek's avatar Michal Štěpánek
Browse files

Fix if scopes

parent 23b820b4
No related branches found
No related tags found
1 merge request!4Task 3 odevzdání
......@@ -90,6 +90,20 @@ LocalScope * create_function_scope ( u8 parameters ) {
return scope;
}
 
void add_scope ( BCCompilerState * state ) {
LocalScope * scope = (LocalScope*) malloc ( sizeof (LocalScope*) + 2 * sizeof (u16) + sizeof (Str) * MAX_SCOPE_VARIABLES );
scope -> local_count = state -> scope -> local_count;
scope -> used_locals = 0;
scope -> prev = state -> scope;
state -> scope = scope;
}
void remove_scope ( BCCompilerState * state ) {
LocalScope * scope = state -> scope;
state -> scope = scope -> prev;
free ( scope );
}
void convert_function_to_bc ( BCCompilerState * state, u16 index ) {
BCConstant * this_constant = & state -> constants . constants [ index ];
AstFunction * func = (AstFunction*) this_constant -> ast;
......@@ -238,11 +252,7 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
case AST_BLOCK: {
AstBlock * block = (AstBlock*) ast;
// make local scope
LocalScope * scope = (LocalScope*) malloc ( sizeof (LocalScope*) + sizeof (u32) + sizeof (Str) * MAX_SCOPE_VARIABLES );
scope -> local_count = state -> scope -> local_count;
scope -> used_locals = 0;
scope -> prev = state -> scope;
state -> scope = scope;
add_scope ( state );
BCFunction * function = & state -> constants . constants [ state -> fp ] . function;
for ( size_t i = 0; i < block -> expression_cnt; ++i ) {
ast_to_bc ( state, block -> expressions [ i ] );
......@@ -250,29 +260,11 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
string_write_byte ( & function -> bc, 0x00 );
}
// delete local scope
state -> scope = state -> scope -> prev;
free ( scope );
remove_scope ( state );
return;
}
case AST_FUNCTION: {
insert_constant ( state, (BCConstant) { .kind = CONSTANT_AST_FUNCTION, .ast = ast } );
// AstFunction * func = (AstFunction*) ast;
// LocalScope * old_scope = state -> scope;
// insert_constant ( state, create_function ( func -> parameter_cnt + 1 ) );
// u16 old_fp = state -> fp;
// u16 new_fp = state -> constants . constant_count - 1;
// state -> fp = new_fp;
// state -> scope = create_function_scope ( func -> parameter_cnt + 1 );
// state -> scope -> prev = state -> top;
// state -> scope -> used_locals = func -> parameter_cnt + 1;
// state -> scope -> local_count = func -> parameter_cnt + 1;
// for ( u16 i = 1; i < func -> parameter_cnt + 1; ++i )
// state -> scope -> locals [ i ] = func -> parameters [ i - 1 ];
// ast_to_bc ( state, func -> body );
// free ( state -> scope );
// state -> scope = old_scope;
// state -> fp = old_fp;
// gen_bc_constant ( state, new_fp );
gen_bc_constant ( state, state -> constants . constant_count - 1 );
return;
}
......@@ -406,15 +398,20 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
string_write_byte ( & function -> bc, 0x0D );
string_write_u16 ( & function -> bc, 0 );
u16 alt_pos = function -> bc . len;
if ( cond -> alternative -> kind != AST_NULL )
if ( cond -> alternative -> kind != AST_NULL ) {
add_scope ( state );
ast_to_bc ( state, cond -> alternative );
remove_scope ( state );
}
string_write_byte ( & function -> bc, 0x0E );
string_write_u16 ( & function -> bc, 0 );
u16 cons_pos = function -> bc . len;
u16 alt_len = cons_pos - alt_pos;
function -> bc . str [ alt_pos - 2 ] = alt_len & 255;
function -> bc . str [ alt_pos - 1 ] = (alt_len >> 8) & 255;
add_scope ( state );
ast_to_bc ( state, cond -> consequent );
remove_scope ( state );
u16 cons_len = function -> bc . len - cons_pos;
function -> bc . str [ cons_pos - 2 ] = cons_len & 255;
function -> bc . str [ cons_pos - 1 ] = (cons_len >> 8) & 255;
......
......@@ -74,6 +74,9 @@ typedef struct BCCompilerState {
BCConstant create_function ( u8 parameters );
LocalScope * create_function_scope ( u8 parameters );
 
void add_scope ( BCCompilerState * state );
void remove_scope ( BCCompilerState * state );
void convert_function_to_bc ( BCCompilerState * state, u16 index );
 
u16 get_string_index ( BCCompilerState * state, Str str );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment