Allow access to lists
This commit is contained in:
@@ -33,6 +33,7 @@ optional<Token> Interpreter::peek(int offset) {
|
||||
|
||||
void Interpreter::initInterpreter(vector<string> 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<Token> tokenList) {
|
||||
if (currentInstruction[i].type == valtype::STR) {
|
||||
string potentialVarName = get<string>(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<Token> 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<List>(currentInstruction[i].value.value).value.size() < get<int>(currentInstruction[i + 2].value.value)) syntaxError.listOutOfBounds();
|
||||
Token newToken;
|
||||
newToken.keyword = keywords::VALUE;
|
||||
newToken.type = get<List>(currentInstruction[i].value.value).type;
|
||||
newToken.value.type = get<List>(currentInstruction[i].value.value).type;
|
||||
log.debug("Writing type " + log.getTypeString(newToken.type));
|
||||
newToken.value = get<List>(currentInstruction[i].value.value).value[get<int>(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) {
|
||||
|
Reference in New Issue
Block a user