Documentation
¶
Overview ¶
Package compiler is used to compile a Risor abstract syntax tree (AST) into the corresponding bytecode.
Index ¶
- Constants
- func CopyInstructions(src []op.Code) []op.Code
- func MarshalCode(code *Code) ([]byte, error)
- type Code
- func (c *Code) CodeName() string
- func (c *Code) Constant(index int) any
- func (c *Code) ConstantsCount() int
- func (c *Code) Flatten() []*Code
- func (c *Code) FunctionID() string
- func (c *Code) Global(index int) *Symbol
- func (c *Code) GlobalNames() []string
- func (c *Code) GlobalsCount() int
- func (c *Code) ID() string
- func (c *Code) Instruction(index int) op.Code
- func (c *Code) InstructionCount() int
- func (c *Code) IsNamed() bool
- func (c *Code) IsRoot() bool
- func (c *Code) Local(index int) *Symbol
- func (c *Code) LocalsCount() int
- func (c *Code) MarshalJSON() ([]byte, error)
- func (c *Code) Name(index int) string
- func (c *Code) NameCount() int
- func (c *Code) Parent() *Code
- func (c *Code) Root() *Code
- func (c *Code) Source() string
- type Compiler
- type Function
- func (f *Function) Code() *Code
- func (f *Function) Default(index int) any
- func (f *Function) DefaultsCount() int
- func (f *Function) ID() string
- func (f *Function) LocalsCount() int
- func (f *Function) Name() string
- func (f *Function) Parameter(index int) string
- func (f *Function) ParametersCount() int
- func (f *Function) RequiredArgsCount() int
- func (f *Function) String() string
- type FunctionOpts
- type InstructionIter
- type Option
- type Resolution
- type Scope
- type Symbol
- type SymbolTable
- func (t *SymbolTable) Count() uint16
- func (t *SymbolTable) FindTable(id string) (*SymbolTable, bool)
- func (t *SymbolTable) Free(index uint16) *Resolution
- func (t *SymbolTable) FreeCount() uint16
- func (t *SymbolTable) FunctionDepth() int
- func (t *SymbolTable) Get(name string) (*Symbol, bool)
- func (t *SymbolTable) GetFunction() (*SymbolTable, bool)
- func (t *SymbolTable) GetFunctionID() (string, bool)
- func (t *SymbolTable) ID() string
- func (t *SymbolTable) InsertConstant(name string, value ...any) (*Symbol, error)
- func (t *SymbolTable) InsertVariable(name string, value ...any) (*Symbol, error)
- func (t *SymbolTable) IsDefined(name string) bool
- func (t *SymbolTable) IsGlobal() bool
- func (t *SymbolTable) LocalTable() *SymbolTable
- func (t *SymbolTable) NewBlock() *SymbolTable
- func (t *SymbolTable) NewChild() *SymbolTable
- func (t *SymbolTable) Parent() *SymbolTable
- func (t *SymbolTable) Resolve(name string) (*Resolution, bool)
- func (t *SymbolTable) Root() *SymbolTable
- func (t *SymbolTable) SetValue(name string, value any) error
- func (t *SymbolTable) Symbol(index uint16) *Symbol
Constants ¶
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 MarshalCode ¶ added in v0.14.0
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 ¶
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
UnmarshalCode converts a JSON representation of a Code object into a Code.
func (*Code) ConstantsCount ¶ added in v0.14.0
func (*Code) FunctionID ¶ added in v0.14.0
func (*Code) GlobalNames ¶ added in v1.4.0
func (*Code) GlobalsCount ¶ added in v0.14.0
func (*Code) InstructionCount ¶ added in v0.14.0
func (*Code) LocalsCount ¶ added in v0.14.0
func (*Code) MarshalJSON ¶ added in v0.14.0
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 ¶
New creates and returns a new Compiler. Any supplied options are used to configure the compilation process.
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) DefaultsCount ¶ added in v0.14.0
func (*Function) LocalsCount ¶ added in v0.14.0
func (*Function) ParametersCount ¶ added in v0.14.0
func (*Function) RequiredArgsCount ¶ added in v0.14.0
type FunctionOpts ¶ added in v0.14.0
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
type Option ¶
type Option func(*Compiler)
Option is a configuration function for a Compiler.
func WithGlobalNames ¶ added in v0.14.0
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.
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) IsConstant ¶ added in v0.14.0
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.