#include "compiler.h" #include "scanner.h" #include "LR1.h" #include "tree.h" #include <iostream> #include <utility> #include <algorithm> using std::vector; using std::string; Compiler::Compiler() {} void Compiler::compile() { std::string line; std::vector<Token> tokens; try { while (getline(std::cin, line)) { // Remove Comment std::size_t found = line.find("//"); if (found != std::string::npos) { line = line.substr(0, found); } std::vector<Token> tokenLine = scan(line); for (auto &token : tokenLine) { tokens.push_back(token); } } } catch (ScanningFailure &f) { throw CompilerFailure(f.what()); } LR1 lr; lr.initialize(); std::vector<std::string> tokenStrings; for (auto token : tokens) { tokenStrings.push_back(token.getString()); } try { lr.parse(tokenStrings); } catch (ParsingFailure &f) { std::string error = "ERROR at "; throw CompilerFailure(error + f.what()); } parsedTokens = lr.generate(); std::reverse(parsedTokens.begin(), parsedTokens.end()); } void Compiler::generate() { Tree t (parsedTokens); try { t.traverse(); } catch (AnalysisError &e) { throw CompilerFailure(e.what()); } t.generate(); } CompilerFailure::CompilerFailure(string message): message(std::move(message)) {} const std::string &CompilerFailure::what() const { return message; }