engine

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package engine compiles model AST programs into flat bytecode and executes them against a host-provided PHP semantics bridge.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(program *Program, host Host) error

Run executes a previously validated flat instruction stream.

Types

type Entry

type Entry struct {
	Key   any
	Value any
}

Entry is one key/value pair produced for foreach.

type Host

type Host interface {
	Construct(string, []any) (any, error)
	CallMethod(any, string, []any) (any, error)
	Call(string, string, []any) (any, error)
	GetProperty(any, string) any
	SetProperty(any, string, any, string) error
	Lookup(string) any
	Array([]model.ArrayItemValue) any
	Index(any, any) any
	SetIndex(any, any, any, bool, string) error
	// SetEntry writes value into container at key when container is a value the
	// script owns. A collection a binding returned belongs to the host, so it is
	// left alone rather than reported as an error, matching what a by-reference
	// foreach over one does in the interpreter.
	SetEntry(container, key, value any) error
	// UnsetIndex removes key from container, PHP's unset($a[$k]). Removing a
	// key that is not there is not an error.
	UnsetIndex(container, key any) error
	// MatchCatch reports whether a catch clause declaring declaredType handles
	// err. The class hierarchy, the `A|B` union form and the rule that
	// `catch (Exception)` does not catch an engine error all live in the host,
	// so both backends select the same clause.
	MatchCatch(declaredType string, err error) bool
	// Throw turns a thrown value into the error it travels as. A built-in
	// throwable is an error already; an instance of a declared class is
	// wrapped so a clause can filter on the class it was declared as, and a
	// catch binding it gets the object back.
	Throw(value any) error
	// CatchValue returns what a catch clause binds for err, which is the object
	// for a thrown instance and the error itself for everything else.
	CatchValue(err error) any
	// ClassConst reads the constant name off class. The compiler has already
	// collapsed `self`, `static` and `parent` to the enclosing class, so the
	// name arrives concrete; `Class::class` still reaches the host, since it
	// resolves without the class being declared.
	ClassConst(class, name string) (any, error)
	// Cast applies a PHP type cast, spelled by its bare type name: "bool",
	// "int", "float", "string", "array".
	Cast(typ string, value any) any
	Binary(string, any, any) (any, error)
	Unary(string, any) (any, error)
	Truthy(any) bool
	Entries(any) []Entry
	Echo(any) error
}

Host owns PHP value semantics and the Go API bridge. The engine itself owns only compilation, operand/local storage, jumps, and iteration state.

type MemoryHost added in v0.3.3

type MemoryHost interface {
	// MemoryCheckInterval returns the number of instructions between memory
	// checks; zero disables checking. Live-value registration happens either
	// way, so usage queries still see VM state.
	MemoryCheckInterval() int
	// PushLiveWalker registers an enumerator over every live VM value;
	// PopLiveWalker removes it. Calls nest across nested Run invocations.
	PushLiveWalker(func(yield func(any)))
	PopLiveWalker()
	// CheckMemory reports the limit error, if any.
	CheckMemory() error
}

MemoryHost is an optional Host extension for memory accounting, discovered by type assertion like the other optional host capabilities. A host that implements it can enumerate the VM's live values while Run executes, and have execution interrupted when its memory limit is exceeded.

type Program

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

Program is immutable bytecode compiled from a complete model.Program.

func Compile

func Compile(ast *model.Program) (program *Program, err error)

Compile validates and lowers the entire AST before execution can cause side effects. Any unsupported nested node rejects the whole program.

Jump to

Keyboard shortcuts

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