From 8d80416c5ccc46fa9b34b288a4478e77b1611f78 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Sat, 30 Aug 2025 10:50:19 +1000 Subject: [PATCH] Exists for lists and lines --- docs/syntax.md | 2 ++ src/main.cpp | 36 +++++++++++++++++++++++++----------- tests/exists.grnd | 20 ++++++++++++++++++++ 3 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 tests/exists.grnd diff --git a/docs/syntax.md b/docs/syntax.md index 78a1963..5fcbe51 100644 --- a/docs/syntax.md +++ b/docs/syntax.md @@ -122,6 +122,8 @@ Checks if a variable exists with a direct reference. If the variable exists, out Usage `exists &var1 &var2` +Note: You can also replace &var1 with a list or line reference to check if it also exists + #### setlist Allows you to initialize a list. diff --git a/src/main.cpp b/src/main.cpp index 6234ab3..d263c8a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -542,7 +542,7 @@ Literal exec(vector in, bool executingFunction) { newLine.lineNum = (*currentLabels)[ln.label]; l.args[j] = newLine; } 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 in, bool executingFunction) { error("Could not find all arguments required for Exists inbuilt"); } { - Direct ref1; - if (holds_alternative(l.args[0])) { - ref1 = get(l.args[0]); - } else { - error("First argument of exists must be a direct reference"); - } - Direct ref2; if (holds_alternative(l.args[1])) { ref2 = get(l.args[1]); + } else { + error("Second argument of exists must be a direct reference"); } - if (variables.find(ref1.varName) != variables.end()) { - variables[ref2.varName].val = true; + bool exists = false; + if (holds_alternative(l.args[0])) { + if (variables.find(get(l.args[0]).varName) != variables.end()) { + exists = true; + } + } else if (holds_alternative(l.args[0])) { + if (lists.find(get(l.args[0]).listName) != lists.end()) { + exists = true; + } + } else if (holds_alternative(l.args[0])) { + Line line = get(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 { - variables[ref2.varName].val = false; + error("First argument of exists must be a direct, list, or line reference"); } + variables[ref2.varName].val = exists; } break; /* diff --git a/tests/exists.grnd b/tests/exists.grnd new file mode 100644 index 0000000..0e6af6d --- /dev/null +++ b/tests/exists.grnd @@ -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