/* * Parsodus - A language agnostic parser generator * Copyright © 2016-2017 Thomas Avé, Robin Jadoul, Kobe Wullaert * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation * the rights to use, copy, modify, merge, publish, distribute, sublicense, * and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included * in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE * OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "parser.h" #include "instruction.h" #include namespace bf { Parser::Parser(BfLexer lex) : bfParser>>(), m_lex(lex) { } Parser::Token Parser::lex() { try { BfLexer::Token orig = m_lex.nextToken(); std::deque pm; switch(orig.type) { case BfLexer::PLUS: { pm.push_back(Instruction::PLUS); std::unique_ptr> p = std::make_unique>(pm); //std::cout << "PARSING: found PLUS " << (int)Instruction::PLUS << std::endl; return Token{ bfParser_Symbol::T_PLUS, std::move(p) }; } case BfLexer::MINUS: { pm.push_back(Instruction::MINUS); std::unique_ptr> p = std::make_unique>(pm); //std::cout << "PARSING: found MINUS" << (int)Instruction::MINUS << std::endl; return Token{ bfParser_Symbol::T_MINUS, std::move(p) }; } case BfLexer::GREATER: { pm.push_back(Instruction::GREATER); std::unique_ptr> p = std::make_unique>(pm); //std::cout << "PARSING: found GREATER" << (int)Instruction::GREATER << std::endl; return Token{ bfParser_Symbol::T_GREATER, std::move(p) }; } case BfLexer::LESS: { pm.push_back(Instruction::LESS); std::unique_ptr> p = std::make_unique>(pm); //std::cout << "PARSING: found LESS" << (int)Instruction::LESS << std::endl; return Token{ bfParser_Symbol::T_LESS, std::move(p) }; } case BfLexer::POINT: { pm.push_back(Instruction::POINT); std::unique_ptr> p = std::make_unique>(pm); //std::cout << "PARSING: found POINT" << (int)Instruction::POINT << std::endl; return Token{ bfParser_Symbol::T_POINT, std::move(p) }; } case BfLexer::COMMA: { pm.push_back(Instruction::COMMA); std::unique_ptr> p = std::make_unique>(pm); //std::cout << "PARSING: found COMMA" << (int)Instruction::COMMA << std::endl; return Token{ bfParser_Symbol::T_COMMA, std::move(p) }; } case BfLexer::LBRACKET: { pm.push_back(Instruction::LBRACKET); std::unique_ptr> p = std::make_unique>(pm); //std::cout << "PARSING: found LBRACKET" << (int)Instruction::LBRACKET << std::endl; return Token{ bfParser_Symbol::T_LBRACKET, std::move(p) }; } case BfLexer::RBRACKET: { pm.push_back(Instruction::RBRACKET); std::unique_ptr> p = std::make_unique>(pm); //std::cout << "PARSING: found RBRACKET" << (int)Instruction::RBRACKET << std::endl; return Token{ bfParser_Symbol::T_RBRACKET, std::move(p) }; } } } catch(BfLexer::NoMoreTokens) { return Token{ bfParser_Symbol::T_EOF, nullptr }; } } std::unique_ptr> Parser::reduce_PROGRAM(std::deque subparts) { // ::= if (subparts[1].value == nullptr) return std::move(subparts[0].value); for(auto& instr : *(subparts[1].value)) subparts[0].value->push_back(instr); return std::move(subparts[0].value); } std::unique_ptr> Parser::reduce_EMPTY(std::deque subparts) { // ::= return nullptr; } std::unique_ptr> Parser::reduce_OPERATION(std::deque subparts) { // ::= "PLUS" | "MINUS" | ... | "COMMA" return std::move(subparts[0].value); } std::unique_ptr> Parser::reduce_LOOP(std::deque subparts) { // ::= "LBRACKET" "RBRACKET" subparts[1].value->push_front(Instruction::LBRACKET); subparts[1].value->push_back(Instruction::RBRACKET); //std::cout << "PARSING: found LOOP" << std::endl; //for(auto& s : *(subparts[1].value)) std::cout << "LOOP: " << (int)s << std::endl; return std::move(subparts[1].value); } } // namespace bf