Make a list variable

This commit is contained in:
2025-05-13 14:05:29 +10:00
parent de3941337c
commit 87bf540458
3 changed files with 28 additions and 4 deletions

View File

@@ -1 +1,3 @@
printlist ["This is an element in the list" "And this is another" "Oh look another list element" "How fun!"]; printlist ["This is an element in the list" "And this is another" "Oh look another list element" "How fun!"];
let str dingus ["Element 1" "Element 2" "Element 3"];
println dingus[1];

View File

@@ -97,7 +97,6 @@ void Interpreter::interpret(vector<Token> tokenList) {
} }
// Allow users to get elements from list objects // Allow users to get elements from list objects
for (int i = 0; i < currentInstruction.size(); i++) { 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) { if (currentInstruction[i].keyword == keywords::LISTOBJ || currentInstruction[i].value.type == valtype::LIST) {
log.debug("Found a list object"); log.debug("Found a list object");
if (i + 3 < currentInstruction.size()) { if (i + 3 < currentInstruction.size()) {
@@ -262,7 +261,7 @@ void Interpreter::interpret(vector<Token> tokenList) {
} }
// Find lists and create list objects // Find lists and create list objects
for (int i = 0; i < currentInstruction.size(); i++) { for (int i = 0; i < currentInstruction.size(); i++) {
if (currentInstruction[i].keyword == keywords::OSQUA) { if (currentInstruction[i].keyword == keywords::OSQUA && currentInstruction[i + 1].keyword != keywords::CSQUA) {
log.debug("Making a list"); log.debug("Making a list");
int startIndex = i; int startIndex = i;
currentInstruction.erase(currentInstruction.begin() + i); currentInstruction.erase(currentInstruction.begin() + i);
@@ -434,6 +433,20 @@ void Interpreter::executeCode(vector<Token> tokens) {
Token typeToken = tokens[i]; Token typeToken = tokens[i];
Token nameToken = tokens[i + 1]; Token nameToken = tokens[i + 1];
Token valueToken = tokens[i + 2]; Token valueToken = tokens[i + 2];
/*
if (tokens[i + 3].keyword == keywords::OSQUA && tokens[i + 4].keyword == keywords::CSQUA) {
log.debug("Creating a list variable");
valueToken = tokens[i + 5];
if (valueToken.keyword != keywords::LISTOBJ) syntaxError.generalError("Trying to add a non-list to a list variable");
else {
string varName = get<string>(nameToken.value.value);
Value newValue;
newValue.type = valtype::LIST;
newValue.value = get<List>(valueToken.value.value);
break;
}
}
*/
i += 2; i += 2;
@@ -447,8 +460,17 @@ void Interpreter::executeCode(vector<Token> tokens) {
string varName = get<string>(nameToken.value.value); string varName = get<string>(nameToken.value.value);
Value newValue; Value newValue;
if (valueToken.keyword == keywords::LISTOBJ) {
if (typeToken.value.type == get<List>(valueToken.value.value).type) {
newValue.type = valtype::LIST;
newValue.value = get<List>(valueToken.value.value);
} else {
syntaxError.fnTypeMismatch("let (listobj)", {log.getTypeString(get<List>(valueToken.value.value).type)}, valueToken.type, "Variable name is " + varName);
}
}
// Check the type declaration matches the value // Check the type declaration matches the value
if (typeToken.keyword == keywords::INT && valueToken.type == valtype::INT) { else if (typeToken.keyword == keywords::INT && valueToken.type == valtype::INT) {
newValue.type = valtype::INT; newValue.type = valtype::INT;
newValue.value = get<int>(valueToken.value.value); newValue.value = get<int>(valueToken.value.value);
} }

View File

@@ -32,7 +32,7 @@ enum class valtype {
}; };
enum class keywords { enum class keywords {
IF, ELSE, WHILE, INT, DEC, STR, BOOL, FUN, RETURN, IF, ELSE, WHILE, INT, DEC, STR, BOOL, LIST, FUN, RETURN,
OPARE, CPARE, OBRAC, CBRAC, OSQUA, CSQUA, COMMA, OPARE, CPARE, OBRAC, CBRAC, OSQUA, CSQUA, COMMA,
SET, ADDTO, SUBTRACTFROM, MULTIPLYTO, DIVIDEFROM, SET, ADDTO, SUBTRACTFROM, MULTIPLYTO, DIVIDEFROM,
ADD, SUBTRACT, MULTIPLY, DIVIDE, ADD, SUBTRACT, MULTIPLY, DIVIDE,