bytecode

package
v0.0.0-...-e334538 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2021 License: MIT Imports: 2 Imported by: 6

Documentation

Index

Constants

View Source
const (
	Nop byte = iota
	NopArg

	/* Storage & constants */
	// LoadConst loads a constant by index: [arg]
	LoadConst

	// LoadName loads a name by index: [arg]
	LoadName

	// StoreName stores $0 in the name indexed by [arg]
	StoreName

	// Declarename is the same as StoreName, but only operates in
	// the single enclosing scope, not the parent ones
	DeclareName

	// LoadSubscript pushes $1[$0]
	LoadSubscript

	// StoreSubscript sets $1[$0] to $2
	StoreSubscript

	/* Operators */
	UnaryInvert
	UnaryNegate
	UnaryTuple
	BinaryAdd
	BinarySub
	BinaryMul
	BinaryDiv
	BinaryExp
	BinaryFloorDiv
	BinaryMod
	BinaryLogicOr
	BinaryLogicAnd
	BinaryBitOr
	BinaryBitAnd
	BinaryEqual
	BinaryNotEqual
	BinaryLess
	BinaryMore
	BinaryLessEq
	BinaryMoreEq
	BinaryTuple

	/* Functions & scopes */
	// CallFunctions calls $0 and pops an item for each argument
	CallFunction

	Return
	PushScope
	PopScope
	Export

	// Jump jumps to target [arg]
	Jump

	// JumpIf jumps to target [arg] if $0 is truthy
	JumpIf

	// JumpUnless jumps to target [arg] unless $0 is truthy
	JumpUnless

	/* Matches */
	// StartMatch begins a match block, pushing $0 to the match-value register
	StartMatch

	// EndMatch ends a match block, popping the match-value register
	EndMatch

	// StartBranch specifies the start of a match branch
	StartBranch

	// EndBranch specifies the end of a match branch
	EndBranch

	/* Loop stuff */
	Break
	Next
	StartLoop
	EndLoop

	/* Iterables */
	// PushIter converts $0 to an iterable if possible and pushes it to the iterable stack
	PushIter

	// PopIter pops the top iterable from the iterable stack, discarding it
	PopIter

	// AdvIterFor advances the top iterable on the iterable stack. If nothing left, breaks
	// out of the loop. Designed for use in for-loops. Will also assign the top iter to [arg]
	AdvIterFor

	/* Data */
	// MakeList pushes a list containing $0, $1, ..., $[arg]
	MakeList

	// MakeMap pushes a map from the top [arg]*2 items, in key, val order
	MakeMap
)

The set of available bytecode instructions. $0 denotes the 0th (top) item in the stack. using $0 pops it from the stack [arg] denotes the instruction's argument.

Variables

View Source
var Instructions = map[byte]Data{
	Nop:    {Name: "NO_OP"},
	NopArg: {Name: "NO_OP_ARG", HasArg: true},

	LoadConst:      {Name: "LOAD_CONST", HasArg: true},
	LoadName:       {Name: "LOAD_NAME", HasArg: true},
	StoreName:      {Name: "STORE_NAME", HasArg: true},
	DeclareName:    {Name: "DECLARE_NAME", HasArg: true},
	LoadSubscript:  {Name: "LOAD_SUBSCRIPT"},
	StoreSubscript: {Name: "STORE_SUBSCRIPT"},

	UnaryInvert:    {Name: "UNARY_INVERT"},
	UnaryNegate:    {Name: "UNARY_NEGATE"},
	UnaryTuple:     {Name: "UNARY_TUPLE"},
	BinaryAdd:      {Name: "BINARY_ADD"},
	BinarySub:      {Name: "BINARY_SUB"},
	BinaryMul:      {Name: "BINARY_MUL"},
	BinaryDiv:      {Name: "BINARY_DIV"},
	BinaryExp:      {Name: "BINARY_EXP"},
	BinaryFloorDiv: {Name: "BINARY_FLOOR_DIV"},
	BinaryMod:      {Name: "BINARY_MODULO"},
	BinaryLogicOr:  {Name: "BINARY_LOGIC_OR"},
	BinaryLogicAnd: {Name: "BINARY_LOGIC_AND"},
	BinaryBitOr:    {Name: "BINARY_BIT_OR"},
	BinaryBitAnd:   {Name: "BINARY_BIT_AND"},
	BinaryEqual:    {Name: "BINARY_EQUAL"},
	BinaryNotEqual: {Name: "BINARY_NOT_EQUAL"},
	BinaryLess:     {Name: "BINARY_LESS_THAN"},
	BinaryMore:     {Name: "BINARY_MORE_THAN"},
	BinaryLessEq:   {Name: "BINARY_LESS_EQ"},
	BinaryMoreEq:   {Name: "BINARY_MORE_EQ"},
	BinaryTuple:    {Name: "BINARY_TUPLE"},

	CallFunction: {Name: "CALL_FUNCTION", HasArg: true},
	Return:       {Name: "RETURN"},
	PushScope:    {Name: "PUSH_SCOPE"},
	PopScope:     {Name: "POP_SCOPE"},
	Export:       {Name: "EXPORT", HasArg: true},

	Jump:        {Name: "JUMP", HasArg: true},
	JumpIf:      {Name: "JUMP_IF", HasArg: true},
	JumpUnless:  {Name: "JUMP_UNLESS", HasArg: true},
	StartMatch:  {Name: "START_MATCH"},
	EndMatch:    {Name: "END_MATCH"},
	StartBranch: {Name: "START_BRANCH"},
	EndBranch:   {Name: "END_BRANCH"},
	Break:       {Name: "BREAK"},
	Next:        {Name: "NEXT"},
	StartLoop:   {Name: "START_LOOP"},
	EndLoop:     {Name: "END_LOOP"},
	PushIter:    {Name: "PUSH_ITER"},
	PopIter:     {Name: "POP_ITER"},
	AdvIterFor:  {Name: "ADV_ITER_FOR", HasArg: true},

	MakeList: {Name: "MAKE_LIST", HasArg: true},
	MakeMap:  {Name: "MAKE_MAP", HasArg: true},
}

Instructions stores data about different instruction types.

Functions

This section is empty.

Types

type Code

type Code []Instruction

Code is a bytecode program. Usually obtained by parsing a series of bytes.

func Read

func Read(r io.Reader) (Code, error)

Read takes an io.Reader and parses it into a series of instructions in the form of a Code instance. This allows it to be executed more efficiently by the virtual machine.

An error can occur if an argument is expected but EOF is found, or if the reader encounters any other error.

type Data

type Data struct {
	Name   string
	HasArg bool
}

Data specifies the name of an instruction, and whether or not it takes an argument.

type Instruction

type Instruction struct {
	Code byte
	Name string
	Arg  rune
}

An Instruction is a parsed bytecode instruction.

Jump to

Keyboard shortcuts

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