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

Little bit of code fixing

parent 4dfe740d
No related branches found
No related tags found
1 merge request!4Task 3 odevzdání
......@@ -84,23 +84,23 @@ LocalScope * create_function_scope ( u8 parameters ) {
u16 get_string_index ( BCCompilerState * state, Str str ) {
u16 i;
for ( i = 0; i < state -> constants . constant_count; ++i )
if ( state -> constants . constants [ i ] . kind == CONSTANT_STRING && str_eq ( state -> constants . constants [ i ] . string, str ) );
if ( state -> constants . constants [ i ] . kind == CONSTANT_STRING && str_eq ( state -> constants . constants [ i ] . string, str ) )
return i;
insert_constant ( state, (BCConstant) { .kind = CONSTANT_STRING, .string = str } );
return i;
}
 
u16 get_local_index ( BCCompilerState * state, Str name ) {
BCFunction * function = & state -> constants . constants [ state -> fp ];
BCFunction * function = & state -> constants . constants [ state -> fp ] . function;
LocalScope * scope = state -> scope;
size_t i;
while ( scope ) {
for ( i = 0; i < scope -> used_locals; ++i )
if ( str_eq ( scope -> locals [ i ], name ) )
return function -> parameters + ( scope -> prev ? scope -> prev -> local_count + i );
return function -> parameters + ( scope -> prev ? scope -> prev -> local_count + i: i );
scope = scope -> prev;
}
u32 * n = state -> scope -> local_count;
u16 * n = state -> scope -> local_count;
assert ( *n != 256 * 256 );
state -> scope -> locals [ *n ] = name;
++(*n);
......@@ -213,7 +213,7 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
ast_to_bc ( state, call -> function );
for ( size_t i = 0; i < call -> argument_cnt; ++i )
ast_to_bc ( state, call -> arguments [ i ] );
BCFunction * function = & state -> constants . constants [ state -> fp ];
BCFunction * function = & state -> constants . constants [ state -> fp ] . function;
string_write_byte ( & function -> bc, 0x08 );
string_write_byte ( & function -> bc, call -> argument_cnt & 255 );
return;
......@@ -223,7 +223,7 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
u16 index = get_string_index ( state, print -> format );
for ( size_t i = 0; i < print -> argument_cnt; ++i )
ast_to_bc ( state, print -> arguments [ i ] );
BCFunction * function = & state -> constants . constants [ state -> fp ];
BCFunction * function = & state -> constants . constants [ state -> fp ] . function;
string_write_byte ( & function -> bc, 0x02 );
string_write_u16 ( & function -> bc, index );
string_write_byte ( & function -> bc, print -> argument_cnt & 255 );
......@@ -235,7 +235,7 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
if ( ! state -> scope -> prev ) {
// GLOBAL
u16 index = get_string_index ( state, def -> name );
BCFunction * function = & state -> constants . constants [ state -> fp ];
BCFunction * function = & state -> constants . constants [ state -> fp ] . function;
u16 * count = & state -> globals . global_count;
for ( size_t i = 0; i < *count; ++i )
if ( state -> globals . names [ i ] == index )
......@@ -247,7 +247,7 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
} else {
// LOCAL
u16 index = get_local_index ( state, def -> name );
BCFunction * function = & state -> constants . constants [ state -> fp ];
BCFunction * function = & state -> constants . constants [ state -> fp ] . function;
string_write_byte ( & function -> bc, 0x09 );
string_write_u16 ( & function -> bc, index );
}
......@@ -258,7 +258,7 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
if ( ! state -> scope -> prev ) {
// GLOBAL
u16 index = get_string_index ( state, var -> name );
BCFunction * function = & state -> constants . constants [ state -> fp ];
BCFunction * function = & state -> constants . constants [ state -> fp ] . function;
u16 * count = & state -> globals . global_count;
size_t i;
for ( i = 0; i < *count; ++i )
......@@ -271,7 +271,7 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
} else {
// LOCAL
u16 index = get_local_index ( state, var -> name );
BCFunction * function = & state -> constants . constants [ state -> fp ];
BCFunction * function = & state -> constants . constants [ state -> fp ] . function;
string_write_byte ( & function -> bc, 0x0A );
string_write_u16 ( & function -> bc, index );
}
......@@ -285,7 +285,7 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
u16 constants = state -> constants . constant_count;
u16 index = get_string_index ( state, assign -> name );
assert ( constants == state -> constants . constant_count );
BCFunction * function = & state -> constants . constants [ state -> fp ];
BCFunction * function = & state -> constants . constants [ state -> fp ] . function;
u16 * count = & state -> globals . global_count;
size_t i;
for ( i = 0; i < *count; ++i )
......@@ -300,7 +300,7 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
u16 locals = state -> scope -> local_count;
u16 index = get_local_index ( state, assign -> name );
assert ( locals == state -> scope -> local_count );
BCFunction * function = & state -> constants . constants [ state -> fp ];
BCFunction * function = & state -> constants . constants [ state -> fp ] . function;
string_write_byte ( & function -> bc, 0x09 );
string_write_u16 ( & function -> bc, index );
}
......
......@@ -69,14 +69,14 @@ typedef struct BCCompilerState {
u16 ep;
} BCCompilerState;
 
BCConstant create_function ( Str name, u8 parameters );
BCConstant create_function ( u8 parameters );
LocalScope * create_function_scope ( u8 parameters );
 
u16 get_string_index ( BCCompilerState * state, Str str );
u16 get_local_index ( BCCompilerState * state, Str name );
 
void gen_bc_constant ( BCCompilerState * state, u16 index );
void insert_constant ( BCCompilerState * state, Constant constant );
void insert_constant ( BCCompilerState * state, BCConstant constant );
 
void bc_state_init ( BCCompilerState * state );
void bc_state_destroy ( BCCompilerState * state );
......
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