~jacksonchen666/brainfuck_interpreter

e691bbecc41afbe5978f99c0abc1e06053e27fc9 — Jackson Chen 1 year, 1 month ago f7a66df
limit memory pointer to size of tape
1 files changed, 9 insertions(+), 0 deletions(-)

M brainfuck.go
M brainfuck.go => brainfuck.go +9 -0
@@ 74,6 74,15 @@ func (ps *ProgramState) Step() error {
		ps.memoryPointer = newMemPointer
	case '>':
		ps.memoryPointer++
		if ps.memoryPointer >= len(*ps.tape) {
			// nuance behavior: limit max mem pointer to whatever size the
			// tape is.
			capacity := cap(*ps.tape)
			if ps.memoryPointer > capacity {
				ps.memoryPointer = capacity
			}
			*ps.tape = (*ps.tape)[:ps.memoryPointer]
		}
	case '.':
		char := string(currentMemory)
		fmt.Print(char)