nicolaswinkler hace 8 años
padre
commit
a79c9acfaa
Se han modificado 4 ficheros con 35 adiciones y 2 borrados
  1. 20 0
      src/Parser.cpp
  2. 10 0
      src/Parser.h
  3. 2 1
      src/main.cpp
  4. 3 1
      src/makefile

+ 20 - 0
src/Parser.cpp

@@ -1,9 +1,29 @@
 #include "Parser.h"
+#include "Optimizer.h"
 #include <string>
 
 using namespace zp;
 using namespace std;
 
+
+void InstructionBlock::accept(AstVisitor& v)
+{
+    v.visitInstructionBlock(*this);
+}
+
+
+void UnionBlock::accept(AstVisitor& v)
+{
+    v.visitUnionBlock(*this);
+}
+
+
+void Loop::accept(AstVisitor& v)
+{
+    v.visitLoop(*this);
+}
+
+
 Parser::Parser(istream& in) :
     in{in}
 {

+ 10 - 0
src/Parser.h

@@ -15,6 +15,9 @@ namespace zp
     struct UnionBlock;
     struct Loop;
 
+    // forward declaration
+    class AstVisitor;
+
     class Parser;
     class ParserException;
 }
@@ -34,6 +37,8 @@ enum class zp::Instruction : char
 struct zp::Block
 {
     virtual ~Block(void) = default;
+
+    virtual void accept(AstVisitor& v) = 0;
 };
 
 
@@ -44,6 +49,8 @@ struct zp::InstructionBlock :
 
     inline void addInstruction(Instruction i)               { instructions.push_back(i); }
     inline bool isEmpty(void) const                         { return instructions.empty(); }
+
+    void accept(AstVisitor& v);
 };
 
 
@@ -52,12 +59,15 @@ struct zp::UnionBlock :
 {
     std::vector<std::unique_ptr<Block>> instructions;
     inline void addBlock(std::unique_ptr<Block>&& block)    { instructions.push_back(std::move(block)); }
+    
+    void accept(AstVisitor& v);
 };
 
 
 struct zp::Loop :
     public UnionBlock
 {
+    void accept(AstVisitor& v);
 };
 
 

+ 2 - 1
src/main.cpp

@@ -29,9 +29,10 @@ int main(int argc, char** argv)
     }
 
     Parser parser{*in};
+    unique_ptr<zp::Block> ast;
 
     try {
-        unique_ptr<zp::Block> ast = parser.parse();
+        ast = parser.parse();
     }
     catch(zp::ParserException& pe) {
         cerr << "Error: " << pe.what() << endl;

+ 3 - 1
src/makefile

@@ -1,7 +1,7 @@
 CXX         := g++
 CXXFLAGS    := -std=c++14
 
-OBJECTS     := main.o Parser.o
+OBJECTS     := main.o Parser.o Optimizer.o
 EXECUTABLE  := zombie
 
 all: $(EXECUTABLE)
@@ -16,4 +16,6 @@ $(EXECUTABLE): $(OBJECTS)
 install: all
 	mv zombie /usr/bin/zombie
 
+clean:
+	rm $(EXECUTABLE) $(OBJECTS)