-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbf.odin
55 lines (50 loc) · 1.5 KB
/
bf.odin
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
package main
import "core:fmt"
import "core:os"
main :: proc() {
mem := [dynamic]u8 {0};
memptr, ip, depth : u32 = 0, 0, 0;
prog, succ := os.read_entire_file("input.bf");
if succ == false {
fmt.println("Cant open input.bf");
return;
}
for int(ip) < len(prog) {
switch prog[ip] {
case '>' : {
memptr += 1;
if u32(len(mem)) <= memptr do append(&mem, 0);
}
case '<' : memptr -= 1;
case '+' : mem[memptr] += 1;
case '-' : mem[memptr] -= 1;
case '.' : os.write_byte(os.stdout, auto_cast mem[memptr]);
case ',' : {
input := []byte {0..<1 = 0};
os.read(os.stdin, input);
mem[memptr] = input[0];
}
case '[' : {
if mem[memptr] == 0 {
depth += 1;
for depth != 0 {
ip += 1;
if prog[ip] == '[' do depth += 1;
else if prog[ip] == ']' do depth -= 1;
}
}
}
case ']' : {
if mem[memptr] != 0 {
depth += 1;
for depth != 0 {
ip -= 1;
if prog[ip] == ']' do depth += 1;
else if prog[ip] == '[' do depth -= 1;
}
}
}
}
ip += 1;
}
}