Machine is now a generic type

This commit is contained in:
2022-08-29 10:48:45 -04:00
parent 810ecdb3ac
commit 635aced0d4
4 changed files with 56 additions and 44 deletions

View File

@@ -9,7 +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 { Program: []int {
machine := cre.Machine[int] { Program: []int {
cre.PUSH, 0,
cre.CAL,
@@ -27,14 +27,14 @@ func main () {
if err != nil { panic(err.Error()) }
}
func read (machine *cre.Machine) (stop bool) {
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) (stop bool) {
func write (machine *cre.Machine[int]) (stop bool) {
print(string(rune(machine.Pop())))
return
}

View File

@@ -20,7 +20,7 @@ func main () {
responseStart := 71
responseLoopStart := 50
machine := cre.Machine { Program: []int {
machine := cre.Machine[int] { Program: []int {
// reset x
cre.PUSH, introStart,
cre.PUSH, x,
@@ -129,14 +129,14 @@ func main () {
if err != nil { panic(err.Error()) }
}
func read (machine *cre.Machine) (stop bool) {
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) (stop bool) {
func write (machine *cre.Machine[int]) (stop bool) {
print(string(rune(machine.Pop())))
return
}