forked from wspace/cybis-hapyli
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainfuck.hpl
74 lines (60 loc) · 2.13 KB
/
brainfuck.hpl
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
66
67
68
69
70
71
72
73
74
import "stdlib/base.hpl"
def read-pgm(*buffer) =
(if (== (read-char *buffer) '!')
(set *buffer 0)
(++ (read-pgm (++ *buffer))))
def inline read-pgm() = (alloc (++ (read-pgm (ref 0))))
var mp = 0
var memsize = 0
def inline getmem(addr) = (ref (ref 0) addr)
def inline setmem(addr val) = (set (ref 0) addr val)
def inline left() = (set mp (-- (ref mp)))
def inline right() = (do
(set mp (++ (ref mp)))
(if (>= (ref mp) (ref memsize))
(do (set memsize (++ (ref memsize)))
(setmem (ref mp) 0))
0))
def inline inc() = (setmem (ref mp) (++ (getmem (ref mp))))
def inline dec() = (setmem (ref mp) (-- (getmem (ref mp))))
def inline inp() = (setmem (ref mp) (read-char))
def inline out() = (print-char (getmem (ref mp)))
var program = 0
var ip = 0
def inline readpgm() = (set program (read-pgm))
def loopahead(depth) =
let curchr = (ref (ref program) (++ (ref ip)))
in (if (== depth 0)
0
(do (set ip (++ (ref ip)))
(if (== curchr '[') (loopahead (++ depth))
(if (== curchr ']') (loopahead (-- depth))
(loopahead depth)))))
def loopback(depth) =
let curchr = (ref (ref program) (-- (ref ip)))
in (if (== depth 0)
0
(do (set ip (-- (ref ip)))
(if (== curchr '[') (loopback (-- depth))
(if (== curchr ']') (loopback (++ depth))
(loopback depth)))))
def inline step() =
let curchr = (ref (ref program) (ref ip))
in (do
(if (== curchr '+') (inc)
(if (== curchr '-') (dec)
(if (== curchr '>') (right)
(if (== curchr '<') (left)
(if (== curchr '.') (out)
(if (== curchr ',') (inp)
(if (and (== curchr '[') (== 0 (getmem (ref mp)))) (loopahead 1)
(if (and (== curchr ']') (!= 0 (getmem (ref mp)))) (loopback 1)
(if (== curchr 0) (end)
0))))))))))
def run() = (do
(step)
(set ip (++ (ref ip)))
(run))
def main() = (do
(readpgm)
(run))