forked from PuqiAR/Fig-TreeWalker
support closure!!!!!
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <Value/BaseValue.hpp>
|
#include <Value/BaseValue.hpp>
|
||||||
#include <Ast/functionParameters.hpp>
|
#include <Ast/functionParameters.hpp>
|
||||||
|
#include <context_forward.hpp>
|
||||||
|
|
||||||
#include <atomic>
|
#include <atomic>
|
||||||
|
|
||||||
@@ -21,18 +22,28 @@ namespace Fig
|
|||||||
std::function<Value(const std::vector<Value> &)> builtin;
|
std::function<Value(const std::vector<Value> &)> builtin;
|
||||||
int builtinParamCount = -1;
|
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
|
id(nextId()), // 分配唯一 ID
|
||||||
paras(std::move(_paras)),
|
paras(std::move(_paras)),
|
||||||
retType(std::move(_retType)),
|
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(std::function<Value(const std::vector<Value> &)> fn, int argc);
|
||||||
|
|
||||||
FunctionStruct(const FunctionStruct &other) :
|
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 &operator=(const FunctionStruct &other) = default;
|
||||||
FunctionStruct(FunctionStruct &&) noexcept = default;
|
FunctionStruct(FunctionStruct &&) noexcept = default;
|
||||||
@@ -63,11 +74,11 @@ namespace Fig
|
|||||||
{
|
{
|
||||||
data = std::make_unique<FunctionStruct>(x);
|
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)
|
__ValueWrapper(ValueType::Function)
|
||||||
{
|
{
|
||||||
data = std::make_unique<FunctionStruct>(
|
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);
|
Function(std::function<Value(const std::vector<Value> &)> fn, int argc);
|
||||||
|
|
||||||
|
|||||||
@@ -132,7 +132,8 @@ namespace Fig
|
|||||||
evaluatedArgs.argv.push_back(defaultVal);
|
evaluatedArgs.argv.push_back(defaultVal);
|
||||||
}
|
}
|
||||||
// create new context for function call
|
// 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;
|
auto previousContext = currentContext;
|
||||||
currentContext = newContext;
|
currentContext = newContext;
|
||||||
// define parameters in new context
|
// define parameters in new context
|
||||||
@@ -238,8 +239,8 @@ namespace Fig
|
|||||||
return Function(
|
return Function(
|
||||||
fn->paras,
|
fn->paras,
|
||||||
ValueType::Any,
|
ValueType::Any,
|
||||||
body
|
body,
|
||||||
);
|
currentContext);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -247,8 +248,8 @@ namespace Fig
|
|||||||
return Function(
|
return Function(
|
||||||
fn->paras,
|
fn->paras,
|
||||||
ValueType::Any,
|
ValueType::Any,
|
||||||
body
|
body,
|
||||||
);
|
currentContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case AstType::ListExpr: {
|
case AstType::ListExpr: {
|
||||||
@@ -341,7 +342,8 @@ namespace Fig
|
|||||||
Value(Function(
|
Value(Function(
|
||||||
fnDef->paras,
|
fnDef->paras,
|
||||||
TypeInfo(fnDef->retType),
|
TypeInfo(fnDef->retType),
|
||||||
fnDef->body)));
|
fnDef->body,
|
||||||
|
currentContext)));
|
||||||
return StatementResult::normal();
|
return StatementResult::normal();
|
||||||
};
|
};
|
||||||
case AstType::StructSt: {
|
case AstType::StructSt: {
|
||||||
|
|||||||
Reference in New Issue
Block a user