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.4 KiB

package main
import (
"github.com/01-edu/z01"
"os"
)
const SIZE = 2048
func main() {
if len(os.Args) == 2 {
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
z01.PrintRune(rune(arby[pos]))
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--
}
i++
}
}
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--
}
i--
}
}
}
i++
}
} else {
z01.PrintRune('\n')
}
}