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

Finish subtask 1

parent 496c2868
No related branches found
No related tags found
Loading
......@@ -97,13 +97,13 @@ Value env_get ( ASTInterpreterState * state, Str name ) {
}
// fprintf ( stderr, "Variable with name %.*s does not exist.\n", (int) name . len, name . str );
// exit ( 1 );
return (Value) { .kind = VALUE_NULL };
// env = state -> current_env;
// env = env ? env : & state -> global_env;
// entry = (EnvironmentEntry*) malloc ( sizeof (EnvironmentEntry) );
// (*entry) = (EnvironmentEntry) {.name = name, .value = (Value) { .kind = VALUE_NULL }, .next = env -> start };
// env -> start = entry;
// return entry -> value;
// return (Value) { .kind = VALUE_NULL };
env = state -> current_env;
env = env ? env : & state -> global_env;
entry = (EnvironmentEntry*) malloc ( sizeof (EnvironmentEntry) );
(*entry) = (EnvironmentEntry) {.name = name, .value = (Value) { .kind = VALUE_NULL }, .next = env -> start };
env -> start = entry;
return entry -> value;
}
 
void * heap_alloc ( Heap * heap, size_t len, size_t align ) {
......@@ -157,6 +157,29 @@ void state_destroy ( ASTInterpreterState * state ) {
}
}
 
Value function_call ( ASTInterpreterState * state, Value function, bool is_function, Ast ** arguments, size_t argc ) {
Value * values = (Value *) malloc ( argc * sizeof (Value) );
for ( size_t i = 0; i < argc; ++i )
values [ i ] = evaluate ( state, arguments [ i ] );
if ( is_function ) {
if ( function . kind != VALUE_FUNCTION ) {
fprintf ( stderr, "Invalid calee.\n" );
exit ( 4 );
}
Environment * tmp = state -> current_env;
state -> current_env = NULL;
env_push ( state );
env_def ( state, STR ("this"), (Value) { .kind = VALUE_NULL} );
for ( size_t i = 0; i < argc; ++i )
env_def ( state, function . function -> parameters [ i ], values [ i ] );
Value ret = evaluate ( state, function . function -> body );
env_pop ( state );
state -> current_env = tmp;
return ret;
}
return (Value) { .kind = VALUE_NULL };
}
void print_value ( Value value ) {
switch ( value . kind ) {
case VALUE_INTEGER:
......@@ -171,6 +194,9 @@ void print_value ( Value value ) {
case VALUE_NULL:
printf ( "null" );
break;
case VALUE_FUNCTION:
printf ( "function" );
break;
}
}
 
......@@ -282,6 +308,13 @@ Value evaluate ( ASTInterpreterState * state, Ast * ast ) {
}
return (Value) { .kind = VALUE_NULL };
}
case AST_FUNCTION:
return (Value) { .kind = VALUE_FUNCTION, .function = (AstFunction*) ast };
case AST_FUNCTION_CALL: {
AstFunctionCall * callAst = (AstFunctionCall*) ast;
Value function = evaluate ( state, callAst -> function );
return function_call ( state, function, true, callAst -> arguments, callAst -> argument_cnt );
}
}
return (Value) { .kind = VALUE_NULL };
}
......@@ -3,7 +3,8 @@
typedef enum {
VALUE_INTEGER,
VALUE_BOOLEAN,
VALUE_NULL
VALUE_NULL,
VALUE_FUNCTION
} ValueType;
 
typedef struct Heap {
......@@ -17,6 +18,7 @@ typedef struct Value {
union {
i32 integer;
bool boolean;
AstFunction * function;
};
} Value;
 
......@@ -50,6 +52,7 @@ void heap_destroy ( Heap * heap );
void state_init ( ASTInterpreterState * state, Heap * heap );
void state_destroy ( ASTInterpreterState * state );
 
Value function_call ( ASTInterpreterState * state, Value function, bool is_function, Ast ** arguments, size_t argc );
void print_value ( Value value );
void fml_print ( Str format, Value * args, size_t argc );
Value evaluate ( ASTInterpreterState * state, Ast * ast );
\ No newline at end of file
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