Functions can return lists

This commit is contained in:
2025-08-30 13:05:28 +10:00
parent f32f76450a
commit d8cc3ff9e0
2 changed files with 46 additions and 8 deletions

View File

@@ -434,7 +434,7 @@ bool isListRef(string in) {
bool isType(string in) {
if (in.size() >= 1 && in[0] == '-') {
string type = in.substr(1);
if (type == "string" || type == "char" || type == "bool" || type == "double" || type == "int") return true;
if (type == "string" || type == "char" || type == "bool" || type == "double" || type == "int" || type == "list") return true;
else return false;
} else return false;
}
@@ -1771,6 +1771,8 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
}
if (holds_alternative<Literal>(l.args[0])) {
return get<Literal>(l.args[0]);
} else if (holds_alternative<ListRef>(l.args[0])) {
return variables[get<ListRef>(l.args[0]).listName];
} else {
error("First argument of return must be a literal value/value reference");
}
@@ -1792,8 +1794,10 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
}
if (holds_alternative<Literal>(l.args[0])) {
fnArgs.push_back(get<Literal>(l.args[0]));
} else if (holds_alternative<ListRef>(l.args[0])) {
fnArgs.push_back(variables[get<ListRef>(l.args[0]).listName]);
} else {
error("First argument of pusharg must be a literal");
error("First argument of pusharg must be a literal or list reference");
}
break;
/*
@@ -1807,7 +1811,9 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
}
FunctionRef ref;
Direct returnRef;
string returnRef;
bool expectList = true;
if (holds_alternative<FunctionRef>(l.args[0])) {
ref = get<FunctionRef>(l.args[0]);
@@ -1816,9 +1822,12 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
}
if (holds_alternative<Direct>(l.args[1])) {
returnRef = get<Direct>(l.args[1]);
returnRef = get<Direct>(l.args[1]).varName;
} else if (holds_alternative<ListRef>(l.args[1])) {
returnRef = get<ListRef>(l.args[1]).listName;
expectList = true;
} else {
error("Second argument of call must be a direct reference");
error("Second argument of call must be a direct reference or list reference");
}
// Check for external function
@@ -1841,7 +1850,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
// Clear arguments and store result
fnArgs.clear();
variables[returnRef.varName] = resultLit;
variables[returnRef] = resultLit;
break;
}
@@ -1885,8 +1894,15 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
fnArgs.clear();
// Now, assign the return value in the current scope.
bool existed = variables.count(returnRef.varName) > 0;
variables[returnRef.varName] = retVal;
if (expectList) {
variables[returnRef] = retVal;
} else {
if (holds_alternative<List>(retVal.val)) {
error("Expecting to output a normal literal to a normal literal");
} else {
variables[returnRef] = retVal;
}
}
}
break;
case Instructions::Use:

View File

@@ -17,3 +17,25 @@ endfun
call !jumpy &tmp
stdlnout "I called a function"
# This function returns a list
fun -list !dingus
stdlnout "Testing lists in functions"
setlist *dingle "heheheha" "hahahahe" "hmmm"
return *dingle
endfun
call !dingus *outlist
getlistsize *outlist &size
set &counter 0
@loopstart
equal $size $counter &cond
if $cond %loopend
getlistat *outlist $counter &tmp
stdlnout $tmp
add 1 $counter &counter
jump %loopstart
@loopend
end 0