Added some helpful comments to the creature command
This commit is contained in:
parent
dcd8c56c0d
commit
c7d948cdcd
BIN
cmd/creature/examples/echobin
Normal file
BIN
cmd/creature/examples/echobin
Normal file
Binary file not shown.
@ -40,6 +40,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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) {
|
func read(machine *cre.Machine[int]) (stop bool) {
|
||||||
ch := []byte{0}
|
ch := []byte{0}
|
||||||
os.Stdin.Read(ch)
|
os.Stdin.Read(ch)
|
||||||
@ -47,15 +50,22 @@ func read(machine *cre.Machine[int]) (stop bool) {
|
|||||||
return
|
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) {
|
func write(machine *cre.Machine[int]) (stop bool) {
|
||||||
print(string(rune(machine.Pop())))
|
print(string(rune(machine.Pop())))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// logLine prints a message to stderr.
|
||||||
func logLine(message ...any) {
|
func logLine(message ...any) {
|
||||||
fmt.Fprintln(os.Stderr, message...)
|
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) {
|
func readFile(reader io.Reader) (program []int, block []int, err error) {
|
||||||
scanner := bufio.NewScanner(reader)
|
scanner := bufio.NewScanner(reader)
|
||||||
scanner.Split(bufio.ScanWords)
|
scanner.Split(bufio.ScanWords)
|
||||||
@ -100,5 +110,3 @@ func readFile (reader io.Reader) (program []int, block []int, err error) {
|
|||||||
program = program[:programLen]
|
program = program[:programLen]
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user