-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainfuck.flight
57 lines (52 loc) · 1.79 KB
/
brainfuck.flight
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
fn main() {
let result = run_brainfuck("++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.")
print(result)
}
fn run_brainfuck(code: string) string {
let tape = int_array(0, 30000)
let tape_pointer = 0
let code_pointer = 0
let output = ""
while code_pointer < len(code) {
let command = code[code_pointer]
if command == "+" {
tape[tape_pointer] = tape[tape_pointer] + 1
} else if command == "-" {
tape[tape_pointer] = tape[tape_pointer] - 1
} else if command == ">" {
tape_pointer = tape_pointer + 1
} else if command == "<" {
tape_pointer = tape_pointer - 1
} else if command == "[" {
if tape[tape_pointer] == 0 {
var depth = 1
while depth > 0 {
code_pointer = code_pointer + 1
let c = code[code_pointer]
if c == "[" {
depth = depth + 1
} else if c == "]" {
depth = depth - 1
}
}
}
} else if command == "]" {
if tape[tape_pointer] != 0 {
let depth = 1
while depth > 0 {
code_pointer = code_pointer - 1
let c = code[code_pointer]
if c == "]" {
depth = depth + 1
} else if c == "[" {
depth = depth - 1
}
}
}
} else if command == "." {
output = output + unicode_to_string(tape[tape_pointer])
}
code_pointer = code_pointer + 1
}
return output
}