Basic function calling support

This commit is contained in:
2025-08-18 09:36:35 +10:00
parent e4cc6b2f14
commit 28a9e389fa
2 changed files with 43 additions and 9 deletions

View File

@@ -376,18 +376,20 @@ Types getType(string in) {
return Types::Int; return Types::Int;
} }
bool processingFunction = false;
string procFnName = "";
bool inFunction = false;
// Forward declaration for the call instruction
Literal exec(vector<Instruction> in);
/* /*
exec function exec function
This function takes a list of instructions (see Instruction struct above and parser This function takes a list of instructions (see Instruction struct above and parser
function below) and acts upon their properties. This is the main interpreter function below) and acts upon their properties. This is the main interpreter
function for the program. function for the program.
*/ */
bool processingFunction = false;
string procFnName = "";
bool inFunction = false;
Literal 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];
@@ -1508,6 +1510,10 @@ Literal exec(vector<Instruction> in) {
case Instructions::Endfun: case Instructions::Endfun:
error("No function is being defined. Cannot end function declaration here"); error("No function is being defined. Cannot end function declaration here");
break; break;
/*
pusharg instruction
This instruction makes new arguments avaliable for functions.
*/
case Instructions::Pusharg: case Instructions::Pusharg:
if (l.args.size() < 1) { if (l.args.size() < 1) {
error("Could not find all arguments required for Endfun inbuilt"); error("Could not find all arguments required for Endfun inbuilt");
@@ -1518,9 +1524,33 @@ Literal exec(vector<Instruction> in) {
error("First argument of pusharg must be a literal"); error("First argument of pusharg must be a literal");
} }
break; break;
case Instructions::Local: /*
break; call instruction
This instruction calls a function, and makes the arguments avaliable for it.
*/
case Instructions::Call: case Instructions::Call:
{
if (l.args.size() < 2) {
error("Could not find all arguments required for Call inbuilt");
}
FunctionRef ref;
Direct returnRef;
if (holds_alternative<FunctionRef>(l.args[0])) {
ref = get<FunctionRef>(l.args[0]);
} else {
error("First argument of call must be a function reference");
}
if (holds_alternative<Direct>(l.args[1])) {
returnRef = get<Direct>(l.args[1]);
} else {
error("Second argument of call must be a direct reference");
}
variables[returnRef.varName] = exec(functions[ref.fnName].instructions);
}
break; break;
case Instructions::Use: case Instructions::Use:
break; break;

View File

@@ -1,6 +1,10 @@
fun -int !dingle -string &silly fun -int !dingle -string &silly
stdlnout &silly stdlnout "This is inside the function"
return 10 return 10
endfun endfun
stdlnout "This is outside the function" stdlnout "This is outside the function"
call !dingle &var
stdlnout $var