Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81319 / bfinterpreter / bf / repl.go
blob: e09bceabf1648ae76bcf7e53c19c8967a05b513e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package bf

import (
	"bufio"
	"fmt"
	"os"
)

type repl struct {
	interpreter *state
	reader      *bufio.Reader
	writer      *bufio.Writer
}

func NewRepl() repl {
	intepreter := NewInterpreter("")
	return repl{
		interpreter: &intepreter,
		reader:      bufio.NewReader(os.Stdin),
		writer:      bufio.NewWriter(os.Stdout),
	}
}

func (r *repl) read() {
	fmt.Print(">>")
	instructions, err := r.reader.ReadString('\n')
	instructions = instructions[:len(instructions)-1]
	if err != nil {
		panic("not implemented")
	}

	r.interpreter.addInstructions(instructions)
}

func (r *repl) eval() {
	r.interpreter.Run()
}

func (r *repl) Start() {
	for {
		r.read()
		r.eval()
	}
}