From 248afa02c8c4d007c0e23cea92a695dc47461650 Mon Sep 17 00:00:00 2001 From: Stefano Sanfilippo Date: Thu, 5 Mar 2015 22:28:45 +0100 Subject: [PATCH] Further simplification of the AST hieararchy and Emitter iface. --- CppEmitter.cpp | 8 ++-- CppEmitter.hpp | 4 +- Emitter.hpp | 4 -- Nodes.hpp | 107 ++++++++++++++++++++----------------------------- 4 files changed, 50 insertions(+), 73 deletions(-) diff --git a/CppEmitter.cpp b/CppEmitter.cpp index 25e01fd..1767f29 100644 --- a/CppEmitter.cpp +++ b/CppEmitter.cpp @@ -159,8 +159,8 @@ void CppEmitter::emit(Loop const& loop) { stream << ")"; } -void CppEmitter::emit(BranchCase const& node) { - node.getCondition().emit(this); +void CppEmitter::emitBranchCase(BranchCase const& node) { + emitBranchCondition(node.getCondition()); stream << ") {\n"; indent(); emitStatements(node.getBody()); @@ -179,7 +179,7 @@ void CppEmitter::emit(Branch const& branch) { if (body.getCases().size() > 0) { BranchCase *last = body.getCases().back(); for (BranchCase *cas: body.getCases()) { - cas->emit(this); + emitBranchCase(*cas); if (cas != last) { stream << " else if ("; var.emit(this); @@ -340,7 +340,7 @@ void CppEmitter::emit(BinaryExpression const& node) { node.getRight().emit(this); } -void CppEmitter::emit(BinarySemiExpression const& node) { +void CppEmitter::emitBranchCondition(SemiExpression const& node) { bool braces = (dynamic_cast(&node.getLeft()) == nullptr); stream << ' ' << node.getOperator() << ' '; diff --git a/CppEmitter.hpp b/CppEmitter.hpp index 1845b4a..162131b 100644 --- a/CppEmitter.hpp +++ b/CppEmitter.hpp @@ -40,7 +40,6 @@ public: virtual void emit(Abort const&) override; virtual void emit(Assert const&) override; virtual void emit(FunctionCall const&) override; - virtual void emit(BranchCase const&) override; virtual void emit(Branch const&) override; virtual void emit(Main const&) override; virtual void emit(Function const&) override; @@ -51,7 +50,6 @@ public: virtual void emit(Integer const&) override; virtual void emit(Float const&) override; virtual void emit(BinaryExpression const&) override; - virtual void emit(BinarySemiExpression const&) override; private: void emitIndent(); @@ -59,6 +57,8 @@ private: void emitFunctionParams(PointerList const& funargs); void emitFunctionArglist(PointerList const& args); void emitStatements(PointerList const& node); + void emitBranchCondition(SemiExpression const& node); + void emitBranchCase(BranchCase const& node); void indent(); void dedent(); diff --git a/Emitter.hpp b/Emitter.hpp index 97c7e66..590b108 100644 --- a/Emitter.hpp +++ b/Emitter.hpp @@ -35,14 +35,12 @@ class Input; class Abort; class Assert; class FunctionCall; -class BranchCase; class Branch; class Main; class Function; class Module; class Program; class BinaryExpression; -class BinarySemiExpression; class Emitter { @@ -56,7 +54,6 @@ public: virtual void emit(Abort const&) = 0; virtual void emit(Assert const&) = 0; virtual void emit(FunctionCall const&) = 0; - virtual void emit(BranchCase const&) = 0; virtual void emit(Branch const&) = 0; virtual void emit(Main const&) = 0; virtual void emit(Function const&) = 0; @@ -67,7 +64,6 @@ public: virtual void emit(Integer const&) = 0; virtual void emit(Float const&) = 0; virtual void emit(BinaryExpression const&) = 0; - virtual void emit(BinarySemiExpression const&) = 0; }; } diff --git a/Nodes.hpp b/Nodes.hpp index 89265e6..328df87 100644 --- a/Nodes.hpp +++ b/Nodes.hpp @@ -41,6 +41,11 @@ enum class Type { VOID }; +enum class Operator { + PLUS, MINUS, TIMES, DIV, + SHL, SHR, + LT, GT, GTE, LTE, EQ +}; class Emittable { public: @@ -54,19 +59,29 @@ public: virtual void emit(Emitter *) const {} }; -class SemiExpression: public Emittable { -public: - virtual void emit(Emitter *) const {} -}; - class Expression: public Emittable { public: virtual void emit(Emitter *) const {} }; class SimpleExpression: public Expression { +}; + +class SemiExpression { public: - virtual void emit(Emitter *) const {} + SemiExpression(Operator op, Expression *l): op(op), left(l) {} + + Expression const& getLeft() const { + return *left; + } + + Operator getOperator() const { + return op; + } + +private: + Operator op; + Pointer left; }; @@ -299,14 +314,10 @@ private: }; -class BranchCase: public Emittable { +class BranchCase { public: BranchCase(SemiExpression *c, PointerList *b): condition(c), body(b) {} - virtual void emit(Emitter *emitter) const { - emitter->emit(*this); - } - SemiExpression const& getCondition() const { return *condition; } @@ -520,13 +531,6 @@ private: }; -enum class Operator { - PLUS, MINUS, TIMES, DIV, - SHL, SHR, - LT, GT, GTE, LTE, EQ -}; - - class BinaryExpression: public Expression { public: BinaryExpression(Expression *l, Operator op, Expression *r): @@ -615,92 +619,69 @@ public: ExpShr(Expression *l, Expression *r): BinaryExpression(l, Operator::SHR, r) {} }; - -class BinarySemiExpression: public SemiExpression { +class SemiExpEq: public SemiExpression { public: - BinarySemiExpression(Operator op, Expression *l): op(op), left(l) {} - - virtual void emit(Emitter *emitter) const { - emitter->emit(*this); - } - - Expression const& getLeft() const { - return *left; - } - - Operator getOperator() const { - return op; - } - -private: - Operator op; - Pointer left; + SemiExpEq(Expression *l): SemiExpression(Operator::EQ, l) {} }; -class SemiExpEq: public BinarySemiExpression { +class SemiExpLt: public SemiExpression { public: - SemiExpEq(Expression *l): BinarySemiExpression(Operator::EQ, l) {} + SemiExpLt(Expression *l): SemiExpression(Operator::LT, l) {} }; -class SemiExpLt: public BinarySemiExpression { +class SemiExpGt: public SemiExpression { public: - SemiExpLt(Expression *l): BinarySemiExpression(Operator::LT, l) {} + SemiExpGt(Expression *l): SemiExpression(Operator::GT, l) {} }; -class SemiExpGt: public BinarySemiExpression { +class SemiExpLte: public SemiExpression { public: - SemiExpGt(Expression *l): BinarySemiExpression(Operator::GT, l) {} + SemiExpLte(Expression *l): SemiExpression(Operator::LTE, l) {} }; -class SemiExpLte: public BinarySemiExpression { +class SemiExpGte: public SemiExpression { public: - SemiExpLte(Expression *l): BinarySemiExpression(Operator::LTE, l) {} + SemiExpGte(Expression *l): SemiExpression(Operator::GTE, l) {} }; -class SemiExpGte: public BinarySemiExpression { +class SemiExpPlus: public SemiExpression { public: - SemiExpGte(Expression *l): BinarySemiExpression(Operator::GTE, l) {} + SemiExpPlus(Expression *l): SemiExpression(Operator::PLUS, l) {} }; -class SemiExpPlus: public BinarySemiExpression { +class SemiExpMinus: public SemiExpression { public: - SemiExpPlus(Expression *l): BinarySemiExpression(Operator::PLUS, l) {} + SemiExpMinus(Expression *l): SemiExpression(Operator::MINUS, l) {} }; -class SemiExpMinus: public BinarySemiExpression { +class SemiExpTimes: public SemiExpression { public: - SemiExpMinus(Expression *l): BinarySemiExpression(Operator::MINUS, l) {} + SemiExpTimes(Expression *l): SemiExpression(Operator::TIMES, l) {} }; -class SemiExpTimes: public BinarySemiExpression { +class SemiExpDiv: public SemiExpression { public: - SemiExpTimes(Expression *l): BinarySemiExpression(Operator::TIMES, l) {} + SemiExpDiv(Expression *l): SemiExpression(Operator::DIV, l) {} }; -class SemiExpDiv: public BinarySemiExpression { +class SemiExpShl: public SemiExpression { public: - SemiExpDiv(Expression *l): BinarySemiExpression(Operator::DIV, l) {} + SemiExpShl(Expression *l): SemiExpression(Operator::SHR, l) {} }; -class SemiExpShl: public BinarySemiExpression { +class SemiExpShr: public SemiExpression { public: - SemiExpShl(Expression *l): BinarySemiExpression(Operator::SHR, l) {} -}; - - -class SemiExpShr: public BinarySemiExpression { -public: - SemiExpShr(Expression *l): BinarySemiExpression(Operator::SHL, l) {} + SemiExpShr(Expression *l): SemiExpression(Operator::SHL, l) {} }; } // namespace