forked from ground/ground
Type conversion
This commit is contained in:
126
src/main.cpp
126
src/main.cpp
@@ -56,7 +56,7 @@ enum class Instructions {
|
||||
End, Set, Empty,
|
||||
Setlist, Getlistat, Setlistat, Getlistsize, Listappend, Listprepend,
|
||||
Getstrcharat, Getstrsize,
|
||||
Stoi, Stod, tostring,
|
||||
Stoi, Stod, Tostring,
|
||||
Fun, Endfun, Pusharg, Call
|
||||
};
|
||||
|
||||
@@ -728,6 +728,127 @@ void exec(vector<Instruction> in) {
|
||||
|
||||
break;
|
||||
}
|
||||
/*
|
||||
stoi instruction
|
||||
This function converts a string to an int, and saves it in a variable.
|
||||
If the string cannot be turned into an integer, there is an error.
|
||||
*/
|
||||
case Instructions::Stoi:
|
||||
if (l.args.size() < 2) {
|
||||
error("Could not find all arguments required for Stoi inbuilt");
|
||||
}
|
||||
{
|
||||
string toConv;
|
||||
Direct ref;
|
||||
|
||||
if (holds_alternative<Literal>(l.args[0])) {
|
||||
if (holds_alternative<string>(get<Literal>(l.args[0]).val)) {
|
||||
toConv = get<string>(get<Literal>(l.args[0]).val);
|
||||
} else {
|
||||
error("First argument of stoi must be a string literal");
|
||||
}
|
||||
} else {
|
||||
error("First argument of stoi must be a string literal");
|
||||
}
|
||||
|
||||
if (holds_alternative<Direct>(l.args[1])) {
|
||||
ref = get<Direct>(l.args[1]);
|
||||
} else {
|
||||
error("Second argument of stoi must be a direct reference");
|
||||
}
|
||||
|
||||
if (isInt(toConv)) {
|
||||
Literal newLit;
|
||||
newLit.val = stoi(toConv);
|
||||
variables[ref.varName] = newLit;
|
||||
} else {
|
||||
error("Cannot convert the value " + toConv + " to an int");
|
||||
}
|
||||
}
|
||||
break;
|
||||
/*
|
||||
stod instruction
|
||||
This function converts a string to a decimal, and saves it in a variable.
|
||||
If the string cannot be turned into a decimal, there is an error.
|
||||
*/
|
||||
case Instructions::Stod:
|
||||
if (l.args.size() < 2) {
|
||||
error("Could not find all arguments required for Stod inbuilt");
|
||||
}
|
||||
{
|
||||
string toConv;
|
||||
Direct ref;
|
||||
|
||||
if (holds_alternative<Literal>(l.args[0])) {
|
||||
if (holds_alternative<string>(get<Literal>(l.args[0]).val)) {
|
||||
toConv = get<string>(get<Literal>(l.args[0]).val);
|
||||
} else {
|
||||
error("First argument of stod must be a string literal");
|
||||
}
|
||||
} else {
|
||||
error("First argument of stod must be a string literal");
|
||||
}
|
||||
|
||||
if (holds_alternative<Direct>(l.args[1])) {
|
||||
ref = get<Direct>(l.args[1]);
|
||||
} else {
|
||||
error("Second argument of stod must be a direct reference");
|
||||
}
|
||||
|
||||
if (isDouble(toConv) || isInt(toConv)) {
|
||||
Literal newLit;
|
||||
newLit.val = stod(toConv);
|
||||
variables[ref.varName] = newLit;
|
||||
} else {
|
||||
error("Cannot convert the value " + toConv + " to a decimal");
|
||||
}
|
||||
}
|
||||
break;
|
||||
/*
|
||||
tostring instruction
|
||||
This function converts any type to a string, and saves it in a variable.
|
||||
*/
|
||||
case Instructions::Tostring:
|
||||
if (l.args.size() < 2) {
|
||||
error("Could not find all arguments required for Tostring inbuilt");
|
||||
}
|
||||
{
|
||||
Literal toConv;
|
||||
Direct ref;
|
||||
|
||||
if (holds_alternative<Literal>(l.args[0])) {
|
||||
toConv = get<Literal>(l.args[0]);
|
||||
} else {
|
||||
error("First argument of tostring must be a literal");
|
||||
}
|
||||
|
||||
if (holds_alternative<Direct>(l.args[1])) {
|
||||
ref = get<Direct>(l.args[1]);
|
||||
} else {
|
||||
error("Second argument of tostring must be a direct reference");
|
||||
}
|
||||
|
||||
Literal newLit;
|
||||
if (holds_alternative<int>(toConv.val)) {
|
||||
newLit.val = to_string(get<int>(toConv.val));
|
||||
} else if (holds_alternative<double>(toConv.val)) {
|
||||
newLit.val = to_string(get<double>(toConv.val));
|
||||
} else if (holds_alternative<string>(toConv.val)) {
|
||||
newLit.val = get<string>(toConv.val);
|
||||
} else if (holds_alternative<char>(toConv.val)) {
|
||||
newLit.val = string().append(&get<char>(toConv.val));
|
||||
} else if (holds_alternative<bool>(toConv.val)) {
|
||||
if (get<bool>(toConv.val)) {
|
||||
newLit.val = "true";
|
||||
} else {
|
||||
newLit.val = "false";
|
||||
}
|
||||
}
|
||||
|
||||
variables[ref.varName] = newLit;
|
||||
|
||||
}
|
||||
break;
|
||||
/*
|
||||
stdin instruction
|
||||
This instruction takes input from the standard character input via
|
||||
@@ -1362,6 +1483,9 @@ vector<Instruction> parser(vector<vector<string>> in) {
|
||||
else if (i == "listappend") newInst.inst = Instructions::Listappend;
|
||||
else if (i == "getstrsize") newInst.inst = Instructions::Getstrsize;
|
||||
else if (i == "getstrcharat") newInst.inst = Instructions::Getstrcharat;
|
||||
else if (i == "stoi") newInst.inst = Instructions::Stoi;
|
||||
else if (i == "stod") newInst.inst = Instructions::Stod;
|
||||
else if (i == "tostring") newInst.inst = Instructions::Tostring;
|
||||
else error("Unexpected token: " + i);
|
||||
} else {
|
||||
Types type = getType(i);
|
||||
|
Reference in New Issue
Block a user