Ran gofmt

This commit is contained in:
Sasha Koshka 2022-09-08 14:43:19 -04:00
parent b0068336cd
commit a52e5d3426
5 changed files with 81 additions and 75 deletions

1
cmd/main.go Normal file
View File

@ -0,0 +1 @@
package main

View File

@ -2,15 +2,15 @@ package creature
// Word is a type constraint defining possible word types for the Machine. // Word is a type constraint defining possible word types for the Machine.
type Word interface { type Word interface {
int8 | int16 | int32 | int64 | int8 | int16 | int32 | int64 |
uint8 | uint16 | uint32 | uint64 | uint8 | uint16 | uint32 | uint64 |
int | uint int | uint
} }
// Machine is a stack machine. It contains an array of integers as its program // Machine is a stack machine. It contains an array of integers as its program
// 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 []WORD
stack []WORD stack []WORD
block []WORD block []WORD
@ -22,7 +22,7 @@ type Machine [WORD Word] struct {
// MachineFunction is a function that can extend the functionality of the stack // MachineFunction is a function that can extend the functionality of the stack
// machine. It is passed a pointer to the machine that is calling it, and the // machine. It is passed a pointer to the machine that is calling it, and the
// machine will halt execution if true is returned. // machine will halt execution if true is returned.
type MachineFunction [WORD Word] func(machine *Machine[WORD]) (stop bool) type MachineFunction[WORD Word] func(machine *Machine[WORD]) (stop bool)
// All supported opcodes // All supported opcodes
const ( const (
@ -103,22 +103,22 @@ func (machine *Machine[WORD]) Execute(offset WORD) (err error) {
case ADD: case ADD:
right := machine.Pop() right := machine.Pop()
left := machine.Pop() left := machine.Pop()
machine.Push(left + right) machine.Push(left + right)
case SUB: case SUB:
right := machine.Pop() right := machine.Pop()
left := machine.Pop() left := machine.Pop()
machine.Push(left - right) machine.Push(left - right)
case MUL: case MUL:
right := machine.Pop() right := machine.Pop()
left := machine.Pop() left := machine.Pop()
machine.Push(left * right) machine.Push(left * right)
case DIV: case DIV:
right := machine.Pop() right := machine.Pop()
left := machine.Pop() left := machine.Pop()
if right == 0 { if right == 0 {
err = ErrorDivideByZero err = ErrorDivideByZero
return return
@ -127,7 +127,7 @@ func (machine *Machine[WORD]) Execute(offset WORD) (err error) {
case EQ: case EQ:
right := machine.Pop() right := machine.Pop()
left := machine.Pop() left := machine.Pop()
equal := 0 equal := 0
if left == right { if left == right {
equal = 1 equal = 1
@ -136,7 +136,7 @@ func (machine *Machine[WORD]) Execute(offset WORD) (err error) {
case GT: case GT:
right := machine.Pop() right := machine.Pop()
left := machine.Pop() left := machine.Pop()
greater := 0 greater := 0
if left > right { if left > right {
greater = 1 greater = 1
@ -145,7 +145,7 @@ func (machine *Machine[WORD]) Execute(offset WORD) (err error) {
case LT: case LT:
right := machine.Pop() right := machine.Pop()
left := machine.Pop() left := machine.Pop()
less := 0 less := 0
if left < right { if left < right {
less = 1 less = 1
@ -154,7 +154,7 @@ func (machine *Machine[WORD]) Execute(offset WORD) (err error) {
case NEQ: case NEQ:
right := machine.Pop() right := machine.Pop()
left := machine.Pop() left := machine.Pop()
notEqual := 0 notEqual := 0
if left != right { if left != right {
notEqual = 1 notEqual = 1
@ -163,7 +163,7 @@ func (machine *Machine[WORD]) Execute(offset WORD) (err error) {
case MOD: case MOD:
right := machine.Pop() right := machine.Pop()
left := machine.Pop() left := machine.Pop()
machine.Push(left % right) machine.Push(left % right)
case HALT: case HALT:
@ -228,7 +228,7 @@ func (machine *Machine[WORD]) Pop() (word WORD) {
if int(machine.pointer) <= 0 || int(machine.pointer) >= len(machine.stack) { if int(machine.pointer) <= 0 || int(machine.pointer) >= len(machine.stack) {
return return
} }
word = machine.stack[machine.pointer] word = machine.stack[machine.pointer]
machine.pointer-- machine.pointer--
@ -259,7 +259,7 @@ func (machine *Machine[WORD]) Poke(address WORD, word WORD) {
// Register registers a function at the specified ID. If there is already a // Register registers a function at the specified ID. If there is already a
// function registered at that ID, this method will return an error. // function registered at that ID, this method will return an error.
func (machine *Machine[WORD]) Register ( func (machine *Machine[WORD]) Register(
id WORD, id WORD,
function MachineFunction[WORD], function MachineFunction[WORD],
) ( ) (

View File

@ -2,14 +2,14 @@ package creature
import "testing" import "testing"
func runMachineTest ( func runMachineTest(
program []int, program []int,
memory []int, memory []int,
test *testing.T, test *testing.T,
) ( ) (
machine *Machine[int], machine *Machine[int],
) { ) {
machine = &Machine[int] {} machine = &Machine[int]{}
machine.LoadProgram(program) machine.LoadProgram(program)
if memory != nil { if memory != nil {
machine.LoadMemory(memory) machine.LoadMemory(memory)
@ -25,12 +25,12 @@ func runMachineTest (
} }
func TestPush(test *testing.T) { func TestPush(test *testing.T) {
machine := runMachineTest ([]int { machine := runMachineTest([]int{
PUSH, 3, PUSH, 3,
POP, POP,
PUSH, 654, PUSH, 654,
}, nil, test) }, nil, test)
result := machine.Pop() result := machine.Pop()
test.Log("popped:", result) test.Log("popped:", result)
@ -41,23 +41,23 @@ func TestPush(test *testing.T) {
} }
func TestArithmetic(test *testing.T) { func TestArithmetic(test *testing.T) {
machine := runMachineTest([]int { machine := runMachineTest([]int{
PUSH, 3, PUSH, 3,
PUSH, 2, PUSH, 2,
ADD, ADD,
PUSH, 10, PUSH, 10,
PUSH, 4, PUSH, 4,
SUB, SUB,
PUSH, 2, PUSH, 2,
PUSH, 7, PUSH, 7,
MUL, MUL,
PUSH, 12, PUSH, 12,
PUSH, 3, PUSH, 3,
DIV, DIV,
PUSH, 8, PUSH, 8,
PUSH, 6, PUSH, 6,
MOD, MOD,
@ -102,23 +102,22 @@ func TestArithmetic(test *testing.T) {
} }
func TestComparison(test *testing.T) { func TestComparison(test *testing.T) {
machine := runMachineTest([]int { machine := runMachineTest([]int{
PUSH, 6, PUSH, 6,
PUSH, 6, PUSH, 6,
EQ, EQ,
PUSH, 324, PUSH, 324,
PUSH, 4, PUSH, 4,
GT, GT,
PUSH, 4, PUSH, 4,
PUSH, 324, PUSH, 324,
LT, LT,
PUSH, 54, PUSH, 54,
PUSH, 6, PUSH, 6,
NEQ, NEQ,
}, nil, test) }, nil, test)
neqResult := machine.Pop() neqResult := machine.Pop()
@ -153,7 +152,7 @@ func TestComparison(test *testing.T) {
} }
func TestPeekPoke(test *testing.T) { func TestPeekPoke(test *testing.T) {
machine := runMachineTest ([]int { machine := runMachineTest([]int{
PUSH, 0, PUSH, 0,
PEEK, PEEK,
@ -163,14 +162,14 @@ func TestPeekPoke(test *testing.T) {
PUSH, 1, PUSH, 1,
PEEK, PEEK,
}, []int { }, []int{
632, 632,
13, 13,
}, test) }, test)
secondResult := machine.Pop() secondResult := machine.Pop()
firstResult := machine.Pop() firstResult := machine.Pop()
test.Log("first:", firstResult) test.Log("first:", firstResult)
test.Log("second:", secondResult) test.Log("second:", secondResult)
@ -186,12 +185,12 @@ func TestPeekPoke(test *testing.T) {
} }
func TestHalt(test *testing.T) { func TestHalt(test *testing.T) {
machine := runMachineTest ([]int { machine := runMachineTest([]int{
PUSH, 32, PUSH, 32,
HALT, HALT,
PUSH, 3, PUSH, 3,
}, nil, test) }, nil, test)
result := machine.Pop() result := machine.Pop()
test.Log("popped:", result) test.Log("popped:", result)
@ -202,11 +201,11 @@ func TestHalt(test *testing.T) {
} }
func TestJump(test *testing.T) { func TestJump(test *testing.T) {
machine := runMachineTest ([]int { machine := runMachineTest([]int{
PUSH, 1, PUSH, 1,
PUSH, 8, PUSH, 8,
JMP, JMP,
PUSH, 3, PUSH, 3,
HALT, HALT,
@ -219,7 +218,7 @@ func TestJump(test *testing.T) {
PUSH, 5, PUSH, 5,
}, nil, test) }, nil, test)
result := machine.Pop() result := machine.Pop()
test.Log("popped:", result) test.Log("popped:", result)
@ -231,8 +230,8 @@ func TestJump(test *testing.T) {
func TestRegister(test *testing.T) { func TestRegister(test *testing.T) {
output := "" output := ""
machine := &Machine[int] {} machine := &Machine[int]{}
machine.LoadProgram([]int { machine.LoadProgram([]int{
PUSH, int('h'), PUSH, int('h'),
PUSH, 4, PUSH, 4,
CAL, CAL,

View File

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

View File

@ -1,3 +1,4 @@
//go:build ignore
// +build ignore // +build ignore
package main package main
@ -5,7 +6,7 @@ package main
import "os" import "os"
import cre "git.tebibyte.media/sashakoshka/creature" import cre "git.tebibyte.media/sashakoshka/creature"
func main () { func main() {
// this is an "ai psychiatrist" program. when it starts, it asks the // this is an "ai psychiatrist" program. when it starts, it asks the
// user "What brings you in today?", and no matter what the user types, // user "What brings you in today?", and no matter what the user types,
// the program will respond with "And how does that make you feel?". // the program will respond with "And how does that make you feel?".
@ -13,15 +14,15 @@ func main () {
// point. // point.
// constant address values // constant address values
x := 0 x := 0
introStart := 1 introStart := 1
introLoopStart := 5 introLoopStart := 5
readLoopStart := 36 readLoopStart := 36
responseStart := 71 responseStart := 71
responseLoopStart := 50 responseLoopStart := 50
machine := cre.Machine[int] {} machine := cre.Machine[int]{}
machine.LoadProgram([]int { machine.LoadProgram([]int{
// reset x // reset x
cre.PUSH, introStart, cre.PUSH, introStart,
cre.PUSH, x, cre.PUSH, x,
@ -38,7 +39,7 @@ func main () {
cre.EQ, cre.EQ,
cre.PUSH, readLoopStart, cre.PUSH, readLoopStart,
cre.JMP, cre.JMP,
// get char // get char
cre.PUSH, x, cre.PUSH, x,
cre.PEEK, cre.PEEK,
@ -87,7 +88,7 @@ func main () {
cre.EQ, cre.EQ,
cre.PUSH, readLoopStart, cre.PUSH, readLoopStart,
cre.JMP, cre.JMP,
// get char // get char
cre.PUSH, x, cre.PUSH, x,
cre.PEEK, cre.PEEK,
@ -111,33 +112,35 @@ func main () {
cre.JMP, cre.JMP,
}) })
stringData := []byte ( stringData := []byte(
"\x00" + "\x00" +
"== Artificial Intelligence Psychiatrist ==\n" + "== Artificial Intelligence Psychiatrist ==\n" +
"What brings you in today?\n\x00" + "What brings you in today?\n\x00" +
"And how does that make you feel?\n\x00") "And how does that make you feel?\n\x00")
block := make([]int, len(stringData)) block := make([]int, len(stringData))
for index, char := range stringData { for index, char := range stringData {
block[index] = int(char) block[index] = int(char)
} }
machine.LoadMemory(block) machine.LoadMemory(block)
machine.Register (0, read) machine.Register(0, read)
machine.Register (1, write) machine.Register(1, write)
err := machine.Execute(0) err := machine.Execute(0)
if err != nil { panic(err.Error()) } if err != nil {
panic(err.Error())
}
} }
func read (machine *cre.Machine[int]) (stop bool) { func read(machine *cre.Machine[int]) (stop bool) {
ch := []byte { 0 } ch := []byte{0}
os.Stdin.Read(ch) os.Stdin.Read(ch)
machine.Push(int(ch[0])) machine.Push(int(ch[0]))
return return
} }
func write (machine *cre.Machine[int]) (stop bool) { func write(machine *cre.Machine[int]) (stop bool) {
print(string(rune(machine.Pop()))) print(string(rune(machine.Pop())))
return return
} }