Finish the basic implementation of the special form
This commit is contained in:
parent
ff3bceac62
commit
f008470de3
|
@ -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();
|
||||
};
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
14
src/list.cpp
14
src/list.cpp
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue