nicolaswinkler пре 8 година
комит
2aefa1f9bf
4 измењених фајлова са 126 додато и 0 уклоњено
  1. 26 0
      src/Parser.cpp
  2. 59 0
      src/Parser.h
  3. BIN
      src/a.out
  4. 41 0
      src/main.cpp

+ 26 - 0
src/Parser.cpp

@@ -0,0 +1,26 @@
+#include "Parser.h"
+#include <string>
+
+using zp::Parser;
+using namespace std;
+
+Parser::Parser(istream& in) :
+    in{in}
+{
+}
+
+
+void Parser::parse(void)
+{
+    while (!in.eof())
+    {
+        char chr;
+        in >> chr;
+        switch(chr) {
+            case '+':
+                program.instructions.push_back(
+                        std::make_unique<UnaryInstruction>());
+        }
+    }
+}
+

+ 59 - 0
src/Parser.h

@@ -0,0 +1,59 @@
+#ifndef ZP_PARSER_H
+#define ZP_PARSER_H
+
+#include <iostream>
+#include <vector>
+#include <memory>
+
+namespace zp
+{
+    struct Instruction;
+    struct UnaryInstruction;
+    struct Loop;
+
+    class Parser;
+}
+
+
+struct zp::Instruction
+{
+    virtual ~Instruction(void) = default;
+};
+
+
+struct zp::UnaryInstruction :
+    public Instruction
+{
+    enum class Type : char
+    {
+        INC,
+        DEC,
+        LEFT,
+        RIGHT,
+        POINT,
+        COMMA
+    };
+
+    Type type;
+};
+
+
+struct zp::Loop :
+    public Instruction
+{
+    std::vector<std::unique_ptr<Instruction>> instructions;
+};
+
+
+class zp::Parser
+{
+    std::istream& in;
+    Loop program;
+public:
+    Parser(std::istream& in);
+
+    void parse(void);
+};
+
+#endif /* ZP_PARSER_H */
+


+ 41 - 0
src/main.cpp

@@ -0,0 +1,41 @@
+#include <iostream>
+#include <string>
+#include <fstream>
+#include "Parser.h"
+
+using namespace std;
+using zp::Parser;
+
+
+int main(int argc, char** argv)
+{
+    string filename = "";
+    for (int i = 1; i < argc; i++) {
+        string arg = argv[i];
+        if (arg == "") {
+
+        }
+        else if (arg[0] == '-') {
+        }
+        else {
+            filename = arg;
+        }
+    }
+
+    istream* in = &cin;
+
+    if (filename != "") {
+        in = new ifstream{filename.c_str(), fstream::in};
+    }
+
+    Parser parser{*in};
+
+    parser.parse();
+
+    if (filename != "") {
+        dynamic_cast<ifstream*> (in)->close();
+        delete in;
+        in = &cin;
+    }
+}
+