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

Finish AST => BC structures conv. for subtask1

parent 4aa553fe
No related branches found
No related tags found
1 merge request!4Task 3 odevzdání
......@@ -57,6 +57,26 @@ u16 get_string_index ( BCCompilerState * state, Str str ) {
return i;
}
 
u16 get_local_index ( BCCompilerState * state, Str name ) {
BCFunction * function = & state -> constants . constants [ state -> fp ];
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 );
scope = scope -> prev;
}
u32 * n = state -> scope -> local_count;
assert ( *n != 256 * 256 );
state -> scope -> locals [ *n ] = name;
++(*n);
++(state -> scope -> used_locals);
if ( *n > function -> locals )
function -> locals = *n;
return *n - 1 + function -> parameters;
}
void gen_bc_constant ( BCCompilerState * state, u16 index ) {
BCFunction * function = & state -> constants . constants [ state -> fp ];
string_write_byte ( function -> bc, 0x01 );
......@@ -176,6 +196,63 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
string_write_byte ( & function -> bc, print -> argument_cnt & 255 );
return;
}
case AST_DEFINITION: {
AstDefinition * def = (AstDefinition*) ast;
ast_to_bc ( state, def -> value );
if ( ! state -> scope -> prev ) {
// GLOBAL
u16 index = get_string_index ( state, def -> name );
BCFunction * function = & state -> constants . constants [ state -> fp ];
string_write_byte ( & function -> bc, 0x0B );
string_write_u16 ( & function -> bc, index );
} else {
// LOCAL
u16 index = get_local_index ( state, def -> name );
BCFunction * function = & state -> constants . constants [ state -> fp ];
string_write_byte ( & function -> bc, 0x09 );
string_write_u16 ( & function -> bc, index );
}
return;
}
case AST_VARIABLE_ACCESS: {
AstVariableAccess * var = (AstVariableAccess*) ast;
if ( ! state -> scope -> prev ) {
// GLOBAL
u16 index = get_string_index ( state, var -> name );
BCFunction * function = & state -> constants . constants [ state -> fp ];
string_write_byte ( & function -> bc, 0x0C );
string_write_u16 ( & function -> bc, index );
} else {
// LOCAL
u16 index = get_local_index ( state, var -> name );
BCFunction * function = & state -> constants . constants [ state -> fp ];
string_write_byte ( & function -> bc, 0x0A );
string_write_u16 ( & function -> bc, index );
}
return;
}
case AST_VARIABLE_ASSIGNMENT: {
AstVariableAssignment * assign = (AstVariableAssignment*) ast;
ast_to_bc ( state, assign -> value );
if ( ! state -> scope -> prev ) {
// GLOBAL
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 ];
string_write_byte ( & function -> bc, 0x0B );
string_write_u16 ( & function -> bc, index );
} else {
// LOCAL
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 ];
string_write_byte ( & function -> bc, 0x09 );
string_write_u16 ( & function -> bc, index );
}
return;
}
default:
assert ( false );
}
......
......@@ -55,8 +55,8 @@ typedef struct Globals {
 
typedef struct LocalScope {
struct LocalScope * prev;
u16 used_locals;
u32 local_count;
u16 used_locals; // locals at use in this scope
u16 local_count; // locals at use at this time
Str locals [];
} LocalScope;
 
......@@ -72,6 +72,7 @@ BCConstant create_function ( Str name, 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 );
......
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