code

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2020 License: MIT Imports: 3 Imported by: 6

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Make

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

Make creates our bytecode. First we find out how long the resulting instruction is going to be. That allows us to allocate a byte slice with the proper length. We then add the Opcode as its first byte by casting it into one. Then we iterate over the defined OperandWidths, take the matching element from operands, and put it in the instruction. After encoding the two byte operand in big endian, we increment offset by its width.

func ReadOperands

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

ReadOperands is Make's counterpart. It decodes the operands of a bytecode instruction.

func ReadUint16

func ReadUint16(ins Instructions) uint16

ReadUint16 turns a byte sequence (Instructions) into a uint16

func ReadUint8

func ReadUint8(ins Instructions) uint8

ReadUint8 turns a byte sequence (Instructions) into a uint16

Types

type Definition

type Definition struct {
	Name          string
	OperandWidths []int
}

Definition for an opcode. Name helps to make an Opcode readable and OperandWidths contains the number of bytes each operand takes up.

func Lookup

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

Lookup finds a definition by opcode. It returns it if it is found otherwise returns an error

type Instructions

type Instructions []byte

Instructions - Type alias for a slice of byte.

func (Instructions) String

func (ins Instructions) String() string

String is a sort of mini dissassembler to print our bytecode in human readable format

type Opcode

type Opcode byte

Opcode is a type alias for byte

const (
	// Constants
	OpConstant Opcode = iota

	// Aritmatic & stack pop
	OpAdd
	OpPop
	OpSub
	OpMul
	OpDiv
	OpMod

	// Boolean
	OpTrue
	OpFalse

	// Comparison/Logical
	OpEqualEqual
	OpNotEqual
	OpGreater
	OpGreaterEqual
	OpAnd
	OpOr

	// Prefix/unary
	OpMinus
	OpBang

	// Postfix/unary
	OpPlusPlus
	OpMinusMinus

	// Jump for conditionals
	OpJumpNotTruthy // Jump to alternative if consequence is not truthy
	OpJump          // Jump no matter what (if we evaluate consequence and dont want the alternative)

	// Null
	OpNull

	// Get and Set global variables
	OpGetGlobal
	OpSetGlobal

	// Data structures and index access
	OpArray
	OpHash
	OpIndex

	// Call expression, return with value, return without value
	OpCall
	OpReturnValue // Tells VM to leave value on top of stack
	OpReturn      // Tells VM implicit return of Null

	// Get and Set local bindings
	OpGetLocal
	OpSetLocal

	// Builtin functions
	OpGetBuiltin

	// Closures and it's variables
	OpClosure
	OpGetFree
	OpCurrentClosure
)

Define our opcode types

Jump to

Keyboard shortcuts

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