/* * 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(); auto pm = std::make_unique>>(); switch(orig.type) { case BfLexer::PLUS: { pm->push_back(std::make_unique()); //std::cout << "PARSING: found PLUS " << (int)std::unique_ptr::PLUS << std::endl; return Token{ bfParser_Symbol::T_PLUS, std::move(pm) }; } case BfLexer::MINUS: { pm->push_back(std::make_unique()); //std::cout << "PARSING: found MINUS" << (int)std::unique_ptr::MINUS << std::endl; return Token{ bfParser_Symbol::T_MINUS, std::move(pm) }; } case BfLexer::GREATER: { pm->push_back(std::make_unique()); //std::cout << "PARSING: found GREATER" << (int)std::unique_ptr::GREATER << std::endl; return Token{ bfParser_Symbol::T_GREATER, std::move(pm) }; } case BfLexer::LESS: { pm->push_back(std::make_unique()); //std::cout << "PARSING: found LESS" << (int)std::unique_ptr::LESS << std::endl; return Token{ bfParser_Symbol::T_LESS, std::move(pm) }; } case BfLexer::POINT: { pm->push_back(std::make_unique()); //std::cout << "PARSING: found POINT" << (int)std::unique_ptr::POINT << std::endl; return Token{ bfParser_Symbol::T_POINT, std::move(pm) }; } case BfLexer::COMMA: { pm->push_back(std::make_unique()); //std::cout << "PARSING: found COMMA" << (int)std::unique_ptr::COMMA << std::endl; return Token{ bfParser_Symbol::T_COMMA, std::move(pm) }; } case BfLexer::LBRACKET: { //std::cout << "PARSING: found LBRACKET" << (int)std::unique_ptr::LBRACKET << std::endl; return Token{ bfParser_Symbol::T_LBRACKET, nullptr }; } case BfLexer::RBRACKET: { //std::cout << "PARSING: found RBRACKET" << (int)std::unique_ptr::RBRACKET << std::endl; return Token{ bfParser_Symbol::T_RBRACKET, nullptr }; } default: return Token { bfParser_Symbol::T_EOF, nullptr }; } } catch(BfLexer::NoMoreTokens) { return Token{ bfParser_Symbol::T_EOF, nullptr }; } } std::unique_ptr>> Parser::reduce_PROGRAM(std::deque subparts) { // ::= for(auto& instr : *(subparts[1].value)) subparts[0].value->push_back(std::move(instr)); return std::move(subparts[0].value); } std::unique_ptr>> Parser::reduce_EMPTY(std::deque) { // ::= return std::make_unique>>(); } 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" auto res = std::make_unique>>(); res->push_back(std::make_unique(std::move(*subparts[1].value.release()))); return res; //std::cout << "PARSING: found LOOP" << std::endl; //for(auto& s : *(subparts[1].value)) std::cout << "LOOP: " << (int)s << std::endl; } } // namespace bf