Website Summary Files Commits Branches @commit @diff @stats
path: root / homework / 81319 / bfinterpreter / bf / tokenizer.go
diff options
context:
space:
mode:
Commitaeb0d4f887a0c18c36b59acc7b5df08e62020c50 (patch)
Author
 Dimitar Trendafilov <dimitar@coherent-labs.com>
2020-10-11 21:32:48 +0300
Committer
 Dimitar Trendafilov <dimitar@coherent-labs.com>
2020-10-11 21:33:38 +0300
Filesb6abdc6e1810326b587f2a160f39570f1f568f27 /homework/81319/bfinterpreter/bf/tokenizer.go
IPL-aeb0d4f887a0c18c36b59acc7b5df08e62020c50.tar
IPL-aeb0d4f887a0c18c36b59acc7b5df08e62020c50.tar.gz
IPL-aeb0d4f887a0c18c36b59acc7b5df08e62020c50.zip
Parent154b7cd30f21af1554ca26986ff1643e28857727 (diff)
Removed old homeworks
1 files changed, 0 insertions, 124 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
deleted file mode 100644
index 10a0342..0000000
--- a/homework/81319/bfinterpreter/bf/tokenizer.go
+++ /dev/null
@@ -1,124 +0,0 @@
-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
-}