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 { class AExpr {
public: public:
ExprId id{0}; virtual ExprId id() const = 0;
virtual std::string string_repr() = 0; virtual std::string string_repr() = 0;
virtual llvm::Value *codegen(Compiler &compiler, State &state) = 0; virtual llvm::Value *codegen(Compiler &compiler, State &state) = 0;
virtual ~AExpr(){}; virtual ~AExpr(){};

View File

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

View File

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

View File

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

View File

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

View File

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