Documentation
¶
Overview ¶
Package value defines Kukel's runtime values and the semantics of the operators on them. The compiler uses the same operations for constant folding as the VM uses at run time.
Index ¶
- Constants
- Variables
- func ArgumentRange(min, max int) string
- func Arguments(n int) string
- func Compare(op CmpOp, a, b Value) (bool, error)
- func Equal(a, b Value) bool
- func FormatFloat(f float64) string
- func Len(v Value) (int, error)
- func Quote(s string) string
- func SetIndex(x, i, v Value) error
- func SetMember(p Value, name string, v Value) error
- type ArithOp
- type BoundMethodObj
- type BuiltinObj
- type CellObj
- type ClassObj
- type ClosureObj
- type CmpOp
- type Env
- type Error
- type ExitError
- type IteratorObj
- type Kind
- type MapObj
- type MethodObj
- type Named
- type NativeMethod
- type ObjectObj
- type Value
- func Arith(op ArithOp, a, b Value) (Value, error)
- func BitNot(v Value) (Value, error)
- func Concat(a, b Value) Value
- func Detached(v Value) Value
- func IncDec(v Value, delta int64) (Value, error)
- func Index(x, i Value) (Value, error)
- func MakeBool(b bool) Value
- func MakeBoundMethod(recv Value, m *MethodObj) Value
- func MakeBuiltin(b *BuiltinObj) Value
- func MakeClass(c *ClassObj) Value
- func MakeClosure(c *ClosureObj) Value
- func MakeFloat(f float64) Value
- func MakeFunction(f Named) Value
- func MakeInt(i int64) Value
- func MakeInternal(x any) Value
- func MakeMap(m *MapObj) Value
- func MakeString(s string) Value
- func MakeVector(elems []Value) Value
- func Member(p Value, name string, function func(Named) Value) (Value, error)
- func Neg(v Value) (Value, error)
- func NewCell(v Value) Value
- func NewIterator(x Value) (Value, error)
- func NewObject(c *ClassObj) Value
- func ObjectValue(o *ObjectObj) Value
- func (v Value) AsBool() bool
- func (v Value) AsBoundMethod() *BoundMethodObj
- func (v Value) AsBuiltin() *BuiltinObj
- func (v Value) AsCell() *CellObj
- func (v Value) AsClass() *ClassObj
- func (v Value) AsClosure() *ClosureObj
- func (v Value) AsFloat() float64
- func (v Value) AsInt() int64
- func (v Value) AsMap() *MapObj
- func (v Value) AsObject() *ObjectObj
- func (v Value) AsString() string
- func (v Value) AsVector() *VectorObj
- func (v Value) IntValue() (int64, bool)
- func (v Value) IsCallable() bool
- func (v Value) IsNil() bool
- func (v Value) Kind() Kind
- func (v Value) Next() (first, second Value, ok bool)
- func (v Value) Obj() any
- func (v Value) OneNameGetsKey() bool
- func (v Value) Repr() string
- func (v Value) String() string
- func (v Value) Truthy() bool
- type VectorObj
Constants ¶
const ( TypeError = "TypeError" ZeroDivision = "ZeroDivisionError" RangeError = "RangeError" ArgumentError = "ArgumentError" StackOverflow = "StackOverflowError" IOError = "IOError" MemberError = "MemberError" ValueError = "ValueError" InternalError = "InternalError" )
Runtime error kinds.
Variables ¶
var ( NilValue = Value{} True = Value{/* contains filtered or unexported fields */} False = Value{/* contains filtered or unexported fields */} )
Functions ¶
func ArgumentRange ¶ added in v0.31.0
ArgumentRange gives a range of numbers of arguments in words, for messages: "1 to 3 arguments", "at most 1 argument".
func Arguments ¶ added in v0.27.0
Arguments gives a number of arguments in words, for messages: "1 argument", "2 arguments".
func Compare ¶
Compare applies a relational operator: numbers with numbers, strings with strings (byte order); other combinations are type errors. Any comparison with NaN is false.
func Equal ¶
Equal reports whether a == b: numbers by value (1 == 1.0), strings by bytes, nil and booleans by value, everything else by identity. Values of different kinds are never equal (except int and float).
func FormatFloat ¶
FormatFloat formats f as the shortest text that reads back as f, always with a "." or an exponent so that it does not look like an int.
Types ¶
type BoundMethodObj ¶
BoundMethodObj is a method together with the object it was taken from (the value of p->m without a call).
type BuiltinObj ¶
BuiltinObj is a function implemented in Go. It returns an error message (a runtime error) or a result. args is a view of the VM's stack, valid only during the call: copy values out of it, never keep the slice.
type CellObj ¶
type CellObj struct {
V Value
}
CellObj holds a captured variable, shared by the frame that declared it and the closures that captured it.
type ClassObj ¶
type ClassObj struct {
Name string
Base *ClassObj
FieldNames []string // instance data members, inherited ones first
FieldIndex map[string]int // name -> slot in ObjectObj.Fields
Methods map[string]*MethodObj // own and inherited member functions (overrides resolved)
Statics []Value // the class's own static data members
StaticNames []string // their names
Ctor *MethodObj // C::C, else the nearest base class's constructor, or nil
// Native classes (written in Go) only: Native is set, and Show is how
// str() shows an object (a native class has no data members).
Native bool
Show func(o *ObjectObj) string
}
ClassObj is a class at run time.
func (*ClassObj) IsSubclassOf ¶
IsSubclassOf reports whether c is k or derives from k.
type ClosureObj ¶
type ClosureObj struct {
Fn Named
Cells []*CellObj
This *ObjectObj // nil outside member functions
}
ClosureObj is a function literal's value: the function, the cells of the variables it captured, and the this of the member function it was made in.
type Env ¶
type Env interface {
Stdout() io.Writer
Stderr() io.Writer
Stdin() *bufio.Reader
// Call calls a Kukel function (or built-in) and returns its result.
Call(fn Value, args []Value) (Value, error)
// Rand is the program's random number source (seed sets it).
Rand() *rand.Rand
// Started is when the program started (for clock).
Started() time.Time
// Local returns the value stored under key for this interpreter,
// storing init() first if there is none: per-program state of a
// native module (such as its event loop).
Local(key any, init func() any) any
// Thrown returns the value a Kukel exception carries, when err (from
// Call) is one that a try could catch; not for exit() or fatal errors.
Thrown(err error) (Value, bool)
// Done is closed when the program is cancelled (nil if it cannot be);
// a Go function that waits should also wait for it and then return
// Err().
Done() <-chan struct{}
Err() error
}
Env is what built-in functions get from the VM.
type Error ¶
Error is a runtime error raised by an operation. Kind names the class of error; it becomes the "kind" field of the Error object the VM throws.
type ExitError ¶
type ExitError struct{ Code int }
ExitError is returned by the exit built-in: the program stops at once with this exit code (try cannot catch it; finally blocks do not run).
type IteratorObj ¶
type IteratorObj struct {
// contains filtered or unexported fields
}
IteratorObj is the state of a for-each loop.
type Kind ¶
type Kind uint8
Kind is the type of a value.
const ( Nil Kind = iota Bool Int Float String Vector Map Function Builtin Class Object BoundMethod Closure // a function literal with its captured variables Iterator // internal: the state of a for-each loop Internal // internal: a Go value the VM keeps in a slot (a pending exception) Cell // internal: a captured variable )
type MapObj ¶
type MapObj struct {
// contains filtered or unexported fields
}
MapObj is the shared, mutable storage of a map. It keeps the insertion order of its keys.
type MethodObj ¶
type MethodObj struct {
Name string
Class *ClassObj // the class that declares it
Static bool
Fn Named // the compiled function; nil if declared but not defined
// A member function of a native class: Native for an instance member
// function or constructor, Builtin for a static one (a plain function).
Native NativeMethod
Builtin *BuiltinObj
}
MethodObj is a member function of a class.
type NativeMethod ¶ added in v0.2.0
NativeMethod is an instance member function (or constructor) written in Go; a static one is a plain built-in (MethodObj.Builtin).
type ObjectObj ¶
type ObjectObj struct {
Class *ClassObj
Fields []Value
Native any // data the VM attaches (the stack trace of a thrown Error)
State any // the Go value inside an object of a native class
}
ObjectObj is an instance of a class.
type Value ¶
type Value struct {
// contains filtered or unexported fields
}
Value is a Kukel value. It is a small tagged struct, so that numbers do not allocate: num holds the bits of a Bool, Int or Float, obj the payload of the other kinds (*VectorObj, a function object, *BuiltinObj, ...). A String holds its length in num and its bytes in obj: a *byte to the first byte (a pointer fits in an interface without an allocation), or the *strBuf of a string made by concatenation. The zero Value is nil.
func Arith ¶
Arith applies a binary arithmetic or bitwise operator.
Integers wrap around (two's complement); an int combined with a float gives a float; int / int truncates toward zero; / and % by integer 0 are errors; + also concatenates two strings. Bitwise operators and shifts take ints only; a shift count must be in 0..63; >> is an arithmetic shift.
func Detached ¶ added in v0.8.0
Detached returns v, with a string's bytes no longer shared with a buffer that its interpreter can still append to: for a value that goes to another goroutine.
func Index ¶
Index returns x[i]: an element of a vector, the byte (an int) of a string, or the value of a map key (nil for a missing key).
func MakeBoundMethod ¶
func MakeBuiltin ¶
func MakeBuiltin(b *BuiltinObj) Value
func MakeClosure ¶
func MakeClosure(c *ClosureObj) Value
func MakeFunction ¶
MakeFunction wraps a function object (a *bytecode.Function); Named gives its name for printing.
func MakeInternal ¶
MakeInternal wraps a Go value that only the VM uses.
func MakeString ¶
func MakeVector ¶
func Member ¶
Member returns the value of p->name: a data member, or else a method (bound to p; a static method gives its plain function).
func NewIterator ¶
NewIterator starts iterating over a vector, map or string.
func ObjectValue ¶
ObjectValue is the value of an existing object.
func (Value) AsBoundMethod ¶
func (v Value) AsBoundMethod() *BoundMethodObj
func (Value) AsBuiltin ¶
func (v Value) AsBuiltin() *BuiltinObj
func (Value) AsClosure ¶
func (v Value) AsClosure() *ClosureObj
func (Value) IntValue ¶ added in v0.8.0
IntValue gives the int that v equals (Equal): an int, or a float with an integral value in the int range.
func (Value) IsCallable ¶
IsCallable reports whether v can be called: a function, function literal, bound method or built-in.
func (Value) Next ¶
Next returns the next pair: (element index, element) for a vector, (key, value) for a map, (byte index, byte) for a string. ok is false at the end. A vector is walked while the index is below its current length; map keys deleted during the loop are skipped.
func (Value) OneNameGetsKey ¶
OneNameGetsKey reports what a for-each loop with one name gets from Next: the first value (the key) for a map, the second (the element or byte) otherwise.