diff --git a/Nodes.hpp b/Nodes.hpp index 9e6fd57..e002998 100644 --- a/Nodes.hpp +++ b/Nodes.hpp @@ -40,25 +40,30 @@ public: }; -class StatementList: public std::vector, public Emittable { +template +class List: public std::vector { public: - virtual ~StatementList() { - for (Statement *s: *this) { - delete s; + virtual ~List() { + for (T *element: *this) { + delete element; } } +}; + +class StatementList: public List, public Emittable { +public: virtual void emit(std::ostream &stream, int indent = 0); }; template -class ListEmittable: public std::vector, public Emittable { +class ListEmittable: public List, public Emittable { public: virtual void emit(std::ostream &stream, int indent = 0) { if (this->size() > 0) { - T last = this->back(); // TODO wut? - for (T e: *this) { + T *last = this->back(); // TODO wut? + for (T *e: *this) { e->emit(stream); if (e != last) { stream << getSeparator(); @@ -67,12 +72,6 @@ public: } } - virtual ~ListEmittable() { - for (T e: *this) { - delete e; - } - } - protected: virtual std::string getSeparator() const { return ", "; @@ -80,7 +79,7 @@ protected: }; -class ExpressionList: public ListEmittable { +class ExpressionList: public ListEmittable { }; @@ -100,7 +99,7 @@ private: }; -class IdList: public ListEmittable { +class IdList: public ListEmittable { }; @@ -226,6 +225,7 @@ private: class BranchCase: public Emittable { public: BranchCase(SemiExpression *c, StatementList *b): condition(c), body(b) {} + virtual ~BranchCase() {} virtual void emit(std::ostream &stream, int indent = 0); private: @@ -234,7 +234,7 @@ private: }; -typedef std::vector BranchCaseList; +typedef List BranchCaseList; class Branch: public Statement { @@ -276,12 +276,6 @@ private: class Program: public Emittable { public: - virtual ~Program() { - for (Function *f: functions) { - delete f; - } - } - virtual void emit(std::ostream &stream, int indent = 0); void setMain(Main *m) { @@ -294,7 +288,7 @@ public: private: std::unique_ptr
main; - std::vector functions; + List functions; };