Function arguments
This commit is contained in:
@@ -72,10 +72,27 @@ Value execute(Instruction inst) {
|
||||
if (!inst.args.empty()) {
|
||||
const std::string& name = inst.args[0].real;
|
||||
if (data::functions.count(name)) {
|
||||
const auto& func = data::functions.at(name);
|
||||
|
||||
if (func.arg_names.size() != inst.args.size() - 1) {
|
||||
error("Function " + name + " expects " + std::to_string(func.arg_names.size()) + " arguments, but got " + std::to_string(inst.args.size() - 1));
|
||||
return Value();
|
||||
}
|
||||
|
||||
data::pushScope();
|
||||
|
||||
for (size_t i = 0; i < func.arg_names.size(); ++i) {
|
||||
Value arg_val = inst.args[i+1];
|
||||
data::scopes.back()[func.arg_names[i]] = arg_val;
|
||||
}
|
||||
|
||||
Value return_val;
|
||||
for (const auto& body_inst : data::functions.at(name)) {
|
||||
for (const auto& body_inst : func.body) {
|
||||
return_val = execute(body_inst);
|
||||
}
|
||||
|
||||
data::popScope();
|
||||
|
||||
return return_val;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user