creature/examples/echo.go

41 lines
807 B
Go

// +build ignore
package main
import "os"
import cre "git.tebibyte.media/sashakoshka/creature"
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] { Program: []int {
cre.PUSH, 0,
cre.CAL,
cre.PUSH, 1,
cre.CAL,
cre.PUSH, 1,
cre.PUSH, 0,
cre.JMP,
}}
machine.Register (0, read)
machine.Register (1, write)
err := machine.Execute(0)
if err != nil { panic(err.Error()) }
}
func read (machine *cre.Machine[int]) (stop bool) {
ch := []byte { 0 }
os.Stdin.Read(ch)
machine.Push(int(ch[0]))
return
}
func write (machine *cre.Machine[int]) (stop bool) {
print(string(rune(machine.Pop())))
return
}