~jacksonchen666/brainfuck_interpreter

cbc828bdc5b12eefa70253780c40baddf95cf2f5 — Jackson Chen 1 year, 1 month ago bb931ba
expand as necessary thing
1 files changed, 7 insertions(+), 3 deletions(-)

M brainfuck.go
M brainfuck.go => brainfuck.go +7 -3
@@ 52,14 52,18 @@ func getChar() (byte, error) {
	return scan[0], nil
}

func (ps *ProgramState) expandAsNecessary() {
	if ps.memoryPointer+1 >= len(*ps.tape) {
		*ps.tape = append(*ps.tape, 0)
	}
}

// Does a single brainfuck instruction.
func (ps *ProgramState) Step() error {
	if ps.codePointer >= len(*ps.code) { // len: wasteful? store in program state?
		return EOF
	}
	if ps.memoryPointer >= len(*ps.tape) {
		*ps.tape = append(*ps.tape, 0)
	}
	ps.expandAsNecessary()
	instruction := (*ps.code)[ps.codePointer]
	currentMemory := (*ps.tape)[ps.memoryPointer]
	switch instruction {