add writer for stdout

This commit is contained in:
Chris Sexton 2019-10-20 08:16:46 -04:00
parent e7a1e23dde
commit aefaf5da87
1 changed files with 5 additions and 3 deletions

View File

@ -23,6 +23,7 @@ type Machine struct {
array []byte array []byte
ptr int ptr int
reader *bufio.Reader reader *bufio.Reader
writer io.Writer
// InstructionLimit prevents a runaway execution if running under a controlled environment // InstructionLimit prevents a runaway execution if running under a controlled environment
InstructionLimit int InstructionLimit int
@ -33,15 +34,16 @@ type Machine struct {
// Returns a new machine with standard memory size // Returns a new machine with standard memory size
func NewStdin() *Machine { 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) bytes := make([]byte, MEM_STD)
return &Machine{ return &Machine{
array: bytes, array: bytes,
ptr: 0, ptr: 0,
reader: bufio.NewReader(in), reader: bufio.NewReader(in),
writer: out,
InstructionLimit: 0, InstructionLimit: 0,
MemMax: 3000000, MemMax: 3000000,
} }
@ -80,7 +82,7 @@ func (m *Machine) ByteDecr() {
// Implements the '.' command // Implements the '.' command
func (m *Machine) Output() { func (m *Machine) Output() {
fmt.Printf("%c", m.array[m.ptr]) fmt.Fprintf(m.writer, "%c", m.array[m.ptr])
} }
// Implements the ',' command // Implements the ',' command