Builtin functions now work with new scope.
This commit is contained in:
parent
4a516d8edb
commit
7b99292547
19
src/exec.c
19
src/exec.c
@ -12,15 +12,17 @@
|
|||||||
AST* exec_start(AST* ast) {
|
AST* exec_start(AST* ast) {
|
||||||
log_dbg("Started execution.");
|
log_dbg("Started execution.");
|
||||||
|
|
||||||
Scope* builtin = scope_init(NULL);
|
Scope* global = scope_init(NULL);
|
||||||
|
|
||||||
for (int i = 0; i < BUILTIN_FNS_LN; i++)
|
for (int i = 0; i < BUILTIN_FNS_LN; i++)
|
||||||
htab_ins(
|
htab_ins(
|
||||||
builtin->here, BUILTIN_FNS[i].name,
|
global->here, BUILTIN_FNS[i].name,
|
||||||
ast_init(AST_TYPE_BIF, ast_bif_data_init(BUILTIN_FNS[i].fn))
|
ast_init(AST_TYPE_BIF, ast_bif_data_init(BUILTIN_FNS[i].fn))
|
||||||
);
|
);
|
||||||
|
|
||||||
return exec_exp(ast, builtin);
|
log_dbg("Completed startup sequence.");
|
||||||
|
|
||||||
|
return exec_exp(ast, global);
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* exec_exp(AST* ast, Scope* parent) {
|
AST* exec_exp(AST* ast, Scope* parent) {
|
||||||
@ -38,6 +40,7 @@ AST* exec_exp(AST* ast, Scope* parent) {
|
|||||||
AST* exec_block(AST* ast, Scope* parent) {
|
AST* exec_block(AST* ast, Scope* parent) {
|
||||||
ASTBlockData* block = (ASTBlockData*)ast->data;
|
ASTBlockData* block = (ASTBlockData*)ast->data;
|
||||||
|
|
||||||
|
// Blocks create their own scope, shared among their expressions.
|
||||||
ast->scope = scope_init(parent);
|
ast->scope = scope_init(parent);
|
||||||
|
|
||||||
// Loop through all but last ast.
|
// Loop through all but last ast.
|
||||||
@ -67,7 +70,7 @@ AST* exec_call(AST* ast, Scope* parent) {
|
|||||||
switch (fdef->type) {
|
switch (fdef->type) {
|
||||||
case AST_TYPE_BIF:
|
case AST_TYPE_BIF:
|
||||||
ASTBIFData bifdata = fdef->data;
|
ASTBIFData bifdata = fdef->data;
|
||||||
return bifdata(argc, argv);
|
return bifdata(argc, argv, parent);
|
||||||
case AST_TYPE_FDEF: return exec_cf(fdef, argc, argv);
|
case AST_TYPE_FDEF: return exec_cf(fdef, argc, argv);
|
||||||
default:
|
default:
|
||||||
return ast_init(AST_TYPE_EXC, ast_exc_data_init("Good job!", NULL));
|
return ast_init(AST_TYPE_EXC, ast_exc_data_init("Good job!", NULL));
|
||||||
@ -87,15 +90,17 @@ AST* exec_cf(AST* ast, size_t argc, AST** argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
AST* exec_vdef(AST* ast, Scope* parent) {
|
AST* exec_vdef(AST* ast, Scope* parent) {
|
||||||
ast->scope = scope_init(parent);
|
// Use parent's scope.
|
||||||
|
ast->scope = parent;
|
||||||
ASTVDefData* data = (ASTVDefData*)ast->data;
|
ASTVDefData* data = (ASTVDefData*)ast->data;
|
||||||
AST* val = data->val;
|
AST* val = data->val;
|
||||||
char* key = data->name;
|
char* key = data->name;
|
||||||
scope_add(parent, key, val);
|
scope_add(parent, key, val); // Add variable definition to parent scope.
|
||||||
return exec_exp(val, ast->scope);
|
return exec_exp(val, parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
AST* exec_vref(AST* ast, Scope* parent) {
|
AST* exec_vref(AST* ast, Scope* parent) {
|
||||||
|
// Use parent's scope.
|
||||||
ast->scope = parent;
|
ast->scope = parent;
|
||||||
log_dbg("attempting to reference var");
|
log_dbg("attempting to reference var");
|
||||||
ASTVrefData* vref = (ASTVrefData*)ast->data;
|
ASTVrefData* vref = (ASTVrefData*)ast->data;
|
||||||
|
@ -69,7 +69,7 @@ void ast_exc_data_destroy(ASTExcData* exc);
|
|||||||
void ast_exc_print(ASTExcData*, int i);
|
void ast_exc_print(ASTExcData*, int i);
|
||||||
|
|
||||||
// A built-in function.
|
// A built-in function.
|
||||||
typedef AST* (*ASTBIFData)(size_t argc, AST** argv);
|
typedef AST* (*ASTBIFData)(size_t argc, AST** argv, Scope* scope);
|
||||||
|
|
||||||
// Create a built-in function.
|
// Create a built-in function.
|
||||||
ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**, Scope*));
|
ASTBIFData* ast_bif_data_init(AST* fn(size_t, AST**, Scope*));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user