Function arguments

This commit is contained in:
2025-10-02 12:41:11 +10:00
parent 2a977707ee
commit 08d3c1545b
10 changed files with 148 additions and 38 deletions

View File

@@ -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;
}
}