From ff3bceac6288c8df88c8536d4537656c7f34df9b Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Sat, 1 Aug 2020 20:14:51 +0100 Subject: [PATCH] Change id attribute of AExpr to virtual method --- include/serene/expr.hpp | 2 +- include/serene/list.hpp | 6 +++--- include/serene/special_forms/def.hpp | 6 +++--- include/serene/symbol.hpp | 6 +++--- src/list.cpp | 7 +++---- src/special_forms/def.cpp | 10 +++++----- 6 files changed, 18 insertions(+), 19 deletions(-) diff --git a/include/serene/expr.hpp b/include/serene/expr.hpp index 33525a8..4387ee6 100644 --- a/include/serene/expr.hpp +++ b/include/serene/expr.hpp @@ -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(){}; diff --git a/include/serene/list.hpp b/include/serene/list.hpp index 0d8b9a7..f2feb4b 100644 --- a/include/serene/list.hpp +++ b/include/serene/list.hpp @@ -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(); }; diff --git a/include/serene/special_forms/def.hpp b/include/serene/special_forms/def.hpp index 6d47366..952934c 100644 --- a/include/serene/special_forms/def.hpp +++ b/include/serene/special_forms/def.hpp @@ -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(); }; diff --git a/include/serene/symbol.hpp b/include/serene/symbol.hpp index 0d50281..4341877 100644 --- a/include/serene/symbol.hpp +++ b/include/serene/symbol.hpp @@ -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(); }; diff --git a/src/list.cpp b/src/list.cpp index 064166c..e184cf6 100644 --- a/src/list.cpp +++ b/src/list.cpp @@ -187,11 +187,10 @@ Value *List::codegen(Compiler &compiler, State &state) { } auto first_expr{head->data.get()}; - auto sym{dynamic_cast(first_expr)}; - if (sym) { - if (sym->name == "def") { - auto def{make_unique(sym, at(2))}; + if (first_expr->id() == symbol) { + if (first_expr->name == "def") { + auto def{make_unique(first_expr, at(2))}; return def->codegen(compiler, state); } } else { diff --git a/src/special_forms/def.cpp b/src/special_forms/def.cpp index 247519f..f352fe7 100644 --- a/src/special_forms/def.cpp +++ b/src/special_forms/def.cpp @@ -47,13 +47,13 @@ Value *Def::codegen(Compiler &compiler, State &state) { auto symobj{dynamic_cast(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"); };