More stuff related to lists
This commit is contained in:
@@ -249,6 +249,34 @@ void Interpreter::interpret(vector<Token> tokenList) {
|
||||
i -= 1;
|
||||
}
|
||||
}
|
||||
// Find lists and create list objects
|
||||
for (int i = 0; i < currentInstruction.size(); i++) {
|
||||
if (currentInstruction[i].keyword == keywords::OSQUA) {
|
||||
log.debug("Making a list");
|
||||
int startIndex = i;
|
||||
currentInstruction.erase(currentInstruction.begin() + i);
|
||||
List buf;
|
||||
buf.type = currentInstruction[i].value.type;
|
||||
while (currentInstruction[i].keyword != keywords::CSQUA) {
|
||||
if (buf.type == currentInstruction[i].value.type) {
|
||||
buf.value.push_back(currentInstruction[i].value);
|
||||
currentInstruction.erase(currentInstruction.begin() + i);
|
||||
} else {
|
||||
syntaxError.listTypeMismatch();
|
||||
}
|
||||
}
|
||||
currentInstruction.erase(currentInstruction.begin() + i);
|
||||
Value newValue;
|
||||
newValue.type = valtype::LIST;
|
||||
newValue.value = buf;
|
||||
Token newToken;
|
||||
newToken.type = valtype::LIST;
|
||||
newToken.value.type = valtype::LIST;
|
||||
newToken.value.value = buf;
|
||||
newToken.keyword = keywords::LIST;
|
||||
currentInstruction.insert(currentInstruction.begin() + startIndex, newToken);
|
||||
}
|
||||
}
|
||||
// Execute the instruction
|
||||
log.debug("Length of line is " + to_string(lengthOfLine));
|
||||
executeCode(currentInstruction);
|
||||
@@ -285,7 +313,7 @@ void Interpreter::executeCode(vector<Token> tokens) {
|
||||
}
|
||||
if (tokens[i].keyword == keywords::PRINTLN) {
|
||||
i++;
|
||||
if (tokens.size() <= i) break;
|
||||
if (tokens.size() <= i) syntaxError.fnNotSufficientArgs("println", 1, 1, 0);
|
||||
Token nextToken = tokens[i];
|
||||
// Handle different value types
|
||||
if (nextToken.keyword == keywords::VALUE) {
|
||||
@@ -307,7 +335,7 @@ void Interpreter::executeCode(vector<Token> tokens) {
|
||||
syntaxError.fnTypeMismatch("println", validTypes, nextToken.type);
|
||||
}
|
||||
} else {
|
||||
syntaxError.fnNotSufficientArgs("println", 1, 1, 0);
|
||||
syntaxError.fnTypeMismatch("println", {}, nextToken.type);
|
||||
}
|
||||
} else if (tokens[i].keyword == keywords::PRINT) {
|
||||
i++;
|
||||
@@ -335,6 +363,39 @@ void Interpreter::executeCode(vector<Token> tokens) {
|
||||
} else {
|
||||
syntaxError.fnNotSufficientArgs("print", 1, 1, 0);
|
||||
}
|
||||
} else if (tokens[i].keyword == keywords::PRINTLIST) {
|
||||
i++;
|
||||
if (tokens.size() <= i) syntaxError.fnNotSufficientArgs("printlist", 1, 1, 0);
|
||||
Token nextToken = tokens[i];
|
||||
log.debug("Printing a list. Type of next token is " + log.getTypeString(nextToken.value.type));
|
||||
if (nextToken.keyword == keywords::LIST && nextToken.type == valtype::LIST) {
|
||||
List list = get<List>(nextToken.value.value);
|
||||
log.debug("List obtained");
|
||||
if (list.value[0].type == valtype::INT) {
|
||||
for (int j = 0; j < list.value.size(); j++) {
|
||||
cout << get<int>(list.value[j].value) << ", ";
|
||||
}
|
||||
}
|
||||
else if (list.value[0].type == valtype::DEC) {
|
||||
for (int j = 0; j < list.value.size(); j++) {
|
||||
cout << get<double>(list.value[j].value) << ", ";
|
||||
}
|
||||
}
|
||||
else if (list.value[0].type == valtype::STR) {
|
||||
for (int j = 0; j < list.value.size(); j++) {
|
||||
cout << get<string>(list.value[j].value) << ", ";
|
||||
}
|
||||
}
|
||||
else if (list.value[0].type == valtype::BOOL) {
|
||||
for (int j = 0; j < list.value.size(); j++) {
|
||||
cout << (get<bool>(list.value[j].value) ? "true" : "false") << ", ";
|
||||
}
|
||||
}
|
||||
cout << endl;
|
||||
} else {
|
||||
syntaxError.fnTypeMismatch("printlist", {}, valtype::UNKNOWN);
|
||||
}
|
||||
break;
|
||||
} else if (tokens[i].keyword == keywords::EXIT) {
|
||||
i++;
|
||||
if (tokens.size() <= i) break;
|
||||
|
Reference in New Issue
Block a user