runtime

package
v0.0.0-...-7aee52b Latest Latest
Warning

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

Go to latest
Published: Aug 28, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package runtime is the Writ value universe and evaluator.

Eval takes already-parsed forms. It does not parse source.

Index

Constants

This section is empty.

Variables

View Source
var (
	True  = internSym("true")
	False = internSym("false")
	Nil   = internSym("nil")
)

True, False, and Nil are interned symbols. True and False are booleans; Nil is not.

Functions

func Exported

func Exported(name string) bool

Exported reports whether a top-level def/defm name is in a module export map. Names that start with '-' are private to the defining script.

func FormatSymbol

func FormatSymbol(name string) string

FormatSymbol renders a symbol name, quoting with ticks when needed.

func IsName

func IsName(v Value, name string) bool

IsName reports whether v is the symbol name.

func PluginsSupported

func PluginsSupported() bool

PluginsSupported reports whether this platform can load native plugins.

func Print

func Print(v Value) string

Print renders v for display, matching the language printer.

Types

type Clause

type Clause struct {
	Params     Params
	Body       []Value
	ParamsForm *Value
}

Clause is one function, macro, or handler clause.

func ParseFn

func ParseFn(args []Value) (kind string, clauses []Clause, err error)

ParseFn parses (fn ...) arguments.

type DefHead

type DefHead struct {
	Name       string
	NameForm   Value
	Params     Params
	ParamsForm Value
	Body       []Value
	HeadForm   Value
}

DefHead is a parsed (def ...) or (defm ...) head.

func AsDefForm

func AsDefForm(form Value, kw string) (DefHead, bool, error)

AsDefForm reports whether form is a (def ...) or (defm ...) when kw matches.

type Error

type Error struct {
	File    string
	Start   int
	End     int
	Message string
	// contains filtered or unexported fields
}

Error is a parse, type, or evaluation error. Start and End are byte offsets into the source when known.

func AsError

func AsError(err error) *Error

AsError converts err to *Error.

func ErrorAt

func ErrorAt(start, end int, msg string) *Error

ErrorAt builds an error covering a byte range.

func ErrorIncomplete

func ErrorIncomplete(start, end int, msg string) *Error

ErrorIncomplete is ErrorAt for a source prefix that needs more input.

func ErrorMsg

func ErrorMsg(msg string) *Error

ErrorMsg builds an error with no source span.

func Errorf

func Errorf(format string, args ...any) *Error

Errorf builds a formatted error with no source span.

func (*Error) Error

func (e *Error) Error() string

func (*Error) IsIncomplete

func (e *Error) IsIncomplete() bool

IsIncomplete reports whether this is an incomplete-parse error.

func (*Error) WithFile

func (e *Error) WithFile(file string) *Error

WithFile sets File when it is empty.

type Func

type Func func(args []Value) (Value, error)

Func is a native function. Arguments are positional.

type Handler

type Handler struct {
	Event   string
	Clauses []Clause
	// contains filtered or unexported fields
}

Handler is a compiled (on ...) form.

type IfClause

type IfClause struct {
	Test *Value
	Not  bool
	Body []Value
}

IfClause is one branch of (if ...).

func ParseIfArgs

func ParseIfArgs(args []Value) ([]IfClause, error)

ParseIfArgs parses (if ...) arguments.

type Importer

type Importer func(spec, fromFile string) (Value, error)

Importer loads (import spec) for the machine. The host wires filesystem policy, parsing, and evaluation.

type KeyPat

type KeyPat struct {
	Name string
	Pat  Pattern
}

KeyPat is one keyword parameter.

type Kind

type Kind int

Kind is the runtime kind of a Value.

const (
	KindInvalid Kind = iota
	KindInt
	KindFloat
	KindString
	KindSymbol
	KindList
	KindMap
	KindFn
	KindMacro
	KindQuote
	KindUnquote
	KindSplice
	KindComment
	KindNative
)

func (Kind) String

func (k Kind) String() string

type Machine

type Machine struct {
	Import Importer
	// contains filtered or unexported fields
}

Machine evaluates already-parsed forms.

func New

func New() *Machine

New constructs a machine.

func (*Machine) ApplyLocked

func (m *Machine) ApplyLocked(fn Value, args []Value) (Value, error)

ApplyLocked is Apply without taking the lock.

func (*Machine) BeginBudget

func (m *Machine) BeginBudget()

BeginBudget resets the remaining eval step budget.

func (*Machine) Checking

func (m *Machine) Checking() bool

Checking reports check mode.

func (*Machine) Eval

func (m *Machine) Eval(forms []Value) (Value, error)

Eval evaluates already-parsed forms (boot forms only).

func (*Machine) EvalLocked

func (m *Machine) EvalLocked(forms []Value) (Value, error)

EvalLocked is Eval without taking the lock.

func (*Machine) EvalModule

func (m *Machine) EvalModule(path string, forms []Value) (Value, error)

EvalModule evaluates an imported script without replacing the caller's env.

func (*Machine) Expand

func (m *Machine) Expand(forms []Value) (Program, error)

Expand compiles and macro-expands forms. It does not parse source.

func (*Machine) ExpandLocked

func (m *Machine) ExpandLocked(forms []Value) (Program, error)

ExpandLocked is Expand without taking the lock.

func (*Machine) File

func (m *Machine) File() string

File returns the current script path.

func (*Machine) FireLocked

func (m *Machine) FireLocked(event string, payload map[string]Value) error

FireLocked is Fire without taking the lock.

func (*Machine) GetPropLocked

func (m *Machine) GetPropLocked(path ...string) Value

GetPropLocked reads the script store. Caller must hold Lock.

func (*Machine) Loaded

func (m *Machine) Loaded(path string) (Value, bool)

func (*Machine) LoadingCycle

func (m *Machine) LoadingCycle(path string) bool

func (*Machine) LoadingPath

func (m *Machine) LoadingPath() []string

func (*Machine) Lock

func (m *Machine) Lock()

Lock serializes eval, Fire, and host callbacks.

func (*Machine) LookupLocked

func (m *Machine) LookupLocked(name string) (Value, bool)

LookupLocked returns a top-level binding. Caller must hold Lock.

func (*Machine) PopLoading

func (m *Machine) PopLoading()

func (*Machine) PushLoading

func (m *Machine) PushLoading(path string)

func (*Machine) RegisterExtra

func (m *Machine) RegisterExtra(name string, fn Func)

RegisterExtra installs a host function visible as a call head.

func (*Machine) RememberPackage

func (m *Machine) RememberPackage(path string, exp Value)

RememberPackage caches a native plugin export. Caller must hold Lock.

func (*Machine) ResetLocked

func (m *Machine) ResetLocked()

ResetLocked clears store, env, macros, handlers, and loaded modules. Caller must hold Lock.

func (*Machine) SetAfterError

func (m *Machine) SetAfterError(fn func(error))

SetAfterError sets a hook for errors from (after ...) callbacks.

func (*Machine) SetChecking

func (m *Machine) SetChecking(v bool)

SetChecking toggles check mode: after is a no-op and plugins stay closed if the host importer honors it.

func (*Machine) SetEvalLimit

func (m *Machine) SetEvalLimit(n int)

SetEvalLimit caps eval/expand steps. Zero means unlimited.

func (*Machine) SetEventKeys

func (m *Machine) SetEventKeys(name string, keys []string)

SetEventKeys records payload key order for Fire.

func (*Machine) SetFile

func (m *Machine) SetFile(file string)

SetFile records the current script path for import resolution.

func (*Machine) SetPropLocked

func (m *Machine) SetPropLocked(val Value, path ...string) error

SetPropLocked writes the script store. Caller must hold Lock.

func (*Machine) SetScheduler

func (m *Machine) SetScheduler(s Scheduler)

SetScheduler replaces the after scheduler.

func (*Machine) Unlock

func (m *Machine) Unlock()

Unlock releases the machine lock.

type MapPair

type MapPair struct {
	Key   Value
	Value Value
}

MapPair is one map entry. Key is a symbol.

type NamedFn

type NamedFn struct {
	Name     string
	Clauses  []Clause
	NameForm Value
}

NamedFn is a top-level def or defm.

type NamedImport

type NamedImport struct {
	Name     string
	PathForm Value
	NameForm Value
}

NamedImport is a top-level keyed (import name: path ...).

type Package

type Package struct {
	Funcs map[string]Func
	Vals  map[string]Value
}

Package is funcs and values for (import). The checker types funcs as fn(...) -> dynamic(any()) and values as any().

func LoadPlugin

func LoadPlugin(path string) (Package, error)

LoadPlugin opens a Go plugin and calls WritPackage.

type Params

type Params struct {
	Key  bool
	Pats []Pattern
	Keys []KeyPat
	Rest string
}

Params is a function, macro, or handler parameter list.

type Pattern

type Pattern struct {
	Bind  bool
	Name  string
	Value Value
}

Pattern is one parameter or literal match.

type Program

type Program struct {
	Handlers []Handler
	Boot     []Value
	Fns      []NamedFn
	Macros   []NamedFn
	Imports  []NamedImport
}

Program is expanded top-level forms.

type Scheduler

type Scheduler func(delay time.Duration, fn func())

Scheduler runs (after seconds ...) bodies. delay is in real time. The default scheduler uses time.AfterFunc; callbacks take the machine lock, so they never overlap eval/Fire/after. A custom Scheduler must not invoke fn on the same goroutine that is still inside Eval, Apply, or Fire (that deadlocks). Overlapping callbacks are serialized by the lock. Default unlimited eval is not a sandbox.

type Span

type Span struct {
	Start int
	End   int
}

Span is a byte range in source text.

type Value

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

Value is a Writ value or source form.

func Bool

func Bool(b bool) Value

Bool returns True or False.

func CallList

func CallList(xs ...Value) Value

CallList returns a call-shaped list (parentheses).

func Comment

func Comment(text string) Value

Comment returns a comment form. text is the source including ';'.

func EmptyMap

func EmptyMap() Value

EmptyMap is `[:]`.

func FilterComments

func FilterComments(xs []Value) []Value

FilterComments drops comment forms.

func Float

func Float(f float64) Value

Float returns a float64 value.

func Int

func Int(v *big.Int) Value

Int returns an integer value. v is copied.

func Int64

func Int64(n int64) Value

Int64 returns an integer value.

func List

func List(xs ...Value) Value

List returns a data list (vector).

func MapFrom

func MapFrom(pairs ...MapPair) Value

MapFrom builds a map from pairs. Later keys win.

func MustInt

func MustInt(s string) Value

MustInt parses a decimal integer. It panics on error.

func Native

func Native(v any) Value

Native boxes a host object. Native values are opaque to Writ.

func PackageValue

func PackageValue(p Package) Value

PackageValue builds the map returned by (import) of a native package.

func ParseInt

func ParseInt(s string) (Value, bool)

ParseInt parses a decimal integer.

func Quote

func Quote(v Value) Value

Quote wraps v.

func Splice

func Splice(v Value) Value

Splice wraps v.

func String

func String(s string) Value

String returns a string value.

func Symbol

func Symbol(name string) Value

Symbol returns an interned symbol.

func Unquote

func Unquote(v Value) Value

Unquote wraps v.

func (Value) As

func (v Value) As(dst any) bool

As type-asserts a native value into dst, which must be a non-nil *T.

func (Value) AsFloat64

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

AsFloat64 converts an int or float to float64.

func (Value) BigInt

func (v Value) BigInt() *big.Int

BigInt returns a copy of the integer. It is 0 if v is not an int.

func (Value) Blank

func (v Value) Blank() bool

Blank reports whether a blank line preceded v.

func (Value) Broke

func (v Value) Broke() bool

Broke reports whether v's source contained a newline.

func (Value) CommentText

func (v Value) CommentText() string

CommentText is the comment form body, including ';'.

func (Value) Equal

func (v Value) Equal(o Value) bool

Equal reports value equality. 1 and 1.0 compare equal. Functions never compare equal. Native values use == when the dynamic type is comparable.

func (Value) Float64

func (v Value) Float64() float64

Float64 returns the float, or 0 if v is not a float.

func (Value) HasSpan

func (v Value) HasSpan() bool

HasSpan reports whether v has a source span.

func (Value) Inner

func (v Value) Inner() Value

Inner returns the wrapped value of quote, unquote, or splice.

func (Value) IsBool

func (v Value) IsBool() bool

func (Value) IsFalse

func (v Value) IsFalse() bool

IsFalse reports whether v is false.

func (Value) IsFloat

func (v Value) IsFloat() bool

IsFloat reports whether v is a float.

func (Value) IsInt

func (v Value) IsInt() bool

IsInt reports whether v is an integer.

func (Value) IsKey

func (v Value) IsKey() bool

IsKey reports whether v is a keyword symbol (name:).

func (Value) IsNil

func (v Value) IsNil() bool

func (Value) IsNum

func (v Value) IsNum() bool

IsNum reports whether v is an int or a float.

func (Value) IsTrue

func (v Value) IsTrue() bool

func (Value) IsVec

func (v Value) IsVec() bool

IsVec reports whether v is a [] list rather than a () call.

func (Value) Items

func (v Value) Items() []Value

Items returns list elements. The slice must not be mutated.

func (Value) KeyName

func (v Value) KeyName() string

KeyName returns the map/keyword name without the trailing colon.

func (Value) KeySpans

func (v Value) KeySpans() map[string]Span

KeySpans returns recorded map key spans.

func (Value) Kind

func (v Value) Kind() Kind

func (Value) MapGet

func (v Value) MapGet(key string) (Value, bool)

MapGet looks up a symbol by name.

func (Value) Name

func (v Value) Name() string

Name returns the interned symbol name, or "" if v is not a symbol.

func (Value) Native

func (v Value) Native() (any, bool)

Native returns the boxed host object.

func (Value) Pairs

func (v Value) Pairs() []MapPair

Pairs returns map entries in insertion order.

func (Value) SetInner

func (v Value) SetInner(in Value) Value

SetInner returns a copy of v wrapping in.

func (Value) Span

func (v Value) Span() (Span, bool)

Span returns the source span, if any.

func (Value) String

func (v Value) String() string

String renders v for display, matching the language printer.

func (Value) Text

func (v Value) Text() string

Text returns the string contents, or "" if v is not a string.

func (Value) TrailingComment

func (v Value) TrailingComment() string

TrailingComment is the inline comment after v, if any.

func (Value) Truthy

func (v Value) Truthy() bool

Truthy is false only for nil and false.

func (Value) WithBlank

func (v Value) WithBlank() Value

WithBlank marks v as following a blank line.

func (Value) WithBroke

func (v Value) WithBroke() Value

WithBroke marks v as containing a newline in source.

func (Value) WithComment

func (v Value) WithComment(cmt string) Value

WithComment returns a copy of v with a trailing comment.

func (Value) WithKeySpans

func (v Value) WithKeySpans(spans map[string]Span) Value

WithKeySpans records source spans of map keys.

func (Value) WithSpan

func (v Value) WithSpan(start, end int) Value

WithSpan returns a copy of v with a source span.

Jump to

Keyboard shortcuts

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