Change id attribute of AExpr to virtual method

This commit is contained in:
Sameer Rahmani 2020-08-01 20:14:51 +01:00
parent 1238898ddc
commit ff3bceac62
6 changed files with 18 additions and 19 deletions

View File

@ -43,7 +43,7 @@ enum ExprId : unsigned char { aexpr = 0, symbol, list, def };
class AExpr {
public:
ExprId id{0};
virtual ExprId id() const = 0;
virtual std::string string_repr() = 0;
virtual llvm::Value *codegen(Compiler &compiler, State &state) = 0;
virtual ~AExpr(){};

View File

@ -45,7 +45,7 @@ public:
ListNode *head;
ListNode *tail;
std::size_t len;
ExprId id{list};
ExprId id() const override { return list; };
List() : head{nullptr}, tail{nullptr}, len{0} {};
List(const List &list);
@ -54,7 +54,7 @@ public:
List &operator=(const List &other);
List &operator=(List &&other);
std::string string_repr();
std::string string_repr() override;
std::size_t length();
void cons(ast_node f);
@ -64,7 +64,7 @@ public:
void cleanup();
llvm::Value *codegen(Compiler &compiler, State &state);
llvm::Value *codegen(Compiler &compiler, State &state) override;
virtual ~List();
};

View File

@ -48,11 +48,11 @@ private:
AExpr *value;
public:
ExprId id{def};
ExprId id() const override { return def; };
Def(AExpr *s, AExpr *v) : sym(s), value(v){};
std::string string_repr();
llvm::Value *codegen(Compiler &compiler, State &state);
std::string string_repr() override;
llvm::Value *codegen(Compiler &compiler, State &state) override;
~Def();
};

View File

@ -36,11 +36,11 @@ class Symbol : public AExpr {
public:
std::string name;
ExprId id{symbol};
ExprId id() const override { return symbol; };
Symbol(const std::string &name) : name(name){};
std::string string_repr();
llvm::Value *codegen(Compiler &compiler, State &state);
std::string string_repr() override;
llvm::Value *codegen(Compiler &compiler, State &state) override;
~Symbol();
};

View File

@ -187,11 +187,10 @@ Value *List::codegen(Compiler &compiler, State &state) {
}
auto first_expr{head->data.get()};
auto sym{dynamic_cast<Symbol *>(first_expr)};
if (sym) {
if (sym->name == "def") {
auto def{make_unique<special_forms::Def>(sym, at(2))};
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);
}
} else {

View File

@ -47,13 +47,13 @@ Value *Def::codegen(Compiler &compiler, State &state) {
auto symobj{dynamic_cast<Symbol *>(this->sym)};
if (symobj) {
state.set_in_current_ns_root_scope(symobj->name,
value->codegen(compiler, state));
return symobj->codegen(compiler, state);
if (symobj->id() != symbol) {
return compiler.log_error("First argument of 'def' should be a symbol.");
}
return compiler.log_error("First argument of 'def' should be a symbol.");
state.set_in_current_ns_root_scope(symobj->name,
value->codegen(compiler, state));
return symobj->codegen(compiler, state);
};
Def::~Def() { EXPR_LOG("Destroying def"); };