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

Debug subtask 1

parent d483c6f8
No related branches found
No related tags found
1 merge request!4Task 3 odevzdání
......@@ -83,7 +83,7 @@ BCConstant create_function ( u8 parameters ) {
}
 
LocalScope * create_function_scope ( u8 parameters ) {
LocalScope * scope = (LocalScope*) malloc ( sizeof (LocalScope*) + sizeof (u32) + sizeof (Str) * parameters );
LocalScope * scope = (LocalScope*) malloc ( sizeof (LocalScope*) + 2 * sizeof (u16) + sizeof (Str) * ( parameters + MAX_SCOPE_VARIABLES ) );
scope -> locals [ 0 ] = STR ( "this" );
scope -> used_locals = 1;
return scope;
......@@ -140,9 +140,11 @@ 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 );
for ( size_t i = 0; i < state -> constants . constant_count; ++i ) {
BCConstant * constant = & state -> constants . constants [ i ];
if ( constant -> kind == CONSTANT_FUNCTION )
string_destroy ( & constant -> function . bc );
}
}
 
void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
......@@ -157,6 +159,7 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
state -> scope -> local_count = 1;
for ( size_t i = 0; i < top -> expression_cnt; ++i )
ast_to_bc ( state, top -> expressions [ i ] );
free ( state -> scope );
return;
}
case AST_NULL: {
......@@ -196,8 +199,13 @@ 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 < block -> expression_cnt; ++i )
state -> top = scope;
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 ] );
if ( i != block -> expression_cnt - 1 )
string_write_byte ( & function -> bc, 0x00 );
}
// delete local scope
state -> scope = state -> scope -> prev;
free ( scope );
......@@ -268,7 +276,7 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
}
case AST_VARIABLE_ACCESS: {
AstVariableAccess * var = (AstVariableAccess*) ast;
if ( ! state -> scope -> prev ) {
if ( state -> scope == state -> top ) {
// GLOBAL
u16 index = get_string_index ( state, var -> name );
BCFunction * function = & state -> constants . constants [ state -> fp ] . function;
......@@ -293,7 +301,7 @@ void ast_to_bc ( BCCompilerState * state, Ast * ast ) {
case AST_VARIABLE_ASSIGNMENT: {
AstVariableAssignment * assign = (AstVariableAssignment*) ast;
ast_to_bc ( state, assign -> value );
if ( ! state -> scope -> prev ) {
if ( state -> scope == state -> top ) {
// GLOBAL
u16 constants = state -> constants . constant_count;
u16 index = get_string_index ( state, assign -> name );
......
......@@ -3,7 +3,7 @@
#include "heap.h"
#include "string.h"
 
#define INIT_STRING_LENGTH 512
#define INIT_STRING_LENGTH 256
#define MAX_SCOPE_VARIABLES 128
 
typedef struct String {
......@@ -65,6 +65,7 @@ typedef struct BCCompilerState {
Constants constants;
Globals globals;
LocalScope * scope;
LocalScope * top;
u16 fp;
u16 ep;
} BCCompilerState;
......
......@@ -50,7 +50,8 @@ int main ( int argc, char **argv ) {
} else if ( len == strlen ( RUN ) && strncmp ( argv [ arg ], RUN, len ) == 0 ) {
bc_interpreter = 1;
bc_compiler = 1;
for ( size_t opt = 0; opt < 2; ++opt ) {
size_t remaining_args = (size_t) argc - arg;
for ( size_t opt = 0; opt < ( remaining_args > 2 ? 2 : remaining_args); ++opt ) {
len = strlen ( argv [ ++arg ] );
if ( len == strlen ( HEAP_SIZE_COMMAND ) && strncmp ( argv [ arg ], HEAP_SIZE_COMMAND, len ) == 0 ) {
heap_size = atoi ( argv [ ++arg ] );
......
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