encoder

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package encoder provides bytecode encoding

Index

Constants

View Source
const (
	// ErrNoLocalScope is raised when no local scope has been created
	ErrNoLocalScope = "no local scopes have been pushed"

	// ErrDuplicateName is raised when an attempt is made to register a
	// duplicated name within the same local scope
	ErrDuplicateName = "name duplicated in scope: %s"
)
View Source
const ErrLabelAlreadyAnchored = "label has already been anchored"

ErrLabelAlreadyAnchored is raised when the finalizer identifies that a label has been anchored more than once in the Instructions stream

Variables

This section is empty.

Functions

This section is empty.

Types

type Cell

type Cell struct {
	Name data.Local
	Type CellType
}

Cell attaches a name to a type/disposition

type CellType

type CellType int

CellType marks a cell as having a certain disposition

const (
	ValueCell CellType = iota
	ReferenceCell
	RestCell
)

Cell dispositions

type Encoded

type Encoded struct {
	Code      isa.Instructions
	Globals   env.Namespace
	Constants data.Vector
	Closure   data.Locals
}

Encoded is a snapshot of the current Encoder's state. It is used as an intermediate step in the compilation process, particularly as input to the optimizer.

func (*Encoded) HasClosure added in v0.3.0

func (e *Encoded) HasClosure() bool

func (*Encoded) Runnable

func (e *Encoded) Runnable() (*isa.Runnable, error)

Runnable takes an Encoded and finalizes it into a Runnable that the abstract machine can execute. Jumps are resolved and unused constants are discarded.

func (*Encoded) WithCode

func (e *Encoded) WithCode(c isa.Instructions) *Encoded

func (*Encoded) WithConstants

func (e *Encoded) WithConstants(c data.Vector) *Encoded

type Encoder

type Encoder interface {
	// Child creates a child encoder, allowing closure resolution
	Child() Encoder

	// Emit an instruction with the given opcode and operands
	Emit(isa.Opcode, ...isa.Operand)

	// Encode returns the encoded bytecode
	Encode() *Encoded

	// Globals returns the global namespace for this Encoder
	Globals() env.Namespace

	// NewLabel creates a new label for jump instructions
	NewLabel() isa.Operand

	// AddConstant adds a constant value and returns its index
	AddConstant(ale.Value) isa.Operand

	// PushParams pushes a new parameter frame
	PushParams(data.Locals, bool)

	// PopParams pops the current parameter frame
	PopParams()

	// PushLocals pushes a new local variable frame
	PushLocals()

	// PopLocals pops the current local variable frame
	PopLocals() error

	// AddLocal adds a local variable and returns its cell
	AddLocal(data.Local, CellType) (*IndexedCell, error)

	// ResolveScoped resolves a scoped variable
	ResolveScoped(data.Local) (*ScopedCell, bool)

	// ResolveClosure resolves a closure variable
	ResolveClosure(data.Local) (*IndexedCell, bool)

	// ResolveParam resolves a parameter variable
	ResolveParam(data.Local) (*IndexedCell, bool)

	// ResolveLocal resolves a local variable
	ResolveLocal(data.Local) (*IndexedCell, bool)
}

Encoder exposes an interface for stateful compiler encoding

func NewEncoder

func NewEncoder(globals env.Namespace) Encoder

NewEncoder instantiates a new Encoder

type IndexedCell

type IndexedCell struct {
	*Cell
	Index isa.Operand
}

IndexedCell attaches an Index to a Cell

type IndexedCells

type IndexedCells []*IndexedCell

IndexedCells encapsulates a group of IndexedCells

type Locals

type Locals map[data.Local]*IndexedCell

Locals track local variable assignments

type Scope

type Scope int

Scope describes the scope of a name

const (
	LocalScope Scope = iota
	ArgScope
	ClosureScope
)

Scope locations

type ScopedCell

type ScopedCell struct {
	Encoder
	*Cell
	Scope
}

ScopedCell attaches a Scope to a Cell

type WrappedEncoder added in v0.3.0

type WrappedEncoder interface {
	Encoder

	// Wrapped returns the wrapped encoder
	Wrapped() Encoder
}

Jump to

Keyboard shortcuts

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