code

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2021 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Make

func Make(op Opcode, operands ...int) []byte

Make creates a single bytecode instruction. The instruction consists of an Opcode and an optional number of operands.

func ReadOperands

func ReadOperands(def *Definition, ins Instructions) ([]int, int)

ReadOperands decodes the operands for the given instruction It returns the decoded operands and tells us how many bytes it read to do that.

func ReadUint16

func ReadUint16(ins Instructions) uint16

ReadUint16 helps use decode the operand correctly. Typically, when we call this function to decode an operand, we pass the entire instructions ([]byte) starting with the operand and then everything else. BigEndian.Uint16 will only return the first decodable int in the []byte which works perfectly to decode operand.

Types

type Definition

type Definition struct {
	Name          string
	OperandWidths []int
}

Definition helps us understand Opcode defintions. A Definition gives more insight on an Opcode's human-readable name (Name) and its operands. OperandWidths records the unique bytewidth that each operand may have

func Lookup

func Lookup(op byte) (*Definition, error)

Lookup simply finds the definition of the provided op (Opcode)

type Instructions

type Instructions []byte

Instructions is used to encapsulate many Instruction(s). A single Instruction consists of an opcode and an optional number of operands, which is effectively a []byte. We defined Instructions, plural for simplicity to work with a series of Instruction

func (Instructions) String

func (ins Instructions) String() string

String builds all the instructions's bytes into human-readable text For a fully decoded instruction, we can expect String to build each one with the position of the first byte that starts the instruction, the Opcode name and its operands.

type Opcode

type Opcode byte

Opcode is used as the first byte in an instruction. An Opcode specifies a unique instruction for the VM to execute. ie: pushing something onto the stack

const (
	OpConstant Opcode = iota
	OpAdd
	OpPop
	OpSub
	OpMul
	OpDiv
	OpTrue
	OpFalse
	OpEqual
	OpNotEqual
	OpGreaterThan
	OpMinus
	OpBang
	OpJumpNotTruthy
	OpJump
	OpNull
	OpGetGlobal
	OpSetGlobal
	OpArray
	OpHash
	OpIndex
	OpCall
	OpReturnValue
	OpReturn
	OpSetLocal
	OpGetLocal
	OpGetBuiltin
	OpClosure
	OpGetFree
	OpCurrentClosure
)

Opcodes, when defined, will have ever increasing byte values. (+1 from the previous definition) The value is not relevant to us, they only need to be distinct from each other and fit in one byte. When the VM executes a specific Op like OpConstant, it will use the iota-generated-value (Opcode) as an index to retrieve the constant (the evaluted expression, object.Object) and push it to the stack.

Jump to

Keyboard shortcuts

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