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