From f32f76450a1a643eb76c570041dd1d422c93e431 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Sat, 30 Aug 2025 12:28:07 +1000 Subject: [PATCH] Lists are now stored in the variables map --- src/main.cpp | 93 ++++++++++++++++++++++++++++------------------------ 1 file changed, 51 insertions(+), 42 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 7a396db..dfe5f29 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -96,24 +96,11 @@ enum class Instructions { See also parser function */ enum class Types { - Int, Double, String, Char, Bool, Value, Direct, Line, ListRef, Label, Type, Function + Int, Double, String, Char, Bool, Value, Direct, Line, List, ListRef, Label, Type, Function }; -/* - Literal struct - Contains literal values. For example, if the following line was written: - stdout "Hello world!" - The Literal struct in the instruction should look like this: - { - val = "Hello world!"; // I am ignoring the variant for simplicity - // of documenting the code - } - All value references are swapped out for their respective Literal they - point to. See also variables map, parser function, interpreter function -*/ -struct Literal { - variant val; -}; +// Forward declaration of Literal for list +struct Literal; /* List struct @@ -143,6 +130,22 @@ struct List { vector val; }; +/* + Literal struct + Contains literal values. For example, if the following line was written: + stdout "Hello world!" + The Literal struct in the instruction should look like this: + { + val = "Hello world!"; // I am ignoring the variant for simplicity + // of documenting the code + } + All value references are swapped out for their respective Literal they + point to. See also variables map, parser function, interpreter function +*/ +struct Literal { + variant val; +}; + /* Direct struct If the program being executed makes a direct reference, it is stored in a Direct @@ -185,13 +188,6 @@ struct ListRef { */ map variables; - -/* - lists map - Contains all lists made while running the program. See also List struct. -*/ -map lists; - /* labels map Contains all labels made in the program, for ease of jumping around the code. @@ -682,7 +678,7 @@ Literal exec(vector in, bool executingFunction) { first = false; } } - lists[listName] = listContents; + variables[listName].val = listContents; } break; /* @@ -721,15 +717,19 @@ Literal exec(vector in, bool executingFunction) { error("Third argument of getlistat must be a direct reference"); } - if (lists.find(listref.listName) != lists.end()) { - if (lists[listref.listName].val.size() > ref) { - bool existed = variables.count(var.varName) > 0; - variables[var.varName] = lists[listref.listName].val[ref]; + if (variables.find(listref.listName) != variables.end()) { + if (holds_alternative(variables[listref.listName].val)) { + if (get(variables[listref.listName].val).val.size() > ref) { + bool existed = variables.count(var.varName) > 0; + variables[var.varName] = get(variables[listref.listName].val).val[ref]; + } else { + error("Index " + to_string(ref) + " out of range of list " + listref.listName); + } } else { - error("Index " + to_string(ref) + " out of range of list " + listref.listName); + error("Unknown list: " + listref.listName); } } else { - error("Unknown list: " + listref.listName); + error("Found a normal variable in place of a list"); } } break; @@ -817,12 +817,15 @@ Literal exec(vector in, bool executingFunction) { } else { error("Third argument of setlistat must be a direct reference"); } - - if (lists.find(listref.listName) != lists.end()) { - if (lists[listref.listName].val.size() > ref) { - lists[listref.listName].val[ref] = value; - } else { - error("Index " + to_string(ref) + " out of range of list " + listref.listName); + if (variables.find(listref.listName) != variables.end()) { + if (holds_alternative(variables[listref.listName].val)) { + if (get(variables[listref.listName].val).val.size() > ref) { + List tmpList = get(variables[listref.listName].val); + tmpList.val[ref] = value; + variables[listref.listName].val = tmpList; + } else { + error("Index " + to_string(ref) + " out of range of list " + listref.listName); + } } } else { error("Unknown list: " + listref.listName); @@ -853,8 +856,10 @@ Literal exec(vector in, bool executingFunction) { error("Second argument of listappend must be a direct reference"); } - if (lists.find(listref.listName) != lists.end()) { - lists[listref.listName].val.push_back(value); + if (variables.find(listref.listName) != variables.end()) { + List tmpList = get(variables[listref.listName].val); + tmpList.val.push_back(value); + variables[listref.listName].val = tmpList; } else { error("Unknown list: " + listref.listName); } @@ -885,8 +890,8 @@ Literal exec(vector in, bool executingFunction) { } Literal newLit; - if (lists.find(ref.listName) != lists.end()) { - newLit.val = int(lists[ref.listName].val.size()); + if (variables.find(ref.listName) != variables.end()) { + newLit.val = int(get(variables[ref.listName].val).val.size()); bool existed = variables.count(var.varName) > 0; variables[var.varName] = newLit; } else { @@ -1677,7 +1682,7 @@ Literal exec(vector in, bool executingFunction) { exists = true; } } else if (holds_alternative(l.args[0])) { - if (lists.find(get(l.args[0]).listName) != lists.end()) { + if (variables.find(get(l.args[0]).listName) != variables.end() && holds_alternative(variables[get(l.args[0]).listName].val)) { exists = true; } } else if (holds_alternative(l.args[0])) { @@ -2227,6 +2232,7 @@ vector parser(vector> in) { else if (type == "double") newType.type = Types::Double; else if (type == "int") newType.type = Types::Int; else if (type == "bool") newType.type = Types::Bool; + else if (type == "list") newType.type = Types::List; else error("Ground could not find type. This is an error with the interpreter, not your code. This line of code should never be reached."); newInst.args.push_back(newType); } @@ -2297,6 +2303,9 @@ vector parser(vector> in) { newInst.args.push_back(newLiteral); } break; + default: + error("This type should not be obtained in normal execution"); + break; } } } @@ -2325,7 +2334,7 @@ int main(int argc, char** argv) { lit.val = argv[i]; argsList.val.push_back(lit); } - lists["args"] = argsList; + variables["args"].val = argsList; ifstream file(argv[1]); string lns;