Compare commits
No commits in common. "b0068336cd2528d4280527d7802fb064e7ff32c4" and "cfae69c0c2d225a63bb4d8f8a0226ffd747a2adf" have entirely different histories.
b0068336cd
...
cfae69c0c2
15
creature.go
15
creature.go
@ -11,7 +11,10 @@ type Word interface {
|
||||
// data, and provides methods to run this program data, as well as interact with
|
||||
// it.
|
||||
type Machine [WORD Word] struct {
|
||||
program []WORD
|
||||
// Program is not modified by the machine, and can be freely set before
|
||||
// the machine is started.
|
||||
Program []WORD
|
||||
|
||||
stack []WORD
|
||||
block []WORD
|
||||
counter WORD
|
||||
@ -84,7 +87,7 @@ func (machine *Machine[WORD]) Reset() {
|
||||
func (machine *Machine[WORD]) Execute(offset WORD) (err error) {
|
||||
machine.counter = offset
|
||||
|
||||
for int(machine.counter) < len(machine.program) {
|
||||
for int(machine.counter) < len(machine.Program) {
|
||||
switch machine.instruction() {
|
||||
case PUSH:
|
||||
machine.counter++
|
||||
@ -199,7 +202,7 @@ func (machine *Machine[WORD]) Execute(offset WORD) (err error) {
|
||||
|
||||
// Instruction returns the current instruction in program memory.
|
||||
func (machine *Machine[WORD]) instruction() (instruction WORD) {
|
||||
instruction = machine.program[machine.counter]
|
||||
instruction = machine.Program[machine.counter]
|
||||
return
|
||||
}
|
||||
|
||||
@ -288,12 +291,6 @@ func (machine *Machine[WORD]) Unregister(id WORD) {
|
||||
delete(machine.functions, id)
|
||||
}
|
||||
|
||||
// LoadProgram loads the contents of program into the machine's program memory.
|
||||
func (machine *Machine[WORD]) LoadProgram(program []WORD) {
|
||||
machine.program = make([]WORD, len(program))
|
||||
copy(machine.program, program)
|
||||
}
|
||||
|
||||
// LoadMemory loads the contents of block into the machine's memory.
|
||||
func (machine *Machine[WORD]) LoadMemory(block []WORD) {
|
||||
machine.block = make([]WORD, len(block))
|
||||
|
@ -9,8 +9,7 @@ func runMachineTest (
|
||||
) (
|
||||
machine *Machine[int],
|
||||
) {
|
||||
machine = &Machine[int] {}
|
||||
machine.LoadProgram(program)
|
||||
machine = &Machine[int] { Program: program }
|
||||
if memory != nil {
|
||||
machine.LoadMemory(memory)
|
||||
}
|
||||
@ -231,8 +230,7 @@ func TestJump(test *testing.T) {
|
||||
|
||||
func TestRegister(test *testing.T) {
|
||||
output := ""
|
||||
machine := &Machine[int] {}
|
||||
machine.LoadProgram([]int {
|
||||
machine := &Machine[int] { Program: []int {
|
||||
PUSH, int('h'),
|
||||
PUSH, 4,
|
||||
CAL,
|
||||
@ -260,7 +258,7 @@ func TestRegister(test *testing.T) {
|
||||
PUSH, int('!'),
|
||||
PUSH, 4,
|
||||
CAL,
|
||||
})
|
||||
}}
|
||||
machine.Register(4, func(machine *Machine[int]) (stop bool) {
|
||||
output += string(rune(machine.Pop()))
|
||||
return
|
||||
|
@ -9,8 +9,7 @@ func main () {
|
||||
// this is a simple echo program. it will take in input indefinetly and
|
||||
// repeat it. due to line buffering in the terminal however, it will
|
||||
// only print output once you have pressed enter.
|
||||
machine := cre.Machine[int] {}
|
||||
machine.LoadProgram([]int {
|
||||
machine := cre.Machine[int] { Program: []int {
|
||||
cre.PUSH, 0,
|
||||
cre.CAL,
|
||||
|
||||
@ -20,7 +19,7 @@ func main () {
|
||||
cre.PUSH, 1,
|
||||
cre.PUSH, 0,
|
||||
cre.JMP,
|
||||
})
|
||||
}}
|
||||
|
||||
machine.Register (0, read)
|
||||
machine.Register (1, write)
|
||||
|
@ -20,8 +20,7 @@ func main () {
|
||||
responseStart := 71
|
||||
responseLoopStart := 50
|
||||
|
||||
machine := cre.Machine[int] {}
|
||||
machine.LoadProgram([]int {
|
||||
machine := cre.Machine[int] { Program: []int {
|
||||
// reset x
|
||||
cre.PUSH, introStart,
|
||||
cre.PUSH, x,
|
||||
@ -109,7 +108,7 @@ func main () {
|
||||
cre.PUSH, 1,
|
||||
cre.PUSH, responseLoopStart,
|
||||
cre.JMP,
|
||||
})
|
||||
}}
|
||||
|
||||
stringData := []byte (
|
||||
"\x00" +
|
||||
|
Loading…
Reference in New Issue
Block a user