Lexical scoping is working, but defun broke because of that.
Need macros next
This commit is contained in:
+3
-2
@@ -109,6 +109,7 @@ void append_to_keyword_argument_list(Keyword_Arguments* args,
|
||||
append_to_Ast_Node_array_list(args->values, default_value);
|
||||
}
|
||||
|
||||
struct Environment;
|
||||
|
||||
struct Function {
|
||||
bool is_macro;
|
||||
@@ -117,10 +118,10 @@ struct Function {
|
||||
Keyword_Arguments* keyword_arguments;
|
||||
// rest_argument will be nullptr if no rest argument is declared
|
||||
char* rest_argument;
|
||||
struct Ast_Node* body; // implicit prog
|
||||
Ast_Node* body; // implicit prog
|
||||
Environment* parent_environment; // we are doing closures now!!
|
||||
};
|
||||
|
||||
struct Environment;
|
||||
|
||||
struct CFunction {
|
||||
std::function<Ast_Node*(Ast_Node*, Environment*)> function;
|
||||
|
||||
+41
-4
@@ -324,7 +324,7 @@ void load_built_ins_into_environment(Environment* env) {
|
||||
|
||||
return value;
|
||||
});
|
||||
defun("macro-define", cLambda {
|
||||
defun("define-upwards", cLambda {
|
||||
try {
|
||||
arguments_length = list_length(arguments);
|
||||
}
|
||||
@@ -345,15 +345,50 @@ void load_built_ins_into_environment(Environment* env) {
|
||||
report_error(Error_Type::Type_Missmatch);
|
||||
}
|
||||
|
||||
if (!env->parent) {
|
||||
report_error(Error_Type::Unknown_Error);
|
||||
}
|
||||
|
||||
|
||||
Ast_Node* value = arguments->value.pair->rest->value.pair->first;
|
||||
try {
|
||||
value = eval_expr(value, env);
|
||||
}
|
||||
|
||||
define_macro_symbol(symbol, value, env);
|
||||
define_symbol(symbol, value, env->parent);
|
||||
|
||||
return value;
|
||||
});
|
||||
// defun("macro-define", cLambda {
|
||||
// try {
|
||||
// arguments_length = list_length(arguments);
|
||||
// }
|
||||
|
||||
// if (arguments_length != 2) {
|
||||
// report_error(Error_Type::Wrong_Number_Of_Arguments);
|
||||
// }
|
||||
|
||||
// Ast_Node* symbol = arguments->value.pair->first;
|
||||
|
||||
// if (symbol->type == Ast_Node_Type::Pair) {
|
||||
// try {
|
||||
// symbol = eval_expr(symbol, env);
|
||||
// }
|
||||
// }
|
||||
|
||||
// if (symbol->type != Ast_Node_Type::Symbol) {
|
||||
// report_error(Error_Type::Type_Missmatch);
|
||||
// }
|
||||
|
||||
// Ast_Node* value = arguments->value.pair->rest->value.pair->first;
|
||||
// try {
|
||||
// value = eval_expr(value, env);
|
||||
// }
|
||||
|
||||
// define_macro_symbol(symbol, value, env);
|
||||
|
||||
// return value;
|
||||
// });
|
||||
defun("mutate", cLambda {
|
||||
try {
|
||||
evaluated_arguments = eval_arguments(arguments, env, &arguments_length);
|
||||
@@ -500,7 +535,7 @@ void load_built_ins_into_environment(Environment* env) {
|
||||
if (arguments_length < 1)
|
||||
report_error(Error_Type::Wrong_Number_Of_Arguments);
|
||||
|
||||
Environment* let_env = create_child_environment(env, Environment_Type::Let);
|
||||
Environment* let_env = create_child_environment(env);
|
||||
Ast_Node* bindings = arguments->value.pair->first;
|
||||
while (true) {
|
||||
if (bindings->type == Ast_Node_Type::Nil) {
|
||||
@@ -564,6 +599,7 @@ void load_built_ins_into_environment(Environment* env) {
|
||||
/* if (lispOperator->value.built_in_function->type == Built_In_Macro) { */
|
||||
/* function->is_macro = true; */
|
||||
/* } else { */
|
||||
function->parent_environment = env;
|
||||
function->is_macro = false;
|
||||
/* } */
|
||||
|
||||
@@ -614,6 +650,7 @@ void load_built_ins_into_environment(Environment* env) {
|
||||
report_error(Error_Type::Wrong_Number_Of_Arguments);
|
||||
|
||||
Function* function = new(Function);
|
||||
function->parent_environment = env;
|
||||
function->is_macro = true;
|
||||
|
||||
// if parameters were specified
|
||||
@@ -962,7 +999,7 @@ void load_built_ins_into_environment(Environment* env) {
|
||||
}
|
||||
|
||||
Environment* create_built_ins_environment() {
|
||||
Environment* ret = create_child_environment(nullptr, Environment_Type::Let);
|
||||
Environment* ret = create_child_environment(nullptr);
|
||||
load_built_ins_into_environment(ret);
|
||||
return ret;
|
||||
}
|
||||
|
||||
+58
-78
@@ -1,12 +1,5 @@
|
||||
enum struct Environment_Type {
|
||||
Let,
|
||||
Lambda,
|
||||
Macro,
|
||||
};
|
||||
|
||||
struct Environment {
|
||||
struct Environment* parent;
|
||||
Environment_Type type;
|
||||
|
||||
int capacity;
|
||||
int next_index;
|
||||
@@ -15,12 +8,11 @@ struct Environment {
|
||||
Ast_Node** values;
|
||||
};
|
||||
|
||||
Environment* create_child_environment(Environment* parent, Environment_Type type) {
|
||||
Environment* create_child_environment(Environment* parent) {
|
||||
Environment* env = new(Environment);
|
||||
|
||||
int start_capacity = 16;
|
||||
|
||||
env->type = type;
|
||||
env->parent = parent;
|
||||
env->capacity = start_capacity;
|
||||
env->next_index = 0;
|
||||
@@ -30,19 +22,19 @@ Environment* create_child_environment(Environment* parent, Environment_Type type
|
||||
return env;
|
||||
}
|
||||
|
||||
Environment* create_empty_environment(Environment_Type type) {
|
||||
return create_child_environment(nullptr, type);
|
||||
Environment* create_empty_environment() {
|
||||
return create_child_environment(nullptr);
|
||||
}
|
||||
|
||||
void define_symbol(Ast_Node* symbol, Ast_Node* value, Environment* env) {
|
||||
if (env->type == Environment_Type::Macro) {
|
||||
// NOTE(Felix): we know we have a parent because every
|
||||
// environment has a parent except the top level environment.
|
||||
// However the top level environment is not a let-environment,
|
||||
// so we would not land here
|
||||
define_symbol(symbol, value, env->parent);
|
||||
return;
|
||||
}
|
||||
// if (env->type == Environment_Type::Macro) {
|
||||
// // NOTE(Felix): we know we have a parent because every
|
||||
// // environment has a parent except the top level environment.
|
||||
// // However the top level environment is not a let-environment,
|
||||
// // so we would not land here
|
||||
// define_symbol(symbol, value, env->parent);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// NOTE(Felix): right now we are simply adding the symol at the
|
||||
// back of the list without checking if it already exists but are
|
||||
@@ -60,16 +52,16 @@ void define_symbol(Ast_Node* symbol, Ast_Node* value, Environment* env) {
|
||||
++env->next_index;
|
||||
}
|
||||
|
||||
void define_macro_symbol(Ast_Node* symbol, Ast_Node* value, Environment* env) {
|
||||
if (env->type != Environment_Type::Macro) {
|
||||
create_error(Error_Type::Unknown_Error, symbol->sourceCodeLocation);
|
||||
return;
|
||||
}
|
||||
// void define_macro_symbol(Ast_Node* symbol, Ast_Node* value, Environment* env) {
|
||||
// if (env->type != Environment_Type::Macro) {
|
||||
// create_error(Error_Type::Unknown_Error, symbol->sourceCodeLocation);
|
||||
// return;
|
||||
// }
|
||||
|
||||
env->type = Environment_Type::Lambda;
|
||||
define_symbol(symbol, value, env);
|
||||
env->type = Environment_Type::Macro;
|
||||
}
|
||||
// env->type = Environment_Type::Lambda;
|
||||
// define_symbol(symbol, value, env);
|
||||
// env->type = Environment_Type::Macro;
|
||||
// }
|
||||
|
||||
void print_environment(Environment* env);
|
||||
|
||||
@@ -80,41 +72,41 @@ Ast_Node* lookup_symbol_in_this_envt(Symbol* sym, Environment* env) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Ast_Node* lookup_symbol_from_lambda_env(Symbol* sym, Environment* env) {
|
||||
Ast_Node* result;
|
||||
do {
|
||||
if (env->type != Environment_Type::Lambda) {
|
||||
result = lookup_symbol_in_this_envt(sym, env);
|
||||
if (result) return result;
|
||||
}
|
||||
env = env->parent;
|
||||
} while (env);
|
||||
return nullptr;
|
||||
}
|
||||
// Ast_Node* lookup_symbol_from_lambda_env(Symbol* sym, Environment* env) {
|
||||
// Ast_Node* result;
|
||||
// do {
|
||||
// if (env->type != Environment_Type::Lambda) {
|
||||
// result = lookup_symbol_in_this_envt(sym, env);
|
||||
// if (result) return result;
|
||||
// }
|
||||
// env = env->parent;
|
||||
// } while (env);
|
||||
// return nullptr;
|
||||
// }
|
||||
|
||||
Ast_Node* lookup_symbol_from_let_or_macro_env(Symbol* sym, Environment* env) {
|
||||
Ast_Node* result;
|
||||
do {
|
||||
result = lookup_symbol_in_this_envt(sym, env);
|
||||
if (result) return result;
|
||||
if (env->type == Environment_Type::Lambda)
|
||||
break;
|
||||
// Ast_Node* lookup_symbol_from_let_or_macro_env(Symbol* sym, Environment* env) {
|
||||
// Ast_Node* result;
|
||||
// do {
|
||||
// result = lookup_symbol_in_this_envt(sym, env);
|
||||
// if (result) return result;
|
||||
// if (env->type == Environment_Type::Lambda)
|
||||
// break;
|
||||
|
||||
env = env->parent;
|
||||
} while (env);
|
||||
// env = env->parent;
|
||||
// } while (env);
|
||||
|
||||
if (env) {
|
||||
do {
|
||||
if (env->type != Environment_Type::Lambda) {
|
||||
result = lookup_symbol_in_this_envt(sym, env);
|
||||
if (result) return result;
|
||||
}
|
||||
env = env->parent;
|
||||
} while (env);
|
||||
}
|
||||
// if (env) {
|
||||
// do {
|
||||
// if (env->type != Environment_Type::Lambda) {
|
||||
// result = lookup_symbol_in_this_envt(sym, env);
|
||||
// if (result) return result;
|
||||
// }
|
||||
// env = env->parent;
|
||||
// } while (env);
|
||||
// }
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
// return nullptr;
|
||||
// }
|
||||
|
||||
Ast_Node* lookup_symbol(Ast_Node* node, Environment* env) {
|
||||
// first check current environment
|
||||
@@ -125,11 +117,7 @@ Ast_Node* lookup_symbol(Ast_Node* node, Environment* env) {
|
||||
return result;
|
||||
|
||||
if (env->parent) {
|
||||
if (env->type == Environment_Type::Lambda) {
|
||||
result = lookup_symbol_from_lambda_env(sym, env->parent);
|
||||
} else {
|
||||
result = lookup_symbol_from_let_or_macro_env(sym, env->parent);
|
||||
}
|
||||
result = lookup_symbol(node, env->parent);
|
||||
|
||||
if (result)
|
||||
return result;
|
||||
@@ -142,14 +130,6 @@ Ast_Node* lookup_symbol(Ast_Node* node, Environment* env) {
|
||||
return create_ast_node_t();
|
||||
}
|
||||
|
||||
// we should not need this anymore when we have c lambdas as built
|
||||
// in functions
|
||||
|
||||
/* result = create_ast_node_built_in_function(sym->identifier); */
|
||||
/* result->sourceCodeLocation = node->sourceCodeLocation; */
|
||||
/* if (result) */
|
||||
/* return result; */
|
||||
|
||||
create_error(Error_Type::Symbol_Not_Defined, node->sourceCodeLocation);
|
||||
/* printf("%s\n", sym->identifier); */
|
||||
return nullptr;
|
||||
@@ -170,12 +150,12 @@ void print_environment_indent(Environment* env, int indent) {
|
||||
if (env->parent) {
|
||||
print_indent(indent);
|
||||
printf("parent");
|
||||
if (env->parent->type == Environment_Type::Lambda)
|
||||
printf(" (lambda)");
|
||||
else if (env->parent->type == Environment_Type::Macro)
|
||||
printf(" (macro)");
|
||||
else if (env->parent->type == Environment_Type::Let)
|
||||
printf(" (let)");
|
||||
// if (env->parent->type == Environment_Type::Lambda)
|
||||
// printf(" (lambda)");
|
||||
// else if (env->parent->type == Environment_Type::Macro)
|
||||
// printf(" (macro)");
|
||||
// else if (env->parent->type == Environment_Type::Let)
|
||||
// printf(" (let)");
|
||||
printf(":\n");
|
||||
print_environment_indent(env->parent, indent+4);
|
||||
}
|
||||
|
||||
+2
-3
@@ -6,7 +6,8 @@ Ast_Node* apply_arguments_to_function(Ast_Node* arguments, Function* function, E
|
||||
// switching between "define_symbol" and "define_macro_symbol" all
|
||||
// the time
|
||||
|
||||
Environment* new_env = create_child_environment(parent, Environment_Type::Lambda);
|
||||
// Environment* new_env = create_child_environment(parent, Environment_Type::Lambda);
|
||||
Environment* new_env = create_child_environment(function->parent_environment);
|
||||
|
||||
// positional arguments
|
||||
for (int i = 0; i < function->positional_arguments->next_index; ++i) {
|
||||
@@ -145,8 +146,6 @@ Ast_Node* apply_arguments_to_function(Ast_Node* arguments, Function* function, E
|
||||
|
||||
// don't have to check every time if it is macro environment or
|
||||
// not
|
||||
if (function->is_macro)
|
||||
new_env->type = Environment_Type::Macro;
|
||||
|
||||
try {
|
||||
result = eval_expr(function->body, new_env);
|
||||
|
||||
+2
-2
@@ -30,7 +30,7 @@ Ast_Node* interprete_file (char* file_name) {
|
||||
program = parse_program(file_name, file_content);
|
||||
}
|
||||
|
||||
Environment* env = create_empty_environment(Environment_Type::Let);
|
||||
Environment* env = create_empty_environment();
|
||||
load_built_ins_into_environment(env);
|
||||
|
||||
try {
|
||||
@@ -50,7 +50,7 @@ Ast_Node* interprete_file (char* file_name) {
|
||||
int interprete_stdin () {
|
||||
printf("Welcome to the lispy interpreter.\n");
|
||||
char* line;
|
||||
Environment* env = create_empty_environment(Environment_Type::Let);
|
||||
Environment* env = create_empty_environment();
|
||||
load_built_ins_into_environment(env);
|
||||
|
||||
built_in_load("pre.slime", env);
|
||||
|
||||
Reference in New Issue
Block a user