From aefaf5da87e154924c336bbaa41ae42172bb9f61 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Sun, 20 Oct 2019 08:16:46 -0400 Subject: [PATCH] add writer for stdout --- brainfuck.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/brainfuck.go b/brainfuck.go index b719866..5ae0759 100644 --- a/brainfuck.go +++ b/brainfuck.go @@ -23,6 +23,7 @@ type Machine struct { array []byte ptr int reader *bufio.Reader + writer io.Writer // InstructionLimit prevents a runaway execution if running under a controlled environment InstructionLimit int @@ -33,15 +34,16 @@ type Machine struct { // Returns a new machine with standard memory size func NewStdin() *Machine { - return New(os.Stdin) + return New(os.Stdin, os.Stdout) } -func New(in io.Reader) *Machine { +func New(in io.Reader, out io.Writer) *Machine { bytes := make([]byte, MEM_STD) return &Machine{ array: bytes, ptr: 0, reader: bufio.NewReader(in), + writer: out, InstructionLimit: 0, MemMax: 3000000, } @@ -80,7 +82,7 @@ func (m *Machine) ByteDecr() { // Implements the '.' command func (m *Machine) Output() { - fmt.Printf("%c", m.array[m.ptr]) + fmt.Fprintf(m.writer, "%c", m.array[m.ptr]) } // Implements the ',' command