Return function

This commit is contained in:
2025-08-13 18:31:54 +10:00
parent 4cd4d9080d
commit bb753e97d4

View File

@@ -254,6 +254,13 @@ struct Function {
*/
map<string, Function> functions;
/*
funargs vector
Contains the arguments to be passed to a function
*/
vector<Literal> args;
/*
error function
Takes a string (which is a debug message) and prints it to the console, letting the
@@ -373,7 +380,7 @@ Types getType(string in) {
bool processingFunction = false;
string procFnName = "";
void exec(vector<Instruction> in) {
Literal exec(vector<Instruction> in) {
for (int i = 0; i < in.size(); i++) {
Instruction l = in[i];
if (processingFunction) {
@@ -1478,6 +1485,14 @@ void exec(vector<Instruction> in) {
Exits a function.
*/
case Instructions::Return:
if (l.args.size() < 1) {
error("Could not find all arguments required for Return inbuilt");
}
if (holds_alternative<Literal>(l.args[0])) {
return get<Literal>(l.args[0]);
} else {
error("First argument of return must be a literal value/value reference");
}
break;
/*
endfun instruction
@@ -1501,6 +1516,8 @@ void exec(vector<Instruction> in) {
break;
}
}
Literal retLiteral;
return retLiteral;
}
/*
@@ -1754,6 +1771,11 @@ int main(int argc, char** argv) {
while (getline(file, lns)) {
in += lns += "\n";
}
exec(parser(lexer(in)));
Literal ret = exec(parser(lexer(in)));
if (holds_alternative<int>(ret.val)) {
return get<int>(ret.val);
} else {
return 0;
}
return 0;
}