compiler

package
v0.5.1 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultSourceFileExt = ".kvn"

DefaultSourceFileExt is the default extension used to resolve file imports.

Variables

This section is empty.

Functions

func ComputeMaxStack

func ComputeMaxStack(instructions bc.Instructions) int

ComputeMaxStack returns the maximum operand-stack depth that the given bytecode instruction stream can reach during execution.

func NewAbortCheck added in v0.5.1

func NewAbortCheck() bc.Instruction

func NewAccessIndex added in v0.5.1

func NewAccessIndex() bc.Instruction

func NewAccessSelector added in v0.5.1

func NewAccessSelector() bc.Instruction

func NewAndJump added in v0.5.1

func NewAndJump(target int) bc.Instruction

func NewBinaryOp added in v0.5.1

func NewBinaryOp(tokenID token.Token) bc.Instruction

func NewCallFunction added in v0.5.1

func NewCallFunction(numArgs int, isSpread bool) bc.Instruction

func NewCallMethod added in v0.5.1

func NewCallMethod(methodIndex int, numArgs int, isSpread bool) bc.Instruction

func NewContains added in v0.5.1

func NewContains() bc.Instruction

func NewDefer added in v0.5.1

func NewDefer(numArgs int) bc.Instruction

func NewDeferMethod added in v0.5.1

func NewDeferMethod(methodIndex int, numArgs int) bc.Instruction

func NewDefineLocal added in v0.5.1

func NewDefineLocal(localIndex int) bc.Instruction

func NewEqual added in v0.5.1

func NewEqual() bc.Instruction

func NewFormatRuntimeSpec added in v0.5.1

func NewFormatRuntimeSpec() bc.Instruction

func NewFormatStaticSpec added in v0.5.1

func NewFormatStaticSpec(formatSpecStaticIndex int) bc.Instruction

func NewImmutable added in v0.5.1

func NewImmutable() bc.Instruction

func NewImportBuiltinModule added in v0.5.1

func NewImportBuiltinModule(moduleStaticID int) bc.Instruction

func NewIterInit added in v0.5.1

func NewIterInit() bc.Instruction

func NewIterKey added in v0.5.1

func NewIterKey() bc.Instruction

func NewIterNext added in v0.5.1

func NewIterNext() bc.Instruction

func NewIterValue added in v0.5.1

func NewIterValue() bc.Instruction

func NewJump added in v0.5.1

func NewJump(target int) bc.Instruction

func NewJumpFalsy added in v0.5.1

func NewJumpFalsy(target int) bc.Instruction

func NewLoadBuiltinFunction added in v0.5.1

func NewLoadBuiltinFunction(builtinFuncID int) bc.Instruction

func NewLoadFree added in v0.5.1

func NewLoadFree(freeIndex int) bc.Instruction

func NewLoadFreePtr added in v0.5.1

func NewLoadFreePtr(freeIndex int) bc.Instruction

func NewLoadGlobal added in v0.5.1

func NewLoadGlobal(globalIndex int) bc.Instruction

func NewLoadLocal added in v0.5.1

func NewLoadLocal(localIndex int) bc.Instruction

func NewLoadLocalPtr added in v0.5.1

func NewLoadLocalPtr(localIndex int) bc.Instruction

func NewLoadStaticBytes added in v0.5.1

func NewLoadStaticBytes(i int) bc.Instruction

func NewLoadStaticCompiledFunction added in v0.5.1

func NewLoadStaticCompiledFunction(i int) bc.Instruction

func NewLoadStaticDecimal added in v0.5.1

func NewLoadStaticDecimal(i int) bc.Instruction

func NewLoadStaticFormatSpec added in v0.5.1

func NewLoadStaticFormatSpec(i int) bc.Instruction

func NewLoadStaticPrimitive added in v0.5.1

func NewLoadStaticPrimitive(i int) bc.Instruction

func NewLoadStaticRunes added in v0.5.1

func NewLoadStaticRunes(i int) bc.Instruction

func NewLoadStaticString added in v0.5.1

func NewLoadStaticString(i int) bc.Instruction

func NewLoadStaticTime added in v0.5.1

func NewLoadStaticTime(i int) bc.Instruction

func NewMakeArray added in v0.5.1

func NewMakeArray(numElements int) bc.Instruction

func NewMakeClosure added in v0.5.1

func NewMakeClosure(staticFuncIndex int, numFreeVars int) bc.Instruction

func NewMakeRecord added in v0.5.1

func NewMakeRecord(numFields int) bc.Instruction

func NewNotEqual added in v0.5.1

func NewNotEqual() bc.Instruction

func NewOrJump added in v0.5.1

func NewOrJump(target int) bc.Instruction

func NewPop added in v0.5.1

func NewPop() bc.Instruction

func NewPushBool added in v0.5.1

func NewPushBool(b bool) bc.Instruction

func NewPushByte added in v0.5.1

func NewPushByte(i byte) bc.Instruction

func NewPushInt added in v0.5.1

func NewPushInt(i int32) bc.Instruction

func NewPushRune added in v0.5.1

func NewPushRune(i rune) bc.Instruction

func NewPushUndefined added in v0.5.1

func NewPushUndefined() bc.Instruction

func NewReturn added in v0.5.1

func NewReturn(hasResult bool) bc.Instruction

func NewSlice added in v0.5.1

func NewSlice() bc.Instruction

func NewSliceStep added in v0.5.1

func NewSliceStep() bc.Instruction

func NewStoreFree added in v0.5.1

func NewStoreFree(freeIndex int) bc.Instruction

func NewStoreGlobal added in v0.5.1

func NewStoreGlobal(globalIndex int) bc.Instruction

func NewStoreIndexedFree added in v0.5.1

func NewStoreIndexedFree(freeIndex int, numSelectors int) bc.Instruction

func NewStoreIndexedGlobal added in v0.5.1

func NewStoreIndexedGlobal(globalIndex int, numSelectors int) bc.Instruction

func NewStoreIndexedLocal added in v0.5.1

func NewStoreIndexedLocal(localIndex int, numSelectors int) bc.Instruction

func NewStoreLocal added in v0.5.1

func NewStoreLocal(localIndex int) bc.Instruction

func NewSuspend added in v0.5.1

func NewSuspend() bc.Instruction

func NewUnaryBitNot added in v0.5.1

func NewUnaryBitNot() bc.Instruction

func NewUnaryNeg added in v0.5.1

func NewUnaryNeg() bc.Instruction

func NewUnaryNot added in v0.5.1

func NewUnaryNot() bc.Instruction

Types

type AssignmentMode

type AssignmentMode int

AssignmentMode controls how plain '=' handles unresolved identifiers.

const (
	// AssignmentModeSmart declares a variable in current scope for unresolved '=' assignments.
	AssignmentModeSmart AssignmentMode = iota

	// AssignmentModeStrict requires variables to already exist for '=' assignments.
	AssignmentModeStrict
)

type Compiler

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

Compiler compiles the AST into a bytecode.

func NewCompiler added in v0.4.1

func NewCompiler(
	sb *StaticBuilder,
	file *parser.SourceFile,
	symbolTable *SymbolTable,
	allowedModules []string,
	customModules map[string][]byte,
	trace io.Writer,
) *Compiler

New creates a Compiler.

func (*Compiler) Bytecode

func (c *Compiler) Bytecode() *vm.Bytecode

Bytecode returns a compiled bytecode.

func (*Compiler) Compile

func (c *Compiler) Compile(node parser.Node) (err error)

Compile compiles the AST node.

func (*Compiler) EnableFileImport

func (c *Compiler) EnableFileImport(enable bool)

EnableFileImport enables or disables module loading from local files. Local file modules are disabled by default.

func (*Compiler) GetAssignmentMode

func (c *Compiler) GetAssignmentMode() AssignmentMode

GetAssignmentMode returns the active assignment mode.

func (*Compiler) GetImportFileExt

func (c *Compiler) GetImportFileExt() []string

GetImportFileExt returns the current list of extension name. These are the complementary suffix of the source file to search and load local module files.

func (*Compiler) SetAssignmentMode

func (c *Compiler) SetAssignmentMode(mode AssignmentMode)

SetAssignmentMode sets how plain '=' handles unresolved identifiers.

func (*Compiler) SetImportDir

func (c *Compiler) SetImportDir(dir string)

SetImportDir sets the initial import directory path for file imports.

func (*Compiler) SetImportFileExt

func (c *Compiler) SetImportFileExt(exts ...string) error

SetImportFileExt sets the extension name of the source file for loading local module files. Use this method if you want other source file extension than ".kvn". This function requires at least one argument, since it will replace the current list of extension name.

type CompilerError

type CompilerError struct {
	FileSet *parser.SourceFileSet
	Node    parser.Node
	Err     error
}

CompilerError represents a compiler error.

func (*CompilerError) Error

func (e *CompilerError) Error() string

type StaticBuilder added in v0.4.1

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

func NewStaticBuilder added in v0.4.1

func NewStaticBuilder() *StaticBuilder

func (*StaticBuilder) AddBytes added in v0.5.1

func (b *StaticBuilder) AddBytes(v core.Bytes) int

func (*StaticBuilder) AddCompiledFunction added in v0.4.1

func (b *StaticBuilder) AddCompiledFunction(v core.CompiledFunction) int

func (*StaticBuilder) AddDecimal added in v0.4.1

func (b *StaticBuilder) AddDecimal(v dec128.Dec128) int

func (*StaticBuilder) AddFormatSpec added in v0.4.1

func (b *StaticBuilder) AddFormatSpec(v core.FormatSpec) int

func (*StaticBuilder) AddPrimitive added in v0.4.1

func (b *StaticBuilder) AddPrimitive(v core.Value) int

func (*StaticBuilder) AddRunes added in v0.4.1

func (b *StaticBuilder) AddRunes(v core.Runes) int

func (*StaticBuilder) AddString added in v0.4.1

func (b *StaticBuilder) AddString(v string) int

func (*StaticBuilder) AddTime added in v0.5.1

func (b *StaticBuilder) AddTime(v time.Time) int

func (*StaticBuilder) Build added in v0.4.1

func (b *StaticBuilder) Build() core.Static

type Symbol added in v0.4.1

type Symbol struct {
	Name          string
	Scope         SymbolScope
	Index         int
	LocalAssigned bool // if the local symbol is assigned at least once
}

Symbol represents a symbol in the symbol table.

type SymbolScope added in v0.4.1

type SymbolScope string

SymbolScope represents a symbol scope.

const (
	ScopeGlobal  SymbolScope = "GLOBAL"
	ScopeLocal   SymbolScope = "LOCAL"
	ScopeBuiltin SymbolScope = "BUILTIN"
	ScopeFree    SymbolScope = "FREE"
)

List of symbol scopes

type SymbolTable added in v0.4.1

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

SymbolTable represents a symbol table.

func NewSymbolTable added in v0.4.1

func NewSymbolTable() *SymbolTable

NewSymbolTable creates a SymbolTable.

func (*SymbolTable) BuiltinSymbols added in v0.4.1

func (t *SymbolTable) BuiltinSymbols() []*Symbol

BuiltinSymbols returns builtin symbols for the scope.

func (*SymbolTable) Define added in v0.4.1

func (t *SymbolTable) Define(name string) *Symbol

Define adds a new symbol in the current scope.

func (*SymbolTable) DefineBuiltin added in v0.4.1

func (t *SymbolTable) DefineBuiltin(index int, name string) *Symbol

DefineBuiltin adds a symbol for builtin function.

func (*SymbolTable) Fork added in v0.4.1

func (t *SymbolTable) Fork(block bool) *SymbolTable

Fork creates a new symbol table for a new scope.

func (*SymbolTable) FreeSymbols added in v0.4.1

func (t *SymbolTable) FreeSymbols() []*Symbol

FreeSymbols returns free symbols for the scope.

func (*SymbolTable) MaxSymbols added in v0.4.1

func (t *SymbolTable) MaxSymbols() int

MaxSymbols returns the total number of symbols defined in the scope.

func (*SymbolTable) Names added in v0.4.1

func (t *SymbolTable) Names() []string

Names returns the name of all the symbols.

func (*SymbolTable) Parent added in v0.4.1

func (t *SymbolTable) Parent(skipBlock bool) *SymbolTable

Parent returns the outer scope of the current symbol table.

func (*SymbolTable) Resolve added in v0.4.1

func (t *SymbolTable) Resolve(
	name string,
	recur bool,
) (*Symbol, int, bool)

Resolve resolves a symbol with a given name.

Jump to

Keyboard shortcuts

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