diff --git a/docs/syntax.md b/docs/syntax.md index eff851e..c59d13f 100644 --- a/docs/syntax.md +++ b/docs/syntax.md @@ -110,6 +110,12 @@ Allows you to set a variable to a value. Usage: `set &var $value` +#### gettype + +Gets the type of a variable. Outputs a string which can be "int", "double", "bool", "string", "char". + +Usage: `gettype $value &var` + #### setlist Allows you to initialize a list. diff --git a/src/main.cpp b/src/main.cpp index 604bfe9..4feb7bd 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, + End, Set, Empty, Gettype, Setlist, Getlistat, Setlistat, Getlistsize, Listappend, Listprepend, Getstrcharat, Getstrsize, Stoi, Stod, Tostring, @@ -1608,6 +1608,53 @@ Literal exec(vector in, bool executingFunction) { } else { error("First argument of end must be an int value"); } + break; + case Instructions::Gettype: + if (l.args.size() < 2) { + error("Could not find all arguments required for Gettype inbuilt"); + } + { + Literal val; + + if (holds_alternative(l.args[0])) { + val = get(l.args[0]); + } else { + error("First argument of gettype must be a literal"); + } + + Types type = getLitType(val); + + Direct ref; + + if (holds_alternative(l.args[1])) { + ref = get(l.args[1]); + } else { + error("Second argument of gettype must be a direct reference"); + } + + switch (type) { + case Types::Int: + variables[ref.varName].val = "int"; + break; + case Types::Double: + variables[ref.varName].val = "double"; + break; + case Types::Bool: + variables[ref.varName].val = "bool"; + break; + case Types::String: + variables[ref.varName].val = "string"; + break; + case Types::Char: + variables[ref.varName].val = "char"; + break; + default: + error("Could not get type?? This should never be reached. Please report this issue"); + break; + } + + } + break; /* fun instruction @@ -2065,6 +2112,7 @@ vector parser(vector> in) { else if (i == "not") newInst.inst = Instructions::Not; 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 == "setlist") newInst.inst = Instructions::Setlist; else if (i == "setlistat") newInst.inst = Instructions::Setlistat; else if (i == "getlistat") newInst.inst = Instructions::Getlistat; diff --git a/tests/gettype.grnd b/tests/gettype.grnd new file mode 100644 index 0000000..c3d6409 --- /dev/null +++ b/tests/gettype.grnd @@ -0,0 +1,5 @@ +set &myVar "dingus" + +gettype $myVar &type + +stdlnout $type