core

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Pos constants
	NoPos Pos = 0

	// Builtin module/function slot constants
	ModuleSlotSize = 128
	MaxModules     = 32
)
View Source
const KindUser = "user"

Variables

View Source
var (
	Undefined   = Value{}
	True        = Value{Type: value.Bool, Immutable: true, Data: 1}
	False       = Value{Type: value.Bool, Immutable: true, Data: 0}
	EmptyString = Value{Type: value.String, Immutable: true, Ptr: unsafe.Pointer(&emptyString)}
)

Builtin module/function registry

View Source
var DefaultValueType = ValueTypeDescr{
	Name:         func(v Value) string { return fmt.Sprintf("<unknown:%d>", v.Type) },
	String:       func(v Value) string { return v.TypeName() },
	Format:       defaultFormat,
	Interface:    func(_ Value) any { return nil },
	EncodeJSON:   func(v Value) ([]byte, error) { return nil, errs.NewJSONEncodingError(v.TypeName()) },
	EncodeBinary: func(v Value) ([]byte, error) { return nil, errs.NewBinaryEncodingError(v.TypeName()) },
	DecodeBinary: func(v *Value, _ []byte) error { return errs.NewBinaryEncodingError(v.TypeName()) },
	IsTrue:       ConstHook(false),
	Clone:        func(v Value) (Value, error) { return v, nil },
	Equal:        func(v Value, r Value) bool { return v == r },

	UnaryOp:    defaultUnaryOp,
	BinaryOp:   defaultBinaryOp,
	MethodCall: defaultMethodCall,

	IsIterable: ConstHook(false),
	Contains:   func(Value, Value) bool { return false },
	Len:        ConstHook(int64(0)),
	Iterator:   ValueHook(Undefined, nil),
	Assign:     func(v Value, _, _ Value) error { return errs.NewNotAssignableError(v.TypeName()) },
	Delete:     defaultDelete,

	Access:    defaultAccess,
	Append:    defaultAppend,
	Slice:     defaultSlice,
	SliceStep: defaultSliceStep,

	IsCallable: ConstHook(false),
	IsVariadic: ConstHook(false),
	Arity:      ConstHook(0),

	Call: defaultCall,

	Next:  ConstHook(false),
	Key:   ValueHook(Undefined, nil),
	Value: ValueHook(Undefined, nil),

	AsBool:    Const2Hook(false, false),
	AsByte:    Const2Hook(byte(0), false),
	AsRune:    Const2Hook(rune(0), false),
	AsInt:     Const2Hook(int64(0), false),
	AsFloat:   Const2Hook(float64(0), false),
	AsDecimal: Const2Hook(dec128.Decimal0, false),
	AsTime:    Const2Hook(time.Time{}, false),
	AsString:  Const2Hook("", false),
	AsBytes:   Const2Hook[[]byte](nil, false),
	AsArray:   func(Value) ([]Value, bool) { return nil, false },
	AsDict:    func(Value) (map[string]Value, bool) { return nil, false },
	AsRunes:   defaultAsRunes,
}

DefaultValueType provides default implementations for all ValueType hooks.

View Source
var TypeArray = ValueTypeDescr{
	Name:         SeqNameHook(arrayTypeName, immutableArrayTypeName),
	String:       arrayTypeString,
	Format:       arrayTypeFormat,
	Interface:    arrayTypeInterface,
	EncodeJSON:   arrayTypeEncodeJSON,
	EncodeBinary: arrayTypeEncodeBinary,
	DecodeBinary: arrayTypeDecodeBinary,
	IsTrue:       func(v Value) bool { return len((*Array)(v.Ptr).Elements) > 0 },
	IsIterable:   ConstHook(true),
	Iterator:     arrayTypeIterator,
	Equal:        arrayTypeEqual,
	Clone:        arrayTypeClone,
	Len:          func(v Value) int64 { return int64(len((*Array)(v.Ptr).Elements)) },
	BinaryOp:     arrayTypeBinaryOp,
	MethodCall:   arrayTypeMethodCall,
	Access:       SeqAccessHook(RefValue, arrayTypeResolve),
	Assign:       SeqAssignHook(arrayTypeResolve, Value.AsValue, anyTypeName),
	Contains:     arrayTypeContains,
	Append:       arrayTypeAppend,
	Slice:        SeqSliceHook(NewArrayValue, arrayTypeResolve),
	SliceStep:    SeqSliceStepHook(NewArrayValue, arrayTypeResolve),
	AsBool:       func(v Value) (bool, bool) { return len((*Array)(v.Ptr).Elements) > 0, true },
	AsString:     arrayTypeAsString,
	AsRunes:      arrayTypeAsRunes,
	AsBytes:      arrayTypeAsBytes,
	AsArray:      func(v Value) ([]Value, bool) { return (*Array)(v.Ptr).Elements, true },
}
View Source
var TypeArrayIterator = ValueTypeDescr{
	Name:   ConstHook(arrayIteratorTypeName),
	String: SeqIterStringHook[Value](arrayIteratorTypeName, arrayIteratorResolve),
	Next:   SeqIterNextHook[Value](arrayIteratorResolve),
	Key:    SeqIterKeyHook[Value](arrayIteratorResolve),
	Value:  SeqIterValueHook(RefValue, arrayIteratorResolve),
}
View Source
var TypeBool = ValueTypeDescr{
	Name:         ConstHook(boolTypeName),
	String:       boolTypeString,
	Format:       boolTypeFormat,
	Interface:    func(v Value) any { return v.Data != 0 },
	EncodeJSON:   boolTypeEncodeJSON,
	EncodeBinary: boolTypeEncodeBinary,
	DecodeBinary: boolTypeDecodeBinary,
	IsTrue:       func(v Value) bool { return v.Data != 0 },
	Equal:        boolTypeEqual,
	MethodCall:   boolTypeMethodCall,
	Len:          ConstHook(int64(1)),
	AsString:     boolTypeAsString,
	AsInt:        boolTypeAsInt,
	AsBool:       func(v Value) (bool, bool) { return v.Data != 0, true },
	AsByte:       boolTypeAsByte,
}
View Source
var TypeBuiltinClosure = ValueTypeDescr{
	Name:       builtinClosureTypeName,
	String:     func(v Value) string { return builtinClosureTypeName(v) },
	IsTrue:     ConstHook(true),
	IsCallable: ConstHook(true),
	IsVariadic: builtinClosureTypeIsVariadic,
	Arity:      builtinClosureTypeArity,
	Call:       builtinClosureTypeCall,
	MethodCall: builtinClosureTypeMethodCall,
}
View Source
var TypeBuiltinFunction = ValueTypeDescr{
	Name:         builtinFunctionTypeName,
	String:       func(v Value) string { return builtinFunctionTypeName(v) },
	EncodeBinary: builtinFunctionTypeEncodeBinary,
	DecodeBinary: builtinFunctionTypeDecodeBinary,
	IsTrue:       ConstHook(true),
	IsCallable:   ConstHook(true),
	IsVariadic:   builtinFunctionTypeIsVariadic,
	Arity:        builtinFunctionTypeArity,
	Call:         builtinFunctionTypeCall,
	MethodCall:   builtinFunctionTypeMethodCall,
}
View Source
var TypeByte = ValueTypeDescr{
	Name:         ConstHook(byteTypeName),
	String:       func(v Value) string { return fmt.Sprintf("byte(%d)", v.Data) },
	Format:       byteTypeFormat,
	Interface:    func(v Value) any { return byte(v.Data) },
	EncodeJSON:   byteTypeEncodeJSON,
	EncodeBinary: byteTypeEncodeBinary,
	DecodeBinary: byteTypeDecodeBinary,
	IsTrue:       func(v Value) bool { return v.Data != 0 },
	Equal:        byteTypeEqual,
	Len:          ConstHook(int64(1)),
	UnaryOp:      byteTypeUnaryOp,
	BinaryOp:     byteTypeBinaryOp,
	MethodCall:   byteTypeMethodCall,
	AsString:     func(v Value) (string, bool) { return strconv.FormatInt(int64(v.Data), 10), true },
	AsInt:        func(v Value) (int64, bool) { return int64(v.Data), true },
	AsBool:       func(v Value) (bool, bool) { return v.Data != 0, true },
	AsRune:       func(v Value) (rune, bool) { return rune(v.Data), true },
	AsByte:       func(v Value) (byte, bool) { return byte(v.Data), true },
	AsFloat:      func(v Value) (float64, bool) { return float64(int64(v.Data)), true },
	AsDecimal:    func(v Value) (dec128.Dec128, bool) { return dec128.FromInt64(int64(v.Data)), true },
}
View Source
var TypeBytes = ValueTypeDescr{
	Name:         SeqNameHook(bytesTypeName, immutableBytesTypeName),
	String:       bytesTypeString,
	Format:       bytesTypeFormat,
	Interface:    func(v Value) any { return (*Bytes)(v.Ptr).Elements },
	EncodeJSON:   bytesTypeEncodeJSON,
	EncodeBinary: bytesTypeEncodeBinary,
	DecodeBinary: bytesTypeDecodeBinary,
	IsTrue:       func(v Value) bool { return len((*Bytes)(v.Ptr).Elements) > 0 },
	IsIterable:   ConstHook(true),
	Iterator:     bytesTypeIterator,
	Equal:        bytesTypeEqual,
	Clone:        bytesTypeClone,
	Len:          func(v Value) int64 { return int64(len((*Bytes)(v.Ptr).Elements)) },
	BinaryOp:     bytesTypeBinaryOp,
	MethodCall:   bytesTypeMethodCall,
	Access:       SeqAccessHook(ByteValue, bytesTypeResolve),
	Assign:       SeqAssignHook(bytesTypeResolve, Value.AsByte, byteTypeName),
	Append:       bytesTypeAppend,
	Contains:     bytesTypeContains,
	Slice:        SeqSliceHook(NewBytesValue, bytesTypeResolve),
	SliceStep:    SeqSliceStepHook(NewBytesValue, bytesTypeResolve),
	AsBool:       func(v Value) (bool, bool) { return conv.ParseBool(string((*Bytes)(v.Ptr).Elements)) },
	AsString:     func(v Value) (string, bool) { return string((*Bytes)(v.Ptr).Elements), true },
	AsBytes:      func(v Value) ([]byte, bool) { return (*Bytes)(v.Ptr).Elements, true },
	AsArray:      bytesTypeAsArray,
}
View Source
var TypeBytesIterator = ValueTypeDescr{
	Name:   ConstHook(bytesIteratorTypeName),
	String: SeqIterStringHook[byte](bytesIteratorTypeName, bytesIteratorResolve),
	Next:   SeqIterNextHook[byte](bytesIteratorResolve),
	Key:    SeqIterKeyHook[byte](bytesIteratorResolve),
	Value:  SeqIterValueHook(ByteValue, bytesIteratorResolve),
}
View Source
var TypeCompiledFunction = ValueTypeDescr{
	Name:         compiledFunctionTypeName,
	String:       func(v Value) string { return compiledFunctionTypeName(v) },
	EncodeBinary: compiledFunctionTypeEncodeBinary,
	DecodeBinary: compiledFunctionTypeDecodeBinary,
	IsTrue:       ConstHook(true),
	IsCallable:   ConstHook(true),
	IsVariadic:   compiledFunctionTypeIsVariadic,
	Equal:        compiledFunctionTypeEqual,
	Arity:        compiledFunctionTypeArity,
	Call:         compiledFunctionTypeCall,
	MethodCall:   compiledFunctionTypeMethodCall,
}
View Source
var TypeDecimal = ValueTypeDescr{
	Name:         ConstHook(decimalTypeName),
	String:       decimalTypeString,
	Format:       decimalTypeFormat,
	Interface:    func(v Value) any { return *(*dec128.Dec128)(v.Ptr) },
	EncodeJSON:   func(v Value) ([]byte, error) { return (*dec128.Dec128)(v.Ptr).MarshalJSON() },
	EncodeBinary: decimalTypeEncodeBinary,
	DecodeBinary: decimalTypeDecodeBinary,
	IsTrue:       func(v Value) bool { return !(*dec128.Dec128)(v.Ptr).IsZero() },
	Equal:        decimalTypeEqual,
	Len:          ConstHook(int64(1)),
	UnaryOp:      decimalTypeUnaryOp,
	BinaryOp:     decimalTypeBinaryOp,
	MethodCall:   decimalTypeMethodCall,
	AsString:     func(v Value) (string, bool) { return (*dec128.Dec128)(v.Ptr).String(), true },
	AsInt:        decimalTypeAsInt,
	AsFloat:      decimalTypeAsFloat,
	AsDecimal:    func(v Value) (dec128.Dec128, bool) { return *(*dec128.Dec128)(v.Ptr), true },
	AsBool:       func(v Value) (bool, bool) { return !(*dec128.Dec128)(v.Ptr).IsZero(), true },
}
View Source
var TypeDict = ValueTypeDescr{
	Name:         SeqNameHook(dictTypeName, immutableDictTypeName),
	String:       dictTypeString,
	Format:       dictTypeFormat,
	Interface:    dictTypeInterface,
	EncodeJSON:   dictTypeEncodeJSON,
	EncodeBinary: dictTypeEncodeBinary,
	DecodeBinary: dictTypeDecodeBinary,
	IsTrue:       dictTypeIsTrue,
	IsIterable:   ConstHook(true),
	Iterator:     dictTypeIterator,
	Equal:        dictTypeEqual,
	Clone:        dictTypeClone,
	Len:          dictTypeLen,
	MethodCall:   dictTypeMethodCall,
	Access:       dictTypeAccess,
	Assign:       dictTypeAssign,
	Contains:     dictTypeContains,
	Delete:       dictTypeDelete,
	AsBool:       dictTypeAsBool,
	AsString:     dictTypeAsString,
	AsDict:       dictTypeAsDict,
}
View Source
var TypeDictIterator = ValueTypeDescr{
	Name:   ConstHook(dictIteratorTypeName),
	String: dictIteratorTypeString,
	Equal:  dictIteratorTypeEqual,
	Next:   dictIteratorTypeNext,
	Key:    dictIteratorTypeKey,
	Value:  dictIteratorTypeValue,
}
View Source
var TypeError = ValueTypeDescr{
	Name:         ConstHook(errorTypeName),
	String:       errorTypeString,
	Format:       errorTypeFormat,
	Interface:    func(v Value) any { return errors.New(v.String()) },
	EncodeJSON:   errorTypeEncodeJSON,
	EncodeBinary: errorTypeEncodeBinary,
	DecodeBinary: errorTypeDecodeBinary,
	IsTrue:       ConstHook(false),
	Equal:        errorTypeEqual,
	Clone:        errorTypeClone,
	MethodCall:   errorTypeMethodCall,
	AsString:     errorTypeAsString,
	AsBool:       Const2Hook(false, true),
}
View Source
var TypeFloat = ValueTypeDescr{
	Name:         ConstHook(floatTypeName),
	String:       floatTypeString,
	Format:       floatTypeFormat,
	Interface:    func(v Value) any { return math.Float64frombits(v.Data) },
	EncodeJSON:   floatTypeEncodeJSON,
	EncodeBinary: floatTypeEncodeBinary,
	DecodeBinary: floatTypeDecodeBinary,
	IsTrue:       func(v Value) bool { return !math.IsNaN(math.Float64frombits(v.Data)) },
	Equal:        floatTypeEqual,
	Len:          ConstHook(int64(1)),
	UnaryOp:      floatTypeUnaryOp,
	BinaryOp:     floatTypeBinaryOp,
	MethodCall:   floatTypeMethodCall,
	AsInt:        floatTypeAsInt,
	AsFloat:      floatTypeAsFloat,
	AsDecimal:    floatTypeAsDecimal,
	AsBool:       floatTypeAsBool,
	AsString:     floatTypeAsString,
}
View Source
var TypeFormatSpec = ValueTypeDescr{
	Name:   ConstHook(formatSpecTypeName),
	String: formatSpecTypeString,
	Equal:  formatSpecTypeEqual,
}
View Source
var TypeInt = ValueTypeDescr{
	Name:         ConstHook(intTypeName),
	String:       func(v Value) string { return strconv.FormatInt(int64(v.Data), 10) },
	Format:       intTypeFormat,
	Interface:    func(v Value) any { return int64(v.Data) },
	EncodeJSON:   intTypeEncodeJSON,
	EncodeBinary: intTypeEncodeBinary,
	DecodeBinary: intTypeDecodeBinary,
	IsTrue:       func(v Value) bool { return v.Data != 0 },
	Equal:        intTypeEqual,
	Len:          ConstHook(int64(1)),
	UnaryOp:      intTypeUnaryOp,
	BinaryOp:     intTypeBinaryOp,
	MethodCall:   intTypeMethodCall,
	AsString:     func(v Value) (string, bool) { return strconv.FormatInt(int64(v.Data), 10), true },
	AsInt:        func(v Value) (int64, bool) { return int64(v.Data), true },
	AsFloat:      func(v Value) (float64, bool) { return float64(int64(v.Data)), true },
	AsDecimal:    func(v Value) (dec128.Dec128, bool) { return dec128.FromInt64(int64(v.Data)), true },
	AsBool:       func(v Value) (bool, bool) { return v.Data != 0, true },
	AsRune:       intTypeAsRune,
	AsTime:       func(v Value) (time.Time, bool) { return time.Unix(int64(v.Data), 0), true },
	AsByte:       intTypeAsByte,
}
View Source
var TypeIntRange = ValueTypeDescr{
	Name:         ConstHook(intRangeTypeName),
	EncodeBinary: intRangeTypeEncodeBinary,
	DecodeBinary: intRangeTypeDecodeBinary,
	String:       intRangeTypeString,
	Format:       intRangeTypeFormat,
	IsTrue:       intRangeTypeIsTrue,
	IsIterable:   ConstHook(true),
	Iterator:     intRangeTypeIterator,
	Equal:        intRangeTypeEqual,
	Len:          intRangeTypeLen,
	MethodCall:   intRangeTypeMethodCall,
	Access:       intRangeTypeAccess,
	Contains:     intRangeTypeContains,
	AsBool:       intRangeTypeAsBool,
	AsArray:      intRangeTypeAsArray,
}
View Source
var TypeIntRangeIterator = ValueTypeDescr{
	Name:   ConstHook(intRangeIteratorTypeName),
	String: intRangeIteratorTypeString,
	Equal:  intRangeIteratorTypeEqual,
	Next:   intRangeIteratorTypeNext,
	Key:    intRangeIteratorTypeKey,
	Value:  intRangeIteratorTypeValue,
}
View Source
var TypeRecord = ValueTypeDescr{
	Name:         SeqNameHook(recordTypeName, immutableRecordTypeName),
	String:       recordTypeString,
	Format:       recordTypeFormat,
	Interface:    recordTypeInterface,
	EncodeJSON:   recordTypeEncodeJSON,
	EncodeBinary: recordTypeEncodeBinary,
	DecodeBinary: recordTypeDecodeBinary,
	IsTrue:       recordTypeIsTrue,
	IsIterable:   ConstHook(true),
	Iterator:     recordTypeIterator,
	Equal:        recordTypeEqual,
	Clone:        recordTypeClone,
	Len:          recordTypeLen,
	MethodCall:   recordTypeMethodCall,
	Access:       recordTypeAccess,
	Assign:       recordTypeAssign,
	Contains:     recordTypeContains,
	Delete:       recordTypeDelete,
	AsBool:       recordTypeAsBool,
	AsString:     recordTypeAsString,
	AsDict:       recordTypeAsDict,
}
View Source
var TypeRune = ValueTypeDescr{
	Name:         ConstHook(runeTypeName),
	String:       func(v Value) string { return fmt.Sprintf("%q", rune(v.Data)) },
	Format:       runeTypeFormat,
	Interface:    func(v Value) any { return rune(v.Data) },
	EncodeJSON:   runeTypeEncodeJSON,
	EncodeBinary: runeTypeEncodeBinary,
	DecodeBinary: runeTypeDecodeBinary,
	IsTrue:       func(v Value) bool { return v.Data != 0 },
	Equal:        runeTypeEqual,
	Len:          ConstHook(int64(1)),
	BinaryOp:     runeTypeBinaryOp,
	MethodCall:   runeTypeMethodCall,
	AsString:     func(v Value) (string, bool) { return string(rune(v.Data)), true },
	AsInt:        func(v Value) (int64, bool) { return int64(v.Data), true },
	AsBool:       func(v Value) (bool, bool) { return v.Data != 0, true },
	AsRune:       func(v Value) (rune, bool) { return rune(v.Data), true },
	AsByte:       runeTypeAsByte,
}
View Source
var TypeRunes = ValueTypeDescr{
	Name:         SeqNameHook(runesTypeName, immutableRunesTypeName),
	String:       func(v Value) string { return "u" + strconv.Quote(string((*Runes)(v.Ptr).Elements)) },
	Format:       runesTypeFormat,
	Interface:    func(v Value) any { return (*Runes)(v.Ptr).Elements },
	EncodeJSON:   runesTypeEncodeJSON,
	EncodeBinary: runesTypeEncodeBinary,
	DecodeBinary: runesTypeDecodeBinary,
	IsTrue:       func(v Value) bool { return len((*Runes)(v.Ptr).Elements) > 0 },
	IsIterable:   ConstHook(true),
	Iterator:     runesTypeIterator,
	Equal:        runesTypeEqual,
	Clone:        runesTypeClone,
	Len:          func(v Value) int64 { return int64(len((*Runes)(v.Ptr).Elements)) },
	BinaryOp:     runesTypeBinaryOp,
	MethodCall:   runesTypeMethodCall,
	Access:       SeqAccessHook(RuneValue, runesTypeResolve),
	Assign:       SeqAssignHook(runesTypeResolve, Value.AsRune, runeTypeName),
	Append:       runesTypeAppend,
	Contains:     runesTypeContains,
	Slice:        SeqSliceHook(NewRunesValue, runesTypeResolve),
	SliceStep:    SeqSliceStepHook(NewRunesValue, runesTypeResolve),
	AsBool:       runesTypeAsBool,
	AsInt:        runesTypeAsInt,
	AsByte:       runesTypeAsByte,
	AsFloat:      runesTypeAsFloat,
	AsDecimal:    runesTypeAsDecimal,
	AsTime:       runesTypeAsTime,
	AsString:     func(v Value) (string, bool) { return string((*Runes)(v.Ptr).Elements), true },
	AsRunes:      func(v Value) ([]rune, bool) { return (*Runes)(v.Ptr).Elements, true },
	AsBytes:      runesTypeAsBytes,
	AsArray:      runesTypeAsArray,
}
View Source
var TypeRunesIterator = ValueTypeDescr{
	Name:   ConstHook(runesIteratorTypeName),
	String: SeqIterStringHook[rune](runesIteratorTypeName, runesIteratorResolve),
	Next:   SeqIterNextHook[rune](runesIteratorResolve),
	Key:    SeqIterKeyHook[rune](runesIteratorResolve),
	Value:  SeqIterValueHook(RuneValue, runesIteratorResolve),
}
View Source
var TypeString = ValueTypeDescr{
	Name:         ConstHook(stringTypeName),
	String:       func(v Value) string { return strconv.Quote(*(*string)(v.Ptr)) },
	Format:       stringTypeFormat,
	Interface:    func(v Value) any { return *(*string)(v.Ptr) },
	EncodeJSON:   stringTypeEncodeJSON,
	EncodeBinary: stringTypeEncodeBinary,
	DecodeBinary: stringTypeDecodeBinary,
	IsTrue:       func(v Value) bool { return len(*(*string)(v.Ptr)) > 0 },
	IsIterable:   ConstHook(true),
	Iterator:     stringTypeIterator,
	Equal:        stringTypeEqual,
	Len:          func(v Value) int64 { return int64(len(*(*string)(v.Ptr))) },
	BinaryOp:     stringTypeBinaryOp,
	MethodCall:   stringTypeMethodCall,
	Access:       stringTypeAccess,
	Contains:     stringTypeContains,
	Slice:        stringTypeSlice,
	SliceStep:    stringTypeSliceStep,
	AsBool:       func(v Value) (bool, bool) { return conv.ParseBool(*(*string)(v.Ptr)) },
	AsInt:        stringTypeAsInt,
	AsByte:       stringTypeAsByte,
	AsFloat:      stringTypeAsFloat,
	AsDecimal:    stringTypeAsDecimal,
	AsTime:       stringTypeAsTime,
	AsString:     func(v Value) (string, bool) { return *(*string)(v.Ptr), true },
	AsRunes:      func(v Value) ([]rune, bool) { return []rune(*(*string)(v.Ptr)), true },
	AsBytes:      func(v Value) ([]byte, bool) { return []byte(*(*string)(v.Ptr)), true },
	AsArray:      stringTypeAsArray,
}

TypeString is a string type descriptor.

View Source
var TypeTime = ValueTypeDescr{
	Name:         ConstHook(timeTypeName),
	String:       timeTypeString,
	Format:       timeTypeFormat,
	Interface:    timeTypeInterface,
	EncodeJSON:   timeTypeEncodeJSON,
	EncodeBinary: timeTypeEncodeBinary,
	DecodeBinary: timeTypeDecodeBinary,
	IsTrue:       timeTypeIsTrue,
	Equal:        timeTypeEqual,
	Len:          ConstHook(int64(1)),
	BinaryOp:     timeTypeBinaryOp,
	MethodCall:   timeTypeMethodCall,
	AsString:     timeTypeAsString,
	AsInt:        timeTypeAsInt,
	AsBool:       timeTypeAsBool,
	AsTime:       timeTypeAsTime,
}

TypeTime is a time type descriptor.

View Source
var TypeUndefined = ValueTypeDescr{
	Name:         ConstHook(undefinedTypeName),
	Interface:    func(Value) any { return nil },
	String:       func(Value) string { return undefinedTypeName },
	Format:       undefinedTypeFormat,
	EncodeJSON:   func(Value) ([]byte, error) { return []byte("null"), nil },
	EncodeBinary: func(Value) ([]byte, error) { return []byte{}, nil },
	DecodeBinary: func(v *Value, _ []byte) error { *v = Undefined; return nil },
	IsTrue:       ConstHook(false),
	IsIterable:   ConstHook(true),
	Equal:        func(v Value, r Value) bool { return v.Type == r.Type },
	MethodCall:   undefinedTypeMethodCall,
	Access:       func(Value, Value, bc.Opcode) (Value, error) { return Undefined, nil },
	AsBool:       func(Value) (bool, bool) { return false, true },
}
View Source
var TypeValuePtr = ValueTypeDescr{
	Name: func(v Value) string {
		return fmt.Sprintf("<%s:%s>", valuePtrTypeName, v.TypeName())
	},
}
View Source
var ValueTypes [256]ValueTypeDescr

ValueTypes is the global registry of value type descriptors, indexed by type ID.

Functions

func Const2Hook added in v0.3.2

func Const2Hook[C1 any, C2 any](c1 C1, c2 C2) func(Value) (C1, C2)

func ConstHook added in v0.3.2

func ConstHook[C any](c C) func(Value) C

func EncodeString

func EncodeString(b []byte, val string) []byte

EncodeString encodes given string as JSON string according to https://www.json.org/img/string.png Implementation is inspired by https://github.com/json-iterator/go

func NormalizeIndex added in v0.3.2

func NormalizeIndex(index int64, length int64) (int64, bool)

NormalizeIndex normalizes index (-1 = last element, -2 = second to last, etc.) and checks if it's within bounds.

func NormalizeSliceBounds added in v0.3.2

func NormalizeSliceBounds(start int64, hasStart bool, end int64, hasEnd bool, length int64) (int64, int64)

NormalizeSliceBounds normalizes slice bounds (negative values count from the end, missing start defaults to 0, missing end defaults to length) and clamps them to [0, length]. If start > end after normalization, start is set to end.

func NormalizeSliceBoundsStep added in v0.3.2

func NormalizeSliceBoundsStep(si int64, hasStart bool, ei int64, hasEnd bool, step int64, length int64) (int64, int64)

NormalizeSliceBoundsStep returns the effective start and end for a step-based slice. Caller must ensure step != 0. For step > 0 the iteration is start..end (exclusive). For step < 0 the iteration is start..end (exclusive, with end possibly -1 to include index 0).

func SeqAccessHook added in v0.3.2

func SeqAccessHook[T any](
	t2v func(T) Value,
	resolve func(Value) *Seq[T],
) func(Value, Value, bc.Opcode) (Value, error)

SeqAccessHook returns a hook function that allows accessing an element of the sequence at a specified index.

func SeqAssignHook added in v0.3.2

func SeqAssignHook[T any](
	resolve func(Value) *Seq[T],
	as func(Value) (T, bool),
	tn string,
) func(Value, Value, Value) error

SeqAssignHook returns a hook function that allows assigning a value to an element of the sequence at a specified index.

func SeqIterKeyHook added in v0.4.1

func SeqIterKeyHook[T any](
	resolve func(v Value) *SeqIter[T],
) func(Value) (Value, error)

func SeqIterNextHook added in v0.4.1

func SeqIterNextHook[T any](
	resolve func(v Value) *SeqIter[T],
) func(Value) bool

func SeqIterStringHook added in v0.4.1

func SeqIterStringHook[T any](
	tn string,
	resolve func(v Value) *SeqIter[T],
) func(Value) string

func SeqIterValueHook added in v0.4.1

func SeqIterValueHook[T any](
	t2v func(T) Value,
	resolve func(v Value) *SeqIter[T],
) func(Value) (Value, error)

func SeqNameHook added in v0.4.1

func SeqNameHook(
	name string,
	immutableName string,
) func(Value) string

SeqNameHook returns a hook function that provides the type name for the sequence based on its mutability.

func SeqSliceHook added in v0.3.2

func SeqSliceHook[T any](
	alloc func([]T, bool) Value,
	resolve func(Value) *Seq[T],
) func(Value, Value, Value) (Value, error)

SeqSliceHook returns a hook function that allows slicing the sequence using start and end indices.

func SeqSliceStepHook added in v0.3.2

func SeqSliceStepHook[T any](
	alloc func([]T, bool) Value,
	resolve func(Value) *Seq[T],
) func(Value, Value, Value, Value) (Value, error)

SeqSliceStepHook returns a hook function that allows slicing the sequence using start and end indices with a specified step.

func SetValueType

func SetValueType(t uint8, f ValueTypeDescr) error

SetValueType registers a user-defined value type descriptor for the given type ID.

func ValueHook added in v0.3.2

func ValueHook(v Value, e error) func(Value) (Value, error)

Types

type Array

type Array = Seq[Value]

type ArrayIterator

type ArrayIterator = SeqIter[Value]

type BuiltinClosure added in v0.4.1

type BuiltinClosure struct {
	Func     NativeFunc
	Name     string
	Arity    int
	Variadic bool
}

func (*BuiltinClosure) Set added in v0.4.1

func (f *BuiltinClosure) Set(fn NativeFunc, name string, arity int, variadic bool)

type BuiltinFunction

type BuiltinFunction struct {
	Func     NativeFunc
	Module   string
	Name     string
	Arity    int
	Variadic bool
}

func NewBuiltinFunction added in v0.4.1

func NewBuiltinFunction(name string, fn NativeFunc, arity int, variadic bool) *BuiltinFunction

NewBuiltinFunction creates a new builtin function object.

type Bytes

type Bytes = Seq[byte]

type BytesIterator

type BytesIterator = SeqIter[byte]

type CompiledFunction

type CompiledFunction struct {
	Instructions  bc.Instructions
	Free          []*Value
	SourceMap     map[int]Pos
	NumLocals     int // number of local variables (including function parameters)
	MaxStack      int // estimated maximum operand-stack depth which can be reached during execution
	NumParameters int
	NamedResult   int // local-slot index of function's named result: 0 = no named result, N > 0 means slot N-1
	VarArgs       bool
}

func (*CompiledFunction) DecodeBinary added in v0.4.1

func (o *CompiledFunction) DecodeBinary(data []byte) error

func (*CompiledFunction) EncodeBinary added in v0.4.1

func (o *CompiledFunction) EncodeBinary() ([]byte, error)

func (*CompiledFunction) GobDecode added in v0.4.1

func (o *CompiledFunction) GobDecode(data []byte) error

GobDecode wraps binary decoding to mirror GobEncode.

func (CompiledFunction) GobEncode added in v0.4.1

func (o CompiledFunction) GobEncode() ([]byte, error)

GobEncode wraps binary encoding so gob does not reflect over fields like Free []*Value (which include unsafe.Pointer).

func (*CompiledFunction) HasNamedResult added in v0.2.1

func (o *CompiledFunction) HasNamedResult() bool

func (*CompiledFunction) NamedResultSlot added in v0.2.1

func (o *CompiledFunction) NamedResultSlot() int

NamedResultSlot returns the local-slot index of the named result. Caller should check HasNamedResult first.

func (*CompiledFunction) Set

func (o *CompiledFunction) Set(instructions bc.Instructions, free []*Value, sourceMap map[int]Pos, numLocals, maxStack int, numParameters int, namedResult int, varArgs bool)

func (*CompiledFunction) Size

func (o *CompiledFunction) Size() int64

func (*CompiledFunction) SourcePos

func (o *CompiledFunction) SourcePos(ip int) Pos

type Dict added in v0.0.8

type Dict struct {
	Elements map[string]Value
}

func (*Dict) Set added in v0.0.8

func (o *Dict) Set(elements map[string]Value)

type DictIterator added in v0.0.8

type DictIterator struct {
	Elements map[string]Value
	Keys     []string
	// contains filtered or unexported fields
}

func (*DictIterator) Set added in v0.0.8

func (o *DictIterator) Set(m map[string]Value)

type Error

type Error struct {
	Payload Value
	Kind    string
	Fatal   bool
}

func (*Error) Set

func (e *Error) Set(payload Value, kind string, fatal bool)

type FormatSpec added in v0.4.1

type FormatSpec struct {
	Spec fspec.FormatSpec
	Text string // original mini-language text (without the leading ':')
}

FormatSpec wraps a fully parsed fspec.FormatSpec together with its original textual form. It is an internal value kind: it lives only in the constant pool (referenced by OpFormat) and is never visible to user code.

func (FormatSpec) Equal added in v0.4.1

func (f FormatSpec) Equal(other FormatSpec) bool

func (*FormatSpec) Set added in v0.4.1

func (f *FormatSpec) Set(spec fspec.FormatSpec, text string)

type IntRange

type IntRange struct {
	Start int64
	Stop  int64
	Step  int64
}

func (*IntRange) Contains

func (o *IntRange) Contains(i int64) bool

func (*IntRange) Empty

func (o *IntRange) Empty() bool

func (*IntRange) Get

func (o *IntRange) Get(i int64) (int64, bool)

func (*IntRange) Len

func (o *IntRange) Len() int64

func (*IntRange) Set

func (o *IntRange) Set(start, stop, step int64)

type IntRangeIterator

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

func (*IntRangeIterator) Set

func (i *IntRangeIterator) Set(start, stop, step int64)

type NativeFunc

type NativeFunc = func(VM, []Value) (Value, error)

type Pos

type Pos int

func (Pos) IsValid

func (p Pos) IsValid() bool

type Primitive added in v0.4.1

type Primitive struct {
	Type uint8
	Data uint64
}

Primitive value (used in static storage)

func (Primitive) Value added in v0.4.1

func (p Primitive) Value() Value

type Record added in v0.4.1

type Record struct {
	Elements map[string]Value
}

func (*Record) Set added in v0.4.1

func (o *Record) Set(elements map[string]Value)

type Runes added in v0.0.6

type Runes = Seq[rune]

type RunesIterator added in v0.0.6

type RunesIterator = SeqIter[rune]

type Seq added in v0.3.2

type Seq[T any] struct {
	Elements []T
}

func (*Seq[T]) Set added in v0.3.2

func (o *Seq[T]) Set(elements []T)

type SeqIter added in v0.4.1

type SeqIter[T any] struct {
	Elements []T
	// contains filtered or unexported fields
}

func (*SeqIter[T]) Set added in v0.4.1

func (i *SeqIter[T]) Set(v []T)

type Static added in v0.4.1

type Static struct {
	Primitives        []Primitive
	Decimals          []dec128.Dec128
	Strings           []string
	Runes             []Runes
	Bytes             []Bytes
	Times             []time.Time
	FormatSpecs       []FormatSpec
	CompiledFunctions []CompiledFunction
}

Static variables

type VM

type VM interface {
	Abort()                             // aborts execution of the current script
	IsStackEmpty() bool                 // returns true if there are no frames on the call stack
	Call(Value, []Value) (Value, error) // calls a compiled function
	Run() error                         // runs the VM until completion
	Recover() Value                     // returns the in-flight error if in "deferred-for" frame
}

type Value

type Value struct {
	Type      uint8
	Immutable bool
	Data      uint64
	Ptr       unsafe.Pointer
}

Value represents a boxed Kavun value.

func BoolValue

func BoolValue(b bool) Value

func BuiltinFunctionValue

func BuiltinFunctionValue(id uint64) Value

BuiltinFunctionValue creates new boxed builtin function value.

func ByteValue added in v0.0.10

func ByteValue(v byte) Value

func FloatValue

func FloatValue(f float64) Value

func ForEachCallback added in v0.3.2

func ForEachCallback(args []Value) (Value, error)

ForEachCallback validates that the only argument is a callback (non-variadic function of arity 1 or 2) and returns it as a Value.

func IntValue

func IntValue(i int64) Value

func NewArrayIteratorValue

func NewArrayIteratorValue(arr []Value) Value

func NewArrayValue

func NewArrayValue(arr []Value, immutable bool) Value

func NewBuiltinClosureValue added in v0.4.1

func NewBuiltinClosureValue(name string, fn NativeFunc, arity int, variadic bool) Value

func NewBytesIteratorValue

func NewBytesIteratorValue(b []byte) Value

func NewBytesValue

func NewBytesValue(b []byte, immutable bool) Value

func NewCompiledFunctionValue

func NewCompiledFunctionValue(
	instructions bc.Instructions,
	free []*Value,
	sourceMap map[int]Pos,
	numLocals int,
	maxStack int,
	numParameters int,
	namedResult int,
	varArgs bool,
) Value

func NewDecimalValue

func NewDecimalValue(d dec128.Dec128) Value

func NewDictIteratorValue added in v0.0.8

func NewDictIteratorValue(m map[string]Value) Value

func NewDictValue added in v0.0.8

func NewDictValue(m map[string]Value, immutable bool) Value

func NewErrorValue

func NewErrorValue(payload Value, kind string, fatal bool) Value

func NewIntRangeIteratorValue

func NewIntRangeIteratorValue(start, stop, step int64) Value

func NewIntRangeValue

func NewIntRangeValue(start, stop, step int64) Value

func NewRecordValue

func NewRecordValue(m map[string]Value, immutable bool) Value

func NewRunesIteratorValue added in v0.0.6

func NewRunesIteratorValue(s []rune) Value

func NewRunesValue added in v0.0.6

func NewRunesValue(r []rune, immutable bool) Value

func NewRuntimeErrorValue added in v0.2.1

func NewRuntimeErrorValue(kind string, fatal bool, message string) Value

func NewStaticBytesValue added in v0.5.1

func NewStaticBytesValue(b *Bytes) Value

func NewStaticCompiledFunctionValue added in v0.4.1

func NewStaticCompiledFunctionValue(cf *CompiledFunction) Value

func NewStaticDecimalValue added in v0.4.1

func NewStaticDecimalValue(d *dec128.Dec128) Value

func NewStaticFormatSpecValue added in v0.4.1

func NewStaticFormatSpecValue(fs *FormatSpec) Value

func NewStaticRunesValue added in v0.4.1

func NewStaticRunesValue(r *Runes) Value

func NewStaticStringValue added in v0.4.1

func NewStaticStringValue(s *string) Value

func NewStaticTimeValue added in v0.5.1

func NewStaticTimeValue(t *time.Time) Value

func NewStringValue

func NewStringValue(s string) Value

func NewTimeValue

func NewTimeValue(t time.Time) Value

func NewValuePtrValue added in v0.4.1

func NewValuePtrValue(p *Value) Value

func RefValue added in v0.3.2

func RefValue(v Value) Value

RefValue is a dummy constructor used in internal generics.

func RuneValue added in v0.0.6

func RuneValue(c rune) Value

func SeqAll added in v0.3.2

func SeqAll[T any](
	vm VM,
	v Value,
	args []Value,
	t2v func(T) Value,
	resolve func(Value) *Seq[T],
) (Value, error)

SeqAll checks if all elements in the sequence satisfy a given condition.

func SeqAny added in v0.3.2

func SeqAny[T any](
	vm VM,
	v Value,
	args []Value,
	t2v func(T) Value,
	resolve func(Value) *Seq[T],
) (Value, error)

SeqAny checks if any element in the sequence satisfy a given condition.

func SeqChunk added in v0.3.2

func SeqChunk[T any](
	v Value,
	args []Value,
	alloc func([]T, bool) Value,
	resolve func(Value) *Seq[T],
) (Value, error)

SeqChunk divides the sequence into chunks of the specified size and returns a new sequence containing the chunks.

func SeqCount added in v0.3.2

func SeqCount[T comparable](
	vm VM,
	v Value,
	args []Value,
	t2v func(T) Value,
	resolve func(Value) *Seq[T],
) (Value, error)

SeqCount counts the number of elements in the sequence that satisfy a given condition.

func SeqFilter added in v0.3.2

func SeqFilter[T comparable](
	vm VM,
	v Value,
	args []Value,
	t2v func(T) Value,
	alloc func([]T, bool) Value,
	resolve func(Value) *Seq[T],
) (Value, error)

SeqFilter filters the elements of the sequence and returns a new sequence. If no arguments provided, it filters out zero values. If a function is provided, it filters out elements for which the function returns false. The function can have arity 1 (element) or 2 (index, element).

func SeqFind added in v0.3.2

func SeqFind[T any](
	vm VM,
	v Value,
	args []Value,
	t2v func(T) Value,
	resolve func(Value) *Seq[T],
) (Value, error)

SeqFind searches for the first element in the sequence that satisfies a given condition and returns its index.

func SeqForEach added in v0.3.2

func SeqForEach[T any](
	vm VM,
	v Value,
	args []Value,
	t2v func(T) Value,
	resolve func(Value) *Seq[T],
) (Value, error)

SeqForEach iterates over the elements of the sequence and calls the provided callback function for each element.

func SeqMap added in v0.3.2

func SeqMap[T any](
	vm VM,
	v Value,
	args []Value,
	t2v func(T) Value,
	resolve func(Value) *Seq[T],
) (Value, error)

SeqMap applies a given function to each element in the sequence and returns a new sequence containing the results.

func SeqReduce added in v0.3.2

func SeqReduce[T any](
	vm VM,
	v Value,
	args []Value,
	t2v func(T) Value,
	resolve func(Value) *Seq[T],
) (Value, error)

SeqReduce reduces the sequence to a single value by applying a given binary function cumulatively to the elements of the sequence, from left to right. The function can have arity 2 (accumulator, element) or 3 (accumulator, index, element).

func (Value) Access

func (v Value) Access(index Value, mode bc.Opcode) (Value, error)

func (Value) Append

func (v Value) Append(args []Value) (Value, error)

func (Value) Arity

func (v Value) Arity() int

func (Value) AsArray

func (v Value) AsArray() ([]Value, bool)

func (Value) AsBool

func (v Value) AsBool() (bool, bool)

func (Value) AsByte

func (v Value) AsByte() (byte, bool)

func (Value) AsBytes

func (v Value) AsBytes() ([]byte, bool)

func (Value) AsDecimal

func (v Value) AsDecimal() (dec128.Dec128, bool)

func (Value) AsDict added in v0.0.8

func (v Value) AsDict() (map[string]Value, bool)

func (Value) AsFloat

func (v Value) AsFloat() (float64, bool)

func (Value) AsInt

func (v Value) AsInt() (int64, bool)

func (Value) AsRune added in v0.0.6

func (v Value) AsRune() (rune, bool)

func (Value) AsRunes added in v0.0.6

func (v Value) AsRunes() ([]rune, bool)

func (Value) AsString

func (v Value) AsString() (string, bool)

func (Value) AsTime

func (v Value) AsTime() (time.Time, bool)

func (Value) AsValue added in v0.3.2

func (v Value) AsValue() (Value, bool)

func (Value) Assign

func (v Value) Assign(idx Value, val Value) error

func (Value) BinaryOp

func (v Value) BinaryOp(op token.Token, rhs Value) (Value, error)

func (Value) Call

func (v Value) Call(vm VM, args []Value) (Value, error)

func (*Value) Clone added in v0.4.1

func (v *Value) Clone() (Value, error)

func (Value) Contains

func (v Value) Contains(e Value) bool

func (*Value) DecodeBinary

func (v *Value) DecodeBinary(data []byte) error

func (Value) Delete

func (v Value) Delete(key Value) (Value, error)

func (Value) EncodeBinary

func (v Value) EncodeBinary() ([]byte, error)

func (Value) EncodeJSON

func (v Value) EncodeJSON() ([]byte, error)

func (Value) Equal

func (v Value) Equal(rhs Value) bool

func (Value) Format added in v0.1.3

func (v Value) Format(sp fspec.FormatSpec) (string, error)

func (*Value) GobDecode

func (v *Value) GobDecode(data []byte) error

GobDecode wraps binary decoding to mirror GobEncode.

func (Value) GobEncode

func (v Value) GobEncode() ([]byte, error)

GobEncode wraps binary encoding so gob does not reflect over unsafe.Pointer field.

func (Value) Interface

func (v Value) Interface() any

func (Value) IsCallable

func (v Value) IsCallable() bool

func (Value) IsIterable

func (v Value) IsIterable() bool

func (Value) IsPrimitive added in v0.4.1

func (v Value) IsPrimitive() bool

func (Value) IsTrue

func (v Value) IsTrue() bool

func (Value) IsUserDefined

func (v Value) IsUserDefined() bool

func (Value) IsVariadic

func (v Value) IsVariadic() bool

func (Value) Iterator

func (v Value) Iterator() (Value, error)

func (Value) Key

func (v Value) Key() (Value, error)

func (Value) Len

func (v Value) Len() int64

func (Value) MethodCall

func (v Value) MethodCall(vm VM, name string, args []Value) (Value, error)

func (Value) Next

func (v Value) Next() bool

func (*Value) Set

func (v *Value) Set(val Value)

func (Value) Slice

func (v Value) Slice(s Value, e Value) (Value, error)

func (Value) SliceStep added in v0.0.10

func (v Value) SliceStep(s Value, e Value, step Value) (Value, error)

func (Value) String

func (v Value) String() string

func (Value) ToImmutable added in v0.3.2

func (v Value) ToImmutable() (Value, error)

func (Value) TypeName

func (v Value) TypeName() string

func (Value) UnaryOp

func (v Value) UnaryOp(op token.Token) (Value, error)

func (Value) Value

func (v Value) Value() (Value, error)

type ValueTypeDescr added in v0.4.1

type ValueTypeDescr struct {
	Name         func(v Value) string
	String       func(v Value) string
	Format       func(v Value, sp fspec.FormatSpec) (string, error)
	Interface    func(v Value) any
	EncodeJSON   func(v Value) ([]byte, error)
	EncodeBinary func(v Value) ([]byte, error)
	DecodeBinary func(v *Value, data []byte) error
	IsTrue       func(v Value) bool
	Clone        func(v Value) (Value, error)
	Equal        func(v Value, r Value) bool
	UnaryOp      func(v Value, op token.Token) (Value, error)
	BinaryOp     func(v Value, r Value, op token.Token) (Value, error)
	MethodCall   func(vm VM, v Value, name string, args []Value) (Value, error)

	IsIterable func(v Value) bool
	Contains   func(v Value, e Value) bool
	Len        func(v Value) int64
	Iterator   func(v Value) (Value, error)
	Access     func(v Value, index Value, mode bc.Opcode) (Value, error)
	Assign     func(v Value, index Value, r Value) error
	Append     func(v Value, args []Value) (Value, error)
	Slice      func(v Value, s Value, e Value) (Value, error)
	Delete     func(v Value, key Value) (Value, error)
	SliceStep  func(v Value, s Value, e Value, step Value) (Value, error)

	IsCallable func(v Value) bool
	IsVariadic func(v Value) bool
	Arity      func(v Value) int
	Call       func(vm VM, v Value, args []Value) (Value, error)

	Next  func(v Value) bool
	Key   func(v Value) (Value, error)
	Value func(v Value) (Value, error)

	AsBool    func(v Value) (bool, bool)
	AsByte    func(v Value) (byte, bool)
	AsRune    func(v Value) (rune, bool)
	AsInt     func(v Value) (int64, bool)
	AsFloat   func(v Value) (float64, bool)
	AsDecimal func(v Value) (dec128.Dec128, bool)
	AsTime    func(v Value) (time.Time, bool)
	AsString  func(v Value) (string, bool)
	AsRunes   func(v Value) ([]rune, bool)
	AsBytes   func(v Value) ([]byte, bool)
	AsArray   func(v Value) ([]Value, bool)
	AsDict    func(v Value) (map[string]Value, bool)
}

ValueTypeDescr is a Kavun data type descriptor structure.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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