|
-
- struct Environment {
- struct Environment* parent;
- int key_count;
- char** keys;
- Ast_Node* values;
- };
-
- typedef struct Environment Environment;
-
- Environment* create_empty_environment() {
- Environment* env = new(Environment);
-
- env->parent = nullptr;
- env->key_count = 0;
- env->keys = nullptr;
- env->values = nullptr;
-
- return env;
- }
-
- Ast_Node* lookup_symbol(Symbol* sym, Environment* env) {
- for (int i = 0; i < env->key_count; ++i)
- if (string_equal(env->keys[i], sym->identifier))
- return env->values+i;
-
- if (env->parent)
- return lookup_symbol(sym, env->parent);
-
- char* built_in_names[] = {
- // Math stuff
- "+", "-", "*", "/",
- ">", "<", "=",
- // Conditional stuff
- "if", "and", "or", "not",
- // Cons stuff
- "first", "rest", "pair",
- // rest
- "load", "define",
- "lambda", "progn",
- "eval", "quote",
- "print", "read",
- "help"
- };
- int built_in_count = 23;
-
- for (int i = 0; i < built_in_count; ++i) {
- if (string_equal(built_in_names[i], sym->identifier)) {
- Ast_Node* ret = new(Ast_Node);
- ret->type = Ast_Node_Type_Built_In_Function;
- ret->value.built_in_function = new(Built_In_Function);
- ret->value.built_in_function->identifier = built_in_names[i];
- return ret;
- }
- }
-
- char* message;
- asprintf(&message, "Symbol not defined: %s\n", sym->identifier);
- panic(message);
-
- return nullptr;
- }
|