diff --git a/mx b/mx index c052949..5b66310 100755 Binary files a/mx and b/mx differ diff --git a/src/Interpreter.cpp b/src/Interpreter.cpp index 5d8bf9d..3781416 100644 --- a/src/Interpreter.cpp +++ b/src/Interpreter.cpp @@ -221,51 +221,53 @@ void Interpreter::executeCode(vector tokens) { } 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) { + if (tokens.size() <= i + 2) { // Need at least 3 tokens: variable, operator, value syntaxError.mathTooFewArgs(); + return; } string varName = get(tokens[i].value.value); i++; - if (variables[varName].type == valtype::INT) { - if (tokens[i].keyword == keywords::INCREMENT) { + + if (tokens[i].keyword == keywords::SET) { + Token valueToken = tokens[i + 1]; + if (valueToken.type != variables[varName].type) { + vector validTypes; + switch(variables[varName].type) { + case valtype::INT: validTypes = {"int"}; break; + case valtype::DEC: validTypes = {"dec"}; break; + case valtype::STR: validTypes = {"str"}; break; + case valtype::BOOL: validTypes = {"bool"}; break; + } + syntaxError.fnTypeMismatch("assignment", validTypes, valueToken.type); + return; + } + variables[varName].value = valueToken.value.value; + i++; // Skip the value token since we've processed it + } else if (tokens[i].keyword == keywords::INCREMENT) { + if (variables[varName].type == valtype::INT) { variables[varName].value = get(variables[varName].value) + 1; - } else if (tokens[i].keyword == keywords::DECREMENT) { + } else { + syntaxError.mathCannotDoOperationOnType("++", "non-int"); + } + } else if (tokens[i].keyword == keywords::DECREMENT) { + if (variables[varName].type == valtype::INT) { 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"); + } else { + syntaxError.mathCannotDoOperationOnType("--", "non-int"); + } + } else if (tokens[i].keyword == keywords::ADDTO) { + if (tokens.size() < i + 1) syntaxError.mathTooFewArgs(); + if (tokens[i + 1].value.type != variables[varName].type) syntaxError.mathTypeMismatch("Expected same type when adding"); + if (variables[varName].type == valtype::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 (tokens[i].keyword == keywords::SET) { - if (tokens.size() < i + 1) syntaxError.mathTooFewArgs(); - if (tokens[i + 1].value.type != valtype::INT) syntaxError.mathTypeMismatch("Expected an int when changing an int"); - variables[varName].value = get(tokens[i + 1].value.value); - } else { - syntaxError.mathCannotDoOperationOnType("unknown", "int"); - } - } 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"); + } else if (variables[varName].type == valtype::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 if (tokens[i].keyword == keywords::SET) { - if (tokens.size() < i + 1) syntaxError.mathTooFewArgs(); - if (tokens[i + 1].value.type != valtype::DEC) syntaxError.mathTypeMismatch("Expected a dec when changing a dec"); - variables[varName].value = get(tokens[i + 1].value.value); } else { - syntaxError.mathCannotDoOperationOnType("unknown", "dec"); + syntaxError.mathCannotDoOperationOnType("+=", "non-numeric"); } + i++; + } else { + syntaxError.mathCannotDoOperationOnType("unknown", "any"); } } else { syntaxError.unknownFn(); diff --git a/test.mx b/test.mx index d3c6c3b..eae21cc 100644 --- a/test.mx +++ b/test.mx @@ -5,4 +5,8 @@ print 1 + 1; let int myInt 3; print myInt; myInt = 5; -print myInt; \ No newline at end of file +print myInt; +let str myString "dingus"; +print myString; +myString = "dongus"; +print myString; \ No newline at end of file