bytecode

package
v0.0.0-...-8c2d49f Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2023 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Method  = "Method"
	Class   = "Class"
	Block   = "Block"
	Program = "Program"
)

instruction set types

View Source
const (
	NoOp int = iota
	GetLocal
	GetConstant
	GetConstantNamespace
	GetInstanceVariable
	SetLocal
	SetOptional
	SetConstant
	SetInstanceVariable
	PutTrue
	PutFalse
	PutString
	PutFloat
	PutSelf
	PutSuper
	PutInt
	PutObject
	PutNull
	NewArray
	ExpandArray
	SplatArray
	SplatBlock
	NewHash
	NewRange
	NewRangeExcl
	BranchUnless
	BranchIf
	Jump
	Break
	DefMethod
	DefMetaMethod
	DefClass
	Send
	BinaryOperator
	Add
	Subtract
	Greater
	Less
	GreaterEqual
	LessEqual
	InvokeBlock
	GetBlock
	HasBlock
	Pop
	Dup
	Defer
	Leave
	InstructionCount
)

instruction actions

View Source
const (
	NormalArg uint8 = iota
	OptionedArg
	SplatArg
	RequiredKeywordArg
	OptionalKeywordArg
)

These constants are enums that represent argument's types

View Source
const (
	NoSuperClass = "__none__"
)

NoSuperClass represents a class without a superclass. Using this will result in the superclass being Object.

Variables

View Source
var Instructions = [...]instruction{
	NoOp:                 {"no_op", 0, nil},
	GetLocal:             {"getlocal", 2, []bool{false, false}},
	GetConstant:          {"getconstant", 1, []bool{true}},
	GetConstantNamespace: {"getconstantnamespace", 1, []bool{true}},
	GetInstanceVariable:  {"getinstancevariable", 1, []bool{true}},
	SetLocal:             {"setlocal", 2, []bool{false, false}},
	SetOptional:          {"setoptional", 2, []bool{false, false}},
	SetConstant:          {"setconstant", 1, []bool{true}},
	SetInstanceVariable:  {"setinstancevariable", 1, []bool{true}},
	PutTrue:              {"puttrue", 0, nil},
	PutFalse:             {"putfalse", 0, nil},
	PutString:            {"putstring", 1, []bool{true}},
	PutFloat:             {"putfloat", 1, []bool{true}},
	PutSelf:              {"putself", 0, nil},
	PutSuper:             {"putsuper", 0, nil},
	PutInt:               {"putint", 1, []bool{false}},
	PutObject:            {"putobject", 1, []bool{true}},
	PutNull:              {"putnil", 0, nil},
	NewArray:             {"newarray", 1, []bool{false}},
	ExpandArray:          {"expand_array", 1, []bool{false}},
	SplatArray:           {"splat_array", 0, nil},
	SplatBlock:           {"splat_block", 0, nil},
	NewHash:              {"newhash", 1, []bool{false}},
	NewRange:             {"newrange", 0, nil},
	NewRangeExcl:         {"newrangeexcl", 0, nil},
	BranchUnless:         {"branchunless", 1, []bool{false}},
	BranchIf:             {"branchif", 1, []bool{false}},
	Jump:                 {"jump", 1, []bool{false}},
	Break:                {"break", 0, nil},
	DefMethod:            {"def_method", 3, []bool{false, true, true}},
	DefMetaMethod:        {"def_meta_method", 3, []bool{false, true, true}},
	DefClass:             {"def_class", 4, []bool{true, true, true, true}},
	Send:                 {"send", 4, []bool{true, false, true, true}},
	BinaryOperator:       {"bin_op", 1, []bool{true}},
	Add:                  {"add", 1, []bool{true}},
	Subtract:             {"subtract", 1, []bool{true}},
	Greater:              {"greater", 1, []bool{true}},
	Less:                 {"less", 1, []bool{true}},
	GreaterEqual:         {"greater_equal", 1, []bool{true}},
	LessEqual:            {"less_equal", 1, []bool{true}},
	InvokeBlock:          {"invokeblock", 1, []bool{false}},
	GetBlock:             {"getblock", 0, nil},
	HasBlock:             {"hasblock", 0, nil},
	Pop:                  {"pop", 0, nil},
	Dup:                  {"dup", 0, nil},
	Defer:                {"defer", 2, []bool{true, true}},
	Leave:                {"leave", 0, nil},
	InstructionCount:     {"instruction_count", 0, nil},
}

Instructions is the table that maps instruction's opcode with its readable name

Functions

This section is empty.

Types

type ArgSet

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

ArgSet stores the metadata of a method definition's parameters.

func (*ArgSet) FindIndex

func (as *ArgSet) FindIndex(name string) int

FindIndex finds the index of the given name in the argset

func (*ArgSet) Names

func (as *ArgSet) Names() []string

Names are the getter method of *ArgSet's names attribute TODO: needs to change the func to simple public variable

func (*ArgSet) Types

func (as *ArgSet) Types() []uint8

Types are the getter method of *ArgSet's types attribute TODO: needs to change the func to simple public variable

type Generator

type Generator struct {
	REPL bool
	// contains filtered or unexported fields
}

Generator contains program's AST and will store generated instruction sets

func NewGenerator

func NewGenerator() *Generator

NewGenerator initialises new Generator with complete AST tree.

func (*Generator) GenerateInstructions

func (g *Generator) GenerateInstructions(stmts []ast.Statement) []*InstructionSet

GenerateInstructions returns compiled instructions

func (*Generator) Index

func (g *Generator) Index() int

Index returns the current count of the instruction sets

func (*Generator) InitTopLevelScope

func (g *Generator) InitTopLevelScope(program *ast.Program)

InitTopLevelScope sets generator's scope with program node, which means it's the top level scope

func (*Generator) ResetMethodInstructionSets

func (g *Generator) ResetMethodInstructionSets()

ResetMethodInstructionSets clears generator's method instruction sets

type InstructionSet

type InstructionSet struct {
	Name         string
	Filename     string
	Type         string
	Instructions []int
	Constants    []interface{}
	SourceMap    []int
	Count        int
	ArgTypes     ArgSet
}

InstructionSet contains a set of Instructions and some metadata

func (*InstructionSet) GetBool

func (is *InstructionSet) GetBool(index int) bool

GetBool returns a bool value from the constant pool

func (*InstructionSet) GetFloat

func (is *InstructionSet) GetFloat(index int) float64

GetFloat returns the int value from the constant pool

func (*InstructionSet) GetObject

func (is *InstructionSet) GetObject(index int) interface{}

GetObject returns an interface{} object

func (*InstructionSet) GetString

func (is *InstructionSet) GetString(index int) string

GetString returns the value of the string from the constant table

func (*InstructionSet) Inspect

func (is *InstructionSet) Inspect() string

Inspect returns a string representation of the InstructionSet

func (*InstructionSet) SetConstant

func (is *InstructionSet) SetConstant(o interface{}) int

SetConstant adds a constant to the constant pool

Jump to

Keyboard shortcuts

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