Compare commits
5 Commits
cfae69c0c2
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c7d948cdcd | ||
|
|
dcd8c56c0d | ||
|
|
a52e5d3426 | ||
|
|
b0068336cd | ||
|
|
3be08736b4 |
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
/cmd/creature/creature
|
||||
10
cmd/creature/examples/echo
Normal file
10
cmd/creature/examples/echo
Normal file
@@ -0,0 +1,10 @@
|
||||
---
|
||||
0 0
|
||||
F
|
||||
|
||||
0 1
|
||||
F
|
||||
|
||||
0 1
|
||||
0 0
|
||||
E
|
||||
BIN
cmd/creature/examples/echobin
Normal file
BIN
cmd/creature/examples/echobin
Normal file
Binary file not shown.
112
cmd/creature/main.go
Normal file
112
cmd/creature/main.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package main
|
||||
|
||||
import "io"
|
||||
import "os"
|
||||
import "fmt"
|
||||
import "bufio"
|
||||
import "strconv"
|
||||
import cre "git.tebibyte.media/sashakoshka/creature"
|
||||
|
||||
func main() {
|
||||
if len(os.Args) != 2 {
|
||||
logLine("ERR file unspecified")
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
file, err := os.Open(os.Args[1])
|
||||
if err != nil {
|
||||
logLine("ERR could not open file: " + err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
program, block, err := readFile(file)
|
||||
if err != nil {
|
||||
logLine("ERR could not read file: " + err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
machine := cre.Machine[int]{}
|
||||
|
||||
machine.LoadProgram(program)
|
||||
machine.LoadMemory(block)
|
||||
|
||||
machine.Register(0, read)
|
||||
machine.Register(1, write)
|
||||
|
||||
err = machine.Execute(0)
|
||||
if err != nil {
|
||||
logLine("XXX machine failed: " + err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// read implements a basic input function for the creature. It waits until it
|
||||
// has recieved one byte of user input, and then pushes that byte onto the
|
||||
// stack.
|
||||
func read(machine *cre.Machine[int]) (stop bool) {
|
||||
ch := []byte{0}
|
||||
os.Stdin.Read(ch)
|
||||
machine.Push(int(ch[0]))
|
||||
return
|
||||
}
|
||||
|
||||
// write implements a basic output function for the creature. It pops one byte
|
||||
// off of the stack, and writes it to stdout.
|
||||
func write(machine *cre.Machine[int]) (stop bool) {
|
||||
print(string(rune(machine.Pop())))
|
||||
return
|
||||
}
|
||||
|
||||
// logLine prints a message to stderr.
|
||||
func logLine(message ...any) {
|
||||
fmt.Fprintln(os.Stderr, message...)
|
||||
}
|
||||
|
||||
// readFile reads data from an io.Reader into a program slice and a block slice.
|
||||
// Data in the file is represented by signed hexidecimal numbers, separated by
|
||||
// whitespace. Three dashes (---) divide the block data from the program data.
|
||||
// See examples/echo.
|
||||
func readFile(reader io.Reader) (program []int, block []int, err error) {
|
||||
scanner := bufio.NewScanner(reader)
|
||||
scanner.Split(bufio.ScanWords)
|
||||
|
||||
blockLen := 0
|
||||
block = make([]int, 8)
|
||||
for scanner.Scan() {
|
||||
if scanner.Text() == "---" {
|
||||
break
|
||||
}
|
||||
if blockLen >= len(block) {
|
||||
newSlice := make([]int, len(block)*2)
|
||||
copy(newSlice, block)
|
||||
block = newSlice
|
||||
}
|
||||
var number int64
|
||||
number, err = strconv.ParseInt(scanner.Text(), 16, 64)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
block[blockLen] = int(number)
|
||||
blockLen++
|
||||
}
|
||||
block = block[:blockLen]
|
||||
|
||||
programLen := 0
|
||||
program = make([]int, 8)
|
||||
for scanner.Scan() {
|
||||
if programLen >= len(program) {
|
||||
newSlice := make([]int, len(program)*2)
|
||||
copy(newSlice, program)
|
||||
program = newSlice
|
||||
}
|
||||
var number int64
|
||||
number, err = strconv.ParseInt(scanner.Text(), 16, 64)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
program[programLen] = int(number)
|
||||
programLen++
|
||||
}
|
||||
program = program[:programLen]
|
||||
return
|
||||
}
|
||||
21
creature.go
21
creature.go
@@ -10,11 +10,8 @@ type Word interface {
|
||||
// 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
|
||||
// it.
|
||||
type Machine [WORD Word] struct {
|
||||
// Program is not modified by the machine, and can be freely set before
|
||||
// the machine is started.
|
||||
Program []WORD
|
||||
|
||||
type Machine[WORD Word] struct {
|
||||
program []WORD
|
||||
stack []WORD
|
||||
block []WORD
|
||||
counter WORD
|
||||
@@ -25,7 +22,7 @@ type Machine [WORD Word] struct {
|
||||
// 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 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
|
||||
const (
|
||||
@@ -87,7 +84,7 @@ func (machine *Machine[WORD]) Reset() {
|
||||
func (machine *Machine[WORD]) Execute(offset WORD) (err error) {
|
||||
machine.counter = offset
|
||||
|
||||
for int(machine.counter) < len(machine.Program) {
|
||||
for int(machine.counter) < len(machine.program) {
|
||||
switch machine.instruction() {
|
||||
case PUSH:
|
||||
machine.counter++
|
||||
@@ -202,7 +199,7 @@ func (machine *Machine[WORD]) Execute(offset WORD) (err error) {
|
||||
|
||||
// Instruction returns the current instruction in program memory.
|
||||
func (machine *Machine[WORD]) instruction() (instruction WORD) {
|
||||
instruction = machine.Program[machine.counter]
|
||||
instruction = machine.program[machine.counter]
|
||||
return
|
||||
}
|
||||
|
||||
@@ -262,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
|
||||
// function registered at that ID, this method will return an error.
|
||||
func (machine *Machine[WORD]) Register (
|
||||
func (machine *Machine[WORD]) Register(
|
||||
id WORD,
|
||||
function MachineFunction[WORD],
|
||||
) (
|
||||
@@ -291,6 +288,12 @@ func (machine *Machine[WORD]) Unregister(id WORD) {
|
||||
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.
|
||||
func (machine *Machine[WORD]) LoadMemory(block []WORD) {
|
||||
machine.block = make([]WORD, len(block))
|
||||
|
||||
@@ -2,14 +2,15 @@ package creature
|
||||
|
||||
import "testing"
|
||||
|
||||
func runMachineTest (
|
||||
func runMachineTest(
|
||||
program []int,
|
||||
memory []int,
|
||||
test *testing.T,
|
||||
) (
|
||||
machine *Machine[int],
|
||||
) {
|
||||
machine = &Machine[int] { Program: program }
|
||||
machine = &Machine[int]{}
|
||||
machine.LoadProgram(program)
|
||||
if memory != nil {
|
||||
machine.LoadMemory(memory)
|
||||
}
|
||||
@@ -24,7 +25,7 @@ func runMachineTest (
|
||||
}
|
||||
|
||||
func TestPush(test *testing.T) {
|
||||
machine := runMachineTest ([]int {
|
||||
machine := runMachineTest([]int{
|
||||
PUSH, 3,
|
||||
POP,
|
||||
PUSH, 654,
|
||||
@@ -40,7 +41,7 @@ func TestPush(test *testing.T) {
|
||||
}
|
||||
|
||||
func TestArithmetic(test *testing.T) {
|
||||
machine := runMachineTest([]int {
|
||||
machine := runMachineTest([]int{
|
||||
PUSH, 3,
|
||||
PUSH, 2,
|
||||
ADD,
|
||||
@@ -101,7 +102,7 @@ func TestArithmetic(test *testing.T) {
|
||||
}
|
||||
|
||||
func TestComparison(test *testing.T) {
|
||||
machine := runMachineTest([]int {
|
||||
machine := runMachineTest([]int{
|
||||
PUSH, 6,
|
||||
PUSH, 6,
|
||||
EQ,
|
||||
@@ -117,7 +118,6 @@ func TestComparison(test *testing.T) {
|
||||
PUSH, 54,
|
||||
PUSH, 6,
|
||||
NEQ,
|
||||
|
||||
}, nil, test)
|
||||
|
||||
neqResult := machine.Pop()
|
||||
@@ -152,7 +152,7 @@ func TestComparison(test *testing.T) {
|
||||
}
|
||||
|
||||
func TestPeekPoke(test *testing.T) {
|
||||
machine := runMachineTest ([]int {
|
||||
machine := runMachineTest([]int{
|
||||
PUSH, 0,
|
||||
PEEK,
|
||||
|
||||
@@ -162,7 +162,7 @@ func TestPeekPoke(test *testing.T) {
|
||||
|
||||
PUSH, 1,
|
||||
PEEK,
|
||||
}, []int {
|
||||
}, []int{
|
||||
632,
|
||||
13,
|
||||
}, test)
|
||||
@@ -185,7 +185,7 @@ func TestPeekPoke(test *testing.T) {
|
||||
}
|
||||
|
||||
func TestHalt(test *testing.T) {
|
||||
machine := runMachineTest ([]int {
|
||||
machine := runMachineTest([]int{
|
||||
PUSH, 32,
|
||||
HALT,
|
||||
PUSH, 3,
|
||||
@@ -201,7 +201,7 @@ func TestHalt(test *testing.T) {
|
||||
}
|
||||
|
||||
func TestJump(test *testing.T) {
|
||||
machine := runMachineTest ([]int {
|
||||
machine := runMachineTest([]int{
|
||||
PUSH, 1,
|
||||
PUSH, 8,
|
||||
JMP,
|
||||
@@ -230,7 +230,8 @@ func TestJump(test *testing.T) {
|
||||
|
||||
func TestRegister(test *testing.T) {
|
||||
output := ""
|
||||
machine := &Machine[int] { Program: []int {
|
||||
machine := &Machine[int]{}
|
||||
machine.LoadProgram([]int{
|
||||
PUSH, int('h'),
|
||||
PUSH, 4,
|
||||
CAL,
|
||||
@@ -258,7 +259,7 @@ func TestRegister(test *testing.T) {
|
||||
PUSH, int('!'),
|
||||
PUSH, 4,
|
||||
CAL,
|
||||
}}
|
||||
})
|
||||
machine.Register(4, func(machine *Machine[int]) (stop bool) {
|
||||
output += string(rune(machine.Pop()))
|
||||
return
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build ignore
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
@@ -5,11 +6,12 @@ package main
|
||||
import "os"
|
||||
import cre "git.tebibyte.media/sashakoshka/creature"
|
||||
|
||||
func main () {
|
||||
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 {
|
||||
machine := cre.Machine[int]{}
|
||||
machine.LoadProgram([]int{
|
||||
cre.PUSH, 0,
|
||||
cre.CAL,
|
||||
|
||||
@@ -19,22 +21,24 @@ func main () {
|
||||
cre.PUSH, 1,
|
||||
cre.PUSH, 0,
|
||||
cre.JMP,
|
||||
}}
|
||||
})
|
||||
|
||||
machine.Register (0, read)
|
||||
machine.Register (1, write)
|
||||
machine.Register(0, read)
|
||||
machine.Register(1, write)
|
||||
err := machine.Execute(0)
|
||||
if err != nil { panic(err.Error()) }
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func read (machine *cre.Machine[int]) (stop bool) {
|
||||
ch := []byte { 0 }
|
||||
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) {
|
||||
func write(machine *cre.Machine[int]) (stop bool) {
|
||||
print(string(rune(machine.Pop())))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
//go:build ignore
|
||||
// +build ignore
|
||||
|
||||
package main
|
||||
@@ -5,7 +6,7 @@ package main
|
||||
import "os"
|
||||
import cre "git.tebibyte.media/sashakoshka/creature"
|
||||
|
||||
func main () {
|
||||
func main() {
|
||||
// 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,
|
||||
// the program will respond with "And how does that make you feel?".
|
||||
@@ -20,7 +21,8 @@ func main () {
|
||||
responseStart := 71
|
||||
responseLoopStart := 50
|
||||
|
||||
machine := cre.Machine[int] { Program: []int {
|
||||
machine := cre.Machine[int]{}
|
||||
machine.LoadProgram([]int{
|
||||
// reset x
|
||||
cre.PUSH, introStart,
|
||||
cre.PUSH, x,
|
||||
@@ -108,9 +110,9 @@ func main () {
|
||||
cre.PUSH, 1,
|
||||
cre.PUSH, responseLoopStart,
|
||||
cre.JMP,
|
||||
}}
|
||||
})
|
||||
|
||||
stringData := []byte (
|
||||
stringData := []byte(
|
||||
"\x00" +
|
||||
"== Artificial Intelligence Psychiatrist ==\n" +
|
||||
"What brings you in today?\n\x00" +
|
||||
@@ -122,21 +124,23 @@ func main () {
|
||||
}
|
||||
|
||||
machine.LoadMemory(block)
|
||||
machine.Register (0, read)
|
||||
machine.Register (1, write)
|
||||
machine.Register(0, read)
|
||||
machine.Register(1, write)
|
||||
|
||||
err := machine.Execute(0)
|
||||
if err != nil { panic(err.Error()) }
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func read (machine *cre.Machine[int]) (stop bool) {
|
||||
ch := []byte { 0 }
|
||||
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) {
|
||||
func write(machine *cre.Machine[int]) (stop bool) {
|
||||
print(string(rune(machine.Pop())))
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user