gettype instruction

This commit is contained in:
2025-08-30 10:24:31 +10:00
parent 76e36b7ca3
commit 8247ba36e4
3 changed files with 60 additions and 1 deletions

View File

@@ -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.

View File

@@ -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<Instruction> 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<Literal>(l.args[0])) {
val = get<Literal>(l.args[0]);
} else {
error("First argument of gettype must be a literal");
}
Types type = getLitType(val);
Direct ref;
if (holds_alternative<Direct>(l.args[1])) {
ref = get<Direct>(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<Instruction> parser(vector<vector<string>> 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;

5
tests/gettype.grnd Normal file
View File

@@ -0,0 +1,5 @@
set &myVar "dingus"
gettype $myVar &type
stdlnout $type