rrcc/src/Parser.cpp

218 lines
9.6 KiB
C++

/*****************************************************************************
* Copyright (c) 2020, rrcc FM10K-Documentation Contributors
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the copyright holder nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
#include "Parser.h"
void Parser::parse() {
offset = 0;
functions.clear();
std::string currentValue;
uint8_t c;
currentValue.reserve(16);
std::vector<std::vector<std::pair<Token::Type, size_t>>> states = {
/* 0 */ {{Token::Type::Label, 1}},
/* 1 */
{{Token::Type::LocalLabel, 2}, {Token::Type::Operator, 3}},
/* 2 */
{{Token::Type::LocalLabel, 2}, {Token::Type::Operator, 3}, {Token::Type::Label, 1}},
/* 3 */
{{Token::Type::CodeLocation, 3}, {Token::Type::CodeLabel, 3}, {Token::Type::RegisterLocation, 3}, {Token::Type::RegisterName, 3}, {Token::Type::Immediate, 3}, {Token::Type::Label, 1}},
};
size_t currentStateIndex = 0;
const size_t resetStateIndex = 2;
bool isComment = false;
Function currentFunction;
Declaration currentDeclaration;
while (true) {
c = getByte();
if (c == '\n' || c == '\0' || (!isComment && (c == ' ' || c == '\t' || c == ',' || c == ';'))) {
//End token
//TODO: identify Token
if (!currentValue.empty()) {
bool foundToken = false;
for (auto &t : states[currentStateIndex]) {
switch (t.first) {
case Token::Type::Label:
if (isLabel(currentValue)) {
if (!currentFunction.label.empty()) {
functions.emplace_back(std::move(currentFunction));
currentFunction = Function();
}
currentFunction.label = currentValue.substr(0, currentValue.size() - 1);
foundToken = true;
}
break;
case Token::Type::LocalLabel:
if (isLocalLabel(currentValue)) {
currentDeclaration.label = currentValue.substr(0, currentValue.size() - 1);
foundToken = true;
}
break;
case Token::Type::Operator:
if (isOperator(currentValue)) {
currentDeclaration.tokens.emplace_back(Token(t.first, currentValue));
foundToken = true;
}
break;
case Token::Type::RegisterName:
if (isRegisterName(currentValue)) {
currentDeclaration.tokens.emplace_back(
Token(t.first, currentValue.substr(1, std::string::npos)));
foundToken = true;
}
break;
case Token::Type::RegisterLocation:
if (isRegisterLocation(currentValue)) {
currentDeclaration.tokens.emplace_back(
Token(t.first, currentValue.substr(1, std::string::npos),
std::stoi(currentValue.substr(1, std::string::npos), nullptr, 0)));
foundToken = true;
}
break;
case Token::Type::CodeLabel:
if (isCodeLabel(currentValue)) {
currentDeclaration.tokens.emplace_back(
Token(t.first, currentValue.substr(1, std::string::npos)));
foundToken = true;
}
break;
case Token::Type::CodeLocation:
if (isCodeLocation(currentValue)) {
currentDeclaration.tokens.emplace_back(
Token(t.first, currentValue.substr(1, std::string::npos),
std::stoi(currentValue.substr(1, std::string::npos), nullptr, 0)));
foundToken = true;
}
break;
case Token::Type::Immediate:
if (isImmediate(currentValue)) {
currentDeclaration.tokens.emplace_back(
Token(t.first, currentValue, std::stoi(currentValue, nullptr, 0)));
foundToken = true;
}
break;
}
if (foundToken) {
currentValue.clear();
currentStateIndex = t.second;
break;
}
}
if (!foundToken) {
//TODO: ERROR
throw std::runtime_error("Could not find new Token state");
}
}
if (c == '\n' || c == '\0') {
isComment = false;
if (resetStateIndex < currentStateIndex) {
currentStateIndex = resetStateIndex;
}
if (!currentDeclaration.tokens.empty()) {
currentFunction.declarations.emplace_back(std::move(currentDeclaration));
currentDeclaration = Declaration();
}
if (c == '\0') {
functions.emplace_back(std::move(currentFunction));
break;
}
}
if (c == ';') {
isComment = true;
}
} else if (!isComment) {
currentValue += c;
}
}
}
bool Parser::isIteratorAllowedValues(std::string::const_iterator it, std::string::const_iterator end,
const std::vector<uint8_t> &values) {
while (it != end) {
if (!isAllowedValues(*it, values)) {
return false;
}
it++;
}
return true;
}
bool Parser::isAllowedValues(uint8_t c, const std::vector<uint8_t> &values) {
return std::find(values.begin(), values.end(), c) != values.end();
}
bool Parser::isOperator(const std::string &v) {
return isIteratorAllowedValues(v.begin(), v.end(), getOperatorValues());
}
bool Parser::isRegisterName(const std::string &v) {
return v.size() >= 2 && v.at(0) == '%' && isIteratorAllowedValues(v.begin() + 1, v.end(), getAlphanumericValues());
}
bool Parser::isRegisterLocation(const std::string &v) {
return v.size() >= 2 && v.at(0) == '%' && isIteratorAllowedValues(v.begin() + 1, v.end(), getNumericValues());
}
bool Parser::isCodeLabel(const std::string &v) {
return v.size() >= 2 && v.at(0) == '@' &&
((v.at(1) == '.' && isIteratorAllowedValues(v.begin() + 2, v.end(), getAlphanumericValues())) ||
isIteratorAllowedValues(v.begin() + 1, v.end(), getAlphanumericValues()));
}
bool Parser::isCodeLocation(const std::string &v) {
return v.size() >= 2 && v.at(0) == '@' && isIteratorAllowedValues(v.begin() + 1, v.end(), getNumericValues());
}
bool Parser::isImmediate(const std::string &v) {
return isIteratorAllowedValues(v.begin(), v.end(), getNumericValues());
}
bool Parser::isLocalLabel(const std::string &v) {
return v.size() >= 3 && v.at(0) == '.' && isAllowedValues(v.at(1), getAlphaValues()) && v.at(v.size() - 1) == ':' &&
isIteratorAllowedValues(v.begin() + 2, v.end() - 1, getAlphanumericValues());
}
bool Parser::isLabel(const std::string &v) {
return v.size() >= 2 && isAllowedValues(v.at(0), getAlphaValues()) && v.at(v.size() - 1) == ':' &&
isIteratorAllowedValues(v.begin() + 1, v.end() - 1, getAlphanumericValues());
}