support closure!!!!!

This commit is contained in:
2025-12-20 20:38:59 +08:00
parent e7ca714a89
commit 56459a5eeb
3 changed files with 31 additions and 17 deletions

View File

@@ -2,6 +2,7 @@
#include <Value/BaseValue.hpp>
#include <Ast/functionParameters.hpp>
#include <context_forward.hpp>
#include <atomic>
@@ -21,18 +22,28 @@ namespace Fig
std::function<Value(const std::vector<Value> &)> builtin;
int builtinParamCount = -1;
FunctionStruct(Ast::FunctionParameters _paras, TypeInfo _retType, Ast::BlockStatement _body) :
std::shared_ptr<Context> closureContext;
FunctionStruct(Ast::FunctionParameters _paras, TypeInfo _retType, Ast::BlockStatement _body, ContextPtr _closureContext) :
id(nextId()), // 分配唯一 ID
paras(std::move(_paras)),
retType(std::move(_retType)),
body(std::move(_body))
body(std::move(_body)),
closureContext(std::move(_closureContext))
{
}
FunctionStruct(std::function<Value(const std::vector<Value> &)> fn, int argc);
FunctionStruct(const FunctionStruct &other) :
id(other.id), paras(other.paras), retType(other.retType), body(other.body), isBuiltin(other.isBuiltin), builtin(other.builtin), builtinParamCount(other.builtinParamCount) {}
id(other.id),
paras(other.paras),
retType(other.retType),
body(other.body),
isBuiltin(other.isBuiltin),
builtin(other.builtin),
builtinParamCount(other.builtinParamCount),
closureContext(other.closureContext) {}
FunctionStruct &operator=(const FunctionStruct &other) = default;
FunctionStruct(FunctionStruct &&) noexcept = default;
@@ -63,11 +74,11 @@ namespace Fig
{
data = std::make_unique<FunctionStruct>(x);
}
Function(Ast::FunctionParameters paras, TypeInfo ret, Ast::BlockStatement body) :
Function(Ast::FunctionParameters paras, TypeInfo ret, Ast::BlockStatement body, ContextPtr closureContext) :
__ValueWrapper(ValueType::Function)
{
data = std::make_unique<FunctionStruct>(
std::move(paras), std::move(ret), std::move(body));
std::move(paras), std::move(ret), std::move(body), std::move(closureContext));
}
Function(std::function<Value(const std::vector<Value> &)> fn, int argc);

View File

@@ -132,7 +132,8 @@ namespace Fig
evaluatedArgs.argv.push_back(defaultVal);
}
// create new context for function call
auto newContext = std::make_shared<Context>(FString(std::format("<Function {}()>", fnName.toBasicString())), currentContext);
auto newContext = std::make_shared<Context>(FString(std::format("<Function {}()>", fnName.toBasicString())),
fnStruct.closureContext);
auto previousContext = currentContext;
currentContext = newContext;
// define parameters in new context
@@ -238,8 +239,8 @@ namespace Fig
return Function(
fn->paras,
ValueType::Any,
body
);
body,
currentContext);
}
else
{
@@ -247,8 +248,8 @@ namespace Fig
return Function(
fn->paras,
ValueType::Any,
body
);
body,
currentContext);
}
}
case AstType::ListExpr: {
@@ -341,7 +342,8 @@ namespace Fig
Value(Function(
fnDef->paras,
TypeInfo(fnDef->retType),
fnDef->body)));
fnDef->body,
currentContext)));
return StatementResult::normal();
};
case AstType::StructSt: {

View File

@@ -1,8 +1,9 @@
func build_adder(x) -> Function
{
return func(a) => a + x;
var x = 1;
func f() {
x = x + 1;
}
var add2 = build_adder(2);
var println = __fstdout_println;
println(add2(1));
f();
var print = __fstdout_println;
print(x);