Documentation
¶
Index ¶
- type Instruction
- type Op
- type Param
- type ParamMode
- type VM
- func (vm *VM) At(i int) int
- func (vm *VM) AtOffset(i int) int
- func (vm *VM) Clone() *VM
- func (vm *VM) Execute() error
- func (vm *VM) MustExecute()
- func (vm *VM) PipeTo(other *VM)
- func (vm *VM) Set(i int, value int)
- func (vm *VM) SetInputChan(input chan int)
- func (vm *VM) SetInputFunc(input func() int)
- func (vm *VM) SetInputSeq(input []int)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Instruction ¶
type Instruction struct { // Op is the opcode for the action the instruction should perform. Op Op // Params is the list of parameters provided to the instruction. Params []Param }
Instruction is a single instruction read from the memory of a VM.
type Param ¶
type Param struct { // Value is the integer value of the parameter. Value int // Mode is the mode that should be used to interpret the value. Mode ParamMode }
Param is a single parameter from an Intcode instruction.
type ParamMode ¶
type ParamMode int
ParamMode represents how to use the value in an Intcode instruction parameter.
const ( // ModePosition indicates the parameter's value refers to an absolute address // memory. ModePosition ParamMode = 0 // ModeImmediate indicates the parameter's value should be used directly, and // does not correspond to another address in memory. ModeImmediate ParamMode = 1 // ModeRelative indicates the parameter's value refers to an offset from the // VM's current relative base. ModeRelative ParamMode = 2 )
type VM ¶
type VM struct { Memory []int Input func() int Output chan int // contains filtered or unexported fields }
VM is an Intcode virtual machine.
Each VM can run a single Intcode program.
func LoadFromString ¶
LoadFromString parses an Intcode program from a string and loads it into a new VM.
func (*VM) AtOffset ¶
AtOffset gets the integer that is at the given offset from the current instruction pointer.
func (*VM) Clone ¶
Clone creates a blank copy of the VM. This can be used to run the same program multiple times. The cloned VM has a copy of the internal state of the original. The clone has its own input and output channels.
func (*VM) Execute ¶
Execute runs the loaded Intcode program to completion.
It only returns an error if the program is invalid in some way: either it uses an unknown instruction opcode or an instruction is malformed.
func (*VM) MustExecute ¶
func (vm *VM) MustExecute()
MustExecute runs the loaded program like Execute, but it panics if there is an error running the program.
func (*VM) PipeTo ¶
PipeTo continuously waits for output from this VM and provides each value as an input to `other`.
PipeTo blocks waiting for output, so it should be run on its own goroutine.
func (*VM) SetInputChan ¶
SetInputChan sets a channel to provide input to the program.
func (*VM) SetInputFunc ¶
SetInputFunc sets a function to provide input to the program when requested.
func (*VM) SetInputSeq ¶
SetInputSeq sets a sequence of values to provide one-by-one as input to the program.