Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81319 / bfinterpreter / bf / repl.go
diff options
context:
space:
mode:
1 files changed, 44 insertions, 0 deletions
-rw-r--r--homework/81319/bfinterpreter/bf/repl.go44
diff --git a/homework/81319/bfinterpreter/bf/repl.go b/homework/81319/bfinterpreter/bf/repl.go
new file mode 100644
index 0000000..e09bcea
--- /dev/null
+++ b/homework/81319/bfinterpreter/bf/repl.go
@@ -0,0 +1,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()
+ }
+}