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

Fix internals => bc conversion

parent 638f995e
No related branches found
No related tags found
1 merge request!4Task 3 odevzdání
#include "bc_compiler.h"
#include <assert.h>
 
// DEBUG
#include <stdio.h>
void string_init ( String * str ) {
str -> capacity = INIT_STRING_LENGTH;
str -> len = 0;
......@@ -38,36 +41,41 @@ void string_write_i32 ( String * str, const i32 data ) {
void string_write_constant ( String * str, BCConstant constant ) {
switch ( constant . kind ) {
case CONSTANT_NULL:
// fprintf ( stderr, "null, " );
string_write_byte ( str, 0x01 );
break;
case CONSTANT_BOOLEAN:
// fprintf ( stderr, "bool, " );
string_write_byte ( str, 0x04 );
string_write_byte ( str, constant . boolean ? 0x01 : 0x00 );
break;
case CONSTANT_INTEGER:
// fprintf ( stderr, "int, " );
string_write_byte ( str, 0x00 );
string_write_i32 ( str, constant . integer );
break;
case CONSTANT_STRING:
// fprintf ( stderr, "str, " );
string_write_byte ( str, 0x02 );
string_write_byte ( str, constant . string . len );
string_write_i32 ( 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:
// fprintf ( stderr, "f, " );
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 ] );
break;
default:
assert ( false );
break;
}
}
 
BCConstant create_function ( u8 parameters ) {
BCFunction function = (BCFunction) { .parameters = parameters, .locals = 0 };
string_init ( & function . bc );
......@@ -100,7 +108,7 @@ u16 get_local_index ( BCCompilerState * state, Str name ) {
return function -> parameters + ( scope -> prev ? scope -> prev -> local_count + i: i );
scope = scope -> prev;
}
u16 * n = state -> scope -> local_count;
u16 * n = & state -> scope -> local_count;
assert ( *n != 256 * 256 );
state -> scope -> locals [ *n ] = name;
++(*n);
......@@ -111,9 +119,9 @@ u16 get_local_index ( BCCompilerState * state, Str name ) {
}
 
void gen_bc_constant ( BCCompilerState * state, u16 index ) {
BCFunction * function = & state -> constants . constants [ state -> fp ];
string_write_byte ( function -> bc, 0x01 );
string_write_u16 ( function -> bc, index );
BCFunction * function = & state -> constants . constants [ state -> fp ] . function;
string_write_byte ( & function -> bc, 0x01 );
string_write_u16 ( & function -> bc, index );
}
 
void insert_constant ( BCCompilerState * state, BCConstant constant ) {
......@@ -132,6 +140,9 @@ void bc_state_init ( BCCompilerState * state ) {
 
void bc_state_destroy ( BCCompilerState * state ) {
// TODO ( free strings )
for ( size_t i = 0; i < state -> constants . constant_count; ++i )
if ( state -> constants . constants [ i ] . kind == CONSTANT_STRING )
string_destroy ( & state -> constants . constants [ i ] . function . bc );
}
 
void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
......@@ -160,11 +171,12 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
AstInteger * integer = (AstInteger*) ast;
u16 i;
for ( i = 0; i < state -> constants . constant_count; ++i )
if ( state -> constants . constants [ i ] . kind == CONSTANT_INTEGER && state -> constants . constants [ i ] . integer == integer -> value );
if ( state -> constants . constants [ i ] . kind == CONSTANT_INTEGER && state -> constants . constants [ i ] . integer == integer -> value )
break;
if ( i == state -> constants . constant_count; ++i )
if ( i == state -> constants . constant_count )
insert_constant ( state, (BCConstant) { .kind = CONSTANT_INTEGER, .integer = integer -> value } );
gen_bc_constant ( state, i );
return;
}
case AST_BOOLEAN: {
AstBoolean * boolean = (AstBoolean*) ast;
......@@ -174,6 +186,7 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
insert_constant ( state, (BCConstant) { .kind = CONSTANT_BOOLEAN, .boolean = boolean -> value } );
}
gen_bc_constant ( state, state -> constants . bool_pos [ pos ] );
return;
}
case AST_BLOCK: {
AstBlock * block = (AstBlock*) ast;
......@@ -183,8 +196,8 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
scope -> used_locals = 0;
scope -> prev = state -> scope;
state -> scope = scope;
for ( size_t i = 0; i < top -> expression_cnt; ++i )
ast_to_bc ( state, top -> expressions [ i ] );
for ( size_t i = 0; i < block -> expression_cnt; ++i )
ast_to_bc ( state, block -> expressions [ i ] );
// delete local scope
state -> scope = state -> scope -> prev;
free ( scope );
......@@ -195,12 +208,12 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
LocalScope * old_scope = state -> scope;
u16 old_ep = state -> ep;
state -> ep = state -> fp = state -> constants . constant_count;
insert_constant ( state, create_function ( func -> parameters ) );
state -> scope = create_function_scope ( func -> parameters );
insert_constant ( state, create_function ( func -> parameter_cnt ) );
state -> scope = create_function_scope ( func -> parameter_cnt );
state -> scope -> prev = NULL;
state -> scope -> used_locals = func -> parameters;
state -> scope -> local_count = func -> parameters;
for ( u16 i = 1; i < func -> parameters )
state -> scope -> used_locals = func -> parameter_cnt;
state -> scope -> local_count = func -> parameter_cnt;
for ( u16 i = 1; i < func -> parameter_cnt; ++i )
state -> scope -> locals [ i ] = func -> parameters [ i ];
ast_to_bc ( state, func -> body );
free ( state -> scope );
......@@ -324,19 +337,30 @@ String generate_bc ( Ast * ast ) {
string_write_byte ( & bc, 'F' );
string_write_byte ( & bc, 'M' );
string_write_byte ( & bc, 'L' );
string_write_byte ( & bc, '\0' );
string_write_byte ( & bc, '\n' );
// fprintf ( stderr, "printed FML\n" );
// constants to bc
// fprintf ( stderr, "%d constants:\n", bc_state . constants . constant_count );
string_write_u16 ( & bc, bc_state . constants . constant_count );
for ( size_t i = 0; i < bc_state . constants . constant_count; ++i )
string_write_constant ( & bc, bc_state . constants . constants [ i ] );
// fprintf ( stderr, "\n" );
// globals to bc
// fprintf ( stderr, "%u globals:\n", bc_state . globals . global_count );
string_write_u16 ( & bc, bc_state . globals . global_count );
for ( size_t i = 0; i < bc_state . globals . global_count; ++i )
for ( size_t i = 0; i < bc_state . globals . global_count; ++i ) {
// fprintf ( stderr, "%u, ", bc_state . globals . names [ i ] );
string_write_u16 ( & bc, bc_state . globals . names [ i ] );
}
// fprintf ( stderr, "\n" );
// EP
string_write_u16 ( & bc, bc_state . ep );
// fprintf ( stderr, "EP=%u\n", bc_state . ep );
 
// free
bc_state_init_destroy ( & bc_state );
bc_state_destroy ( & bc_state );
// fprintf ( stderr, "len: %u, %.*s\n", bc . len, (int) bc . len, bc . str );
 
return bc;
}
\ No newline at end of file
......@@ -82,4 +82,4 @@ void bc_state_init ( BCCompilerState * state );
void bc_state_destroy ( BCCompilerState * state );
 
void ast_to_bc ( BCCompilerState * state, Ast * ast );
void generate_bc ( Ast * ast );
\ No newline at end of file
String generate_bc ( Ast * ast );
\ No newline at end of file
......@@ -151,8 +151,12 @@ int main ( int argc, char **argv ) {
 
if ( bc_compiler ) {
if ( ! bc_interpreter )
printf ( "%.*s", (int) bc . len, bc . str );
for ( size_t i = 0; i < bc . len; ++i ) {
printf ( "%c", bc . str [ i ] );
//printf ( "%u ", bc . str [ i ] );
}
//printf ( "%.*s", (int) bc . len, bc . str );
//printf ( "\n" );
string_destroy ( & bc );
}
 
......
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