Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81319 / bfinterpreter / bf / interpreter.go
diff options
context:
space:
mode:
1 files changed, 189 insertions, 0 deletions
-rw-r--r--homework/81319/bfinterpreter/bf/interpreter.go189
diff --git a/homework/81319/bfinterpreter/bf/interpreter.go b/homework/81319/bfinterpreter/bf/interpreter.go
new file mode 100644
index 0000000..87c9870
--- /dev/null
+++ b/homework/81319/bfinterpreter/bf/interpreter.go
@@ -0,0 +1,189 @@
+package bf
+
+import (
+ "bufio"
+ "errors"
+ "fmt"
+ "io"
+ "os"
+ "strconv"
+)
+
+type instruction struct {
+ idx uint
+ instructionType token
+
+ matchingBracketIdx uint
+}
+
+type state struct {
+ mem memoryState
+ src []instruction
+ instructionPtr uint
+
+ buffer []byte
+ reader io.Reader
+ writer io.Writer
+}
+
+func (s *state) moveRight() {
+ s.mem.incrementPtr()
+}
+
+func (s *state) moveLeft() {
+ s.mem.decrementPtr()
+}
+
+func (s *state) incrementValue() {
+ s.mem.incrementCell()
+}
+
+func (s *state) decrementValue() {
+ s.mem.decerementCell()
+}
+
+func (s *state) readCellValue() error {
+ count, err := s.reader.Read(s.buffer)
+ if err == io.EOF {
+ return nil
+ }
+
+ if err != nil {
+ return errors.New("couldn't read from input")
+ }
+
+ if count < 1 {
+ return errors.New("couldn't read from input")
+ }
+
+ s.mem.setCell(s.buffer[0])
+
+ return nil
+}
+
+func (s *state) writeCellValue() error {
+ count, err := s.writer.Write([]byte{s.mem.getCell()})
+
+ if err != nil {
+ return errors.New("couldn't write to output")
+ }
+
+ if count != 1 {
+ return errors.New("couldn't write to output")
+ }
+
+ return nil
+}
+
+func (s *state) interpretInstruction() {
+ instr := s.src[s.instructionPtr]
+
+ switch instr.instructionType {
+ case movR:
+ s.moveRight()
+ case movL:
+ s.moveLeft()
+ case incD:
+ s.incrementValue()
+ case decD:
+ s.decrementValue()
+ case outD:
+ s.writeCellValue()
+ case inD:
+ s.readCellValue()
+ case loopS:
+ if s.mem.getCell() == 0 {
+ s.instructionPtr = instr.matchingBracketIdx
+ }
+ case loopE:
+ if s.mem.getCell() != 0 {
+ s.instructionPtr = instr.matchingBracketIdx
+ }
+ }
+ s.instructionPtr += 1
+}
+
+func (s *state) addInstructions(src string) {
+ instructions := parseInput(tokenizeInput(src))
+
+ s.src = append(s.src, instructions...)
+}
+
+type flushingWriter struct {
+ w *bufio.Writer
+}
+
+func newFlushingWriter() io.Writer {
+ writer := &flushingWriter{
+ w: bufio.NewWriter(os.Stdout),
+ }
+
+ return writer
+}
+
+func (w *flushingWriter) Write(p []byte) (int, error) {
+ fmt.Print("#=> ")
+ count, err := w.w.Write(p)
+ if err != nil {
+ return 0, err
+ }
+ w.w.Flush()
+
+ fmt.Println()
+
+ return count, nil
+}
+
+type lineReader struct {
+ r *bufio.Reader
+}
+
+func newLineReader() io.Reader {
+ reader := &lineReader{
+ r: bufio.NewReader(os.Stdin),
+ }
+
+ return reader
+}
+
+func (r *lineReader) Read(buffer []byte) (int, error) {
+ stringVal, err := r.r.ReadString('\n')
+
+ if err != nil {
+ return 0, err
+ }
+
+ result, err := strconv.ParseInt(stringVal[:len(stringVal)-1], 10, 8)
+
+ if err != nil {
+ return 0, err
+ }
+
+ buffer[0] = byte(result)
+
+ return 1, nil
+}
+
+func NewInterpreter(src string) state {
+ instructions := parseInput(tokenizeInput(src))
+
+ state := state{
+ instructionPtr: 0,
+ src: instructions,
+ buffer: make([]byte, 1),
+ writer: newFlushingWriter(),
+ reader: newLineReader(),
+ }
+
+ return state
+}
+
+func (s *state) Run() {
+ for {
+ if int(s.instructionPtr) == len(s.src) {
+ return
+ }
+
+ s.interpretInstruction()
+ }
+}