types

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 10, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package types defines the core type system and Object interface for Nxlang

Index

Constants

View Source
const (
	TypeUndefined      = 0x00
	TypeNull           = 0x01
	TypeBool           = 0x02
	TypeByte           = 0x03
	TypeChar           = 0x04
	TypeInt            = 0x05
	TypeUInt           = 0x06
	TypeFloat          = 0x07
	TypeString         = 0x08
	TypeArray          = 0x10
	TypeMap            = 0x11
	TypeOrderedMap     = 0x12
	TypeStack          = 0x13
	TypeQueue          = 0x14
	TypeSeq            = 0x15
	TypeFunction       = 0x20
	TypeClosure        = 0x21
	TypeNativeFunc     = 0x22
	TypeClass          = 0x30
	TypeObject         = 0x31
	TypeInterface      = 0x32
	TypeBoundMethod    = 0x33
	TypeSuperReference = 0x34
	TypeRef            = 0x35 // Object reference type
	TypeObjectType     = 0x36 // Type object (for int, float, string etc. with static methods)
	TypeError          = 0x40
	TypeMutex          = 0x50
	TypeRWMutex        = 0x51
	TypeThread         = 0x52
	TypeFile           = 0x60
	TypeReader         = 0x61
	TypeWriter         = 0x62
	TypeStringBuilder  = 0x63
	TypeBytesBuffer    = 0x64
)

Fixed type code constants (no iota for cross-version compatibility)

Variables

View Source
var NullValue = &Null{}
View Source
var UndefinedValue = &Undefined{}

Functions

func ToBool

func ToBool(obj Object) bool

ToBool converts an Object to a boolean value following Nxlang conversion rules

func ToByte

func ToByte(obj Object) (Byte, *Error)

ToByte converts an Object to a Byte value

func ToChar

func ToChar(obj Object) (Char, *Error)

ToChar converts an Object to a Char value

func ToFloat

func ToFloat(obj Object) (Float, *Error)

ToFloat converts an Object to a Float value following Nxlang conversion rules

func ToInt

func ToInt(obj Object) (Int, *Error)

ToInt converts an Object to an Int value following Nxlang conversion rules

func ToUint

func ToUint(obj Object) (UInt, *Error)

ToUint converts an Object to a UInt value following Nxlang conversion rules

Types

type Bool

type Bool bool

Bool represents a boolean value

func (Bool) Equals

func (b Bool) Equals(other Object) bool

Equals implements Object interface

func (Bool) ToStr

func (b Bool) ToStr() string

ToStr implements Object interface

func (Bool) TypeCode

func (b Bool) TypeCode() uint8

TypeCode implements Object interface

func (Bool) TypeName

func (b Bool) TypeName() string

TypeName implements Object interface

type BoundMethod

type BoundMethod struct {
	Instance *Instance // The instance this method is bound to
	Method   *Function // The actual function to call
}

BoundMethod represents a method bound to a specific instance

func (*BoundMethod) Equals

func (bm *BoundMethod) Equals(other Object) bool

func (*BoundMethod) ToStr

func (bm *BoundMethod) ToStr() string

func (*BoundMethod) TypeCode

func (bm *BoundMethod) TypeCode() uint8

func (*BoundMethod) TypeName

func (bm *BoundMethod) TypeName() string

type Byte

type Byte byte

Byte represents a byte value (uint8)

func (Byte) Equals

func (b Byte) Equals(other Object) bool

Equals implements Object interface

func (Byte) ToStr

func (b Byte) ToStr() string

ToStr implements Object interface

func (Byte) TypeCode

func (b Byte) TypeCode() uint8

TypeCode implements Object interface

func (Byte) TypeName

func (b Byte) TypeName() string

TypeName implements Object interface

type Char

type Char rune

Char represents a Unicode character

func (Char) Equals

func (c Char) Equals(other Object) bool

Equals implements Object interface

func (Char) ToStr

func (c Char) ToStr() string

ToStr implements Object interface

func (Char) TypeCode

func (c Char) TypeCode() uint8

TypeCode implements Object interface

func (Char) TypeName

func (c Char) TypeName() string

TypeName implements Object interface

type Class

type Class struct {
	Name          string
	SuperClass    *Class
	Methods       map[string]*Function // Map of method name to function
	StaticFields  map[string]Object    // Static properties of the class
	StaticMethods map[string]*Function // Map of static method name to function
}

Class represents a compiled class definition

func (*Class) Equals

func (c *Class) Equals(other Object) bool

func (*Class) ToStr

func (c *Class) ToStr() string

func (*Class) TypeCode

func (c *Class) TypeCode() uint8

func (*Class) TypeName

func (c *Class) TypeName() string

type Error

type Error struct {
	Message    string
	Line       int
	Column     int
	Filename   string
	Code       string               // Code snippet where the error occurred
	Stack      []StackFrame         // Call stack trace
	ErrorType  string               // Custom error type name
	Properties map[string]Object    // Custom properties for user-defined errors
	Methods    map[string]*Function // Custom methods for user-defined errors
}

Error represents a runtime error in Nxlang

func NewCustomError

func NewCustomError(typeName string, message string, line int, column int, filename string) *Error

NewCustomError creates a new custom Error instance with the given type name

func NewError

func NewError(message string, line int, column int, filename string) *Error

NewError creates a new Error instance with basic message

func NewErrorWithCode

func NewErrorWithCode(message string, line int, column int, filename string, code string) *Error

NewErrorWithCode creates a new Error instance with code snippet

func NewErrorWithStack

func NewErrorWithStack(message string, line int, column int, filename string, stack []StackFrame) *Error

NewErrorWithStack creates a new Error instance with call stack trace

func (*Error) Equals

func (e *Error) Equals(other Object) bool

Equals implements Object interface

func (*Error) Error

func (e *Error) Error() string

Error implements the standard Go error interface

func (*Error) ToStr

func (e *Error) ToStr() string

ToStr implements Object interface

func (*Error) TypeCode

func (e *Error) TypeCode() uint8

TypeCode implements Object interface

func (*Error) TypeName

func (e *Error) TypeName() string

TypeName implements Object interface

type Float

type Float float64

Float represents a floating point value (float64)

func (Float) Equals

func (f Float) Equals(other Object) bool

Equals implements Object interface

func (Float) ToStr

func (f Float) ToStr() string

ToStr implements Object interface

func (Float) TypeCode

func (f Float) TypeCode() uint8

TypeCode implements Object interface

func (Float) TypeName

func (f Float) TypeName() string

TypeName implements Object interface

type Function

type Function struct {
	Name           string
	NumLocals      int
	NumParameters  int
	IsVariadic     bool
	IsStatic       bool   // Whether this is a static method
	AccessModifier uint8  // 0=public, 1=private, 2=protected
	IsGetter       bool   // Whether this is a getter property
	IsSetter       bool   // Whether this is a setter property
	OwnerClass     *Class // The class that owns this method (for methods only)
	DefaultValues  []int  // Indices of default values in constant pool
	Instructions   []byte
	ConstantPool   []bytecode.Constant // Constant pool this function belongs to
}

Function represents a compiled function

func (*Function) Equals

func (f *Function) Equals(other Object) bool

Equals implements Object interface

func (*Function) ToStr

func (f *Function) ToStr() string

ToStr implements Object interface

func (*Function) TypeCode

func (f *Function) TypeCode() uint8

TypeCode implements Object interface

func (*Function) TypeName

func (f *Function) TypeName() string

TypeName implements Object interface

type Instance

type Instance struct {
	Class      *Class
	Properties map[string]Object // Object properties
}

Instance represents an instance of a class

func (*Instance) Equals

func (i *Instance) Equals(other Object) bool

func (*Instance) ToStr

func (i *Instance) ToStr() string

func (*Instance) TypeCode

func (i *Instance) TypeCode() uint8

func (*Instance) TypeName

func (i *Instance) TypeName() string

type Int

type Int int64

Int represents an integer value (int64)

func (Int) Equals

func (i Int) Equals(other Object) bool

Equals implements Object interface

func (Int) ToStr

func (i Int) ToStr() string

ToStr implements Object interface

func (Int) TypeCode

func (i Int) TypeCode() uint8

TypeCode implements Object interface

func (Int) TypeName

func (i Int) TypeName() string

TypeName implements Object interface

type Interface

type Interface struct {
	Name    string
	Methods map[string][]string // Method name -> parameter names
}

Interface represents a Nxlang interface type

func (*Interface) Equals

func (iface *Interface) Equals(other Object) bool

func (*Interface) ToStr

func (iface *Interface) ToStr() string

func (*Interface) TypeCode

func (iface *Interface) TypeCode() uint8

func (*Interface) TypeName

func (iface *Interface) TypeName() string

type NativeFunction

type NativeFunction struct {
	Fn func(args ...Object) Object
}

NativeFunction represents a native Go function callable from Nxlang

func (*NativeFunction) Equals

func (nf *NativeFunction) Equals(other Object) bool

func (*NativeFunction) ToStr

func (nf *NativeFunction) ToStr() string

func (*NativeFunction) TypeCode

func (nf *NativeFunction) TypeCode() uint8

func (*NativeFunction) TypeName

func (nf *NativeFunction) TypeName() string

type Null

type Null struct{}

Null represents the null value

func (*Null) Equals

func (n *Null) Equals(other Object) bool

Equals implements Object interface

func (*Null) ToStr

func (n *Null) ToStr() string

ToStr implements Object interface

func (*Null) TypeCode

func (n *Null) TypeCode() uint8

TypeCode implements Object interface

func (*Null) TypeName

func (n *Null) TypeName() string

TypeName implements Object interface

type Object

type Object interface {
	// TypeCode returns the fixed type code for this object
	TypeCode() uint8

	// TypeName returns the human-readable type name
	TypeName() string

	// ToStr returns the string representation of the object
	ToStr() string

	// Equals checks if this object is equal to another object
	Equals(other Object) bool
}

Object is the core interface implemented by all Nxlang values

type Ref

type Ref struct {
	Value Object // The value held by this reference (exported for VM access)
}

Ref represents a reference to any Nxlang object, allowing indirect modification

func NewRef

func NewRef(val Object) *Ref

NewRef creates a new reference to a value

func (*Ref) Equals

func (r *Ref) Equals(other Object) bool

func (*Ref) Get

func (r *Ref) Get() Object

Get returns the value held by this reference

func (*Ref) Set

func (r *Ref) Set(val Object)

Set sets the value held by this reference

func (*Ref) ToStr

func (r *Ref) ToStr() string

func (*Ref) TypeCode

func (r *Ref) TypeCode() uint8

func (*Ref) TypeName

func (r *Ref) TypeName() string

type StackFrame

type StackFrame struct {
	FunctionName string
	Line         int
	Column       int
	Filename     string
}

StackFrame represents a single frame in the call stack

type String

type String string

String represents a string value

func ToString

func ToString(obj Object) String

ToString converts an Object to a String value

func (String) Equals

func (s String) Equals(other Object) bool

Equals implements Object interface

func (String) ToStr

func (s String) ToStr() string

ToStr implements Object interface

func (String) TypeCode

func (s String) TypeCode() uint8

TypeCode implements Object interface

func (String) TypeName

func (s String) TypeName() string

TypeName implements Object interface

type SuperReference

type SuperReference struct {
	Instance *Instance // The current instance
	Super    *Class    // The superclass
}

SuperReference represents a reference to superclass with bound instance

func (*SuperReference) Equals

func (sr *SuperReference) Equals(other Object) bool

func (*SuperReference) ToStr

func (sr *SuperReference) ToStr() string

func (*SuperReference) TypeCode

func (sr *SuperReference) TypeCode() uint8

func (*SuperReference) TypeName

func (sr *SuperReference) TypeName() string

type TypeWrapper

type TypeWrapper struct {
	Name          string                      // Type name (e.g., "int", "float")
	ConvertFn     func(args ...Object) Object // Conversion function when called as type(x)
	StaticMethods map[string]*NativeFunction  // Static methods accessible via type.method()
}

TypeWrapper represents a type with static methods (e.g., int, float, string) It can be called as a function (for type conversion) and have static methods accessed

func (*TypeWrapper) Call

func (t *TypeWrapper) Call(args ...Object) Object

Call invokes the type conversion function

func (*TypeWrapper) Equals

func (t *TypeWrapper) Equals(other Object) bool

func (*TypeWrapper) ToStr

func (t *TypeWrapper) ToStr() string

func (*TypeWrapper) TypeCode

func (t *TypeWrapper) TypeCode() uint8

func (*TypeWrapper) TypeName

func (t *TypeWrapper) TypeName() string

type UInt

type UInt uint64

UInt represents an unsigned integer value (uint64)

func (UInt) Equals

func (u UInt) Equals(other Object) bool

Equals implements Object interface

func (UInt) ToStr

func (u UInt) ToStr() string

ToStr implements Object interface

func (UInt) TypeCode

func (u UInt) TypeCode() uint8

TypeCode implements Object interface

func (UInt) TypeName

func (u UInt) TypeName() string

TypeName implements Object interface

type Undefined

type Undefined struct{}

Undefined represents the undefined value

func (*Undefined) Equals

func (u *Undefined) Equals(other Object) bool

Equals implements Object interface

func (*Undefined) ToStr

func (u *Undefined) ToStr() string

ToStr implements Object interface

func (*Undefined) TypeCode

func (u *Undefined) TypeCode() uint8

TypeCode implements Object interface

func (*Undefined) TypeName

func (u *Undefined) TypeName() string

TypeName implements Object interface

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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