-
Notifications
You must be signed in to change notification settings - Fork 0
/
brainfuck.wuzh
56 lines (52 loc) · 1.57 KB
/
brainfuck.wuzh
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
func runBrainfuck(code) {
memory := Array(3000, 0);
pointer := 0;
stack := [];
char := "";
for (i := 0, i < Length(code), i = i + 1) {
char = code[i];
if (char == "+") {
SetValue(memory, pointer, memory[pointer] + 1);
}
if (char == "-") {
SetValue(memory, pointer, memory[pointer] - 1);
}
if (char == ".") {
Print(IntAsAscii(memory[pointer]));
}
if (char == ",") {
SetValue(memory, pointer, AsciiCode(ReadLine()));
}
if (char == "<") {
pointer = pointer - 1;
}
if (char == ">") {
pointer = pointer + 1;
}
if (char == "[") {
if (memory[pointer] == 0) {
level := 1;
while (level > 0) {
i = i + 1;
if (code[i] == "[") {
level = level + 1;
}
if (code[i] == "]") {
level = level - 1;
}
}
} else {
Append(stack, i);
}
}
if (char == "]") {
if (memory[pointer] != 0) {
i = stack[Length(stack) - 1];
} else {
Remove(stack, Length(stack) - 1);
}
}
}
}
runBrainfuck(">+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.>>>++++++++[<++++>-]<.>>>++++++++++[<+++++++++>-]<---.<<<<.+++.------.--------.>>+.>++++++++++.");
# Expected result: Hello world!