50 lines
1.4 KiB
Plaintext
50 lines
1.4 KiB
Plaintext
func main[] : i64
|
|
// https://brainfuck.org/sierpinski.b
|
|
src := "++++++++[>+>++++<<-]>++>>+<[-[>>+<<-]+>>]>+[-<<<[->[+[-]+>++>>>-<<]<[<]>>++++++[<<+++++>>-]+<<++.[-]<<]>.>+[>>]>+]"
|
|
src_len := str.len(src)
|
|
i := 0
|
|
|
|
memory := mem.alloc(30000)
|
|
mem.zero(memory, 30000)
|
|
p := 0
|
|
|
|
while i < src_len
|
|
op := src[i]
|
|
|
|
if op == '>'
|
|
p = p + 1
|
|
else if op == '<'
|
|
p = p - 1
|
|
else if op == '+'
|
|
memory[p] = memory[p] + 1
|
|
else if op == '-'
|
|
memory[p] = memory[p] - 1
|
|
else if op == '.'
|
|
io.print_char(memory[p])
|
|
else if op == ','
|
|
memory[p] = io.read_char()
|
|
else if op == '['
|
|
if !memory[p]
|
|
i = i + 1
|
|
opened := 0
|
|
while i < src_len && !(src[i] == ']' && !opened)
|
|
if src[i] == '['
|
|
opened = opened + 1
|
|
else if src[i] == ']'
|
|
opened = opened - 1
|
|
i = i + 1
|
|
else if op == ']'
|
|
if memory[p]
|
|
i = i - 1
|
|
closed := 0
|
|
while i >= 0 && !(src[i] == '[' && !closed)
|
|
if src[i] == ']'
|
|
closed = closed + 1
|
|
else if src[i] == '['
|
|
closed = closed - 1
|
|
i = i - 1
|
|
|
|
i = i + 1
|
|
|
|
mem.free(memory)
|