[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

@@ -31,6 +31,44 @@ public interface Error
getErrorMessage() -> String;
}
// Operation interface
public interface Operation
{
// math
Add(T, T) -> T;
Sub(T, T) -> T;
Mul(T, T) -> T;
Div(T, T) -> T;
Mod(T, T) -> T;
Pow(T, T) -> T;
// logic
Neg(T) -> T;
Not(T) -> T;
And(T, T) -> T;
Or(T, T) -> T;
// comparision
Equal(T, T) -> T;
NotEqual(T, T) -> T;
LessThan(T, T) -> T;
LessEqual(T, T) -> T;
GreaterThan(T, T) -> T;
GreaterEqual(T, T) -> T;
Is(T, T) -> T;
// Bit
BitNot(T) -> T;
BitAnd(T, T) -> T;
BitOr(T, T) -> T;
BitXor(T, T) -> T;
ShiftLeft(T, T) -> T;
ShiftRight(T, T) -> T:
}
public struct Any {}
public struct Int {}
public struct Null {}

View File

@@ -28,6 +28,7 @@ namespace Fig::Builtins
Ast::FunctionParameters({}, {}),
std::make_shared<Ast::VarExprAst>(u8"String"),
nullptr)}))},
{u8"Operation", std::make_shared<Object>(InterfaceType(getOperationInterfaceTypeInfo(), {}))},
{u8"Any", std::make_shared<Object>(StructType(ValueType::Any, nullptr, {}, true))},
{u8"Int", std::make_shared<Object>(StructType(ValueType::Int, nullptr, {}, true))},

View File

@@ -7,22 +7,15 @@
#include <Evaluator/Value/value.hpp>
#include <Core/runtimeTime.hpp>
#include <unordered_map>
#include <functional>
#include <vector>
namespace Fig
{
namespace Builtins
{
inline static const TypeInfo &getErrorInterfaceTypeInfo()
{
static const TypeInfo ErrorInterfaceTypeInfo(u8"Error", true);
return ErrorInterfaceTypeInfo;
}
/*
// error's interface like:
interface Error
@@ -33,15 +26,33 @@ namespace Fig
}
*/
inline static const TypeInfo &getErrorInterfaceTypeInfo()
{
static const TypeInfo ErrorInterfaceTypeInfo(u8"Error", true);
return ErrorInterfaceTypeInfo;
}
/*
interface Operation
{
add(..., ...) -> ...;
}
*/
inline static const TypeInfo &getOperationInterfaceTypeInfo()
{
static const TypeInfo OperationInterfaceTypeInfo(u8"Operation", true);
return OperationInterfaceTypeInfo;
}
const std::unordered_map<FString, ObjectPtr> &getBuiltinValues();
using BuiltinFunction = std::function<ObjectPtr(const std::vector<ObjectPtr> &)>;
const std::unordered_map<FString, int> &getBuiltinFunctionArgCounts();
const std::unordered_map<FString, BuiltinFunction> &getBuiltinFunctions();
inline bool isBuiltinFunction(const FString &name)
{
return getBuiltinFunctions().find(name) != getBuiltinFunctions().end();