Finish the basic implementation of the special form

This commit is contained in:
Sameer Rahmani 2020-08-01 21:32:21 +01:00
parent ff3bceac62
commit f008470de3
5 changed files with 24 additions and 4 deletions

View File

@ -49,6 +49,8 @@ public:
Namespace(std::string &n) : name(n){};
llvm::Value *lookup(std::string &name);
void insert_symbol(std::string &name, llvm::Value *v);
void print_scope();
~Namespace();
};

View File

@ -70,6 +70,7 @@ void Compiler::compile(string &input) {
fmt::print("No gen\n");
}
}
state->current_ns->print_scope();
delete r;
COMPILER_LOG("Done!")
return;

View File

@ -189,9 +189,17 @@ Value *List::codegen(Compiler &compiler, State &state) {
auto first_expr{head->data.get()};
if (first_expr->id() == symbol) {
if (first_expr->name == "def") {
auto def{make_unique<special_forms::Def>(first_expr, at(2))};
return def->codegen(compiler, state);
auto sym{static_cast<Symbol *>(first_expr)};
if (sym->name == "def") {
if (at(1)->id() == symbol) {
auto binding{static_cast<Symbol *>(at(1))};
auto def{make_unique<special_forms::Def>(binding, at(2))};
return def->codegen(compiler, state);
} else {
// first argument has to be symbol.
compiler.log_error("First argument of `def` has to be a symbol");
}
}
} else {
// if it's not symbol, it can be a list or keyword

View File

@ -34,6 +34,15 @@ namespace serene {
Value *Namespace::lookup(string &name) { return scope[name]; };
void Namespace::insert_symbol(string &name, Value *v) { scope[name] = v; }
void Namespace::print_scope() {
typedef map<string, Value *>::const_iterator Iter;
fmt::print("Scope of '{}' ns.\n", name);
for (Iter iter = scope.begin(); iter != scope.end(); iter++) {
fmt::print("{}\n", iter->first);
}
};
Namespace::~Namespace() {}
} // namespace serene

View File

@ -68,7 +68,7 @@ bool State::set_current_ns(Namespace *ns) {
};
Value *State::lookup_in_current_scope(string &name) {
if (this->current_ns) {
if (current_ns) {
return current_ns->lookup(name);
}