-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbf.crdm
More file actions
65 lines (60 loc) · 1.79 KB
/
Copy pathbf.crdm
File metadata and controls
65 lines (60 loc) · 1.79 KB
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
58
59
60
61
62
63
64
65
import io;
import str;
fn bf(code: string) {
let tape: int[] = [0];
let ptr: int = 0;
let code_len: int = str.len(code);
let code_ptr: int = 0;
let loop_stack: int[] = [0];
while (code_ptr < code_len) {
let command: string = str.charAt(code, code_ptr);
if (command == ">") {
ptr += 1;
if (ptr == tape.len()) {
tape.push(0);
}
} else if (command == "<") {
if (ptr > 0) {
ptr -= 1;
}
} else if (command == "+") {
tape[ptr] += 1;
} else if (command == "-") {
tape[ptr] -= 1;
} else if (command == ".") {
io.print(str.fromASCII(tape[ptr]));
} else if (command == ",") {
let input_char: string = io.input();
if (str.len(input_char) > 0) {
tape[ptr] = str.charCodeAt(input_char, 0);
}
} else if (command == "[") {
if (tape[ptr] == 0) {
let open_brackets: int = 1;
while (open_brackets > 0) {
code_ptr += 1;
if (str.charAt(code, code_ptr) == "[") {
open_brackets += 1;
} else if (str.charAt(code, code_ptr) == "]") {
open_brackets -= 1;
}
}
} else {
loop_stack.push(code_ptr);
}
} else if (command == "]") {
if (tape[ptr] != 0) {
code_ptr = loop_stack[loop_stack.len() - 1];
} else {
loop_stack.pop();
}
}
code_ptr += 1;
}
}
fn main() -> int {
io.println("Enter bf code: ");
let x: string = io.input();
bf(x);
return 0;
}