Deprecate *list syntax, use &direct refs instead

This commit is contained in:
2025-09-25 08:09:27 +10:00
parent 7066b4300d
commit 81d6e21a00
3 changed files with 59 additions and 74 deletions

View File

@@ -44,12 +44,6 @@ and jump to that (setting labels will be discussed below):
jump %myLabel jump %myLabel
``` ```
Reference a list (a list reference) with an asterisk:
```
setlist *myList $value1 $value2 # and so on
```
Add comments with a `#`: Add comments with a `#`:
``` ```
@@ -128,31 +122,31 @@ Note: You can also replace &var1 with a list or line reference to check if it al
Allows you to initialize a list. Allows you to initialize a list.
Usage: `setlist *list $value1 $value2 $value3...` Usage: `setlist &list $value1 $value2 $value3...`
#### setlistat #### setlistat
Sets a list item at an index. The item at the index must already exist. Lists are index 0. Sets a list item at an index. The item at the index must already exist. Lists are index 0.
Usage: `setlistat *list $intvalue $value` Usage: `setlistat &list $intvalue $value`
#### getlistat #### getlistat
Gets a list item at an index, and puts it in the variable provided. The item at the index must already exist. Lists are index 0. Gets a list item at an index, and puts it in the variable provided. The item at the index must already exist. Lists are index 0.
Usage: `getlistat *list $intvalue &var` Usage: `getlistat &list $intvalue &var`
#### getlistsize #### getlistsize
Gets the size of a list and puts it in the variable provided. Gets the size of a list and puts it in the variable provided.
Usage: `getlistsize *list &var` Usage: `getlistsize &list &var`
#### listappend #### listappend
Appends an item to a list. Appends an item to a list.
Usage: `listappend *list $var` Usage: `listappend &list $var`
### String Operations ### String Operations

View File

@@ -833,12 +833,8 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
else if (holds_alternative<char>(get<Literal>(l.args[0]).val)) { else if (holds_alternative<char>(get<Literal>(l.args[0]).val)) {
cout << get<char>(get<Literal>(l.args[0]).val); cout << get<char>(get<Literal>(l.args[0]).val);
} }
else { else if (holds_alternative<List>(get<Literal>(l.args[0]).val)) {
return error("Couldn't print that", "printError"); List list = get<List>(get<Literal>(l.args[0]).val);
}
} else if (holds_alternative<ListRef>(l.args[0])) {
if (variables.find(get<ListRef>(l.args[0]).listName) != variables.end()) {
List list = get<List>(variables[get<ListRef>(l.args[0]).listName].val);
cout << "["; cout << "[";
for (int l = 0; l < list.val.size(); l++) { for (int l = 0; l < list.val.size(); l++) {
if (holds_alternative<string>(list.val[l].val)) { if (holds_alternative<string>(list.val[l].val)) {
@@ -867,9 +863,10 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
cout << ", "; cout << ", ";
} }
} }
cout << "]"; cout << "]" << endl;
} else { }
return error("Couldn't find list named " + get<ListRef>(l.args[0]).listName); else {
return error("Couldn't print that", "printError");
} }
} else { } else {
return error("Argument of stdlnout must be a value (literal or a value reference)"); return error("Argument of stdlnout must be a value (literal or a value reference)");
@@ -904,12 +901,8 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
else if (holds_alternative<char>(get<Literal>(l.args[0]).val)) { else if (holds_alternative<char>(get<Literal>(l.args[0]).val)) {
cout << get<char>(get<Literal>(l.args[0]).val) << endl; cout << get<char>(get<Literal>(l.args[0]).val) << endl;
} }
else { else if (holds_alternative<List>(get<Literal>(l.args[0]).val)) {
return error("Couldn't print that", "printError"); List list = get<List>(get<Literal>(l.args[0]).val);
}
} else if (holds_alternative<ListRef>(l.args[0])) {
if (variables.find(get<ListRef>(l.args[0]).listName) != variables.end()) {
List list = get<List>(variables[get<ListRef>(l.args[0]).listName].val);
cout << "["; cout << "[";
for (int l = 0; l < list.val.size(); l++) { for (int l = 0; l < list.val.size(); l++) {
if (holds_alternative<string>(list.val[l].val)) { if (holds_alternative<string>(list.val[l].val)) {
@@ -939,8 +932,9 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
} }
} }
cout << "]" << endl; cout << "]" << endl;
} else { }
return error("Couldn't find list named " + get<ListRef>(l.args[0]).listName); else {
return error("Couldn't print that", "printError");
} }
} else { } else {
return error("Argument of stdlnout must be a value (literal or a value reference) or a list reference"); return error("Argument of stdlnout must be a value (literal or a value reference) or a list reference");
@@ -1096,8 +1090,8 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
string listName; string listName;
List listContents; List listContents;
if (holds_alternative<ListRef>(l.args[0])) { if (holds_alternative<Direct>(l.args[0])) {
listName = get<ListRef>(l.args[0]).listName; listName = get<Direct>(l.args[0]).varName;
} else { } else {
return error("First argument of setlist must be a list reference"); return error("First argument of setlist must be a list reference");
} }
@@ -1126,12 +1120,12 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
return error("Could not find all arguments required for Getlistat inbuilt"); return error("Could not find all arguments required for Getlistat inbuilt");
} }
{ {
ListRef listref; string listref;
int ref; int ref;
Direct var; Direct var;
if (holds_alternative<ListRef>(l.args[0])) { if (holds_alternative<Direct>(l.args[0])) {
listref = get<ListRef>(l.args[0]); listref = get<Direct>(l.args[0]).varName;
} else { } else {
return error("First argument of getlistat must be a list reference"); return error("First argument of getlistat must be a list reference");
} }
@@ -1152,19 +1146,19 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
return error("Third argument of getlistat must be a direct reference"); return error("Third argument of getlistat must be a direct reference");
} }
if (variables.find(listref.listName) != variables.end()) { if (variables.find(listref) != variables.end()) {
if (holds_alternative<List>(variables[listref.listName].val)) { if (holds_alternative<List>(variables[listref].val)) {
if (get<List>(variables[listref.listName].val).val.size() > ref) { if (get<List>(variables[listref].val).val.size() > ref) {
bool existed = variables.count(var.varName) > 0; bool existed = variables.count(var.varName) > 0;
setVal(var.varName, get<List>(variables[listref.listName].val).val[ref]); setVal(var.varName, get<List>(variables[listref].val).val[ref]);
} else { } else {
return error("Index " + to_string(ref) + " out of range of list " + listref.listName, "rangeError"); return error("Index " + to_string(ref) + " out of range of list " + listref, "rangeError");
} }
} else { } else {
return error("Found a normal variable in place of a list"); return error("Variable " + listref + " is not a list");
} }
} else { } else {
return error("Unknown list: " + listref.listName); return error("Unknown list: " + listref);
} }
} }
break; break;
@@ -1227,12 +1221,12 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
return error("Could not find all arguments required for Setlistat inbuilt"); return error("Could not find all arguments required for Setlistat inbuilt");
} }
{ {
ListRef listref; string listref;
int ref; int ref;
Literal value; Literal value;
if (holds_alternative<ListRef>(l.args[0])) { if (holds_alternative<Direct>(l.args[0])) {
listref = get<ListRef>(l.args[0]); listref = get<Direct>(l.args[0]).varName;
} else { } else {
return error("First argument of setlistat must be a list reference"); return error("First argument of setlistat must be a list reference");
} }
@@ -1252,20 +1246,22 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
} else { } else {
return error("Third argument of setlistat must be a direct reference"); return error("Third argument of setlistat must be a direct reference");
} }
if (variables.find(listref.listName) != variables.end()) { if (variables.find(listref) != variables.end()) {
if (holds_alternative<List>(variables[listref.listName].val)) { if (holds_alternative<List>(variables[listref].val)) {
if (get<List>(variables[listref.listName].val).val.size() > ref) { if (get<List>(variables[listref].val).val.size() > ref) {
List tmpList = get<List>(variables[listref.listName].val); List tmpList = get<List>(variables[listref].val);
tmpList.val[ref] = value; tmpList.val[ref] = value;
Literal tmpLit; Literal tmpLit;
tmpLit.val = tmpList; tmpLit.val = tmpList;
setVal(listref.listName, tmpLit); setVal(listref, tmpLit);
} else { } else {
return error("Index " + to_string(ref) + " out of range of list " + listref.listName, "rangeError"); return error("Index " + to_string(ref) + " out of range of list " + listref, "rangeError");
}
} }
} else { } else {
return error("Unknown list: " + listref.listName); return error("Variable " + listref + " is not a list");
}
} else {
return error("Unknown list: " + listref);
} }
} }
break; break;
@@ -1278,11 +1274,11 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
return error("Could not find all arguments required for Listappend inbuilt"); return error("Could not find all arguments required for Listappend inbuilt");
} }
{ {
ListRef listref; string listref;
Literal value; Literal value;
if (holds_alternative<ListRef>(l.args[0])) { if (holds_alternative<Direct>(l.args[0])) {
listref = get<ListRef>(l.args[0]); listref = get<Direct>(l.args[0]).varName;
} else { } else {
return error("First argument of listappend must be a list reference"); return error("First argument of listappend must be a list reference");
} }
@@ -1293,17 +1289,17 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
return error("Second argument of listappend must be a direct reference"); return error("Second argument of listappend must be a direct reference");
} }
if (variables.find(listref.listName) != variables.end()) { if (variables.find(listref) != variables.end()) {
if (!holds_alternative<List>(variables[listref.listName].val)) { if (!holds_alternative<List>(variables[listref].val)) {
return error("Variable " + listref.listName + "is not a list"); return error("Variable " + listref + "is not a list");
} }
List tmpList = get<List>(variables[listref.listName].val); List tmpList = get<List>(variables[listref].val);
tmpList.val.push_back(value); tmpList.val.push_back(value);
Literal tmpLit; Literal tmpLit;
tmpLit.val = tmpList; tmpLit.val = tmpList;
setVal(listref.listName, tmpLit); setVal(listref, tmpLit);
} else { } else {
return error("Unknown list: " + listref.listName); return error("Unknown list: " + listref);
} }
} }
break; break;
@@ -1316,11 +1312,11 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
return error("Could not find all arguments required for Getlistsize inbuilt"); return error("Could not find all arguments required for Getlistsize inbuilt");
} }
{ {
ListRef ref; string ref;
Direct var; Direct var;
if (holds_alternative<ListRef>(l.args[0])) { if (holds_alternative<Direct>(l.args[0])) {
ref = get<ListRef>(l.args[0]); ref = get<Direct>(l.args[0]).varName;
} else { } else {
return error("First argument of getlistsize must be a list reference"); return error("First argument of getlistsize must be a list reference");
} }
@@ -1332,12 +1328,12 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
} }
Literal newLit; Literal newLit;
if (variables.find(ref.listName) != variables.end()) { if (variables.find(ref) != variables.end()) {
newLit.val = int(get<List>(variables[ref.listName].val).val.size()); newLit.val = int(get<List>(variables[ref].val).val.size());
bool existed = variables.count(var.varName) > 0; bool existed = variables.count(var.varName) > 0;
setVal(var.varName, newLit); setVal(var.varName, newLit);
} else { } else {
return error("Couldn't find the list " + ref.listName); return error("Couldn't find the list " + ref);
} }
break; break;
@@ -2125,10 +2121,6 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
if (variables.find(get<Direct>(l.args[0]).varName) != variables.end()) { if (variables.find(get<Direct>(l.args[0]).varName) != variables.end()) {
exists = true; exists = true;
} }
} else if (holds_alternative<ListRef>(l.args[0])) {
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])) { } else if (holds_alternative<Line>(l.args[0])) {
Line line = get<Line>(l.args[0]); Line line = get<Line>(l.args[0]);
if (line.isLabel) { if (line.isLabel) {
@@ -2236,8 +2228,6 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
} }
if (holds_alternative<Literal>(l.args[0])) { if (holds_alternative<Literal>(l.args[0])) {
return get<Literal>(l.args[0]); return get<Literal>(l.args[0]);
} else if (holds_alternative<ListRef>(l.args[0])) {
return variables[get<ListRef>(l.args[0]).listName];
} else { } else {
return error("First argument of return must be a literal value/value reference"); return error("First argument of return must be a literal value/value reference");
} }
@@ -2861,8 +2851,9 @@ vector<Instruction> parser(vector<vector<string>> in) {
break; break;
case Types::ListRef: case Types::ListRef:
{ {
ListRef newLR; cout << "Note: List References are no longer supported and will be removed in future. Please reference lists using a direct reference (&) instead. Converting to a Direct reference." << endl;
newLR.listName = i.substr(1); Direct newLR;
newLR.varName = i.substr(1);
newInst.args.push_back(newLR); newInst.args.push_back(newLR);
} }
break; break;

View File

@@ -1,6 +1,6 @@
# A cool list # A cool list
setlist *favWords "hello" "there" "general" "kenobi" setlist *favWords "hello" "there" "general" "kenobi"
stdlnout *favWords stdlnout $favWords
set &count 0 set &count 0
set &passedThrough true set &passedThrough true