forked from ground/ground
Functions can return lists
This commit is contained in:
32
src/main.cpp
32
src/main.cpp
@@ -434,7 +434,7 @@ bool isListRef(string in) {
|
|||||||
bool isType(string in) {
|
bool isType(string in) {
|
||||||
if (in.size() >= 1 && in[0] == '-') {
|
if (in.size() >= 1 && in[0] == '-') {
|
||||||
string type = in.substr(1);
|
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;
|
||||||
} else return false;
|
} else return false;
|
||||||
}
|
}
|
||||||
@@ -1771,6 +1771,8 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
|
|||||||
}
|
}
|
||||||
if (holds_alternative<Literal>(l.args[0])) {
|
if (holds_alternative<Literal>(l.args[0])) {
|
||||||
return get<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 {
|
} else {
|
||||||
error("First argument of return must be a literal value/value reference");
|
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])) {
|
if (holds_alternative<Literal>(l.args[0])) {
|
||||||
fnArgs.push_back(get<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 {
|
} else {
|
||||||
error("First argument of pusharg must be a literal");
|
error("First argument of pusharg must be a literal or list reference");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
/*
|
/*
|
||||||
@@ -1807,7 +1811,9 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
FunctionRef ref;
|
FunctionRef ref;
|
||||||
Direct returnRef;
|
string returnRef;
|
||||||
|
|
||||||
|
bool expectList = true;
|
||||||
|
|
||||||
if (holds_alternative<FunctionRef>(l.args[0])) {
|
if (holds_alternative<FunctionRef>(l.args[0])) {
|
||||||
ref = get<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])) {
|
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 {
|
} 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
|
// Check for external function
|
||||||
@@ -1841,7 +1850,7 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
|
|||||||
|
|
||||||
// Clear arguments and store result
|
// Clear arguments and store result
|
||||||
fnArgs.clear();
|
fnArgs.clear();
|
||||||
variables[returnRef.varName] = resultLit;
|
variables[returnRef] = resultLit;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -1885,8 +1894,15 @@ Literal exec(vector<Instruction> in, bool executingFunction) {
|
|||||||
fnArgs.clear();
|
fnArgs.clear();
|
||||||
|
|
||||||
// Now, assign the return value in the current scope.
|
// Now, assign the return value in the current scope.
|
||||||
bool existed = variables.count(returnRef.varName) > 0;
|
if (expectList) {
|
||||||
variables[returnRef.varName] = retVal;
|
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;
|
break;
|
||||||
case Instructions::Use:
|
case Instructions::Use:
|
||||||
|
@@ -17,3 +17,25 @@ endfun
|
|||||||
call !jumpy &tmp
|
call !jumpy &tmp
|
||||||
|
|
||||||
stdlnout "I called a function"
|
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
|
||||||
|
Reference in New Issue
Block a user