compiler

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2024 License: Apache-2.0 Imports: 10 Imported by: 4

Documentation

Overview

Package compiler is used to compile a Risor abstract syntax tree (AST) into the corresponding bytecode.

Index

Constants

View Source
const (
	// MaxArgs is the maximum number of arguments a function can have.
	MaxArgs = 255

	// Placeholder is a temporary value written during compilation, which is
	// always replaced before compilation is complete.
	Placeholder = uint16(math.MaxUint16)
)

Variables

This section is empty.

Functions

func CopyInstructions added in v0.14.0

func CopyInstructions(src []op.Code) []op.Code

func MarshalCode added in v0.14.0

func MarshalCode(code *Code) ([]byte, error)

MarshalCode converts a Code object into a JSON representation.

Types

type Code added in v0.14.0

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

func Compile

func Compile(node ast.Node, options ...Option) (*Code, error)

Compile the given AST node and return the compiled code object. This is a shorthand for compiler.New(options).Compile(node).

func UnmarshalCode added in v0.14.0

func UnmarshalCode(data []byte) (*Code, error)

UnmarshalCode converts a JSON representation of a Code object into a Code.

func (*Code) CodeName added in v0.14.0

func (c *Code) CodeName() string

func (*Code) Constant added in v0.14.0

func (c *Code) Constant(index int) any

func (*Code) ConstantsCount added in v0.14.0

func (c *Code) ConstantsCount() int

func (*Code) Flatten added in v0.14.0

func (c *Code) Flatten() []*Code

func (*Code) FunctionID added in v0.14.0

func (c *Code) FunctionID() string

func (*Code) Global added in v0.14.0

func (c *Code) Global(index int) *Symbol

func (*Code) GlobalNames added in v1.4.0

func (c *Code) GlobalNames() []string

func (*Code) GlobalsCount added in v0.14.0

func (c *Code) GlobalsCount() int

func (*Code) ID added in v0.14.0

func (c *Code) ID() string

func (*Code) Instruction added in v0.14.0

func (c *Code) Instruction(index int) op.Code

func (*Code) InstructionCount added in v0.14.0

func (c *Code) InstructionCount() int

func (*Code) IsNamed added in v0.14.0

func (c *Code) IsNamed() bool

func (*Code) IsRoot added in v1.4.0

func (c *Code) IsRoot() bool

func (*Code) Local added in v0.14.0

func (c *Code) Local(index int) *Symbol

func (*Code) LocalsCount added in v0.14.0

func (c *Code) LocalsCount() int

func (*Code) MarshalJSON added in v0.14.0

func (c *Code) MarshalJSON() ([]byte, error)

func (*Code) Name added in v0.14.0

func (c *Code) Name(index int) string

func (*Code) NameCount added in v0.14.0

func (c *Code) NameCount() int

func (*Code) Parent added in v0.14.0

func (c *Code) Parent() *Code

func (*Code) Root added in v0.14.0

func (c *Code) Root() *Code

func (*Code) Source added in v0.14.0

func (c *Code) Source() string

type Compiler

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

Compiler is used to compile Risor AST into its corresponding bytecode. This implements the ICompiler interface.

func New

func New(options ...Option) (*Compiler, error)

New creates and returns a new Compiler. Any supplied options are used to configure the compilation process.

func (*Compiler) Code added in v0.14.0

func (c *Compiler) Code() *Code

Code returns the compiled code for the entrypoint.

func (*Compiler) Compile

func (c *Compiler) Compile(node ast.Node) (*Code, error)

Compile the given AST node and return the compiled code object.

type Function added in v0.14.0

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

func NewFunction added in v0.14.0

func NewFunction(opts FunctionOpts) *Function

func (*Function) Code added in v0.14.0

func (f *Function) Code() *Code

func (*Function) Default added in v0.14.0

func (f *Function) Default(index int) any

func (*Function) DefaultsCount added in v0.14.0

func (f *Function) DefaultsCount() int

func (*Function) ID added in v0.14.0

func (f *Function) ID() string

func (*Function) LocalsCount added in v0.14.0

func (f *Function) LocalsCount() int

func (*Function) Name added in v0.14.0

func (f *Function) Name() string

func (*Function) Parameter added in v0.14.0

func (f *Function) Parameter(index int) string

func (*Function) ParametersCount added in v0.14.0

func (f *Function) ParametersCount() int

func (*Function) RequiredArgsCount added in v0.14.0

func (f *Function) RequiredArgsCount() int

func (*Function) String added in v0.14.0

func (f *Function) String() string

type FunctionOpts added in v0.14.0

type FunctionOpts struct {
	ID         string
	Name       string
	Parameters []string
	Defaults   []any
	Code       *Code
}

type InstructionIter added in v0.14.0

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

func NewInstructionIter added in v0.14.0

func NewInstructionIter(code *Code) *InstructionIter

func (*InstructionIter) All added in v0.14.0

func (i *InstructionIter) All() [][]op.Code

func (*InstructionIter) Next added in v0.14.0

func (i *InstructionIter) Next() ([]op.Code, bool)

type Option

type Option func(*Compiler)

Option is a configuration function for a Compiler.

func WithCode

func WithCode(code *Code) Option

WithCode configures the compiler to compile into the given code object.

func WithGlobalNames added in v0.14.0

func WithGlobalNames(names []string) Option

WithGlobalNames configures the compiler with the given global variable names.

type Resolution added in v0.14.0

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

Resolution holds information about where a symbol resides, relative to the current scope.

func outer() {
	x := 1
	func inner() {
		print(x)
	}
	return inner
}

In this example, if we look up "x" while compiling "inner", we will get a resolution with a depth of 1 and a scope of "free". This indicates that "x" is defined by the immediate parent.

func (*Resolution) Depth added in v0.14.0

func (r *Resolution) Depth() int

func (*Resolution) FreeIndex added in v0.14.0

func (r *Resolution) FreeIndex() int

func (*Resolution) Scope added in v0.14.0

func (r *Resolution) Scope() Scope

func (*Resolution) String added in v0.14.0

func (r *Resolution) String() string

func (*Resolution) Symbol added in v0.14.0

func (r *Resolution) Symbol() *Symbol

type Scope added in v0.14.0

type Scope string

Scope represents the scope of a symbol. It can be local, global, or free.

const (
	// Local indicates that a symbol is local to a function.
	Local Scope = "local"

	// Global indicates that a symbol is global to a module.
	Global Scope = "global"

	// Free indicates that a symbol is owned by an enclosing parent function.
	Free Scope = "free"
)

type Symbol added in v0.14.0

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

Symbol represents an identifier in a program. It is used to store information about the identifier, such as its name, index, and optionally a value.

func (*Symbol) Index added in v0.14.0

func (s *Symbol) Index() uint16

func (*Symbol) IsConstant added in v0.14.0

func (s *Symbol) IsConstant() bool

func (*Symbol) Name added in v0.14.0

func (s *Symbol) Name() string

func (*Symbol) String added in v0.14.0

func (s *Symbol) String() string

func (*Symbol) Value added in v0.14.0

func (s *Symbol) Value() any

type SymbolTable added in v0.14.0

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

SymbolTable tracks which symbols are defined and referenced in a given scope. These tables may have a parent table, which indicates that they represent a nested scope. If "isBlock" is set to true, this table represents a block within a function (like inside an if { ... }), rather than a function itself. Note there may be more symbols in the symbols array than there are in symbolsByName, because symbols defined in nested blocks don't use a name in the enclosing table.

func NewSymbolTable added in v0.14.0

func NewSymbolTable() *SymbolTable

NewSymbolTable returns a new root symbol table.

func (*SymbolTable) Count added in v0.14.0

func (t *SymbolTable) Count() uint16

Count returns the number of symbols defined in this table.

func (*SymbolTable) FindTable added in v0.14.0

func (t *SymbolTable) FindTable(id string) (*SymbolTable, bool)

FindTable returns the table with the specified ID. This may be the current table or any child table.

func (*SymbolTable) Free added in v0.14.0

func (t *SymbolTable) Free(index uint16) *Resolution

Free returns the free variable Resolution located at the specified index.

func (*SymbolTable) FreeCount added in v0.14.0

func (t *SymbolTable) FreeCount() uint16

FreeCount returns the number of free variables defined in this table.

func (*SymbolTable) FunctionDepth added in v1.4.0

func (t *SymbolTable) FunctionDepth() int

func (*SymbolTable) Get added in v0.14.0

func (t *SymbolTable) Get(name string) (*Symbol, bool)

Get returns the symbol with the specified name and a boolean indicating whether the symbol was found. Does not check any parent tables.

func (*SymbolTable) GetFunction added in v1.4.0

func (t *SymbolTable) GetFunction() (*SymbolTable, bool)

func (*SymbolTable) GetFunctionID added in v1.4.0

func (t *SymbolTable) GetFunctionID() (string, bool)

func (*SymbolTable) ID added in v0.14.0

func (t *SymbolTable) ID() string

func (*SymbolTable) InsertConstant added in v0.14.0

func (t *SymbolTable) InsertConstant(name string, value ...any) (*Symbol, error)

InsertConstant adds a new constant into this symbol table, with an optional value. The symbol will be assigned the next available index.

func (*SymbolTable) InsertVariable added in v0.14.0

func (t *SymbolTable) InsertVariable(name string, value ...any) (*Symbol, error)

InsertVariable adds a new variable into this symbol table, with an optional value. The symbol will be assigned the next available index.

func (*SymbolTable) IsDefined added in v0.14.0

func (t *SymbolTable) IsDefined(name string) bool

IsDefined returns true if the specified symbol is defined in this table. Does not check any parent tables.

func (*SymbolTable) IsGlobal added in v0.14.0

func (t *SymbolTable) IsGlobal() bool

IsGlobal returns true if this table represents the top-level scope. In other words, this checks if the table has no parent.

func (*SymbolTable) LocalTable added in v0.14.0

func (t *SymbolTable) LocalTable() *SymbolTable

LocalTable returns the table that defines the local variables for this table. This is useful to find the enclosing function when in a block.

func (*SymbolTable) NewBlock added in v0.14.0

func (t *SymbolTable) NewBlock() *SymbolTable

NewBlock creates a new symbol table that is a child of the current table, and represents a block within a function. Blocks allocate symbol indexes from the enclosing function's symbol table.

func (*SymbolTable) NewChild added in v0.14.0

func (t *SymbolTable) NewChild() *SymbolTable

NewChild creates a new symbol table that is a child of the current table.

func (*SymbolTable) Parent added in v0.14.0

func (t *SymbolTable) Parent() *SymbolTable

Parent returns the parent table of this table, if any.

func (*SymbolTable) Resolve added in v0.14.0

func (t *SymbolTable) Resolve(name string) (*Resolution, bool)

Resolve the specified symbol in this table or any parent tables, returning a Resolution if the symbol is found. The Resolution indicates the symbol's relative scope and depth. If the symbol is found to be a "free" variable, it will be added to the free map for this table.

func (*SymbolTable) Root added in v0.14.0

func (t *SymbolTable) Root() *SymbolTable

Root returns the outermost table that encloses this table.

func (*SymbolTable) SetValue added in v0.14.0

func (t *SymbolTable) SetValue(name string, value any) error

SetValue associates a value with the specified symbol.

func (*SymbolTable) Symbol added in v0.14.0

func (t *SymbolTable) Symbol(index uint16) *Symbol

Symbol returns the Symbol located at the specified index.

Jump to

Keyboard shortcuts

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