object

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 26, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Copyright 2025 The Hulo Authors. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright 2025 The Hulo Authors. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright 2025 The Hulo Authors. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright 2025 The Hulo Authors. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright 2025 The Hulo Authors. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright 2025 The Hulo Authors. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright 2025 The Hulo Authors. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright 2025 The Hulo Authors. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Copyright 2025 The Hulo Authors. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.

Index

Constants

This section is empty.

Variables

View Source
var (
	// 数值类型约束
	NumericConstraint = NewInterfaceConstraint(numberType)

	// 可比较类型约束
	ComparableConstraint = NewCompositeConstraint(
		NewOperatorConstraint(OpEqual, anyType, boolType),
		NewOperatorConstraint(OpLess, anyType, boolType),
		NewOperatorConstraint(OpGreater, anyType, boolType),
	)

	// 可序列化类型约束
	SerializableConstraint = NewInterfaceConstraint(anyType) // 简化处理

	// 字符串类型约束
	StringConstraint = NewInterfaceConstraint(stringType)

	// 布尔类型约束
	BoolConstraint = NewInterfaceConstraint(boolType)
)

预定义的约束

View Source
var (
	NULL  = &NullValue{}
	TRUE  = &BoolValue{Value: true}
	FALSE = &BoolValue{Value: false}
)
View Source
var GlobalDecoratorRegistry = NewDecoratorRegistry()

GlobalDecoratorRegistry 全局装饰器注册表

View Source
var GlobalTraitRegistry = NewTraitRegistry()

全局trait注册表

Functions

func Entries

func Entries(t Type) (map[string]Type, error)

func ExampleAdvancedConstraintCombination

func ExampleAdvancedConstraintCombination()

示例:高级约束组合

func ExampleCompositeConstraint

func ExampleCompositeConstraint()

示例:复合约束

func ExampleConstrainedGeneric

func ExampleConstrainedGeneric()

示例:使用Constraint的泛型类

func ExampleConstraintIntegration

func ExampleConstraintIntegration()

示例:将Constraint系统集成到泛型系统中

func ExampleEnumUsage

func ExampleEnumUsage()

示例:枚举类型使用

func ExampleGenericTraitConstraintInheritance

func ExampleGenericTraitConstraintInheritance()

示例:泛型trait约束继承

func ExampleServiceTrait

func ExampleServiceTrait()

示例:Service trait

func ExampleStaticMethodsAndInheritance

func ExampleStaticMethodsAndInheritance()

示例:静态方法和继承

func ExampleTraitGenericInheritanceIntegration

func ExampleTraitGenericInheritanceIntegration()

示例:trait、泛型和继承联动

func ExampleUnionConstraint

func ExampleUnionConstraint()

示例:联合约束

func ImplementsTrait

func ImplementsTrait(t Type, traitName string) bool

检查类型是否实现了trait

func Keys

func Keys(t Type) ([]string, error)

Types

type ASTConverter

type ASTConverter struct{}

func (*ASTConverter) ConvertField

func (c *ASTConverter) ConvertField(field *ast.Field) (*Field, error)

ConvertField 转换字段

func (*ASTConverter) ConvertModifiers

func (c *ASTConverter) ConvertModifiers(modifiers []ast.Modifier) []FieldModifier

ConvertModifiers 转换修饰符

func (*ASTConverter) ConvertParameter

func (c *ASTConverter) ConvertParameter(param *ast.Parameter) (*Parameter, error)

ConvertParameter 转换参数

func (*ASTConverter) ConvertType

func (c *ASTConverter) ConvertType(node ast.Node) (Type, error)

ConvertType 将AST节点转换为Type

func (*ASTConverter) ConvertValue

func (c *ASTConverter) ConvertValue(node ast.Node) (Value, error)

ConvertValue 将AST节点转换为Value

type ArrayPattern

type ArrayPattern struct {
	ArrayType Type
	Elements  []PatternMatch
}

ArrayPattern 数组模式

func (*ArrayPattern) Match

func (ap *ArrayPattern) Match(value Value) bool

type ArrayType

type ArrayType struct {
	*ObjectType
	// contains filtered or unexported fields
}

func NewArrayType

func NewArrayType(elementType Type) *ArrayType

func (*ArrayType) AssignableTo

func (a *ArrayType) AssignableTo(t Type) bool

func (*ArrayType) ConvertibleTo

func (a *ArrayType) ConvertibleTo(t Type) bool

func (*ArrayType) ElementType

func (a *ArrayType) ElementType() Type

func (*ArrayType) Implements

func (a *ArrayType) Implements(t Type) bool

type ArrayValue

type ArrayValue struct {
	Elements []Value
	// contains filtered or unexported fields
}

ArrayValue represents an array value.

func NewArrayValue

func NewArrayValue(elementType Type, elements ...Value) *ArrayValue

NewArrayValue creates a new array value with the given elements.

func (*ArrayValue) Append

func (a *ArrayValue) Append(value Value)

Append adds an element to the end of the array.

func (*ArrayValue) Get

func (a *ArrayValue) Get(index int) Value

Get returns the element at the given index.

func (*ArrayValue) Interface

func (a *ArrayValue) Interface() any

func (*ArrayValue) Length

func (a *ArrayValue) Length() int

Length returns the length of the array.

func (*ArrayValue) Set

func (a *ArrayValue) Set(index int, value Value)

Set sets the element at the given index.

func (*ArrayValue) Text

func (a *ArrayValue) Text() string

func (*ArrayValue) Type

func (a *ArrayValue) Type() Type

type BoolType

type BoolType struct {
	*ObjectType
}

func NewBoolType

func NewBoolType() *BoolType

type BoolValue

type BoolValue struct {
	Value bool
}

BoolValue represents the boolean value.

func (*BoolValue) Clone added in v0.3.0

func (b *BoolValue) Clone() *BoolValue

func (*BoolValue) Interface

func (b *BoolValue) Interface() any

func (*BoolValue) Text

func (b *BoolValue) Text() string

func (*BoolValue) Type

func (b *BoolValue) Type() Type

type BuiltinFunction

type BuiltinFunction func(args ...Value) Value

type ClassConstructor

type ClassConstructor struct {
	// contains filtered or unexported fields
}

func NewClassConstructor

func NewClassConstructor(name string, clsType *ClassType, signature []Type, isNamed bool) *ClassConstructor

NewClassConstructor 创建新的构造函数

func NewDefaultConstructor

func NewDefaultConstructor(clsType *ClassType) *ClassConstructor

NewDefaultConstructor 创建默认构造函数

func (*ClassConstructor) Call

func (cc *ClassConstructor) Call(args ...Value) Value

func (*ClassConstructor) In

func (cc *ClassConstructor) In(i int) Type

func (*ClassConstructor) IsNamed

func (cc *ClassConstructor) IsNamed() bool

func (*ClassConstructor) Match

func (cc *ClassConstructor) Match(args []Value) bool

func (*ClassConstructor) Name

func (cc *ClassConstructor) Name() string

func (*ClassConstructor) NumIn

func (cc *ClassConstructor) NumIn() int

func (*ClassConstructor) Signature

func (cc *ClassConstructor) Signature() []Type

type ClassInstance

type ClassInstance struct {
	// contains filtered or unexported fields
}

func (*ClassInstance) CallMethod

func (ci *ClassInstance) CallMethod(name string, args []Value, namedArgs map[string]Value) (Value, error)

CallMethod 调用方法

func (*ClassInstance) GetField

func (ci *ClassInstance) GetField(name string) Value

func (*ClassInstance) GetMethod

func (ci *ClassInstance) GetMethod(name string) Method

GetMethod 获取方法,支持继承和方法重写

func (*ClassInstance) Interface

func (ci *ClassInstance) Interface() any

func (*ClassInstance) SetField

func (ci *ClassInstance) SetField(name string, value Value)

func (*ClassInstance) Text

func (ci *ClassInstance) Text() string

func (*ClassInstance) Type

func (ci *ClassInstance) Type() Type

type ClassType

type ClassType struct {
	*ObjectType
	*GenericBase // 嵌入泛型基础结构
	// contains filtered or unexported fields
}

func ConvertClassDecl

func ConvertClassDecl(decl *ast.ClassDecl) *ClassType

func NewClassType

func NewClassType(name string) *ClassType

func NewGenericClass

func NewGenericClass(name string, typeParams []string, constraints map[string]Constraint) *ClassType

func (*ClassType) AddCompositeConstraint

func (ct *ClassType) AddCompositeConstraint(typeParam string, constraints ...Constraint)

AddCompositeConstraint 添加复合约束(便捷方法)

func (*ClassType) AddConstraint

func (ct *ClassType) AddConstraint(typeParam string, constraint Constraint)

AddConstraint 添加类型参数约束

func (*ClassType) AddConstructor

func (ct *ClassType) AddConstructor(ctor Constructor)

AddConstructor 添加构造函数

func (*ClassType) AddField

func (ct *ClassType) AddField(name string, filed *Field)

func (*ClassType) AddInterface

func (ct *ClassType) AddInterface(iface *TraitType)

AddInterface 添加实现的接口

func (*ClassType) AddOperator

func (ct *ClassType) AddOperator(op Operator, fn *FunctionType)

func (*ClassType) AddOperatorConstraint

func (ct *ClassType) AddOperatorConstraint(typeParam string, op Operator, rightType, returnType Type)

AddOperatorConstraint 添加运算符约束(便捷方法)

func (*ClassType) AddStaticField

func (ct *ClassType) AddStaticField(name string, field *Field)

AddStaticField 添加静态字段

func (*ClassType) AddStaticMethod

func (ct *ClassType) AddStaticMethod(name string, method *FunctionType)

AddStaticMethod 添加静态方法

func (*ClassType) AddTraitConstraint

func (ct *ClassType) AddTraitConstraint(typeParam string, trait *TraitType)

AddTraitConstraint 为类型参数添加trait约束

func (*ClassType) AddTypeConstraint

func (ct *ClassType) AddTypeConstraint(typeParam string, targetType Type)

AddTypeConstraint 添加类型约束(便捷方法)

func (*ClassType) AddUnionConstraint

func (ct *ClassType) AddUnionConstraint(typeParam string, constraints ...Constraint)

AddUnionConstraint 添加联合约束(便捷方法)

func (*ClassType) CheckConstraints

func (ct *ClassType) CheckConstraints(typeArgs []Type) error

CheckConstraints 检查类型参数是否满足所有约束

func (*ClassType) CheckTraitConstraints

func (ct *ClassType) CheckTraitConstraints(typeArgs []Type) error

CheckTraitConstraints 检查类型参数是否满足trait约束

func (*ClassType) FieldByName

func (ct *ClassType) FieldByName(name string) Type

FieldByName 重写字段查找,支持继承

func (*ClassType) GetConstraint

func (ct *ClassType) GetConstraint(typeParam string) Constraint

GetConstraint 获取类型参数的约束

func (*ClassType) GetConstructorByName

func (ct *ClassType) GetConstructorByName(name string) Constructor

GetConstructorByName 根据名称获取命名构造函数

func (*ClassType) GetConstructors

func (ct *ClassType) GetConstructors() []Constructor

GetConstructors 获取所有构造函数

func (*ClassType) GetInterfaces

func (ct *ClassType) GetInterfaces() []*TraitType

GetInterfaces 获取所有实现的接口

func (*ClassType) GetOverriddenMethod

func (ct *ClassType) GetOverriddenMethod(name string) *FunctionType

GetOverriddenMethod 获取重写的方法

func (*ClassType) GetParent

func (ct *ClassType) GetParent() *ClassType

GetParent 获取父类

func (*ClassType) GetStaticField

func (ct *ClassType) GetStaticField(name string) *Field

GetStaticField 获取静态字段

func (*ClassType) GetStaticMethod

func (ct *ClassType) GetStaticMethod(name string) *FunctionType

GetStaticMethod 获取静态方法

func (*ClassType) GetStaticValue

func (ct *ClassType) GetStaticValue(name string) Value

GetStaticValue 获取静态字段的值

func (*ClassType) GetTraitConstraints

func (ct *ClassType) GetTraitConstraints(typeParam string) []*TraitType

GetTraitConstraints 获取类型参数的trait约束

func (*ClassType) HasConstraint

func (ct *ClassType) HasConstraint(typeParam string) bool

HasConstraint 检查类型参数是否有约束

func (*ClassType) Implements

func (ct *ClassType) Implements(t Type) bool

Implements 实现Type接口的Implements方法

func (*ClassType) ImplementsTrait

func (ct *ClassType) ImplementsTrait(iface *TraitType) bool

ImplementsTrait 检查是否实现了指定trait

func (*ClassType) Instantiate

func (ct *ClassType) Instantiate(typeArgs []Type) (*ClassType, error)

func (*ClassType) InstantiateWithTraitConstraints

func (ct *ClassType) InstantiateWithTraitConstraints(typeArgs []Type) (*ClassType, error)

重写Instantiate方法以支持trait约束检查

func (*ClassType) IsSubclassOf

func (ct *ClassType) IsSubclassOf(parentType *ClassType) bool

IsSubclassOf 检查是否为指定类的子类

func (*ClassType) MethodByName

func (ct *ClassType) MethodByName(name string) Method

MethodByName 重写方法查找,支持继承和方法重写

func (*ClassType) New

func (ct *ClassType) New(values ...Value) Value

func (*ClassType) OverrideMethod

func (ct *ClassType) OverrideMethod(name string, method *FunctionType)

OverrideMethod 重写方法

func (*ClassType) SetParent

func (ct *ClassType) SetParent(parent *ClassType)

SetParent 设置父类

func (*ClassType) SetStaticValue

func (ct *ClassType) SetStaticValue(name string, value Value)

SetStaticValue 设置静态字段的值

type CompositeConstraint

type CompositeConstraint struct {
	// contains filtered or unexported fields
}

func NewCompositeConstraint

func NewCompositeConstraint(constraints ...Constraint) *CompositeConstraint

func TypesToCompositeConstraint

func TypesToCompositeConstraint(types ...Type) *CompositeConstraint

从多个Type创建CompositeConstraint的辅助函数

func (*CompositeConstraint) SatisfiedBy

func (cc *CompositeConstraint) SatisfiedBy(t Type) bool

func (*CompositeConstraint) String

func (cc *CompositeConstraint) String() string

type ConstrainedGenericClass

type ConstrainedGenericClass struct {
	*ClassType
	// contains filtered or unexported fields
}

增强的泛型类,支持Constraint

func NewConstrainedGenericClass

func NewConstrainedGenericClass(name string, typeParams []string, constraints map[string]Constraint) *ConstrainedGenericClass

func (*ConstrainedGenericClass) Instantiate

func (cgc *ConstrainedGenericClass) Instantiate(typeArgs []Type) (*ClassType, error)

type Constraint

type Constraint interface {
	SatisfiedBy(Type) bool
	String() string
}

func TypeToConstraint

func TypeToConstraint(t Type) Constraint

从Type创建Constraint的辅助函数

type Constructor

type Constructor interface {
	Function
	Name() string
	IsNamed() bool // 是否为命名构造函数
}

type ConstructorPattern

type ConstructorPattern struct {
	ConstructorName string
	ConstructorType Type
	Arguments       []PatternMatch
}

ConstructorPattern 构造函数模式

func (*ConstructorPattern) Match

func (cp *ConstructorPattern) Match(value Value) bool

type Converter

type Converter interface {
	ConvertType(node ast.Node) (Type, error)
	ConvertValue(node ast.Node) (Value, error)
	ConvertModifiers(modifiers []ast.Modifier) []FieldModifier
	ConvertField(field *ast.Field) (*Field, error)
	ConvertParameter(param *ast.Parameter) (*Parameter, error)
}

type DecoratorProcessor

type DecoratorProcessor interface {
	Process(target any, args []Value) (any, error)
}

DecoratorProcessor 装饰器处理器接口

type DecoratorRegistry

type DecoratorRegistry struct {
	// contains filtered or unexported fields
}

装饰器注册表

func NewDecoratorRegistry

func NewDecoratorRegistry() *DecoratorRegistry

NewDecoratorRegistry 创建装饰器注册表

func (*DecoratorRegistry) GetDecorator

func (dr *DecoratorRegistry) GetDecorator(name string) *DecoratorType

GetDecorator 获取装饰器

func (*DecoratorRegistry) RegisterDecorator

func (dr *DecoratorRegistry) RegisterDecorator(decorator *DecoratorType)

RegisterDecorator 注册装饰器

type DecoratorTarget

type DecoratorTarget int

装饰器相关类型定义

const (
	DecoratorTargetClass DecoratorTarget = iota
	DecoratorTargetMethod
	DecoratorTargetField
	DecoratorTargetFunction
	DecoratorTargetParameter
	DecoratorTargetEnum
	DecoratorTargetTrait
)

type DecoratorType

type DecoratorType struct {
	*ObjectType
	// contains filtered or unexported fields
}

DecoratorType 装饰器类型

func NewDecoratorType

func NewDecoratorType(name string, target DecoratorTarget, processor DecoratorProcessor) *DecoratorType

NewDecoratorType 创建新的装饰器类型

func (*DecoratorType) Process

func (d *DecoratorType) Process(target any, args []Value) (any, error)

Process 处理装饰器

func (*DecoratorType) Target

func (d *DecoratorType) Target() DecoratorTarget

Target 获取装饰器目标类型

type DefaultTypeInference

type DefaultTypeInference struct {
	// contains filtered or unexported fields
}

DefaultTypeInference 默认类型推断实现

func (*DefaultTypeInference) CheckConstraintSatisfaction

func (ti *DefaultTypeInference) CheckConstraintSatisfaction(t Type, constraint Constraint) bool

CheckConstraintSatisfaction 检查约束满足性

func (*DefaultTypeInference) InferBidirectional

func (ti *DefaultTypeInference) InferBidirectional(target Type, source Type) (map[string]Type, error)

InferBidirectional 双向类型推断

func (*DefaultTypeInference) InferFromAssignment

func (ti *DefaultTypeInference) InferFromAssignment(targetType Type, sourceType Type) (map[string]Type, error)

InferFromAssignment 从赋值推断类型

func (*DefaultTypeInference) InferFromBinaryOp

func (ti *DefaultTypeInference) InferFromBinaryOp(op Operator, left, right Type) (map[string]Type, error)

InferFromBinaryOp 从二元操作推断类型

func (*DefaultTypeInference) InferFromCall

func (ti *DefaultTypeInference) InferFromCall(funcType Type, args []Type) (map[string]Type, error)

InferFromCall 从函数调用推断类型

func (*DefaultTypeInference) InferFromConstraints

func (ti *DefaultTypeInference) InferFromConstraints(constraints map[string]Constraint) (map[string]Type, error)

InferFromConstraints 从约束推断类型

func (*DefaultTypeInference) InferFromContext

func (ti *DefaultTypeInference) InferFromContext(context *TypeContext) (map[string]Type, error)

InferFromContext 从上下文推断类型

func (*DefaultTypeInference) InferFromExpression

func (ti *DefaultTypeInference) InferFromExpression(expr *ExpressionContext) map[string]Type

InferFromExpression 从表达式推断类型(增强版)

func (*DefaultTypeInference) InferFromIndexOp

func (ti *DefaultTypeInference) InferFromIndexOp(container, index Type) (map[string]Type, error)

InferFromIndexOp 从索引操作推断类型

func (*DefaultTypeInference) InferFromLiteral

func (ti *DefaultTypeInference) InferFromLiteral(literal Value) Type

InferFromLiteral 从字面量推断类型

func (*DefaultTypeInference) InferFromOverload

func (ti *DefaultTypeInference) InferFromOverload(overloads []*FunctionType, args []Value) (map[string]Type, error)

InferFromOverload 从重载函数推断类型

func (*DefaultTypeInference) InferFromPattern

func (ti *DefaultTypeInference) InferFromPattern(pattern PatternMatch) (map[string]Type, error)

InferFromPattern 从模式匹配推断类型

func (*DefaultTypeInference) InferFromUnaryOp

func (ti *DefaultTypeInference) InferFromUnaryOp(op Operator, operand Type) (map[string]Type, error)

InferFromUnaryOp 从一元操作推断类型

func (*DefaultTypeInference) InferFromUsage

func (ti *DefaultTypeInference) InferFromUsage(usage *TypeUsage) (map[string]Type, error)

InferFromUsage 从使用场景推断类型

func (*DefaultTypeInference) InferRecursive

func (ti *DefaultTypeInference) InferRecursive(types []Type) (map[string]Type, error)

InferRecursive 递归类型推断

func (*DefaultTypeInference) InferTypeArgs

func (ti *DefaultTypeInference) InferTypeArgs(concreteType Type, genericType Type) ([]Type, error)

InferTypeArgs 从具体类型推断泛型类型参数

func (*DefaultTypeInference) InferWithContext

func (ti *DefaultTypeInference) InferWithContext(target Type, context *TypeContext) (map[string]Type, error)

InferWithContext 带上下文的类型推断

func (*DefaultTypeInference) PropagateConstraints

func (ti *DefaultTypeInference) PropagateConstraints(constraints map[string]Constraint, unifier map[string]Type) (map[string]Constraint, error)

PropagateConstraints 约束传播

func (*DefaultTypeInference) SolveConstraints

func (ti *DefaultTypeInference) SolveConstraints(constraints map[string]Constraint) (map[string]Type, error)

SolveConstraints 求解约束系统

func (*DefaultTypeInference) SubstituteTypes

func (ti *DefaultTypeInference) SubstituteTypes(t Type, substitutions map[string]Type) Type

SubstituteTypes 类型替换

func (*DefaultTypeInference) UnifyTypes

func (ti *DefaultTypeInference) UnifyTypes(t1, t2 Type) (map[string]Type, error)

UnifyTypes 类型统一算法

type DefaultTypeUtils

type DefaultTypeUtils struct{}

DefaultTypeUtils 默认类型工具实现

func (*DefaultTypeUtils) DeepMutable

func (tu *DefaultTypeUtils) DeepMutable(t Type) (Type, error)

DeepMutable<T> - 深度可变

func (*DefaultTypeUtils) DeepPartial

func (tu *DefaultTypeUtils) DeepPartial(t Type) (Type, error)

DeepPartial<T> - 深度可选

func (*DefaultTypeUtils) DeepReadonly

func (tu *DefaultTypeUtils) DeepReadonly(t Type) (Type, error)

DeepReadonly<T> - 深度只读

func (*DefaultTypeUtils) DeepRequired

func (tu *DefaultTypeUtils) DeepRequired(t Type) (Type, error)

DeepRequired<T> - 深度必需

func (*DefaultTypeUtils) Entries

func (tu *DefaultTypeUtils) Entries(t Type) (map[string]Type, error)

Entries<T> - 获取对象类型的键值对

func (*DefaultTypeUtils) Exclude

func (tu *DefaultTypeUtils) Exclude(unionType, excludeType Type) (Type, error)

Exclude<T, U> - 从联合类型中排除指定类型

func (*DefaultTypeUtils) Extract

func (tu *DefaultTypeUtils) Extract(unionType, extractType Type) (Type, error)

Extract<T, U> - 从联合类型中提取指定类型

func (*DefaultTypeUtils) Filter

func (tu *DefaultTypeUtils) Filter(t Type, predicate func(Type) bool) (Type, error)

Filter<T, P> - 过滤类型

func (*DefaultTypeUtils) If

func (tu *DefaultTypeUtils) If(condition bool, trueType, falseType Type) Type

If<C, T, F> - 条件类型

func (*DefaultTypeUtils) Infer

func (tu *DefaultTypeUtils) Infer(template Type, constraints map[string]Type) (Type, error)

Infer<T, C> - 类型推断

func (*DefaultTypeUtils) Intersect

func (tu *DefaultTypeUtils) Intersect(t1, t2 Type) (Type, error)

Intersect<T1, T2> - 类型交集

func (*DefaultTypeUtils) Keys

func (tu *DefaultTypeUtils) Keys(t Type) ([]string, error)

Keys<T> - 获取对象类型的键

func (*DefaultTypeUtils) Map

func (tu *DefaultTypeUtils) Map(t Type, mapper func(Type) Type) (Type, error)

Map<T, F> - 映射类型

func (*DefaultTypeUtils) Merge

func (tu *DefaultTypeUtils) Merge(t1, t2 Type) (Type, error)

Merge<T1, T2> - 合并两个对象类型

func (*DefaultTypeUtils) Mutable

func (tu *DefaultTypeUtils) Mutable(t Type) (Type, error)

Mutable<T> - 使所有字段变为可变

func (*DefaultTypeUtils) NonNullable

func (tu *DefaultTypeUtils) NonNullable(t Type) (Type, error)

NonNullable<T> - 排除 null 和 undefined

func (*DefaultTypeUtils) Omit

func (tu *DefaultTypeUtils) Omit(t Type, keys ...string) (Type, error)

Omit<T, K> - 从对象类型中排除指定字段

func (*DefaultTypeUtils) Optional

func (tu *DefaultTypeUtils) Optional(t Type) Type

Optional<T> - 使类型变为可选(T | undefined)

func (*DefaultTypeUtils) Partial

func (tu *DefaultTypeUtils) Partial(t Type) (Type, error)

Partial<T> - 使所有字段变为可选

func (*DefaultTypeUtils) Pick

func (tu *DefaultTypeUtils) Pick(t Type, keys ...string) (Type, error)

Pick<T, K> - 从对象类型中选择指定字段

func (*DefaultTypeUtils) Readonly

func (tu *DefaultTypeUtils) Readonly(t Type) (Type, error)

Readonly<T> - 使所有字段变为只读

func (*DefaultTypeUtils) Record

func (tu *DefaultTypeUtils) Record(keyType, valueType Type) Type

Record<K, V> - 创建键值对类型

func (*DefaultTypeUtils) Required

func (tu *DefaultTypeUtils) Required(t Type) (Type, error)

Required<T> - 使所有字段变为必需

func (*DefaultTypeUtils) Union

func (tu *DefaultTypeUtils) Union(types ...Type) Type

Union - 创建联合类型

func (*DefaultTypeUtils) Values

func (tu *DefaultTypeUtils) Values(t Type) ([]Type, error)

Values<T> - 获取对象类型的值

type EnumType

type EnumType struct {
	*ObjectType
	// contains filtered or unexported fields
}

EnumType 表示枚举类型

func NewADTEnum

func NewADTEnum(name string, variants map[string]map[string]Type) *EnumType

创建Rust风格的代数数据类型枚举

func NewAssociatedEnum

func NewAssociatedEnum(name string, fields map[string]*Field, valueMap map[string]Value) *EnumType

创建Dart风格的关联值枚举

func NewEnumType

func NewEnumType(name string) *EnumType

func NewExplicitNumericEnum

func NewExplicitNumericEnum(name string, valueMap map[string]int) *EnumType

创建显式数字枚举

func NewMixedEnum

func NewMixedEnum(name string, valueMap map[string]Value) *EnumType

创建混合类型枚举

func NewNumericEnum

func NewNumericEnum(name string, valueNames []string) *EnumType

创建标准数字枚举

func NewStringEnum

func NewStringEnum(name string, valueMap map[string]string) *EnumType

创建字符串枚举

func (*EnumType) AddField

func (e *EnumType) AddField(name string, field *Field)

AddField 添加关联字段(Dart风格)

func (*EnumType) AddVariant

func (e *EnumType) AddVariant(variant *EnumVariant)

AddVariant 添加枚举变体

func (*EnumType) AssignableTo

func (e *EnumType) AssignableTo(u Type) bool

func (*EnumType) ConvertibleTo

func (e *EnumType) ConvertibleTo(u Type) bool

func (*EnumType) GetValue

func (e *EnumType) GetValue(name string) *EnumVariant

GetValue 获取枚举变体(别名,保持向后兼容性)

func (*EnumType) GetValues

func (e *EnumType) GetValues() []*EnumVariant

GetValues 获取所有枚举变体(别名,保持向后兼容性)

func (*EnumType) GetVariant

func (e *EnumType) GetVariant(name string) *EnumVariant

GetVariant 获取枚举变体

func (*EnumType) GetVariants

func (e *EnumType) GetVariants() []*EnumVariant

GetVariants 获取所有枚举变体

func (*EnumType) Implements

func (e *EnumType) Implements(u Type) bool

func (*EnumType) IsADT

func (e *EnumType) IsADT() bool

IsADT 是否为代数数据类型

func (*EnumType) Kind

func (e *EnumType) Kind() ObjKind

func (*EnumType) Name

func (e *EnumType) Name() string

func (*EnumType) SetADT

func (e *EnumType) SetADT(discriminant string)

SetADT 设置为代数数据类型

func (*EnumType) Text

func (e *EnumType) Text() string

type EnumVariant

type EnumVariant struct {
	// contains filtered or unexported fields
}

EnumVariant 表示枚举变体

func NewEnumVariant

func NewEnumVariant(name string, value Value) *EnumVariant

func (*EnumVariant) AddADTFieldType

func (ev *EnumVariant) AddADTFieldType(name string, fieldType Type)

AddADTFieldType 添加ADT字段类型(Rust风格)

func (*EnumVariant) GetADTField

func (ev *EnumVariant) GetADTField(name string) Value

GetADTField 获取ADT字段值(Rust风格)

func (*EnumVariant) GetField

func (ev *EnumVariant) GetField(name string) Value

GetField 获取关联字段值(Dart风格)

func (*EnumVariant) Interface

func (ev *EnumVariant) Interface() any

func (*EnumVariant) Name

func (ev *EnumVariant) Name() string

func (*EnumVariant) SetADTField

func (ev *EnumVariant) SetADTField(name string, value Value)

SetADTField 设置ADT字段值(Rust风格)

func (*EnumVariant) SetField

func (ev *EnumVariant) SetField(name string, value Value)

SetField 设置关联字段值(Dart风格)

func (*EnumVariant) Text

func (ev *EnumVariant) Text() string

func (*EnumVariant) Type

func (ev *EnumVariant) Type() Type

func (*EnumVariant) Value

func (ev *EnumVariant) Value() Value

type ErrorType

type ErrorType struct {
	*ObjectType
}

func NewErrorType

func NewErrorType() *ErrorType

type ErrorValue

type ErrorValue struct {
	Value string
}

ErrorValue represents the error value.

func (*ErrorValue) Interface

func (e *ErrorValue) Interface() any

func (*ErrorValue) Text

func (e *ErrorValue) Text() string

func (*ErrorValue) Type

func (e *ErrorValue) Type() Type

type ExpressionContext

type ExpressionContext struct {
	Expression string
	Type       Type
}

ExpressionContext 表达式上下文

type Field

type Field struct {
	// contains filtered or unexported fields
}

func (*Field) AddModifier

func (f *Field) AddModifier(mod FieldModifier) *Field

func (*Field) GetDefaultValue

func (f *Field) GetDefaultValue() Value

GetDefaultValue 获取字段的默认值

func (*Field) HasDefaultValue

func (f *Field) HasDefaultValue() bool

HasDefaultValue 检查字段是否有默认值

func (*Field) HasModifier

func (f *Field) HasModifier(mod FieldModifier) bool

func (*Field) IsRequired

func (f *Field) IsRequired() bool

func (*Field) SetDefaultValue

func (f *Field) SetDefaultValue(value Value)

SetDefaultValue 设置字段的默认值

type FieldModifier

type FieldModifier int
const (
	FieldModifierNone     FieldModifier = iota // none
	FieldModifierRequired                      // required
	FieldModifierOptional                      // ?
	FieldModifierReadonly                      // readonly
	FieldModifierStatic                        // static
	FieldModifierPub                           // pub
)

func (FieldModifier) String

func (i FieldModifier) String() string

type Function

type Function interface {
	Signature() []Type
	Call(args ...Value) Value
	Match(args []Value) bool

	// NumIn returns a function type's input parameter count.
	// It panics if the type's Kind is not Func.
	NumIn() int

	// In returns the type of a function type's i'th input parameter.
	// It panics if the type's Kind is not Func.
	// It panics if i is not in the range [0, NumIn()).
	In(i int) Type
}

type FunctionBuilder added in v0.3.0

type FunctionBuilder struct {
	// contains filtered or unexported fields
}

FunctionBuilder 函数构建器

func NewFunctionBuilder added in v0.3.0

func NewFunctionBuilder(name string) *FunctionBuilder

NewFunctionBuilder 创建一个新的函数构建器

func (*FunctionBuilder) Build added in v0.3.0

func (fb *FunctionBuilder) Build() *FunctionType

Build 构建FunctionType

func (*FunctionBuilder) WithBody added in v0.3.0

func (fb *FunctionBuilder) WithBody(body *ast.FuncDecl) *FunctionBuilder

WithBody 设置函数体(用于非内置函数)

func (*FunctionBuilder) WithBuiltin added in v0.3.0

func (fb *FunctionBuilder) WithBuiltin(builtin BuiltinFunction) *FunctionBuilder

WithBuiltin 设置内置函数实现

func (*FunctionBuilder) WithOptionalParameter added in v0.3.0

func (fb *FunctionBuilder) WithOptionalParameter(name string, typ Type, defaultValue Value) *FunctionBuilder

WithOptionalParameter 添加可选参数

func (*FunctionBuilder) WithParameter added in v0.3.0

func (fb *FunctionBuilder) WithParameter(name string, typ Type) *FunctionBuilder

WithParameter 添加位置参数

func (*FunctionBuilder) WithReturnType added in v0.3.0

func (fb *FunctionBuilder) WithReturnType(returnType Type) *FunctionBuilder

WithReturnType 设置返回类型

func (*FunctionBuilder) WithVariadicParameter added in v0.3.0

func (fb *FunctionBuilder) WithVariadicParameter(name string, typ Type) *FunctionBuilder

WithVariadicParameter 设置可变参数

type FunctionCallContext

type FunctionCallContext struct {
	FunctionName string
	Arguments    []Type
	ReturnType   Type
}

FunctionCallContext 函数调用上下文

type FunctionSignature

type FunctionSignature struct {
	// contains filtered or unexported fields
}

type FunctionType

type FunctionType struct {
	*GenericBase // 嵌入泛型基础结构
	// contains filtered or unexported fields
}

func NewGenericFunction

func NewGenericFunction(name string, typeParams []string, constraints map[string]Constraint) *FunctionType

func (*FunctionType) AppendSignatures added in v0.3.0

func (ft *FunctionType) AppendSignatures(sigs []*FunctionSignature)

func (*FunctionType) AssignableTo

func (ft *FunctionType) AssignableTo(t Type) bool

func (*FunctionType) Call

func (ft *FunctionType) Call(args []Value, namedArgs map[string]Value, evaluator ...core.FunctionEvaluator) (Value, error)

func (*FunctionType) ConvertibleTo

func (ft *FunctionType) ConvertibleTo(t Type) bool

func (*FunctionType) Implements

func (ft *FunctionType) Implements(t Type) bool

func (*FunctionType) Kind

func (ft *FunctionType) Kind() ObjKind

func (*FunctionType) MatchSignature

func (ft *FunctionType) MatchSignature(args []Value, namedArgs map[string]Value) (*FunctionSignature, error)

func (*FunctionType) Name

func (ft *FunctionType) Name() string

func (*FunctionType) Signatures added in v0.3.0

func (ft *FunctionType) Signatures() []*FunctionSignature

func (*FunctionType) Text

func (ft *FunctionType) Text() string

type FunctionValue

type FunctionValue struct {
	// contains filtered or unexported fields
}

FunctionValue represents a function value.

func NewFunctionValue

func NewFunctionValue(functionType *FunctionType) *FunctionValue

NewFunctionValue creates a new function value.

func (*FunctionValue) Call

func (f *FunctionValue) Call(args []Value, namedArgs map[string]Value) (Value, error)

Call calls the function with the given arguments.

func (*FunctionValue) Interface

func (f *FunctionValue) Interface() any

func (*FunctionValue) Text

func (f *FunctionValue) Text() string

func (*FunctionValue) Type

func (f *FunctionValue) Type() Type

type Generic

type Generic interface {
	Type
	// 泛型相关方法
	IsGeneric() bool
	TypeParameters() []string
	Constraints() map[string]Constraint
	Instantiate(typeArgs []Type) (Type, error)
	CheckConstraints(typeArgs []Type) error
}

Generic 统一的泛型接口

type GenericBase

type GenericBase struct {
	// contains filtered or unexported fields
}

GenericBase 泛型基础结构,可以被其他类型嵌入

func NewGenericBase

func NewGenericBase(typeParams []string, constraints map[string]Constraint) *GenericBase

func (*GenericBase) CheckConstraints

func (gb *GenericBase) CheckConstraints(typeArgs []Type) error

func (*GenericBase) Constraints

func (gb *GenericBase) Constraints() map[string]Constraint

func (*GenericBase) IsGeneric

func (gb *GenericBase) IsGeneric() bool

func (*GenericBase) TypeParameters

func (gb *GenericBase) TypeParameters() []string

type InterfaceConstraint

type InterfaceConstraint struct {
	// contains filtered or unexported fields
}

func NewInterfaceConstraint

func NewInterfaceConstraint(interfaceType Type) *InterfaceConstraint

func NewTypeConstraint

func NewTypeConstraint(targetType Type) *InterfaceConstraint

便捷的约束创建函数

func (*InterfaceConstraint) SatisfiedBy

func (ic *InterfaceConstraint) SatisfiedBy(t Type) bool

func (*InterfaceConstraint) String

func (ic *InterfaceConstraint) String() string

type IntersectionType

type IntersectionType struct {
	// contains filtered or unexported fields
}

func NewIntersectionType

func NewIntersectionType(types ...Type) *IntersectionType

func (*IntersectionType) AssignableTo

func (i *IntersectionType) AssignableTo(t Type) bool

func (*IntersectionType) ConvertibleTo

func (i *IntersectionType) ConvertibleTo(t Type) bool

func (*IntersectionType) Implements

func (i *IntersectionType) Implements(t Type) bool

func (*IntersectionType) Kind

func (i *IntersectionType) Kind() ObjKind

func (*IntersectionType) Name

func (i *IntersectionType) Name() string

func (*IntersectionType) New

func (i *IntersectionType) New(values ...Value) Value

func (*IntersectionType) Text

func (i *IntersectionType) Text() string

type KeyValuePair

type KeyValuePair struct {
	Key   Value
	Value Value
}

KeyValuePair represents a key-value pair in a map.

type MapType

type MapType struct {
	*ObjectType
	// contains filtered or unexported fields
}

func NewMapType

func NewMapType(keyType, valueType Type) *MapType

func (*MapType) AssignableTo

func (m *MapType) AssignableTo(t Type) bool

func (*MapType) ConvertibleTo

func (m *MapType) ConvertibleTo(t Type) bool

func (*MapType) Implements

func (m *MapType) Implements(t Type) bool

func (*MapType) KeyType

func (m *MapType) KeyType() Type

func (*MapType) ValueType

func (m *MapType) ValueType() Type

type MapValue

type MapValue struct {
	Pairs []*KeyValuePair
	// contains filtered or unexported fields
}

MapValue represents a map value.

func NewMapValue

func NewMapValue(keyType, valueType Type) *MapValue

NewMapValue creates a new map value with the given key and value types.

func (*MapValue) Clear

func (m *MapValue) Clear()

Clear removes all key-value pairs from the map.

func (*MapValue) Delete

func (m *MapValue) Delete(key Value)

Delete removes the key-value pair with the given key.

func (*MapValue) Get

func (m *MapValue) Get(key Value) Value

Get returns the value associated with the given key.

func (*MapValue) Has

func (m *MapValue) Has(key Value) bool

Has returns true if the map contains the given key.

func (*MapValue) Interface

func (m *MapValue) Interface() any

func (*MapValue) Keys

func (m *MapValue) Keys() []Value

Keys returns all keys in the map.

func (*MapValue) Length

func (m *MapValue) Length() int

Length returns the number of key-value pairs in the map.

func (*MapValue) Set

func (m *MapValue) Set(key, value Value)

Set sets the value associated with the given key.

func (*MapValue) Text

func (m *MapValue) Text() string

func (*MapValue) Type

func (m *MapValue) Type() Type

func (*MapValue) Values

func (m *MapValue) Values() []Value

Values returns all values in the map.

type Method

type Method interface {
	Type
	Call(args []Value, namedArgs map[string]Value, evaluator ...core.FunctionEvaluator) (Value, error)
}

type NullValue

type NullValue struct{}

NullValue represents the null value.

func (*NullValue) Interface

func (n *NullValue) Interface() any

func (*NullValue) Text

func (n *NullValue) Text() string

func (*NullValue) Type

func (n *NullValue) Type() Type

type NullableType

type NullableType struct {
	// contains filtered or unexported fields
}

NullableType represents a nullable type (e.g., T?)

func NewNullableType

func NewNullableType(baseType Type) *NullableType

func (*NullableType) AssignableTo

func (n *NullableType) AssignableTo(t Type) bool

func (*NullableType) BaseType

func (n *NullableType) BaseType() Type

func (*NullableType) ConvertibleTo

func (n *NullableType) ConvertibleTo(t Type) bool

func (*NullableType) Implements

func (n *NullableType) Implements(t Type) bool

func (*NullableType) Kind

func (n *NullableType) Kind() ObjKind

func (*NullableType) Name

func (n *NullableType) Name() string

func (*NullableType) New

func (n *NullableType) New(values ...Value) Value

func (*NullableType) Text

func (n *NullableType) Text() string

type NumberType

type NumberType struct {
	*ObjectType
}

func NewNumberType

func NewNumberType() *NumberType

type NumberValue

type NumberValue struct {
	// TODO 根据精度选择存储模型
	Value *big.Float
}

NumberValue represents the number value.

func NewNumberValue

func NewNumberValue(value string) *NumberValue

NewNumberValue creates a new number value from the given string.

func (*NumberValue) Clone added in v0.3.0

func (n *NumberValue) Clone() *NumberValue

func (*NumberValue) Interface

func (n *NumberValue) Interface() any

func (*NumberValue) Text

func (n *NumberValue) Text() string

func (*NumberValue) Type

func (n *NumberValue) Type() Type

type ObjKind

type ObjKind int
const (
	O_NUM    ObjKind = iota // num
	O_STR                   // str
	O_BOOL                  // bool
	O_SYMBOL                // symbol

	O_ARR   // arr
	O_TUPLE // tuple
	O_SET   // set
	O_OBJ   // object
	O_MAP   // map
	O_FUNC  // func

	O_NULL  // null
	O_ANY   // any
	O_VOID  // void
	O_NEVER // never
	O_ERROR // error

	O_TRAIT // trait
	O_CLASS // class
	O_ENUM  // enum

	O_BUILTIN
	O_LITERAL
	O_RET
	O_QUOTE

	// A | B
	O_UNION // union
	// A & B
	O_INTERSECTION // intersection
	// T?
	O_NULLABLE // nullable

	// Decorators
	O_DECORATOR // decorator

	// Generic types
	O_TYPE_PARAM // type_param
)

func (ObjKind) Equal

func (o ObjKind) Equal(x ObjKind) bool

Equal reports whether o and x represent the same kind.

func (ObjKind) String

func (i ObjKind) String() string

type Object

type Object interface {
	Type
	NumMethod() int
	Method(i int) Method
	MethodByName(name string) Method
	NumField() int
	Field(i int) Type
	FieldByName(name string) Type
}

type ObjectType

type ObjectType struct {
	// contains filtered or unexported fields
}

func NewObjectType

func NewObjectType(name string, kind ObjKind) *ObjectType

func (*ObjectType) AddField

func (o *ObjectType) AddField(name string, field *Field)

func (*ObjectType) AddMethod

func (o *ObjectType) AddMethod(name string, signature Method)

func (*ObjectType) AssignableTo

func (t *ObjectType) AssignableTo(u Type) bool

func (*ObjectType) ConvertibleTo

func (t *ObjectType) ConvertibleTo(u Type) bool

func (*ObjectType) Field

func (t *ObjectType) Field(i int) Type

func (*ObjectType) FieldByName

func (t *ObjectType) FieldByName(name string) Type

func (*ObjectType) Implements

func (t *ObjectType) Implements(u Type) bool

func (*ObjectType) Kind

func (t *ObjectType) Kind() ObjKind

func (*ObjectType) Method

func (t *ObjectType) Method(i int) Method

func (*ObjectType) MethodByName

func (t *ObjectType) MethodByName(name string) Method

func (*ObjectType) Name

func (t *ObjectType) Name() string

func (*ObjectType) New

func (t *ObjectType) New(values ...Value) Value

func (*ObjectType) NumField

func (t *ObjectType) NumField() int

func (*ObjectType) NumMethod

func (t *ObjectType) NumMethod() int

func (*ObjectType) Text

func (t *ObjectType) Text() string

type Operator

type Operator int
const (
	OpIllegal      Operator = iota
	OpAdd                   // +
	OpSub                   // -
	OpMul                   // *
	OpDiv                   // /
	OpMod                   // %
	OpConcat                // ..
	OpAnd                   // &&
	OpOr                    // ||
	OpEqual                 // ==
	OpNot                   // !
	OpLess                  // <
	OpLessEqual             // <=
	OpGreater               // >
	OpGreaterEqual          // >=
	OpAssign                // =
	OpIndex                 // []
	OpCall                  // ()

	Opa // a""
	Opb // b""
	Opc // c""
	Opd // d""
	Ope // e""
	Opf // f""
	Opg // g""
	Oph // h""
	Opi // i""
	Opj // j""
	Opk // k""
	Opl // l""
	Opm // m""
	Opn // n""
	Opo // o""
	Opp // p""
	Opq // q""
	Opr // r""
	Ops // s""
	Ott // t""
	Opu // u""
	Opv // v""
	Opw // w""
	Opz // z""

	OpA // A""
	OpB // B""
	OpC // C""
	OpD // D""
	OpE // E""
	OpF // F""
	OpG // G""
	OpH // H""
	OpI // I""
	OpJ // J""
	OpK // K""
	OpL // L""
	OpM // M""
	OpN // N""
	OpO // O""
	OpP // P""
	OpQ // Q""
	OpR // R""
	OpS // S""
	OpT // T""
	OpU // U""
	OpV // V""
	OpW // W""
	OpX // X""
	OpY // Y""
	OpZ // Z""
)

func (Operator) String

func (i Operator) String() string

type OperatorConstraint

type OperatorConstraint struct {
	// contains filtered or unexported fields
}

func NewOperatorConstraint

func NewOperatorConstraint(op Operator, rightType, returnType Type) *OperatorConstraint

func NewOperatorConstraintSimple

func NewOperatorConstraintSimple(op Operator) *OperatorConstraint

func (*OperatorConstraint) SatisfiedBy

func (oc *OperatorConstraint) SatisfiedBy(t Type) bool

func (*OperatorConstraint) String

func (oc *OperatorConstraint) String() string

type OperatorOverload

type OperatorOverload struct {
	// contains filtered or unexported fields
}

type Parameter

type Parameter struct {
	// contains filtered or unexported fields
}

func NewOptionalParameter added in v0.3.0

func NewOptionalParameter(name string, typ Type, defaultValue Value) *Parameter

NewOptionalParameter 创建一个可选参数

func NewParameter added in v0.3.0

func NewParameter(name string, typ Type) *Parameter

NewParameter 创建一个新的参数

func NewVariadicParameter added in v0.3.0

func NewVariadicParameter(name string, typ Type) *Parameter

NewVariadicParameter 创建一个可变参数

type PatternMatch

type PatternMatch interface {
	Match(value Value) bool
}

PatternMatch 模式匹配接口

type PrimitiveType

type PrimitiveType struct {
	// contains filtered or unexported fields
}

func NewPrimitiveType

func NewPrimitiveType(name string, kind ObjKind) *PrimitiveType

func (*PrimitiveType) AssignableTo

func (pt *PrimitiveType) AssignableTo(u Type) bool

func (*PrimitiveType) ConvertibleTo

func (pt *PrimitiveType) ConvertibleTo(u Type) bool

func (*PrimitiveType) Implements

func (pt *PrimitiveType) Implements(u Type) bool

func (*PrimitiveType) Kind

func (pt *PrimitiveType) Kind() ObjKind

func (*PrimitiveType) Name

func (pt *PrimitiveType) Name() string

func (*PrimitiveType) Text

func (pt *PrimitiveType) Text() string

type Registry

type Registry struct {
	// contains filtered or unexported fields
}

func NewRegistry

func NewRegistry() *Registry

func (*Registry) LookupFunction

func (r *Registry) LookupFunction(name string) (*FunctionType, bool)

func (*Registry) LookupOperator

func (r *Registry) LookupOperator(op Operator, left, right Value) (*OperatorOverload, error)

func (*Registry) RegisterFunction

func (r *Registry) RegisterFunction(name string, signature *FunctionSignature) error

func (*Registry) RegisterOperator

func (r *Registry) RegisterOperator(op Operator, left, right, returnType Type, fn *FunctionType)

type SetType

type SetType struct {
	*ObjectType
	// contains filtered or unexported fields
}

SetType 表示集合类型

func NewSetType

func NewSetType(elementType Type) *SetType

func (*SetType) AssignableTo

func (s *SetType) AssignableTo(u Type) bool

func (*SetType) ConvertibleTo

func (s *SetType) ConvertibleTo(u Type) bool

func (*SetType) ElementType

func (s *SetType) ElementType() Type

func (*SetType) Implements

func (s *SetType) Implements(u Type) bool

func (*SetType) Kind

func (s *SetType) Kind() ObjKind

func (*SetType) Name

func (s *SetType) Name() string

func (*SetType) Text

func (s *SetType) Text() string

type SetValue

type SetValue struct {
	// contains filtered or unexported fields
}

SetValue 表示集合值

func NewSetValue

func NewSetValue(setType *SetType, elements ...Value) *SetValue

func (*SetValue) Add

func (sv *SetValue) Add(element Value) error

Add 添加元素

func (*SetValue) Clear

func (sv *SetValue) Clear()

Clear 清空集合

func (*SetValue) Contains

func (sv *SetValue) Contains(element Value) (bool, error)

Contains 类型安全的包含检查

func (*SetValue) Difference

func (sv *SetValue) Difference(other *SetValue) (*SetValue, error)

Difference 差集运算

func (*SetValue) Elements

func (sv *SetValue) Elements() map[string]Value

func (*SetValue) Has

func (sv *SetValue) Has(element Value) bool

Has 检查元素是否存在

func (*SetValue) Interface

func (sv *SetValue) Interface() any

func (*SetValue) Intersect

func (sv *SetValue) Intersect(other *SetValue) (*SetValue, error)

Intersect 交集运算

func (*SetValue) IsEmpty

func (sv *SetValue) IsEmpty() bool

func (*SetValue) Length

func (sv *SetValue) Length() int

func (*SetValue) Remove

func (sv *SetValue) Remove(element Value) error

Remove 删除元素

func (*SetValue) Text

func (sv *SetValue) Text() string

func (*SetValue) ToArray

func (sv *SetValue) ToArray() *ArrayValue

ToArray 转换为数组

func (*SetValue) Type

func (sv *SetValue) Type() Type

func (*SetValue) Union

func (sv *SetValue) Union(other *SetValue) (*SetValue, error)

Union 并集运算

type SourceLocation

type SourceLocation interface {
	File() string
	Line() int
	Col() int
}

type StringType

type StringType struct {
	*ObjectType
}

func NewStringType

func NewStringType() *StringType

type StringValue

type StringValue struct {
	Value string
}

StringValue represents the string value.

func (*StringValue) Clone added in v0.3.0

func (s *StringValue) Clone() *StringValue

func (*StringValue) Interface

func (s *StringValue) Interface() any

func (*StringValue) Text

func (s *StringValue) Text() string

func (*StringValue) Type

func (s *StringValue) Type() Type

type TraitImpl

type TraitImpl struct {
	// contains filtered or unexported fields
}

TraitImpl 表示trait的实现

func ConvertImplDecl

func ConvertImplDecl(decl *ast.ImplDecl) *TraitImpl

ConvertImplDecl 转换impl声明

func NewTraitImpl

func NewTraitImpl(trait *TraitType, implType Type) *TraitImpl

func (*TraitImpl) AddField

func (ti *TraitImpl) AddField(name string, field *Field)

func (*TraitImpl) AddMethod

func (ti *TraitImpl) AddMethod(name string, method *FunctionType)

func (*TraitImpl) AddOperator

func (ti *TraitImpl) AddOperator(op Operator, fn *FunctionType)

func (*TraitImpl) GetMethod

func (ti *TraitImpl) GetMethod(name string) *FunctionType

GetMethod 获取方法实现,优先返回自定义实现,否则返回默认实现

type TraitRegistry

type TraitRegistry struct {
	// contains filtered or unexported fields
}

TraitRegistry trait注册表

func NewTraitRegistry

func NewTraitRegistry() *TraitRegistry

func (*TraitRegistry) GetImpl

func (tr *TraitRegistry) GetImpl(traitName, typeName string) []*TraitImpl

func (*TraitRegistry) GetTrait

func (tr *TraitRegistry) GetTrait(name string) *TraitType

func (*TraitRegistry) RegisterImpl

func (tr *TraitRegistry) RegisterImpl(impl *TraitImpl)

func (*TraitRegistry) RegisterTrait

func (tr *TraitRegistry) RegisterTrait(trait *TraitType)

type TraitType

type TraitType struct {
	*ObjectType
	// contains filtered or unexported fields
}

TraitType 表示trait类型

func ConvertTraitDecl

func ConvertTraitDecl(decl *ast.TraitDecl) *TraitType

ConvertTraitDecl 转换trait声明

func NewTraitType

func NewTraitType(name string) *TraitType

func (*TraitType) AddDefaultImpl

func (t *TraitType) AddDefaultImpl(name string, impl *FunctionType)

AddDefaultImpl 添加默认实现

func (*TraitType) AddField

func (t *TraitType) AddField(name string, field *Field)

AddField 添加trait字段

func (*TraitType) AddMethod

func (t *TraitType) AddMethod(name string, method *FunctionType)

AddMethod 添加trait方法

func (*TraitType) AddOperator

func (t *TraitType) AddOperator(op Operator, fn *FunctionType)

AddOperator 添加trait运算符

func (*TraitType) AssignableTo

func (t *TraitType) AssignableTo(u Type) bool

func (*TraitType) ConvertibleTo

func (t *TraitType) ConvertibleTo(u Type) bool

func (*TraitType) GetDefaultImpl

func (t *TraitType) GetDefaultImpl(name string) *FunctionType

GetDefaultImpl 获取默认实现

func (*TraitType) Implements

func (t *TraitType) Implements(u Type) bool

func (*TraitType) Kind

func (t *TraitType) Kind() ObjKind

func (*TraitType) Name

func (t *TraitType) Name() string

func (*TraitType) Text

func (t *TraitType) Text() string

type TuplePattern

type TuplePattern struct {
	TupleType *TupleType
	Elements  []PatternMatch
}

TuplePattern 元组模式

func (*TuplePattern) Match

func (tp *TuplePattern) Match(value Value) bool

type TupleType

type TupleType struct {
	*ObjectType
	// contains filtered or unexported fields
}

TupleType 表示元组类型

func NewNamedTupleType

func NewNamedTupleType(fieldNames []string, elementTypes []Type) *TupleType

func NewNamedTupleTypeWithMethods

func NewNamedTupleTypeWithMethods(fieldNames []string, elementTypes []Type) *TupleType

func NewTupleType

func NewTupleType(elementTypes ...Type) *TupleType

func NewTupleTypeWithMethods

func NewTupleTypeWithMethods(elementTypes ...Type) *TupleType

创建元组类型的工厂函数

func (*TupleType) AssignableTo

func (t *TupleType) AssignableTo(u Type) bool

func (*TupleType) ConvertibleTo

func (t *TupleType) ConvertibleTo(u Type) bool

func (*TupleType) ElementTypes

func (t *TupleType) ElementTypes() []Type

func (*TupleType) FieldNames

func (t *TupleType) FieldNames() []string

func (*TupleType) GetElementType

func (t *TupleType) GetElementType(index int) Type

GetElementType 获取指定位置的元素类型

func (*TupleType) GetFieldIndex

func (t *TupleType) GetFieldIndex(fieldName string) int

GetFieldIndex 根据字段名获取索引

func (*TupleType) Implements

func (t *TupleType) Implements(u Type) bool

func (*TupleType) Kind

func (t *TupleType) Kind() ObjKind

func (*TupleType) Length

func (t *TupleType) Length() int

func (*TupleType) Name

func (t *TupleType) Name() string

func (*TupleType) Text

func (t *TupleType) Text() string

type TupleValue

type TupleValue struct {
	// contains filtered or unexported fields
}

TupleValue 表示元组值

func NewTupleValue

func NewTupleValue(tupleType *TupleType, elements ...Value) *TupleValue

func (*TupleValue) Destructure

func (tv *TupleValue) Destructure() []Value

Destructure 解构元组

func (*TupleValue) Elements

func (tv *TupleValue) Elements() []Value

func (*TupleValue) Get

func (tv *TupleValue) Get(index int) Value

Get 通过索引获取元素

func (*TupleValue) GetByName

func (tv *TupleValue) GetByName(fieldName string) Value

GetByName 通过字段名获取元素

func (*TupleValue) Interface

func (tv *TupleValue) Interface() any

func (*TupleValue) Length

func (tv *TupleValue) Length() int

func (*TupleValue) Set

func (tv *TupleValue) Set(index int, value Value) error

Set 设置指定位置的元素

func (*TupleValue) Text

func (tv *TupleValue) Text() string

func (*TupleValue) Type

func (tv *TupleValue) Type() Type

func (*TupleValue) With

func (tv *TupleValue) With(index int, value Value) (*TupleValue, error)

With 创建新的元组,更新指定位置的元素

type Type

type Type interface {
	Name() string

	Text() string

	Kind() ObjKind

	// Implements reports whether the type implements the interface type u.
	Implements(u Type) bool

	AssignableTo(u Type) bool

	ConvertibleTo(u Type) bool
}

func DeepMutable

func DeepMutable(t Type) (Type, error)

func DeepPartial

func DeepPartial(t Type) (Type, error)

func DeepReadonly

func DeepReadonly(t Type) (Type, error)

func DeepRequired

func DeepRequired(t Type) (Type, error)

func Exclude

func Exclude(unionType, excludeType Type) (Type, error)

func Extract

func Extract(unionType, extractType Type) (Type, error)

func Filter

func Filter(t Type, predicate func(Type) bool) (Type, error)

func GetAnyType added in v0.3.0

func GetAnyType() Type

func GetBoolType added in v0.3.0

func GetBoolType() Type

func GetErrorType added in v0.3.0

func GetErrorType() Type

func GetNeverType added in v0.3.0

func GetNeverType() Type

func GetNullType added in v0.3.0

func GetNullType() Type

func GetNumberType added in v0.3.0

func GetNumberType() Type

func GetStringType added in v0.3.0

func GetStringType() Type

func GetVoidType added in v0.3.0

func GetVoidType() Type

func If

func If(condition bool, trueType, falseType Type) Type

func Infer

func Infer(template Type, constraints map[string]Type) (Type, error)

func Intersect

func Intersect(t1, t2 Type) (Type, error)

func Map

func Map(t Type, mapper func(Type) Type) (Type, error)

func Merge

func Merge(t1, t2 Type) (Type, error)

func Mutable

func Mutable(t Type) (Type, error)

func NonNullable

func NonNullable(t Type) (Type, error)

func Omit

func Omit(t Type, keys ...string) (Type, error)

func Optional

func Optional(t Type) Type

func Partial

func Partial(t Type) (Type, error)

便捷函数

func Pick

func Pick(t Type, keys ...string) (Type, error)

func Readonly

func Readonly(t Type) (Type, error)

func Record

func Record(keyType, valueType Type) Type

func Required

func Required(t Type) (Type, error)

func Union

func Union(types ...Type) Type

func Values

func Values(t Type) ([]Type, error)

type TypeContext

type TypeContext struct {
	Variables     map[string]Type        // 变量类型
	FunctionCalls []*FunctionCallContext // 函数调用
	Expressions   []*ExpressionContext   // 表达式
	Constraints   map[string]Constraint  // 约束
	Hints         map[string]Type        // 类型提示
}

TypeContext 表示类型推断上下文

type TypeInference

type TypeInference interface {
	// InferTypeArgs 从具体类型推断泛型类型参数
	InferTypeArgs(concreteType Type, genericType Type) ([]Type, error)

	// InferFromUsage 从使用场景推断类型
	InferFromUsage(usage *TypeUsage) (map[string]Type, error)

	// InferFromConstraints 从约束推断类型
	InferFromConstraints(constraints map[string]Constraint) (map[string]Type, error)

	// InferFromAssignment 从赋值推断类型
	InferFromAssignment(targetType Type, sourceType Type) (map[string]Type, error)

	// InferFromContext 从上下文推断类型
	InferFromContext(context *TypeContext) (map[string]Type, error)

	// InferBidirectional 双向类型推断
	InferBidirectional(target Type, source Type) (map[string]Type, error)

	// InferFromPattern 从模式匹配推断类型
	InferFromPattern(pattern PatternMatch) (map[string]Type, error)

	// SolveConstraints 求解约束系统
	SolveConstraints(constraints map[string]Constraint) (map[string]Type, error)

	// InferFromOverload 从重载函数推断类型
	InferFromOverload(overloads []*FunctionType, args []Value) (map[string]Type, error)

	// UnifyTypes 类型统一算法
	UnifyTypes(t1, t2 Type) (map[string]Type, error)

	// PropagateConstraints 约束传播
	PropagateConstraints(constraints map[string]Constraint, unifier map[string]Type) (map[string]Constraint, error)

	// InferRecursive 递归类型推断
	InferRecursive(types []Type) (map[string]Type, error)

	// InferWithContext 带上下文的类型推断
	InferWithContext(target Type, context *TypeContext) (map[string]Type, error)

	// SubstituteTypes 类型替换
	SubstituteTypes(t Type, substitutions map[string]Type) Type

	// CheckConstraintSatisfaction 检查约束满足性
	CheckConstraintSatisfaction(t Type, constraint Constraint) bool

	// InferFromLiteral 从字面量推断类型
	InferFromLiteral(literal Value) Type

	// InferFromExpression 从表达式推断类型(增强版)
	InferFromExpression(expr *ExpressionContext) map[string]Type

	// InferFromBinaryOp 从二元操作推断类型
	InferFromBinaryOp(op Operator, left, right Type) (map[string]Type, error)

	// InferFromUnaryOp 从一元操作推断类型
	InferFromUnaryOp(op Operator, operand Type) (map[string]Type, error)

	// InferFromIndexOp 从索引操作推断类型
	InferFromIndexOp(container, index Type) (map[string]Type, error)

	// InferFromCall 从函数调用推断类型
	InferFromCall(funcType Type, args []Type) (map[string]Type, error)
}

TypeInference 类型推断接口

func NewTypeInference

func NewTypeInference() TypeInference

NewTypeInference 创建类型推断实例

type TypeOperator

type TypeOperator interface {
	// Unify returns the most specific type that is a supertype of both types.
	Unify(other Type) (Type, error)
	// Intersect returns the most specific type that is a subtype of both types.
	Intersect(other Type) (Type, error)
	// Union returns the most specific type that is a supertype of both types.
	Union(other Type) Type
}

TypeOperator represents the operator between two types.

type TypeParameter

type TypeParameter struct {
	// contains filtered or unexported fields
}

func NewTypeParameter

func NewTypeParameter(name string, constraints map[string]Constraint) *TypeParameter

func (*TypeParameter) AssignableTo

func (tp *TypeParameter) AssignableTo(u Type) bool

func (*TypeParameter) ConvertibleTo

func (tp *TypeParameter) ConvertibleTo(u Type) bool

func (*TypeParameter) Implements

func (tp *TypeParameter) Implements(u Type) bool

func (*TypeParameter) Kind

func (tp *TypeParameter) Kind() ObjKind

func (*TypeParameter) Name

func (tp *TypeParameter) Name() string

func (*TypeParameter) SatisfiesConstraint

func (tp *TypeParameter) SatisfiesConstraint(constraint Constraint) bool

SatisfiesConstraint 检查类型参数是否满足约束

func (*TypeParameter) Text

func (tp *TypeParameter) Text() string

type TypeRelation

type TypeRelation interface {
	// IsAssignableTo reports whether a type can be assigned to the type u.
	IsAssignableTo(u Type) bool
	// IsSubtypeOf reports whether a type is a subtype of the type u.
	IsSubtypeOf(u Type) bool
	// IsSupertypeOf reports whether a type is a supertype of the type u.
	IsSupertypeOf(u Type) bool
	// IsCompatibleWith reports whether a type is compatible with the type u.
	IsCompatibleWith(u Type) bool
}

TypeRelation represents the relation between two types.

type TypeSubstituter

type TypeSubstituter interface {
	SubstituteType(t Type, typeMap map[string]Type) Type
}

TODO

type TypeUsage

type TypeUsage struct {
	Context     string                // 使用上下文
	Arguments   []Type                // 参数类型
	ReturnType  Type                  // 返回类型
	Constraints map[string]Constraint // 约束信息
	Hints       map[string]Type       // 类型提示
}

TypeUsage 表示类型使用场景

type TypeUtils

type TypeUtils interface {
	// 基础类型工具
	Partial(t Type) (Type, error)
	Required(t Type) (Type, error)
	Optional(t Type) Type
	Readonly(t Type) (Type, error)
	Mutable(t Type) (Type, error)

	// 对象类型工具
	Pick(t Type, keys ...string) (Type, error)
	Omit(t Type, keys ...string) (Type, error)
	Record(keyType, valueType Type) Type

	// 联合类型工具
	Exclude(unionType, excludeType Type) (Type, error)
	Extract(unionType, extractType Type) (Type, error)
	NonNullable(t Type) (Type, error)

	// 条件类型工具
	If(condition bool, trueType, falseType Type) Type
	Infer(template Type, constraints map[string]Type) (Type, error)

	// 映射类型工具
	Map(t Type, mapper func(Type) Type) (Type, error)
	Filter(t Type, predicate func(Type) bool) (Type, error)

	// 高级类型工具
	DeepPartial(t Type) (Type, error)
	DeepRequired(t Type) (Type, error)
	DeepReadonly(t Type) (Type, error)
	DeepMutable(t Type) (Type, error)

	// 类型组合工具
	Merge(t1, t2 Type) (Type, error)
	Intersect(t1, t2 Type) (Type, error)
	Union(types ...Type) Type

	// 类型查询工具
	Keys(t Type) ([]string, error)
	Values(t Type) ([]Type, error)
	Entries(t Type) (map[string]Type, error)
}

TypeUtils 类型工具接口

var GlobalTypeUtils TypeUtils = NewTypeUtils()

全局类型工具实例

func NewTypeUtils

func NewTypeUtils() TypeUtils

NewTypeUtils 创建类型工具实例

type UnionConstraint

type UnionConstraint struct {
	// contains filtered or unexported fields
}

func NewUnionConstraint

func NewUnionConstraint(constraints ...Constraint) *UnionConstraint

func TypesToUnionConstraint

func TypesToUnionConstraint(types ...Type) *UnionConstraint

从多个Type创建UnionConstraint的辅助函数

func (*UnionConstraint) SatisfiedBy

func (uc *UnionConstraint) SatisfiedBy(t Type) bool

func (*UnionConstraint) String

func (uc *UnionConstraint) String() string

type UnionType

type UnionType struct {
	// contains filtered or unexported fields
}

func NewUnionType

func NewUnionType(types ...Type) *UnionType

func (*UnionType) AssignableTo

func (u *UnionType) AssignableTo(t Type) bool

func (*UnionType) ConvertibleTo

func (u *UnionType) ConvertibleTo(t Type) bool

func (*UnionType) Implements

func (u *UnionType) Implements(t Type) bool

func (*UnionType) Kind

func (u *UnionType) Kind() ObjKind

func (*UnionType) Name

func (u *UnionType) Name() string

func (*UnionType) New

func (u *UnionType) New(values ...Value) Value

func (*UnionType) Text

func (u *UnionType) Text() string

type Value

type Value interface {
	Type() Type // 获取值的类型

	Text() string // 值的文本表示

	Interface() any // 获取底层 Go 值
}

type VariablePattern

type VariablePattern struct {
	VariableName string
	Type         Type
}

VariablePattern 变量模式

func (*VariablePattern) Match

func (vp *VariablePattern) Match(value Value) bool

type Variance

type Variance int

Type variance

const (
	Invariant Variance = iota
	Covariant
	Contravariant
	Bivariant
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL