diff --git a/examples/args.io b/examples/args.io index 491de73..80e5509 100644 --- a/examples/args.io +++ b/examples/args.io @@ -1 +1,2 @@ printlist args; +println args[0]; diff --git a/src/Interpreter.cpp b/src/Interpreter.cpp index e5efa0f..c121f44 100644 --- a/src/Interpreter.cpp +++ b/src/Interpreter.cpp @@ -33,6 +33,7 @@ optional Interpreter::peek(int offset) { void Interpreter::initInterpreter(vector args) { List arguments; + arguments.type = valtype::STR; for (int i = 0; i < args.size(); i++) { Value buf; buf.type = valtype::STR; @@ -75,20 +76,13 @@ void Interpreter::interpret(vector tokenList) { if (currentInstruction[i].type == valtype::STR) { string potentialVarName = get(currentInstruction[i].value.value); auto varIt = variables.find(potentialVarName); - if (varIt != variables.end()) { - // This is pretty crappy code but it exists for now. Lists are gonna break this so much it's not even gonna be funny - //if (varIt->second.type == valtype::LIST) { - - //} else { - // Replace the token with the variable's value - Token newToken; - newToken.keyword = keywords::VALUE; - if (varIt->second.type == valtype::LIST) newToken.keyword = keywords::LISTOBJ; - newToken.type = varIt->second.type; - newToken.value = varIt->second; - currentInstruction[i] = newToken; - //} + Token newToken; + newToken.keyword = keywords::VALUE; + if (varIt->second.type == valtype::LIST) newToken.keyword = keywords::LISTOBJ; + newToken.type = varIt->second.type; + newToken.value = varIt->second; + currentInstruction[i] = newToken; } } else if (currentInstruction[i].keyword == keywords::INPUT) { Token newToken; @@ -101,6 +95,31 @@ void Interpreter::interpret(vector tokenList) { currentInstruction[i] = newToken; } } + // Allow users to get elements from list objects + for (int i = 0; i < currentInstruction.size(); i++) { + log.debug("Trying to find a list object"); + if (currentInstruction[i].keyword == keywords::LISTOBJ || currentInstruction[i].value.type == valtype::LIST) { + log.debug("Found a list object"); + if (i + 3 < currentInstruction.size()) { + log.debug("Instruction is large enough to have a list index"); + if (currentInstruction[i + 1].keyword == keywords::OSQUA && currentInstruction[i + 3].keyword == keywords::CSQUA && currentInstruction[i + 2].value.type == valtype::INT) { + log.debug("Finding list object item"); + if (currentInstruction[i + 2].value.type != valtype::INT) syntaxError.generalError("List index requires an int"); + if (get(currentInstruction[i].value.value).value.size() < get(currentInstruction[i + 2].value.value)) syntaxError.listOutOfBounds(); + Token newToken; + newToken.keyword = keywords::VALUE; + newToken.type = get(currentInstruction[i].value.value).type; + newToken.value.type = get(currentInstruction[i].value.value).type; + log.debug("Writing type " + log.getTypeString(newToken.type)); + newToken.value = get(currentInstruction[i].value.value).value[get(currentInstruction[i + 2].value.value)]; + currentInstruction[i] = newToken; + currentInstruction.erase(currentInstruction.begin() + i + 1); + currentInstruction.erase(currentInstruction.begin() + i + 1); + currentInstruction.erase(currentInstruction.begin() + i + 1); + } + } + } + } // Do math 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) { diff --git a/src/SyntaxError.cpp b/src/SyntaxError.cpp index 1af1c1d..d4218bc 100644 --- a/src/SyntaxError.cpp +++ b/src/SyntaxError.cpp @@ -82,6 +82,12 @@ void SyntaxError::listTypeMismatch(string notes) { exit(1); } +void SyntaxError::listOutOfBounds(string notes) { + cerr << "GeneralError: List out of bounds access" << endl; + if (!notes.empty()) cerr << "Notes: " << notes << endl; + exit(1); +} + void SyntaxError::comparisonTypeError(string notes) { cerr << "ComparisonError: cannot use a non-bool type in an if or while statement" << endl; if (!notes.empty()) cerr << "Notes: " << notes << endl; diff --git a/src/SyntaxError.h b/src/SyntaxError.h index 397aa95..1fd8236 100644 --- a/src/SyntaxError.h +++ b/src/SyntaxError.h @@ -31,5 +31,6 @@ public: void cannotCompareDifferentTypes(string notes = ""); void comparisonTypeError(string notes = ""); void listTypeMismatch(string notes = ""); + void listOutOfBounds(string notes = ""); void generalError(string notes = ""); }; diff --git a/src/common.h b/src/common.h index b8398a0..e927ceb 100644 --- a/src/common.h +++ b/src/common.h @@ -64,23 +64,6 @@ struct Token { struct var { valtype type; variant value; - - string toString() const { - if (type == valtype::INT) { - return to_string(get(value)); - } - else if (type == valtype::DEC) { - return to_string(get(value)); - } - else if (type == valtype::STR) { - return get(value); - } - else if (type == valtype::BOOL) { - return to_string(get(value)); - } else { - return "unknown"; - } - } }; // Global variable