Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81319 / bfinterpreter / bf / tokenizer.go
diff options
context:
space:
mode:
Commitc0567e2492ece720f87d1b5f74abce86fad815dc (patch)
Author
 Georgi Stefanov <geosteffanov@gmail.com>
2018-11-29 15:03:08 +0200
Committer
 dtrendafilov <trendafilov.dn@gmail.com>
2018-11-29 15:03:08 +0200
Files48669f35b66d15eb29cb26d29ab6e03a8b4a142a /homework/81319/bfinterpreter/bf/tokenizer.go
IPL-c0567e2492ece720f87d1b5f74abce86fad815dc.tar
IPL-c0567e2492ece720f87d1b5f74abce86fad815dc.tar.gz
IPL-c0567e2492ece720f87d1b5f74abce86fad815dc.zip
Parent99a9f9389a41a1df544ab51fc0170e48f10e34c8 (diff)
Add brainfuck interpreter (#65)
Not sure if it builds correctly. You have to have go compiler.
1 files changed, 124 insertions, 0 deletions
-rw-r--r--homework/81319/bfinterpreter/bf/tokenizer.go124
diff --git a/homework/81319/bfinterpreter/bf/tokenizer.go b/homework/81319/bfinterpreter/bf/tokenizer.go
new file mode 100644
index 0000000..10a0342
--- /dev/null
+++ b/homework/81319/bfinterpreter/bf/tokenizer.go
@@ -0,0 +1,124 @@
+package bf
+
+import (
+ "bytes"
+ "io"
+)
+
+type token int
+
+const (
+ unkwn token = iota
+ movR
+ movL
+ incD
+ decD
+ outD
+ inD
+ loopS
+ loopE
+)
+
+func tokenize(symbol string) token {
+ switch symbol {
+ case ">":
+ return movR
+ case "<":
+ return movL
+ case ".":
+ return outD
+ case ",":
+ return inD
+ case "+":
+ return incD
+ case "-":
+ return decD
+ case "[":
+ return loopS
+ case "]":
+ return loopE
+ }
+
+ return 0
+}
+
+type tokenizingStream struct {
+ reader io.Reader
+}
+
+func (s *tokenizingStream) Read(p []byte) (int, error) {
+ buffer := make([]byte, len(p))
+
+ count, err := s.reader.Read(buffer)
+ if err != nil {
+ return 0, err
+ }
+
+ for i := 0; i < count; i++ {
+ p[i] = byte(tokenize(string(buffer[i])))
+ }
+
+ return count, nil
+}
+
+func tokenizeInput(input string) []instruction {
+ byteBuff := []byte(input)
+ reader := bytes.NewReader(byteBuff)
+ tokenizingReader := tokenizingStream{
+ reader: reader,
+ }
+
+ bytesRead := make([]byte, 1)
+ instructions := make([]instruction, 0)
+
+ for {
+ _, ok := tokenizingReader.Read(bytesRead)
+ if ok == io.EOF {
+ break
+ }
+ if ok == nil {
+ inst := instruction{
+ instructionType: token(bytesRead[0]),
+ }
+
+ instructions = append(instructions, inst)
+ }
+ }
+
+ return instructions
+}
+
+func parseInput(instructions []instruction) []instruction {
+ stack := make([]instruction, 0)
+
+ for idx, inst := range instructions {
+ instructions[idx].idx = uint(idx)
+
+ if inst.instructionType != loopS && inst.instructionType != loopE {
+ continue
+ }
+
+ if inst.instructionType == loopS {
+ inst.idx = uint(idx)
+ stack = append(stack, inst)
+
+ continue
+ }
+
+ if len(stack) < 1 {
+ panic("input data invalid!")
+ }
+
+ prevInst := stack[len(stack)-1]
+ prevInst.matchingBracketIdx = uint(idx)
+ instructions[prevInst.idx] = prevInst
+
+ inst.idx = uint(idx)
+ inst.matchingBracketIdx = prevInst.idx
+ instructions[idx] = inst
+
+ stack = stack[:len(stack)-1]
+ }
+
+ return instructions
+}