Exists for lists and lines

This commit is contained in:
2025-08-30 10:50:19 +10:00
parent 0eb5670dfd
commit 8d80416c5c
3 changed files with 47 additions and 11 deletions

View File

@@ -122,6 +122,8 @@ Checks if a variable exists with a direct reference. If the variable exists, out
Usage `exists &var1 &var2` Usage `exists &var1 &var2`
Note: You can also replace &var1 with a list or line reference to check if it also exists
#### setlist #### setlist
Allows you to initialize a list. Allows you to initialize a list.

View File

@@ -542,7 +542,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
newLine.lineNum = (*currentLabels)[ln.label]; newLine.lineNum = (*currentLabels)[ln.label];
l.args[j] = newLine; l.args[j] = newLine;
} else { } else {
error("Could not find label " + ln.label); if (l.inst != Instructions::Exists) error("Could not find label " + ln.label);
} }
} }
} }
@@ -1661,23 +1661,37 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
error("Could not find all arguments required for Exists inbuilt"); error("Could not find all arguments required for Exists inbuilt");
} }
{ {
Direct ref1;
if (holds_alternative<Direct>(l.args[0])) {
ref1 = get<Direct>(l.args[0]);
} else {
error("First argument of exists must be a direct reference");
}
Direct ref2; Direct ref2;
if (holds_alternative<Direct>(l.args[1])) { if (holds_alternative<Direct>(l.args[1])) {
ref2 = get<Direct>(l.args[1]); ref2 = get<Direct>(l.args[1]);
} else {
error("Second argument of exists must be a direct reference");
} }
if (variables.find(ref1.varName) != variables.end()) { bool exists = false;
variables[ref2.varName].val = true; if (holds_alternative<Direct>(l.args[0])) {
} else { if (variables.find(get<Direct>(l.args[0]).varName) != variables.end()) {
variables[ref2.varName].val = false; exists = true;
} }
} else if (holds_alternative<ListRef>(l.args[0])) {
if (lists.find(get<ListRef>(l.args[0]).listName) != lists.end()) {
exists = true;
}
} else if (holds_alternative<Line>(l.args[0])) {
Line line = get<Line>(l.args[0]);
if (line.isLabel) {
if (labels.find(line.label) != labels.end()) {
exists = true;
}
} else {
if (line.lineNum > 0 && line.lineNum <= in.size()) {
exists = true;
}
}
} else {
error("First argument of exists must be a direct, list, or line reference");
}
variables[ref2.varName].val = exists;
} }
break; break;
/* /*

20
tests/exists.grnd Normal file
View File

@@ -0,0 +1,20 @@
set &testVar "dingus"
exists &testVar &exist
stdlnout $exist
setlist *myList "item"
exists *myList &exist
stdlnout $exist
@dingus
exists %dingus &exist
stdlnout $exist
exists &doesNotExist &exist
stdlnout $exist
exists *doesNotExist &exist
stdlnout $exist
exists %doesNotExist &exist
stdlnout $exist