From 87bf540458f0c85948fe67bff038f43599046c59 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Tue, 13 May 2025 14:05:29 +1000 Subject: [PATCH] Make a list variable --- examples/list.io | 2 ++ src/Interpreter.cpp | 28 +++++++++++++++++++++++++--- src/common.h | 2 +- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/examples/list.io b/examples/list.io index 722e29e..888f966 100644 --- a/examples/list.io +++ b/examples/list.io @@ -1 +1,3 @@ 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]; diff --git a/src/Interpreter.cpp b/src/Interpreter.cpp index c121f44..7bee270 100644 --- a/src/Interpreter.cpp +++ b/src/Interpreter.cpp @@ -97,7 +97,6 @@ void Interpreter::interpret(vector tokenList) { } // 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()) { @@ -262,7 +261,7 @@ void Interpreter::interpret(vector tokenList) { } // Find lists and create list objects 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"); int startIndex = i; currentInstruction.erase(currentInstruction.begin() + i); @@ -434,6 +433,20 @@ void Interpreter::executeCode(vector tokens) { Token typeToken = tokens[i]; Token nameToken = tokens[i + 1]; 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(nameToken.value.value); + Value newValue; + newValue.type = valtype::LIST; + newValue.value = get(valueToken.value.value); + break; + } + } +*/ i += 2; @@ -447,8 +460,17 @@ void Interpreter::executeCode(vector tokens) { string varName = get(nameToken.value.value); Value newValue; + if (valueToken.keyword == keywords::LISTOBJ) { + if (typeToken.value.type == get(valueToken.value.value).type) { + newValue.type = valtype::LIST; + newValue.value = get(valueToken.value.value); + } else { + syntaxError.fnTypeMismatch("let (listobj)", {log.getTypeString(get(valueToken.value.value).type)}, valueToken.type, "Variable name is " + varName); + } + } + // 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.value = get(valueToken.value.value); } diff --git a/src/common.h b/src/common.h index e927ceb..d690f31 100644 --- a/src/common.h +++ b/src/common.h @@ -32,7 +32,7 @@ enum class valtype { }; 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, SET, ADDTO, SUBTRACTFROM, MULTIPLYTO, DIVIDEFROM, ADD, SUBTRACT, MULTIPLY, DIVIDE,