compiler

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Feb 10, 2019 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

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

List of symbol scopes

Variables

View Source
var OpcodeNames = [...]string{
	OpConstant:         "CONST",
	OpPop:              "POP",
	OpTrue:             "TRUE",
	OpFalse:            "FALSE",
	OpAdd:              "ADD",
	OpSub:              "SUB",
	OpMul:              "MUL",
	OpDiv:              "DIV",
	OpRem:              "REM",
	OpBAnd:             "AND",
	OpBOr:              "OR",
	OpBXor:             "XOR",
	OpBAndNot:          "ANDN",
	OpBShiftLeft:       "SHL",
	OpBShiftRight:      "SHR",
	OpBComplement:      "NEG",
	OpEqual:            "EQL",
	OpNotEqual:         "NEQ",
	OpGreaterThan:      "GTR",
	OpGreaterThanEqual: "GEQ",
	OpMinus:            "NEG",
	OpLNot:             "NOT",
	OpJumpFalsy:        "JMPF",
	OpAndJump:          "ANDJMP",
	OpOrJump:           "ORJMP",
	OpJump:             "JMP",
	OpNull:             "NULL",
	OpGetGlobal:        "GETG",
	OpSetGlobal:        "SETG",
	OpSetSelGlobal:     "SETSG",
	OpArray:            "ARR",
	OpMap:              "MAP",
	OpError:            "ERROR",
	OpImmutable:        "IMMUT",
	OpIndex:            "INDEX",
	OpSliceIndex:       "SLICE",
	OpCall:             "CALL",
	OpReturn:           "RET",
	OpReturnValue:      "RETVAL",
	OpExport:           "EXPORT",
	OpGetLocal:         "GETL",
	OpSetLocal:         "SETL",
	OpDefineLocal:      "DEFL",
	OpSetSelLocal:      "SETSL",
	OpGetBuiltin:       "BUILTIN",
	OpClosure:          "CLOSURE",
	OpGetFree:          "GETF",
	OpSetFree:          "SETF",
	OpSetSelFree:       "SETSF",
	OpIteratorInit:     "ITER",
	OpIteratorNext:     "ITNXT",
	OpIteratorKey:      "ITKEY",
	OpIteratorValue:    "ITVAL",
}

OpcodeNames is opcode names.

View Source
var OpcodeOperands = [...][]int{
	OpConstant:         {2},
	OpPop:              {},
	OpTrue:             {},
	OpFalse:            {},
	OpAdd:              {},
	OpSub:              {},
	OpMul:              {},
	OpDiv:              {},
	OpRem:              {},
	OpBAnd:             {},
	OpBOr:              {},
	OpBXor:             {},
	OpBAndNot:          {},
	OpBShiftLeft:       {},
	OpBShiftRight:      {},
	OpBComplement:      {},
	OpEqual:            {},
	OpNotEqual:         {},
	OpGreaterThan:      {},
	OpGreaterThanEqual: {},
	OpMinus:            {},
	OpLNot:             {},
	OpJumpFalsy:        {2},
	OpAndJump:          {2},
	OpOrJump:           {2},
	OpJump:             {2},
	OpNull:             {},
	OpGetGlobal:        {2},
	OpSetGlobal:        {2},
	OpSetSelGlobal:     {2, 1},
	OpArray:            {2},
	OpMap:              {2},
	OpError:            {},
	OpImmutable:        {},
	OpIndex:            {},
	OpSliceIndex:       {},
	OpCall:             {1},
	OpReturn:           {},
	OpReturnValue:      {},
	OpExport:           {},
	OpGetLocal:         {1},
	OpSetLocal:         {1},
	OpDefineLocal:      {1},
	OpSetSelLocal:      {1, 1},
	OpGetBuiltin:       {1},
	OpClosure:          {2, 1},
	OpGetFree:          {1},
	OpSetFree:          {1},
	OpSetSelFree:       {1, 1},
	OpIteratorInit:     {},
	OpIteratorNext:     {},
	OpIteratorKey:      {},
	OpIteratorValue:    {},
}

OpcodeOperands is the number of operands.

Functions

func FormatInstructions

func FormatInstructions(b []byte, posOffset int) []string

FormatInstructions returns string representation of bytecode instructions.

func MakeInstruction

func MakeInstruction(opcode Opcode, operands ...int) []byte

MakeInstruction returns a bytecode for an opcode and the operands.

func ReadOperands

func ReadOperands(numOperands []int, ins []byte) (operands []int, offset int)

ReadOperands reads operands from the bytecode.

Types

type Bytecode

type Bytecode struct {
	Instructions []byte
	Constants    []objects.Object
}

Bytecode is a compiled instructions and constants.

func (*Bytecode) Decode

func (b *Bytecode) Decode(r io.Reader) error

Decode reads Bytecode data from the reader.

func (*Bytecode) Encode

func (b *Bytecode) Encode(w io.Writer) error

Encode writes Bytecode data to the writer.

type CompilationScope

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

CompilationScope represents a compiled instructions and the last two instructions that were emitted.

type Compiler

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

Compiler compiles the AST into a bytecode.

func NewCompiler

func NewCompiler(symbolTable *SymbolTable, stdModules map[string]*objects.ImmutableMap, trace io.Writer) *Compiler

NewCompiler creates a Compiler. User can optionally provide the symbol table if one wants to add or remove some global- or builtin- scope symbols. If not (nil), Compile will create a new symbol table and use the default builtin functions. Likewise, standard modules can be explicitly provided if user wants to add or remove some modules. By default, Compile will use all the standard modules otherwise.

func (*Compiler) Bytecode

func (c *Compiler) Bytecode() *Bytecode

Bytecode returns a compiled bytecode.

func (*Compiler) Compile

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

Compile compiles the AST node.

func (*Compiler) SetModuleLoader

func (c *Compiler) SetModuleLoader(moduleLoader ModuleLoader)

SetModuleLoader sets or replaces the current module loader. Note that the module loader is used for user modules, not for the standard modules.

type EmittedInstruction

type EmittedInstruction struct {
	Opcode   Opcode
	Position int
}

EmittedInstruction represents an opcode with its emitted position.

type Loop

type Loop struct {
	Continues []int
	Breaks    []int
}

Loop represents a loop construct that the compiler uses to track the current loop.

type ModuleLoader

type ModuleLoader func(moduleName string) ([]byte, error)

ModuleLoader should take a module name and return the module data.

type Opcode

type Opcode byte

Opcode represents a single byte operation code.

const (
	OpConstant         Opcode = iota // Load constant
	OpAdd                            // Add
	OpSub                            // Sub
	OpMul                            // Multiply
	OpDiv                            // Divide
	OpRem                            // Remainder
	OpBAnd                           // bitwise AND
	OpBOr                            // bitwise OR
	OpBXor                           // bitwise XOR
	OpBShiftLeft                     // bitwise shift left
	OpBShiftRight                    // bitwise shift right
	OpBAndNot                        // bitwise AND NOT
	OpBComplement                    // bitwise complement
	OpPop                            // Pop
	OpTrue                           // Push true
	OpFalse                          // Push false
	OpEqual                          // Equal ==
	OpNotEqual                       // Not equal !=
	OpGreaterThan                    // Greater than >=
	OpGreaterThanEqual               // Greater than or equal to >=
	OpMinus                          // Minus -
	OpLNot                           // Logical not !
	OpJumpFalsy                      // Jump if falsy
	OpAndJump                        // Logical AND jump
	OpOrJump                         // Logical OR jump
	OpJump                           // Jump
	OpNull                           // Push null
	OpArray                          // Array object
	OpMap                            // Map object
	OpError                          // Error object
	OpImmutable                      // Immutable object
	OpIndex                          // Index operation
	OpSliceIndex                     // Slice operation
	OpCall                           // Call function
	OpReturn                         // Return
	OpReturnValue                    // Return value
	OpExport                         // Export
	OpGetGlobal                      // Get global variable
	OpSetGlobal                      // Set global variable
	OpSetSelGlobal                   // Set global variable using selectors
	OpGetLocal                       // Get local variable
	OpSetLocal                       // Set local variable
	OpDefineLocal                    // Define local variable
	OpSetSelLocal                    // Set local variable using selectors
	OpGetFree                        // Get free variables
	OpSetFree                        // Set free variables
	OpSetSelFree                     // Set free variables using selectors
	OpGetBuiltin                     // Get builtin function
	OpClosure                        // Push closure
	OpIteratorInit                   // Iterator init
	OpIteratorNext                   // Iterator next
	OpIteratorKey                    // Iterator key
	OpIteratorValue                  // Iterator value
)

List of opcodes

type Symbol

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

type SymbolScope string

SymbolScope represents a symbol scope.

type SymbolTable

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

SymbolTable represents a symbol table.

func NewSymbolTable

func NewSymbolTable() *SymbolTable

NewSymbolTable creates a SymbolTable.

func (*SymbolTable) Define

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

Define adds a new symbol in the current scope.

func (*SymbolTable) DefineBuiltin

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

DefineBuiltin adds a symbol for builtin function.

func (*SymbolTable) Fork

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

Fork creates a new symbol table for a new scope.

func (*SymbolTable) FreeSymbols

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

FreeSymbols returns free symbols for the scope.

func (*SymbolTable) MaxSymbols

func (t *SymbolTable) MaxSymbols() int

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

func (*SymbolTable) Names

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

Names returns the name of all the symbols.

func (*SymbolTable) Parent

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

Parent returns the outer scope of the current symbol table.

func (*SymbolTable) Resolve

func (t *SymbolTable) Resolve(name string) (symbol *Symbol, depth int, ok bool)

Resolve resolves a symbol with a given name.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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