Compare commits

...

2 Commits

Author SHA1 Message Date
Sasha Koshka b0068336cd Programs are now loaded with a function and not a member var 2022-09-08 14:41:50 -04:00
Sasha Koshka 3be08736b4 Downgraded to go 1.18 so it works on my laptop 2022-09-08 14:36:28 -04:00
5 changed files with 21 additions and 14 deletions

View File

@ -11,10 +11,7 @@ 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 is not modified by the machine, and can be freely set before program []WORD
// the machine is started.
Program []WORD
stack []WORD stack []WORD
block []WORD block []WORD
counter WORD counter WORD
@ -87,7 +84,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++
@ -202,7 +199,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
} }
@ -291,6 +288,12 @@ 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,7 +9,8 @@ func runMachineTest (
) ( ) (
machine *Machine[int], machine *Machine[int],
) { ) {
machine = &Machine[int] { Program: program } machine = &Machine[int] {}
machine.LoadProgram(program)
if memory != nil { if memory != nil {
machine.LoadMemory(memory) machine.LoadMemory(memory)
} }
@ -230,7 +231,8 @@ func TestJump(test *testing.T) {
func TestRegister(test *testing.T) { func TestRegister(test *testing.T) {
output := "" output := ""
machine := &Machine[int] { Program: []int { machine := &Machine[int] {}
machine.LoadProgram([]int {
PUSH, int('h'), PUSH, int('h'),
PUSH, 4, PUSH, 4,
CAL, CAL,
@ -258,7 +260,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,7 +9,8 @@ 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] { Program: []int { machine := cre.Machine[int] {}
machine.LoadProgram([]int {
cre.PUSH, 0, cre.PUSH, 0,
cre.CAL, cre.CAL,
@ -19,7 +20,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,7 +20,8 @@ func main () {
responseStart := 71 responseStart := 71
responseLoopStart := 50 responseLoopStart := 50
machine := cre.Machine[int] { Program: []int { machine := cre.Machine[int] {}
machine.LoadProgram([]int {
// reset x // reset x
cre.PUSH, introStart, cre.PUSH, introStart,
cre.PUSH, x, cre.PUSH, x,
@ -108,7 +109,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.19 go 1.18