base functional unit tests for transpiler

This commit is contained in:
jorenchik
2024-11-10 11:03:09 +02:00
parent 084f6afb13
commit f7f3ac5d4c
8 changed files with 481 additions and 32 deletions

View File

@@ -144,25 +144,18 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
};
if (isInBounds(i) && tokens[i].tokenType == TokenType::TextFragment) {
try {
auto parseToUTCTime = [](const std::string datetime, std::string format) {
std::tm tm = {};
std::istringstream ss(datetime);
ss >> std::get_time(&tm, format.c_str());
if (ss.fail()) {
throw std::runtime_error("Neizdevās nolasīt datuma un laiku");
}
std::time_t time = timegm(&tm);
return time;
};
time = parseToUTCTime(tokens[i].content.c_str(), "%d.%m.%Y %H:%M");
} catch (std::exception e) {
const std::string format = "%d.%m.%Y %H:%M";
const std::string datetime = tokens[i].content.c_str();
std::tm tm = {};
std::istringstream ss(datetime);
ss >> std::get_time(&tm, format.c_str());
if (ss.fail()) {
return makeResult(
e.what(),
"Neizdevās nolasīt datuma un laiku",
tokens[i]
);
}
time = timegm(&tm);
i++;
}
@@ -171,17 +164,17 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
std::string questionText;
std::vector<QuestionElement> questionElements;
double cooldown;
bool isOrderQuestion = false;
bool isGroupQuestion = false;
bool isPlusQuestion = false;
bool isOrderQuestion = false;
bool isGroupQuestion = false;
bool isPlusQuestion = false;
bool hasGroupEncountered = false;
Token questionStartToken;
// Start element parsing & add to the offset.
if (isInBounds(i + 1) && tokens[i + 1].tokenType == TokenType::ElementOrderModifier) {
return makeResult(
"Nevar izmantot secības modifikatoru ('^') jautājuma sākumā",
tokens[i + 1]
);
tokens[i + 1]);
}
questionStartToken = tokens[i];
@@ -235,6 +228,18 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
} else {
isDash = false;
isPlusQuestion = true;
if (isGroupQuestion) {
return makeResult(
"jautājumos ar grupām nevar būt secības elementu ('+' and ':')",
tokens[i]
);
}
if (isOrderQuestion) {
return makeResult(
"secības jautājumos nevar būt izvēles elementu ('-^' and '+')",
tokens[i]
);
}
}
if (isInBounds(i+1) && tokens[i + 1].tokenType == TokenType::ElementOrderModifier) {
isOrder = true;
@@ -247,7 +252,13 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
}
if (isGroupQuestion) {
return makeResult(
"jautājumos ar grupām nevar būt secības elementu ('-^' and ':')",
"jautājumos ar grupām nevar būt secības elementu ('-^' and ':')",
tokens[i]
);
}
if (isPlusQuestion) {
return makeResult(
"izvēles jautājumos nevar būt secības elementu ('+' and '-^')",
tokens[i]
);
}
@@ -259,14 +270,23 @@ Result<ParseInfo> parseQuestions(const std::vector<Token>& tokens) {
}
}
if (isInBounds(i + 2) && tokens[i + 2].tokenType == TokenType::MatchGroupEnd) {
isGroup = true;
isGroupQuestion = true;
isGroup = true;
isGroupQuestion = true;
if (!isDash) {
return makeResult(
"grupas jautājumus var definēt tikai ar svītru elementiem ('-')",
tokens[i]
);
}
if (!hasGroupEncountered) {
if (questionElements.size() > 0) {
return makeResult(
"elementi grupas jautājumā nevar eksistēt bez grupas",
tokens[i]
);
}
}
hasGroupEncountered = true;
}
QuestionElement questionElement;