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 ¶
MarshalCode converts a Code object into a JSON representation.
Types ¶
type Code ¶
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 ¶
UnmarshalCode converts a JSON representation of a Code object into a Code.
func (*Code) ConstantsCount ¶
func (*Code) FunctionID ¶
func (*Code) GlobalNames ¶
func (*Code) GlobalsCount ¶
func (*Code) InstructionCount ¶
func (*Code) LocalsCount ¶
func (*Code) MarshalJSON ¶
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 ¶
type Function struct {
// contains filtered or unexported fields
}
func NewFunction ¶
func NewFunction(opts FunctionOpts) *Function
func (*Function) DefaultsCount ¶
func (*Function) LocalsCount ¶
func (*Function) ParametersCount ¶
func (*Function) RequiredArgsCount ¶
type FunctionOpts ¶
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
type Option ¶
type Option func(*Compiler)
Option is a configuration function for a Compiler.
func WithGlobalNames ¶
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.
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) IsConstant ¶
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.