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 See also parser function
*/ */
enum class Types { 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
}; };
/* // Forward declaration of Literal for list
Literal struct struct Literal;
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;
};
/* /*
List struct List struct
@@ -143,6 +130,22 @@ struct List {
vector<Literal> val; 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 Direct struct
If the program being executed makes a direct reference, it is stored in a Direct 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; map<string, Literal> variables;
/*
lists map
Contains all lists made while running the program. See also List struct.
*/
map<string, List> lists;
/* /*
labels map labels map
Contains all labels made in the program, for ease of jumping around the code. 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; first = false;
} }
} }
lists[listName] = listContents; variables[listName].val = listContents;
} }
break; break;
/* /*
@@ -721,16 +717,20 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
error("Third argument of getlistat must be a direct reference"); error("Third argument of getlistat must be a direct reference");
} }
if (lists.find(listref.listName) != lists.end()) { if (variables.find(listref.listName) != variables.end()) {
if (lists[listref.listName].val.size() > ref) { 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; bool existed = variables.count(var.varName) > 0;
variables[var.varName] = lists[listref.listName].val[ref]; variables[var.varName] = get<List>(variables[listref.listName].val).val[ref];
} else { } else {
error("Index " + to_string(ref) + " out of range of list " + listref.listName); error("Index " + to_string(ref) + " out of range of list " + listref.listName);
} }
} else { } else {
error("Unknown list: " + listref.listName); error("Unknown list: " + listref.listName);
} }
} else {
error("Found a normal variable in place of a list");
}
} }
break; break;
/* /*
@@ -817,13 +817,16 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
} else { } else {
error("Third argument of setlistat must be a direct reference"); error("Third argument of setlistat must be a direct reference");
} }
if (variables.find(listref.listName) != variables.end()) {
if (lists.find(listref.listName) != lists.end()) { if (holds_alternative<List>(variables[listref.listName].val)) {
if (lists[listref.listName].val.size() > ref) { if (get<List>(variables[listref.listName].val).val.size() > ref) {
lists[listref.listName].val[ref] = value; List tmpList = get<List>(variables[listref.listName].val);
tmpList.val[ref] = value;
variables[listref.listName].val = tmpList;
} else { } else {
error("Index " + to_string(ref) + " out of range of list " + listref.listName); error("Index " + to_string(ref) + " out of range of list " + listref.listName);
} }
}
} else { } else {
error("Unknown list: " + listref.listName); 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"); error("Second argument of listappend must be a direct reference");
} }
if (lists.find(listref.listName) != lists.end()) { if (variables.find(listref.listName) != variables.end()) {
lists[listref.listName].val.push_back(value); List tmpList = get<List>(variables[listref.listName].val);
tmpList.val.push_back(value);
variables[listref.listName].val = tmpList;
} else { } else {
error("Unknown list: " + listref.listName); error("Unknown list: " + listref.listName);
} }
@@ -885,8 +890,8 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
} }
Literal newLit; Literal newLit;
if (lists.find(ref.listName) != lists.end()) { if (variables.find(ref.listName) != variables.end()) {
newLit.val = int(lists[ref.listName].val.size()); newLit.val = int(get<List>(variables[ref.listName].val).val.size());
bool existed = variables.count(var.varName) > 0; bool existed = variables.count(var.varName) > 0;
variables[var.varName] = newLit; variables[var.varName] = newLit;
} else { } else {
@@ -1677,7 +1682,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
exists = true; exists = true;
} }
} else if (holds_alternative<ListRef>(l.args[0])) { } 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; exists = true;
} }
} else if (holds_alternative<Line>(l.args[0])) { } 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 == "double") newType.type = Types::Double;
else if (type == "int") newType.type = Types::Int; else if (type == "int") newType.type = Types::Int;
else if (type == "bool") newType.type = Types::Bool; 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."); 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); newInst.args.push_back(newType);
} }
@@ -2297,6 +2303,9 @@ vector<Instruction> parser(vector<vector<string>> in) {
newInst.args.push_back(newLiteral); newInst.args.push_back(newLiteral);
} }
break; 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]; lit.val = argv[i];
argsList.val.push_back(lit); argsList.val.push_back(lit);
} }
lists["args"] = argsList; variables["args"].val = argsList;
ifstream file(argv[1]); ifstream file(argv[1]);
string lns; string lns;