mirror of https://github.com/01-edu/public.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.3 KiB
67 lines
1.3 KiB
5 years ago
|
package main
|
||
|
|
||
|
import (
|
||
5 years ago
|
"fmt"
|
||
5 years ago
|
"os"
|
||
|
)
|
||
|
|
||
|
const SIZE = 2048
|
||
|
|
||
|
func main() {
|
||
5 years ago
|
if len(os.Args) != 2 {
|
||
|
return
|
||
|
}
|
||
|
progpoint := []byte(os.Args[1])
|
||
|
var arby [SIZE]byte
|
||
|
pos := 0
|
||
|
openBr := 0 // opened brackets
|
||
|
i := 0 // iterates through the source code passed in the argument
|
||
|
N := len(progpoint) // length of the source code
|
||
|
for i >= 0 && i < N {
|
||
|
switch progpoint[i] {
|
||
|
case '>':
|
||
|
// Increment the pointer
|
||
|
pos++
|
||
|
case '<':
|
||
|
// decrement the pointes
|
||
|
pos--
|
||
|
case '+':
|
||
|
// increment the pointed byte
|
||
|
arby[pos]++
|
||
|
case '-':
|
||
|
// decrement the pointed byte
|
||
|
arby[pos]--
|
||
|
case '.':
|
||
|
// print the pointed byte on std output
|
||
5 years ago
|
fmt.Printf("%c", rune(arby[pos]))
|
||
5 years ago
|
case '[':
|
||
|
// go to the matching ']' if the pointed byte is 0 (while start)
|
||
|
openBr = 0
|
||
|
if arby[pos] == 0 {
|
||
|
for i < N && (progpoint[i] != byte(']') || openBr > 1) {
|
||
|
if progpoint[i] == byte('[') {
|
||
|
openBr++
|
||
|
} else if progpoint[i] == byte(']') {
|
||
|
openBr--
|
||
5 years ago
|
}
|
||
5 years ago
|
i++
|
||
5 years ago
|
}
|
||
5 years ago
|
}
|
||
|
case ']':
|
||
|
// go to the matching '[' if the pointed byte is not 0 (while end)
|
||
|
openBr = 0
|
||
|
if arby[pos] != 0 {
|
||
|
for i >= 0 && (progpoint[i] != byte('[') || openBr > 1) {
|
||
|
if progpoint[i] == byte(']') {
|
||
|
openBr++
|
||
|
} else if progpoint[i] == byte('[') {
|
||
|
openBr--
|
||
5 years ago
|
}
|
||
5 years ago
|
i--
|
||
5 years ago
|
}
|
||
|
}
|
||
|
}
|
||
5 years ago
|
i++
|
||
5 years ago
|
}
|
||
|
}
|