expr

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package expr is the runner's expression engine: model.Expr compiles to a chain of typed Go closures (direct.go) that call the PHP-semantic helpers directly and read variables by slot from the Env carrier. It grew as the seam in front of github.com/expr-lang/expr and replaced it engine-first: the closure-chain technique is guamoko995/expr-cls's, applied to the model AST once the transpile round-trip had nothing left to do.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(p *Program, env *Env) (any, error)

Run evaluates a compiled program against the runner's environment.

Types

type BoundClosure

type BoundClosure struct {
	ID   string
	Slot int
	Decl *model.Closure
}

BoundClosure is one compiled closure literal: the synthetic identifier, the declaration Eval binds a callable for, and its slot.

type BoundVar

type BoundVar struct {
	Name  string
	Const bool
	Slot  int
}

BoundVar is one variable the compiled expression reads: the PHP name, the bare-name flag Eval's resolution needs, and the slot the value lands in.

type Compiled

type Compiled struct {
	Program  *Program
	Vars     []BoundVar
	Calls    []string
	Closures []BoundClosure
	// Exprs holds the marked sub-expressions (`++`/`--`, include) the
	// compiled closures evaluate through the __eval base helper, keyed by
	// the mark id, the same contract the transpiler's marks use.
	Exprs map[string]model.Expr
}

Compiled is a model.Expr compiled straight to a closure chain, with the bindings Eval fills before running: variables by slot, closure literals by slot, and the registered-function names to install into the base env. It carries no bytecode; the string round-trip through the transpiler and expr-lang's parser never happens for an expression compiled here.

func CompileExpr

func CompileExpr(e model.Expr, h *Helpers) (*Compiled, error)

CompileExpr compiles a model.Expr directly to a closure chain. It covers the whole expression vocabulary the runtime evaluates; an expression shape outside it is an error, spelled the way the transpiler spelled it when it was the compiler.

type Env

type Env struct {
	Vars []any
	Base map[string]any
}

Env is the engine's evaluation environment. Per-expression values (PHP variables, bare-name constants, closure literals) live in Vars, indexed by the slot the compiler assigned to their identifier; everything persistent (the PHP-semantic helpers, installed functions) stays in Base.

type Helpers

type Helpers struct {
	Truthy     func(v any) bool
	Concat     func(a, b any) string
	Pair       func(key, val any) model.ArrayItemValue
	Array      func(items ...model.ArrayItemValue) *model.Array
	Index      func(base, idx any) any
	Cast       func(typ string, v any) any
	Arith      func(op string, a, b any) any
	Compare    func(op string, a, b any) bool
	Bitwise    func(op string, a, b any) (any, error)
	BitNot     func(v any) any
	InstanceOf func(value, class any) bool
	Negate     func(v any) any

	// PanicError converts a recovered panic into the error the runner's own
	// call boundary would have produced, so a host panic stays catchable as
	// the same PHP exception on both engines.
	PanicError func(recovered any) error

	// Slotted reports whether an identifier is a per-evaluation value: a
	// PHP variable, a bare name, a closure literal. Those compile to slot
	// reads; everything else (function names) stays a Base lookup.
	Slotted func(name string) bool
}

Helpers carries the typed implementations of the PHP-semantic helper functions. The engine calls them directly, without the []any argument slice or a per-call panic guard. Only the pure helpers belong here; anything that re-enters the interpreter (__call, __get, __new, registered functions) stays an env lookup, because those are per-runtime closures.

type Program

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

Program is one compiled expression: a closure chain and the slot table its per-evaluation identifiers resolve through.

func (*Program) NumSlots

func (p *Program) NumSlots() int

NumSlots is the size of the Vars slice Run expects.

func (*Program) Slots

func (p *Program) Slots() map[string]int

Slots reports the slot index assigned to each per-evaluation identifier, keyed by identifier. The runner resolves its variable list against it once per compiled expression; the map is read-only after compilation.

Jump to

Keyboard shortcuts

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