compiler

package
v0.0.3-alpha Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

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

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

func MarshalCode

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

MarshalCode converts a Code object into a JSON representation.

Types

type Code

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

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

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

func (*Code) CodeName

func (c *Code) CodeName() string

func (*Code) Constant

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

func (*Code) ConstantsCount

func (c *Code) ConstantsCount() int

func (*Code) Flatten

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

func (*Code) FunctionID

func (c *Code) FunctionID() string

func (*Code) Global

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

func (*Code) GlobalNames

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

func (*Code) GlobalsCount

func (c *Code) GlobalsCount() int

func (*Code) ID

func (c *Code) ID() string

func (*Code) Instruction

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

func (*Code) InstructionCount

func (c *Code) InstructionCount() int

func (*Code) IsNamed

func (c *Code) IsNamed() bool

func (*Code) IsRoot

func (c *Code) IsRoot() bool

func (*Code) Local

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

func (*Code) LocalsCount

func (c *Code) LocalsCount() int

func (*Code) MarshalJSON

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

func (*Code) Name

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

func (*Code) NameCount

func (c *Code) NameCount() int

func (*Code) Parent

func (c *Code) Parent() *Code

func (*Code) Root

func (c *Code) Root() *Code

func (*Code) Source

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

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

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

func NewFunction

func NewFunction(opts FunctionOpts) *Function

func (*Function) Code

func (f *Function) Code() *Code

func (*Function) Default

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

func (*Function) DefaultsCount

func (f *Function) DefaultsCount() int

func (*Function) ID

func (f *Function) ID() string

func (*Function) LocalsCount

func (f *Function) LocalsCount() int

func (*Function) Name

func (f *Function) Name() string

func (*Function) Parameter

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

func (*Function) ParametersCount

func (f *Function) ParametersCount() int

func (*Function) RequiredArgsCount

func (f *Function) RequiredArgsCount() int

func (*Function) String

func (f *Function) String() string

type FunctionOpts

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

type InstructionIter

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

func NewInstructionIter

func NewInstructionIter(code *Code) *InstructionIter

func (*InstructionIter) All

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

func (*InstructionIter) Next

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

func WithGlobalNames(names []string) Option

WithGlobalNames configures the compiler with the given global variable names.

type Resolution

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

func (r *Resolution) Depth() int

func (*Resolution) FreeIndex

func (r *Resolution) FreeIndex() int

func (*Resolution) Scope

func (r *Resolution) Scope() Scope

func (*Resolution) String

func (r *Resolution) String() string

func (*Resolution) Symbol

func (r *Resolution) Symbol() *Symbol

type Scope

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

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

func (s *Symbol) Index() uint16

func (*Symbol) IsConstant

func (s *Symbol) IsConstant() bool

func (*Symbol) Name

func (s *Symbol) Name() string

func (*Symbol) String

func (s *Symbol) String() string

func (*Symbol) Value

func (s *Symbol) Value() any

type SymbolTable

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

func NewSymbolTable() *SymbolTable

NewSymbolTable returns a new root symbol table.

func (*SymbolTable) Count

func (t *SymbolTable) Count() uint16

Count returns the number of symbols defined in this table.

func (*SymbolTable) FindTable

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

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

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

func (*SymbolTable) FreeCount

func (t *SymbolTable) FreeCount() uint16

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

func (*SymbolTable) FunctionDepth

func (t *SymbolTable) FunctionDepth() int

func (*SymbolTable) Get

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

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

func (*SymbolTable) GetFunctionID

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

func (*SymbolTable) ID

func (t *SymbolTable) ID() string

func (*SymbolTable) InsertConstant

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

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

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

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

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

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

func (t *SymbolTable) NewChild() *SymbolTable

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

func (*SymbolTable) Parent

func (t *SymbolTable) Parent() *SymbolTable

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

func (*SymbolTable) Resolve

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

func (t *SymbolTable) Root() *SymbolTable

Root returns the outermost table that encloses this table.

func (*SymbolTable) SetValue

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

SetValue associates a value with the specified symbol.

func (*SymbolTable) Symbol

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