-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainfuck.kn
63 lines (45 loc) · 1.41 KB
/
brainfuck.kn
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
IF < LENGTH ARGS 1 ; ECHO "Need a filename" EXIT
= "filename" INDEX 1 ARGS
= "fd" OPEN_FD filename "r"
IF == fd null ; ECHO "File not found" EXIT
= "instructions" SPLIT (READ_FD fd) ""
= "ip" 1
= "sp" 1
= "stack" , 0 ARRAY
= "depth" 1
FN "JUMP" ""
; GLOBAL= "ip" + ip 1
; IF == (INDEX ip instructions) "[" GLOBAL= "depth" + depth 1
; IF && == (INDEX ip instructions) "]" != depth 1; GLOBAL= "depth" - depth 1 JUMP
IF != (INDEX ip instructions) "]" JUMP
FN "BACK" ""
; GLOBAL= "ip" - ip 1
; IF == (INDEX ip instructions) "]" GLOBAL= "depth" + depth 1
; IF && == (INDEX ip instructions) "[" != depth 1; GLOBAL= "depth" - depth 1 BACK
IF != (INDEX ip instructions) "[" BACK
FN "EVALUATE" ""
; = "inst" INDEX ip instructions
# Increase pointer
; IF == inst ">" GLOBAL= "sp" + sp 1
# Decrease pointer
; IF == inst "<" GLOBAL= "sp" - sp 1
# Increase value
; IF == inst "+" INDEX= sp stack (+ INDEX sp stack 1)
# Decrease value
; IF == inst "-" INDEX= sp stack (- INDEX sp stack 1)
# Log value
; IF == inst "." WRITE_FD 1 INDEX sp stack
# Prompt value
# Doesn't really work, can't convert to int
; IF == inst "," INDEX= sp stack INPUT
# Jump
; IF == inst "[" IF == INDEX sp stack 0 JUMP
# Jump back
; IF == inst "]" IF != INDEX sp stack 0 BACK
# Dynamically increase stack if needed
; IF > sp LENGTH stack PUSH 0 stack
; GLOBAL= "ip" + ip 1
; IF < ip LENGTH instructions
EVALUATE
EXIT
EVALUATE