support function literal, set builtins to global context. great!

This commit is contained in:
2025-12-20 20:27:36 +08:00
parent b9a98150ae
commit e7ca714a89
19 changed files with 362 additions and 502 deletions

View File

@@ -1,12 +1,14 @@
#pragma once
#include <Value/BaseValue.hpp>
#include <Value/Type.hpp>
#include <Ast/functionParameters.hpp>
#include <atomic>
namespace Fig
{
class Value;
/* complex objects */
struct FunctionStruct
{
@@ -15,6 +17,10 @@ namespace Fig
TypeInfo retType;
Ast::BlockStatement body;
bool isBuiltin = false;
std::function<Value(const std::vector<Value> &)> builtin;
int builtinParamCount = -1;
FunctionStruct(Ast::FunctionParameters _paras, TypeInfo _retType, Ast::BlockStatement _body) :
id(nextId()), // 分配唯一 ID
paras(std::move(_paras)),
@@ -23,8 +29,10 @@ namespace Fig
{
}
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) {}
id(other.id), paras(other.paras), retType(other.retType), body(other.body), isBuiltin(other.isBuiltin), builtin(other.builtin), builtinParamCount(other.builtinParamCount) {}
FunctionStruct &operator=(const FunctionStruct &other) = default;
FunctionStruct(FunctionStruct &&) noexcept = default;
@@ -61,6 +69,8 @@ namespace Fig
data = std::make_unique<FunctionStruct>(
std::move(paras), std::move(ret), std::move(body));
}
Function(std::function<Value(const std::vector<Value> &)> fn, int argc);
bool operator==(const Function &other) const noexcept
{
if (!data || !other.data) return false;