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; map<string, Function> functions;
/*
funargs vector
Contains the arguments to be passed to a function
*/
vector<Literal> args;
/* /*
error function error function
Takes a string (which is a debug message) and prints it to the console, letting the 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; bool processingFunction = false;
string procFnName = ""; string procFnName = "";
void exec(vector<Instruction> in) { Literal exec(vector<Instruction> in) {
for (int i = 0; i < in.size(); i++) { for (int i = 0; i < in.size(); i++) {
Instruction l = in[i]; Instruction l = in[i];
if (processingFunction) { if (processingFunction) {
@@ -1478,6 +1485,14 @@ void exec(vector<Instruction> in) {
Exits a function. Exits a function.
*/ */
case Instructions::Return: 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; break;
/* /*
endfun instruction endfun instruction
@@ -1501,6 +1516,8 @@ void exec(vector<Instruction> in) {
break; break;
} }
} }
Literal retLiteral;
return retLiteral;
} }
/* /*
@@ -1754,6 +1771,11 @@ int main(int argc, char** argv) {
while (getline(file, lns)) { while (getline(file, lns)) {
in += lns += "\n"; 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; return 0;
} }