Compare commits

..

No commits in common. "b0068336cd2528d4280527d7802fb064e7ff32c4" and "cfae69c0c2d225a63bb4d8f8a0226ffd747a2adf" have entirely different histories.

5 changed files with 14 additions and 21 deletions

View File

@ -11,7 +11,10 @@ type Word interface {
// data, and provides methods to run this program data, as well as interact with // data, and provides methods to run this program data, as well as interact with
// it. // it.
type Machine [WORD Word] struct { 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 stack []WORD
block []WORD block []WORD
counter WORD counter WORD
@ -84,7 +87,7 @@ func (machine *Machine[WORD]) Reset() {
func (machine *Machine[WORD]) Execute(offset WORD) (err error) { func (machine *Machine[WORD]) Execute(offset WORD) (err error) {
machine.counter = offset machine.counter = offset
for int(machine.counter) < len(machine.program) { for int(machine.counter) < len(machine.Program) {
switch machine.instruction() { switch machine.instruction() {
case PUSH: case PUSH:
machine.counter++ machine.counter++
@ -199,7 +202,7 @@ func (machine *Machine[WORD]) Execute(offset WORD) (err error) {
// Instruction returns the current instruction in program memory. // Instruction returns the current instruction in program memory.
func (machine *Machine[WORD]) instruction() (instruction WORD) { func (machine *Machine[WORD]) instruction() (instruction WORD) {
instruction = machine.program[machine.counter] instruction = machine.Program[machine.counter]
return return
} }
@ -288,12 +291,6 @@ func (machine *Machine[WORD]) Unregister(id WORD) {
delete(machine.functions, id) 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. // LoadMemory loads the contents of block into the machine's memory.
func (machine *Machine[WORD]) LoadMemory(block []WORD) { func (machine *Machine[WORD]) LoadMemory(block []WORD) {
machine.block = make([]WORD, len(block)) machine.block = make([]WORD, len(block))

View File

@ -9,8 +9,7 @@ func runMachineTest (
) ( ) (
machine *Machine[int], machine *Machine[int],
) { ) {
machine = &Machine[int] {} machine = &Machine[int] { Program: program }
machine.LoadProgram(program)
if memory != nil { if memory != nil {
machine.LoadMemory(memory) machine.LoadMemory(memory)
} }
@ -231,8 +230,7 @@ func TestJump(test *testing.T) {
func TestRegister(test *testing.T) { func TestRegister(test *testing.T) {
output := "" output := ""
machine := &Machine[int] {} machine := &Machine[int] { Program: []int {
machine.LoadProgram([]int {
PUSH, int('h'), PUSH, int('h'),
PUSH, 4, PUSH, 4,
CAL, CAL,
@ -260,7 +258,7 @@ func TestRegister(test *testing.T) {
PUSH, int('!'), PUSH, int('!'),
PUSH, 4, PUSH, 4,
CAL, CAL,
}) }}
machine.Register(4, func(machine *Machine[int]) (stop bool) { machine.Register(4, func(machine *Machine[int]) (stop bool) {
output += string(rune(machine.Pop())) output += string(rune(machine.Pop()))
return return

View File

@ -9,8 +9,7 @@ func main () {
// this is a simple echo program. it will take in input indefinetly and // 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 // repeat it. due to line buffering in the terminal however, it will
// only print output once you have pressed enter. // only print output once you have pressed enter.
machine := cre.Machine[int] {} machine := cre.Machine[int] { Program: []int {
machine.LoadProgram([]int {
cre.PUSH, 0, cre.PUSH, 0,
cre.CAL, cre.CAL,
@ -20,7 +19,7 @@ func main () {
cre.PUSH, 1, cre.PUSH, 1,
cre.PUSH, 0, cre.PUSH, 0,
cre.JMP, cre.JMP,
}) }}
machine.Register (0, read) machine.Register (0, read)
machine.Register (1, write) machine.Register (1, write)

View File

@ -20,8 +20,7 @@ func main () {
responseStart := 71 responseStart := 71
responseLoopStart := 50 responseLoopStart := 50
machine := cre.Machine[int] {} machine := cre.Machine[int] { Program: []int {
machine.LoadProgram([]int {
// reset x // reset x
cre.PUSH, introStart, cre.PUSH, introStart,
cre.PUSH, x, cre.PUSH, x,
@ -109,7 +108,7 @@ func main () {
cre.PUSH, 1, cre.PUSH, 1,
cre.PUSH, responseLoopStart, cre.PUSH, responseLoopStart,
cre.JMP, cre.JMP,
}) }}
stringData := []byte ( stringData := []byte (
"\x00" + "\x00" +

2
go.mod
View File

@ -1,3 +1,3 @@
module git.tebibyte.media/sashakoshka/creature module git.tebibyte.media/sashakoshka/creature
go 1.18 go 1.19