#include "Parser.h" #include using namespace zp; using namespace std; Parser::Parser(istream& in) : in{in} { } unique_ptr Parser::parse(void) { return parseBlock(false); } unique_ptr Parser::parseBlock(bool isLoop) { unique_ptr wrapper = nullptr; unique_ptr block = make_unique(); char chr; while (in >> chr) { switch(chr) { case '+': block->addInstruction(Instruction::PLUS); break; case '-': block->addInstruction(Instruction::MINUS); break; case '<': block->addInstruction(Instruction::LEFT); break; case '>': block->addInstruction(Instruction::RIGHT); break; case '.': block->addInstruction(Instruction::DOT); break; case ',': block->addInstruction(Instruction::COMMA); break; case '[': if (wrapper == nullptr) { wrapper = isLoop ? make_unique() : make_unique(); } if (!block->isEmpty()) { wrapper->addBlock(move(block)); block = make_unique(); } wrapper->addBlock(parseBlock(true)); break; case ']': if (isLoop) { if (wrapper == nullptr) return block; else return wrapper; } else throw ParserException("encountered unexpected ']'"); default:; } } if (isLoop) throw ParserException("number of '['s and ']'s not matching"); else if (wrapper == nullptr) return block; else return wrapper; }