[Feat] 支持运算符重载!详见文档或 Library/lang/lang.fig中的定义。通过 impl Operation for xxx实现重载

[Impl] 函数参数指定现在也接受一个 exp,逐渐改动其他中...
This commit is contained in:
2026-02-02 16:11:08 +08:00
parent 41bff72d44
commit 01c16dee3f
20 changed files with 698 additions and 355 deletions

View File

@@ -16,6 +16,7 @@ namespace Fig
{
public:
std::size_t id;
FString name;
enum FnType
{
@@ -52,11 +53,13 @@ namespace Fig
new (&body) Ast::BlockStatement();
}
Function(Ast::FunctionParameters _paras,
Function(const FString &_name,
Ast::FunctionParameters _paras,
TypeInfo _retType,
Ast::BlockStatement _body,
ContextPtr _closureContext) :
id(nextId()), // 分配唯一 ID
name(_name),
paras(std::move(_paras)),
retType(std::move(_retType)),
body(std::move(_body)),
@@ -65,25 +68,22 @@ namespace Fig
type = Normal;
}
Function(std::function<std::shared_ptr<Object>(const std::vector<std::shared_ptr<Object>> &)> fn, int argc) :
id(nextId()), type(Builtin), builtin(fn), builtinParamCount(argc)
Function(const FString &_name, std::function<std::shared_ptr<Object>(const std::vector<std::shared_ptr<Object>> &)> fn, int argc) :
id(nextId()), name(_name), type(Builtin), builtin(fn), builtinParamCount(argc)
{
type = Builtin;
}
Function(std::function<std::shared_ptr<Object>(std::shared_ptr<Object>,
Function(const FString &_name, std::function<std::shared_ptr<Object>(std::shared_ptr<Object>,
const std::vector<std::shared_ptr<Object>> &)> fn,
int argc) :
id(nextId()), type(MemberType), mtFn(fn), builtinParamCount(argc)
id(nextId()), name(_name), type(MemberType), mtFn(fn), builtinParamCount(argc)
{
type = MemberType;
}
// ===== Copy / Move =====
Function(const Function &other)
{
copyFrom(other);
}
Function(const Function &other) { copyFrom(other); }
Function &operator=(const Function &other)
{
if (this != &other)
@@ -94,10 +94,7 @@ namespace Fig
return *this;
};
~Function()
{
destroy();
}
~Function() { destroy(); }
// ===== Comparison =====
bool operator==(const Function &other) const noexcept { return id == other.id; }
@@ -125,6 +122,7 @@ namespace Fig
void copyFrom(const Function &other)
{
name = other.name;
type = other.type;
id = nextId(); // 每个复制都生成新的ID
builtinParamCount = other.builtinParamCount;

View File

@@ -1,3 +1,4 @@
#include <Evaluator/Value/interface.hpp>
#include <Evaluator/Value/structType.hpp>
#include <Evaluator/Value/value_forward.hpp>
#include <Evaluator/Value/Type.hpp>
@@ -84,6 +85,11 @@ namespace Fig
return obj->as<StructType>().type;
}
if (t == ValueType::InterfaceType)
{
return obj->as<InterfaceType>().type;
}
if (t == ValueType::StructInstance) return obj->as<StructInstance>().parentType;
return t;
}

View File

@@ -12,6 +12,7 @@
#include <cassert>
#include <iostream>
#include <memory>
#include <unordered_set>
#include <variant>
#include <cmath>
#include <string>
@@ -87,9 +88,8 @@ namespace Fig
Module,
InterfaceType>;
static std::unordered_map<TypeInfo, std::unordered_map<FString, BuiltinTypeMemberFn>, TypeInfoHash> getMemberTypeFunctions()
static std::unordered_map<TypeInfo, std::unordered_map<FString, BuiltinTypeMemberFn>, TypeInfoHash>
getMemberTypeFunctions()
{
static const std::unordered_map<TypeInfo, std::unordered_map<FString, BuiltinTypeMemberFn>, TypeInfoHash>
memberTypeFunctions{
@@ -241,9 +241,8 @@ namespace Fig
return memberTypeFunctions;
}
static std::unordered_map<TypeInfo, std::unordered_map<FString, int>, TypeInfoHash> getMemberTypeFunctionsParas()
static std::unordered_map<TypeInfo, std::unordered_map<FString, int>, TypeInfoHash>
getMemberTypeFunctionsParas()
{
static const std::unordered_map<TypeInfo, std::unordered_map<FString, int>, TypeInfoHash>
memberTypeFunctionsParas{
@@ -425,7 +424,7 @@ namespace Fig
return toString();
}
FString toString() const
FString toString(std::unordered_set<const Object *> &visited) const
{
if (is<ValueType::NullClass>()) return FString(u8"null");
if (is<ValueType::IntClass>()) return FString(std::to_string(as<ValueType::IntClass>()));
@@ -445,13 +444,16 @@ namespace Fig
static_cast<const void *>(&as<StructInstance>())));
if (is<List>())
{
if (visited.contains(this)) { return u8"[...]"; }
visited.insert(this);
FString output(u8"[");
const List &list = as<List>();
bool first_flag = true;
for (auto &ele : list)
{
if (!first_flag) output += u8", ";
output += ele.value->toString();
output += ele.value->toString(visited);
first_flag = false;
}
output += u8"]";
@@ -459,13 +461,16 @@ namespace Fig
}
if (is<Map>())
{
if (visited.contains(this)) { return u8"{...}"; }
visited.insert(this);
FString output(u8"{");
const Map &map = as<Map>();
bool first_flag = true;
for (auto &[key, value] : map)
{
if (!first_flag) output += u8", ";
output += key.value->toString() + FString(u8" : ") + value->toString();
output += key.value->toString(visited) + FString(u8" : ") + value->toString(visited);
first_flag = false;
}
output += u8"}";
@@ -486,6 +491,12 @@ namespace Fig
return FString(u8"<error>");
}
FString toString() const
{
std::unordered_set<const Object *> visited{};
return toString(visited);
}
private:
static std::string
makeTypeErrorMessage(const char *prefix, const char *op, const Object &lhs, const Object &rhs)
@@ -612,7 +623,7 @@ namespace Fig
friend Object operator!(const Object &v)
{
if (!v.is<ValueType::BoolClass>())
throw ValueError(
throw ValueError(
FString(std::format("Logical NOT requires bool: '{}'", v.getTypeInfo().name.toBasicString())));
return Object(!v.as<ValueType::BoolClass>());
}