diff --git a/.fleet/run.json b/.fleet/run.json index dd0ce1f..64c9d3f 100644 --- a/.fleet/run.json +++ b/.fleet/run.json @@ -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"] } ] } \ No newline at end of file diff --git a/mx b/mx index 0bdac1f..81e508a 100755 Binary files a/mx and b/mx differ diff --git a/src/main.cpp b/src/main.cpp index b120b8e..c766958 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#include #include #include #include @@ -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 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(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 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(tokens[i].value.value)) != variables.end()) { + log.debug("Manipulating variable..."); + if (tokens.size() < i + 1) { + syntaxError.mathTooFewArgs(); + } + string varName = get(tokens[i].value.value); + i++; + if (variables[varName].type == valtype::INT) { + if (tokens[i].keyword == keywords::INCREMENT) { + variables[varName].value = get(variables[varName].value) + 1; + } else if (tokens[i].keyword == keywords::DECREMENT) { + variables[varName].value = get(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(variables[varName].value) + get(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(variables[varName].value) - get(tokens[i + 1].value.value); + } + } else if (variables[varName].type == valtype::DEC) { + if (tokens[i].keyword == keywords::INCREMENT) { + variables[varName].value = get(variables[varName].value) + 1; + } else if (tokens[i].keyword == keywords::DECREMENT) { + variables[varName].value = get(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(variables[varName].value) + get(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(variables[varName].value) - get(tokens[i + 1].value.value); + } + } + } else { + syntaxError.unknownFn(); + } } } } diff --git a/test.fish b/test.fish index 9e82140..19c2e8a 100644 --- a/test.fish +++ b/test.fish @@ -2,4 +2,4 @@ bob -./mx test.mx \ No newline at end of file +./mx --debug test.mx \ No newline at end of file diff --git a/test.mx b/test.mx index 6a65159..5954cd4 100644 --- a/test.mx +++ b/test.mx @@ -2,4 +2,5 @@ print "dingusify"; let str bingus "heheheha its bingusing time"; print bingus; print 1 + 1; -exit 0; +let dec myInt 3; +myInt += 69; \ No newline at end of file