Before splitting code into smaller bits
This commit is contained in:
parent
f8c8296d01
commit
21064d7ce7
|
@ -18,6 +18,13 @@
|
|||
"program": "fish",
|
||||
"workingDir": "$PROJECT_DIR$",
|
||||
"args": ["test.fish"]
|
||||
},
|
||||
{
|
||||
"type": "command",
|
||||
"name": "Run with --debug",
|
||||
"program": "$PROJECT_DIR$/mx",
|
||||
"workingDir": "$PROJECT_DIR$",
|
||||
"args": ["--debug", "test.mx"]
|
||||
}
|
||||
]
|
||||
}
|
67
src/main.cpp
67
src/main.cpp
|
@ -1,3 +1,4 @@
|
|||
#include <ios>
|
||||
#include <iostream>
|
||||
#include <csignal>
|
||||
#include <fstream>
|
||||
|
@ -16,7 +17,7 @@ enum class valtype {
|
|||
enum class keywords {
|
||||
IF, ELSE, WHILE, INT, DEC, STR, BOOL, FUN, RETURN,
|
||||
OPARE, CPARE, OBRAC, CBRAC,
|
||||
ADDTO, TAKEFROM, MULTIPLYTO, DIVIDEFROM,
|
||||
ADDTO, SUBTRACTFROM, MULTIPLYTO, DIVIDEFROM,
|
||||
ADD, SUBTRACT, MULTIPLY, DIVIDE,
|
||||
EQUAL, INEQUAL, LESS, GREATER, EQLESS, EQGREATER,
|
||||
INCREMENT, DECREMENT,
|
||||
|
@ -304,6 +305,12 @@ class Parser {
|
|||
else if (ct == "-") token.keyword = keywords::SUBTRACT;
|
||||
else if (ct == "*") token.keyword = keywords::MULTIPLY;
|
||||
else if (ct == "/") token.keyword = keywords::DIVIDE;
|
||||
else if (ct == "++") token.keyword = keywords::INCREMENT;
|
||||
else if (ct == "--") token.keyword = keywords::DECREMENT;
|
||||
else if (ct == "+=") token.keyword = keywords::ADDTO;
|
||||
else if (ct == "-=") token.keyword = keywords::SUBTRACTFROM;
|
||||
else if (ct == "*=") token.keyword = keywords::MULTIPLYTO;
|
||||
else if (ct == "/=") token.keyword = keywords::DIVIDEFROM;
|
||||
else {
|
||||
token.keyword = keywords::VALUE;
|
||||
// Convert the value based on its type
|
||||
|
@ -366,6 +373,7 @@ class Interpreter {
|
|||
|
||||
public:
|
||||
void convertToTokens(vector<Token> tokenList) {
|
||||
if (debugMode) log.toggleDebugPrint();
|
||||
tokens = tokenList;
|
||||
log.debug("Alright we got " + to_string(tokens.size()) + " tokens");
|
||||
|
||||
|
@ -386,7 +394,8 @@ class Interpreter {
|
|||
currentInstruction.push_back(nextToken.value());
|
||||
}
|
||||
// Apply variables to tokens
|
||||
for (int i = 0; i < currentInstruction.size(); i++) {
|
||||
// We start at 1 so we can reassign variables in the execution of code
|
||||
for (int i = 1; i < currentInstruction.size(); i++) {
|
||||
if (currentInstruction[i].type == valtype::STR) {
|
||||
string potentialVarName = get<string>(currentInstruction[i].value.value);
|
||||
auto varIt = variables.find(potentialVarName);
|
||||
|
@ -405,6 +414,7 @@ class Interpreter {
|
|||
for (int i = 0; i < currentInstruction.size(); i++) {
|
||||
if (currentInstruction[i].keyword == keywords::ADD || currentInstruction[i].keyword == keywords::SUBTRACT || currentInstruction[i].keyword == keywords::MULTIPLY || currentInstruction[i].keyword == keywords::DIVIDE) {
|
||||
Token newToken;
|
||||
newToken.keyword = keywords::VALUE;
|
||||
if (currentInstruction.size() < i + 1) syntaxError.mathTooFewArgs();
|
||||
Token before = currentInstruction[i - 1];
|
||||
Token after = currentInstruction[i + 1];
|
||||
|
@ -452,7 +462,16 @@ class Interpreter {
|
|||
} else {
|
||||
syntaxError.mathCannotDoOperationOnType("/", "bool or string");
|
||||
}
|
||||
} else {
|
||||
// Something has gone terribly wrong
|
||||
// We should never reach this point in the code
|
||||
syntaxError.generalError("The math aint mathing");
|
||||
}
|
||||
// Insert our cool new token and get rid of the boring old stuff
|
||||
currentInstruction[i - 1] = newToken;
|
||||
currentInstruction.erase(currentInstruction.begin() + i);
|
||||
currentInstruction.erase(currentInstruction.begin() + i);
|
||||
i -= 1;
|
||||
}
|
||||
}
|
||||
// Execute the instruction
|
||||
|
@ -461,6 +480,7 @@ class Interpreter {
|
|||
}
|
||||
void executeCode(vector<Token> tokens) {
|
||||
SyntaxError syntaxError;
|
||||
log.debug("Token length for this expression is " + to_string(tokens.size()));
|
||||
for (int i = 0; i < tokens.size(); i++) {
|
||||
if (tokens[i].keyword == keywords::PRINT) {
|
||||
i++;
|
||||
|
@ -557,9 +577,46 @@ class Interpreter {
|
|||
|
||||
// Store the variable
|
||||
variables[varName] = newValue;
|
||||
}
|
||||
else {
|
||||
syntaxError.unknownFn();
|
||||
} else {
|
||||
if (tokens[i].keyword == keywords::VALUE && tokens[i].value.type == valtype::STR && variables.find(get<string>(tokens[i].value.value)) != variables.end()) {
|
||||
log.debug("Manipulating variable...");
|
||||
if (tokens.size() < i + 1) {
|
||||
syntaxError.mathTooFewArgs();
|
||||
}
|
||||
string varName = get<string>(tokens[i].value.value);
|
||||
i++;
|
||||
if (variables[varName].type == valtype::INT) {
|
||||
if (tokens[i].keyword == keywords::INCREMENT) {
|
||||
variables[varName].value = get<int>(variables[varName].value) + 1;
|
||||
} else if (tokens[i].keyword == keywords::DECREMENT) {
|
||||
variables[varName].value = get<int>(variables[varName].value) - 1;
|
||||
} else if (tokens[i].keyword == keywords::ADDTO) {
|
||||
if (tokens.size() < i + 1) syntaxError.mathTooFewArgs();
|
||||
if (tokens[i + 1].value.type != valtype::INT) syntaxError.mathTypeMismatch("Expected an int when adding to an int");
|
||||
variables[varName].value = get<int>(variables[varName].value) + get<int>(tokens[i + 1].value.value);
|
||||
} else if (tokens[i].keyword == keywords::SUBTRACTFROM) {
|
||||
if (tokens.size() < i + 1) syntaxError.mathTooFewArgs();
|
||||
if (tokens[i + 1].value.type != valtype::INT) syntaxError.mathTypeMismatch("Expected an int when subtracting from an int");
|
||||
variables[varName].value = get<int>(variables[varName].value) - get<int>(tokens[i + 1].value.value);
|
||||
}
|
||||
} else if (variables[varName].type == valtype::DEC) {
|
||||
if (tokens[i].keyword == keywords::INCREMENT) {
|
||||
variables[varName].value = get<double>(variables[varName].value) + 1;
|
||||
} else if (tokens[i].keyword == keywords::DECREMENT) {
|
||||
variables[varName].value = get<double>(variables[varName].value) - 1;
|
||||
} else if (tokens[i].keyword == keywords::ADDTO) {
|
||||
if (tokens.size() < i + 1) syntaxError.mathTooFewArgs();
|
||||
if (tokens[i + 1].value.type != valtype::DEC) syntaxError.mathTypeMismatch("Expected an dec when adding to an dec");
|
||||
variables[varName].value = get<double>(variables[varName].value) + get<double>(tokens[i + 1].value.value);
|
||||
} else if (tokens[i].keyword == keywords::SUBTRACTFROM) {
|
||||
if (tokens.size() < i + 1) syntaxError.mathTooFewArgs();
|
||||
if (tokens[i + 1].value.type != valtype::DEC) syntaxError.mathTypeMismatch("Expected an dec when subtracting from an dec");
|
||||
variables[varName].value = get<double>(variables[varName].value) - get<double>(tokens[i + 1].value.value);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
syntaxError.unknownFn();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user