From 0eb5670dfdef307a2eb716ecae53e43847044bf2 Mon Sep 17 00:00:00 2001 From: Maxwell Jeffress Date: Sat, 30 Aug 2025 10:40:59 +1000 Subject: [PATCH] Add exists instruction --- docs/syntax.md | 6 ++++++ src/main.cpp | 27 ++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/docs/syntax.md b/docs/syntax.md index c59d13f..78a1963 100644 --- a/docs/syntax.md +++ b/docs/syntax.md @@ -116,6 +116,12 @@ Gets the type of a variable. Outputs a string which can be "int", "double", "boo Usage: `gettype $value &var` +#### exists + +Checks if a variable exists with a direct reference. If the variable exists, outputs true. Otherwise outputs false. + +Usage `exists &var1 &var2` + #### setlist Allows you to initialize a list. diff --git a/src/main.cpp b/src/main.cpp index 4feb7bd..6234ab3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -73,7 +73,7 @@ enum class Instructions { Stdout, Stdin, Stdlnout, Add, Subtract, Multiply, Divide, Equal, Inequal, Greater, Lesser, Not, - End, Set, Empty, Gettype, + End, Set, Empty, Gettype, Exists, Setlist, Getlistat, Setlistat, Getlistsize, Listappend, Listprepend, Getstrcharat, Getstrsize, Stoi, Stod, Tostring, @@ -1655,6 +1655,30 @@ Literal exec(vector in, bool executingFunction) { } + break; + case Instructions::Exists: + if (l.args.size() < 2) { + 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]); + } + + if (variables.find(ref1.varName) != variables.end()) { + variables[ref2.varName].val = true; + } else { + variables[ref2.varName].val = false; + } + } break; /* fun instruction @@ -2113,6 +2137,7 @@ vector parser(vector> in) { else if (i == "end") newInst.inst = Instructions::End; else if (i == "set") newInst.inst = Instructions::Set; else if (i == "gettype") newInst.inst = Instructions::Gettype; + else if (i == "exists") newInst.inst = Instructions::Exists; else if (i == "setlist") newInst.inst = Instructions::Setlist; else if (i == "setlistat") newInst.inst = Instructions::Setlistat; else if (i == "getlistat") newInst.inst = Instructions::Getlistat;