nicolaswinkler 8 gadi atpakaļ
vecāks
revīzija
2d4783b8cb
11 mainītis faili ar 107 papildinājumiem un 29 dzēšanām
  1. 6 0
      src/.vscode/settings.json
  2. 56 19
      src/AltParser.h
  3. 22 4
      src/Optimizer.cpp
  4. 9 2
      src/Optimizer.h
  5. BIN
      src/Optimizer.o
  6. 8 3
      src/Parser.cpp
  7. BIN
      src/Parser.o
  8. 5 0
      src/main.cpp
  9. BIN
      src/main.o
  10. 1 1
      src/makefile
  11. BIN
      src/zombie

+ 6 - 0
src/.vscode/settings.json

@@ -0,0 +1,6 @@
+{
+    "files.associations": {
+        "memory": "cpp",
+        "iostream": "cpp"
+    }
+}

+ 56 - 19
src/AltParser.h

@@ -23,6 +23,10 @@ struct BfIterator
     std::istream& source;
     Instruction currentInst;
 
+    BfIterator(const BfIterator& other) = delete;
+    BfIterator(BfIterator&& other) :
+        source(other.source), currentInst(other.currentInst) {}
+
     inline BfIterator& operator++(void)
     {
         while (!source.eof()) {
@@ -41,10 +45,10 @@ struct BfIterator
                     currentInst = Instruction::RIGHT;
                     return *this;
                 case '.':
-                    currentInst = Instruction::PLUS;
+                    currentInst = Instruction::DOT;
                     return *this;
                 case ',':
-                    currentInst = Instruction::PLUS;
+                    currentInst = Instruction::COMMA;
                     return *this;
                 case '[':
                     currentInst = Instruction::LEFT_PARAN;
@@ -69,36 +73,69 @@ struct BfIterator
 };
 
 
-typename<template Dispatcher>
-struct LoopIterator
+class BlockIterator
 {
-    BfIterator it;
-    Dispatcher& d;
+    BfIterator& it;
+    // iterate, until special character
 
-    LoopIterator& operator++(void)
+    bool isSpecialCharacter(void)
     {
-        d(Block());
+        return *it == Instruction::LEFT_PARAN ||
+               *it == Instruction::LEFT_PARAN ||
+               *it == Instruction::COMMA ||
+               *it == Instruction::DOT;
+    }
+public:
+    BlockIterator& operator++(void)
+    {
+        if (!isSpecialCharacter())
+            it++;
+        return *this;
     }
-
-    // until ']'
-    //
-    // calls d.loop() and d.block()                                 
 };
 
 
-struct BlockIterator
+class BlockParser
 {
-    BfIterator it;
-    // iterate, until special character
+    BfIterator& begin;
+public:
+    inline BlockIterator begin(void) { return begin; }
+    inline BlockIterator& end(void) { return ; }
 };
 
 
-class BlockParser
+template<typename Visitor, typename Value>
+struct LoopIterator
 {
-public:
-    BfIterator 
-}
+    BfIterator& it;
+    Visitor& visitor;
+    Value value;
+
+    LoopIterator(const LoopIterator&) = delete;
+
+    LoopIterator& operator++(void)
+    {
+        if (*it == Instruction::RIGHT_PARAN)
+            return *this;
+        if (*it == Instruction::LEFT_PARAN) {
+            value = visitor(LoopParser(it))
+        }
+        else {
+            value = visitor(BlockParser(it));
+        }
+        return *this;
+    }
+
+
+    Value& operator * (void)
+    {
+        return value;
+    }
 
+    // until ']'
+    //
+    // calls d.loop() and d.block()                                 
+};
 
 
 #endif /* ALTPARSER_H */

+ 22 - 4
src/Optimizer.cpp

@@ -1,29 +1,47 @@
 #include "Optimizer.h"
 #include "Parser.h"
+#include <iostream>
 
 using namespace zp;
 
 std::unique_ptr<BlockInstruction> AstVisitor::visit(Block& block)
 {
-        block.accept(*this);
+    return block.accept(*this);
 }
 
 
 std::unique_ptr<BlockInstruction> Optimizer::visitInstructionBlock(InstructionBlock& ib)
 {
-
+    std::cout << "InstructionBlock" << std::endl;
+    bool isSimple = true;
+    for (auto instr : ib.instructions) {
+        switch(instr) {
+            case Instruction::COMMA:
+            case Instruction::DOT:
+            
+        }
+    }
+    return std::make_unique<SimpleBlockInstruction>();
 }
 
 
 std::unique_ptr<BlockInstruction> Optimizer::visitUnionBlock(UnionBlock& ub)
 {
-
+    std::cout << "UnionBlock" << std::endl;
+    for (auto& block : ub.instructions) {
+        visit(*block.get());
+    }
+    return std::make_unique<SimpleBlockInstruction>();
 }
 
 
 std::unique_ptr<BlockInstruction> Optimizer::visitLoop(Loop& loop)
 {
-
+    std::cout << "Loop" << std::endl;
+    for (auto& block : loop.instructions) {
+        visit(*block.get());
+    }
+    return std::make_unique<SimpleBlockInstruction>();
 }
 
 

+ 9 - 2
src/Optimizer.h

@@ -18,6 +18,7 @@ namespace zp
     class AstVisitor;
 
     struct BlockInstruction;
+    struct UnionBlockInstruction;
     struct SimpleBlockInstruction;
     struct LinearLoopInstruction;
     struct LoopInstruction;
@@ -49,6 +50,13 @@ struct zp::BlockInstruction
 };
 
 
+struct zp::UnionBlockInstruction :
+    public BlockInstruction
+{
+    std::vector<std::unique_ptr<BlockInstruction>> content;
+};
+
+
 struct zp::SimpleBlockInstruction :
     public BlockInstruction
 {
@@ -65,9 +73,8 @@ struct zp::LinearLoopInstruction :
 
 
 struct zp::LoopInstruction :
-    public BlockInstruction
+    public UnionBlockInstruction
 {
-    std::vector<std::unique_ptr<BlockInstruction>> content;
 };
 
 

BIN
src/Optimizer.o


+ 8 - 3
src/Parser.cpp

@@ -75,9 +75,10 @@ unique_ptr<Block> Parser::parseBlock(bool isLoop)
             case ']':
                 if (isLoop) {
                     if (wrapper == nullptr)
-                        return block;
-                    else
-                        return wrapper;
+                        wrapper = make_unique<Loop>();
+                    if (!block->isEmpty())
+                        wrapper->addBlock(move(block));
+                    return wrapper;
                 }
                 else
                     throw ParserException("encountered unexpected ']'");
@@ -89,5 +90,9 @@ unique_ptr<Block> Parser::parseBlock(bool isLoop)
     else if (wrapper == nullptr)
         return block;
     else
+    {
+        if (!block->isEmpty())
+            wrapper->addBlock(move(block));
         return wrapper;
+    }
 }

BIN
src/Parser.o


+ 5 - 0
src/main.cpp

@@ -2,9 +2,11 @@
 #include <string>
 #include <fstream>
 #include "Parser.h"
+#include "Optimizer.h"
 
 using namespace std;
 using zp::Parser;
+using zp::Optimizer;
 
 
 int main(int argc, char** argv)
@@ -39,6 +41,9 @@ int main(int argc, char** argv)
         return 1;
     }
 
+    Optimizer optimizer;
+    ast->accept(optimizer);
+    
     if (filename != "") {
         dynamic_cast<ifstream*> (in)->close();
         delete in;

BIN
src/main.o


+ 1 - 1
src/makefile

@@ -1,5 +1,5 @@
 CXX         := g++
-CXXFLAGS    := -std=c++14
+CXXFLAGS    := -std=c++14 -Wall
 
 OBJECTS     := main.o Parser.o Optimizer.o
 EXECUTABLE  := zombie

BIN
src/zombie