Lists are now stored in the variables map

This commit is contained in:
2025-08-30 12:28:07 +10:00
parent cea66aa583
commit f32f76450a

View File

@@ -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<int, double, bool, string, char> val;
};
// Forward declaration of Literal for list
struct Literal;
/*
List struct
@@ -143,6 +130,22 @@ struct List {
vector<Literal> 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<int, double, bool, string, char, List> 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<string, Literal> variables;
/*
lists map
Contains all lists made while running the program. See also List struct.
*/
map<string, List> lists;
/*
labels map
Contains all labels made in the program, for ease of jumping around the code.
@@ -682,7 +678,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
first = false;
}
}
lists[listName] = listContents;
variables[listName].val = listContents;
}
break;
/*
@@ -721,15 +717,19 @@ Literal exec(vector<Instruction> 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<List>(variables[listref.listName].val)) {
if (get<List>(variables[listref.listName].val).val.size() > ref) {
bool existed = variables.count(var.varName) > 0;
variables[var.varName] = get<List>(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<Instruction> 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<List>(variables[listref.listName].val)) {
if (get<List>(variables[listref.listName].val).val.size() > ref) {
List tmpList = get<List>(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<Instruction> 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<List>(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<Instruction> 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<List>(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<Instruction> in, bool executingFunction) {
exists = true;
}
} else if (holds_alternative<ListRef>(l.args[0])) {
if (lists.find(get<ListRef>(l.args[0]).listName) != lists.end()) {
if (variables.find(get<ListRef>(l.args[0]).listName) != variables.end() && holds_alternative<List>(variables[get<ListRef>(l.args[0]).listName].val)) {
exists = true;
}
} else if (holds_alternative<Line>(l.args[0])) {
@@ -2227,6 +2232,7 @@ vector<Instruction> parser(vector<vector<string>> 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<Instruction> parser(vector<vector<string>> 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;