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

Add bc structures => string conv.

parent 7dc4bca2
No related branches found
No related tags found
1 merge request!4Task 3 odevzdání
......@@ -35,6 +35,39 @@ void string_write_i32 ( String * str, const i32 data ) {
string_write_byte ( str, (data >> 24) & 255 );
}
 
void string_write_constant ( String * str, BCConstant constant ) {
switch ( constant . kind ) {
case CONSTANT_NULL:
string_write_byte ( str, 0x01 );
break;
case CONSTANT_BOOLEAN:
string_write_byte ( str, 0x04 );
string_write_byte ( str, constant . boolean ? 0x01 : 0x00 );
break;
case CONSTANT_INTEGER:
string_write_byte ( str, 0x00 );
string_write_i32 ( str, constant . integer );
break;
case CONSTANT_STRING:
string_write_byte ( str, 0x02 );
string_write_byte ( str, constant . string . len );
for ( size_t i = 0; i < constant . string . len; ++i )
string_write_byte ( str, constant . string . str [ i ] );
break;
case CONSTANT_FUNCTION:
string_write_byte ( str, 0x03 );
string_write_byte ( str, constant . function . parameters );
string_write_u16 ( str, constant . function . locals );
string_write_i32 ( str, constant . function . bc . len );
for ( size_t i = 0; i < constant . function . bc . len; ++i )
string_write_byte ( str, constant . function . bc . str [ i ] );
default:
assert ( false );
break;
}
}
BCConstant create_function ( u8 parameters ) {
BCFunction function = (BCFunction) { .parameters = parameters, .locals = 0 };
string_init ( & function . bc );
......@@ -203,6 +236,12 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
// GLOBAL
u16 index = get_string_index ( state, def -> name );
BCFunction * function = & state -> constants . constants [ state -> fp ];
u16 * count = & state -> globals . global_count;
for ( size_t i = 0; i < *count; ++i )
if ( state -> globals . names [ i ] == index )
assert ( false );
state -> globals . names [ *count ] = index;
++(*count);
string_write_byte ( & function -> bc, 0x0B );
string_write_u16 ( & function -> bc, index );
} else {
......@@ -220,6 +259,13 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
// GLOBAL
u16 index = get_string_index ( state, var -> name );
BCFunction * function = & state -> constants . constants [ state -> fp ];
u16 * count = & state -> globals . global_count;
size_t i;
for ( i = 0; i < *count; ++i )
if ( state -> globals . names [ i ] == index )
break;
if ( i == *count )
assert ( false );
string_write_byte ( & function -> bc, 0x0C );
string_write_u16 ( & function -> bc, index );
} else {
......@@ -240,6 +286,13 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
u16 index = get_string_index ( state, assign -> name );
assert ( constants == state -> constants . constant_count );
BCFunction * function = & state -> constants . constants [ state -> fp ];
u16 * count = & state -> globals . global_count;
size_t i;
for ( i = 0; i < *count; ++i )
if ( state -> globals . names [ i ] == index )
break;
if ( i == *count )
assert ( false );
string_write_byte ( & function -> bc, 0x0B );
string_write_u16 ( & function -> bc, index );
} else {
......@@ -260,18 +313,30 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
 
String generate_bc ( Ast * ast ) {
// init
String bc;
string_init ( & bc );
BCCompilerState bc_state;
bc_state_init ( & bc_state );
// generate header
// generate internals (constants (exp. functions), globals)
ast_to_bc ( &state, ast );
ast_to_bc ( &bc_state, ast );
 
// header
string_write_byte ( & bc, 'F' );
string_write_byte ( & bc, 'M' );
string_write_byte ( & bc, 'L' );
string_write_byte ( & bc, '\0' );
// constants to bc
for ( size_t i = 0; i < bc_state . constants . constant_count; ++i )
string_write_constant ( & bc, bc_state . constants . constants [ i ] );
// globals to bc
string_write_u16 ( & bc, bc_state . globals . global_count );
for ( size_t i = 0; i < bc_state . globals . global_count; ++i )
string_write_u16 ( & bc, bc_state . globals . names [ i ] );
// EP
string_write_u16 ( & bc, bc_state . ep );
 
// free
bc_state_init_destroy ( & bc_state );
return bc;
}
\ No newline at end of file
......@@ -40,6 +40,7 @@ void string_destroy ( String * str );
void string_write_byte ( String * str, const u8 data );
void string_write_u16 ( String * str, const u16 data );
void string_write_i32 ( String * str, const i32 data );
void string_write_constant ( String * str, BCConstant constant );
 
typedef struct Constants {
BCConstant constants [ 256 * 256 ];
......@@ -49,7 +50,7 @@ typedef struct Constants {
} Constants;
 
typedef struct Globals {
Str names [ 256 * 256 ];
u16 names [ 256 * 256 ];
u16 global_count;
} Globals;
 
......
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