/
bf.ps
46 lines (37 loc) · 1.88 KB
/
bf.ps
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
% A Brainfuck interpreter written in PostScript
% 2023-12-05 Nicolas Seriot https://github.com/nst/bfps
% ps2pdf bf.ps && open bf.pdf
/Courier findfont 12 scalefont setfont
32 740 moveto
/P (+[-[<<[+[--->]-[<<<]]]>>>-]>-.-- -.>..>.<<<<-.<+.>>>>>.>.<<.<-.) def
/p 0 def % program pointer
/C { P p 1 getinterval } def % read program char
/M 30000 string def % memory array
/m 0 def % memory pointer
/V { M m get } def % read mem value
{
p P length ge { exit } if % exit if out of prog
C (+) eq { M m V 1 add 255 and put } if % increment mem value
C (-) eq { M m V 1 sub 255 and put } if % decrement mem value
C (>) eq { /m m 1 add def } if % move p to right
C (<) eq { /m m 1 sub def } if % move p to left
C ([) eq {
p % push p on stack
V 0 eq { % if V == 0
mark
{ % move after matching ]
C ([) eq { 0 } if
C (]) eq { pop } if
counttomark 0 eq { pop exit } if
/p p 1 add def
} loop
} if
} if
C (]) eq {
/x exch def % pop p for matching [
V 0 ne { /p x def p } if % jump back if V != 0
} if
C (.) eq { /s 1 string def s 0 V put s show } if % show mem value
C (,) eq { /f (%lineedit) (r) file def f read pop M m 3 -1 roll put } if
/p p 1 add def % move p to right
} loop