forked from ground/ground
gettype instruction
This commit is contained in:
@@ -110,6 +110,12 @@ Allows you to set a variable to a value.
|
|||||||
|
|
||||||
Usage: `set &var $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
|
#### setlist
|
||||||
|
|
||||||
Allows you to initialize a list.
|
Allows you to initialize a list.
|
||||||
|
50
src/main.cpp
50
src/main.cpp
@@ -73,7 +73,7 @@ enum class Instructions {
|
|||||||
Stdout, Stdin, Stdlnout,
|
Stdout, Stdin, Stdlnout,
|
||||||
Add, Subtract, Multiply, Divide,
|
Add, Subtract, Multiply, Divide,
|
||||||
Equal, Inequal, Greater, Lesser, Not,
|
Equal, Inequal, Greater, Lesser, Not,
|
||||||
End, Set, Empty,
|
End, Set, Empty, Gettype,
|
||||||
Setlist, Getlistat, Setlistat, Getlistsize, Listappend, Listprepend,
|
Setlist, Getlistat, Setlistat, Getlistsize, Listappend, Listprepend,
|
||||||
Getstrcharat, Getstrsize,
|
Getstrcharat, Getstrsize,
|
||||||
Stoi, Stod, Tostring,
|
Stoi, Stod, Tostring,
|
||||||
@@ -1608,6 +1608,53 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
|
|||||||
} else {
|
} else {
|
||||||
error("First argument of end must be an int value");
|
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;
|
break;
|
||||||
/*
|
/*
|
||||||
fun instruction
|
fun instruction
|
||||||
@@ -2065,6 +2112,7 @@ vector<Instruction> parser(vector<vector<string>> in) {
|
|||||||
else if (i == "not") newInst.inst = Instructions::Not;
|
else if (i == "not") newInst.inst = Instructions::Not;
|
||||||
else if (i == "end") newInst.inst = Instructions::End;
|
else if (i == "end") newInst.inst = Instructions::End;
|
||||||
else if (i == "set") newInst.inst = Instructions::Set;
|
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 == "setlist") newInst.inst = Instructions::Setlist;
|
||||||
else if (i == "setlistat") newInst.inst = Instructions::Setlistat;
|
else if (i == "setlistat") newInst.inst = Instructions::Setlistat;
|
||||||
else if (i == "getlistat") newInst.inst = Instructions::Getlistat;
|
else if (i == "getlistat") newInst.inst = Instructions::Getlistat;
|
||||||
|
5
tests/gettype.grnd
Normal file
5
tests/gettype.grnd
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
set &myVar "dingus"
|
||||||
|
|
||||||
|
gettype $myVar &type
|
||||||
|
|
||||||
|
stdlnout $type
|
Reference in New Issue
Block a user