3086
|
1 ********************************************************************
|
|
2 * bf - Brainfuck Language Interpreter
|
|
3 *
|
|
4 * $Id$
|
|
5 *
|
|
6 * This is a simple interpreter for the Brainfuck language:
|
|
7 * https://en.wikipedia.org/wiki/Brainfuck
|
|
8 *
|
|
9 * Edt/Rev YYYY/MM/DD Modified by
|
|
10 * Comment
|
|
11 * ------------------------------------------------------------------
|
|
12 * 1 2015/10/02 Boisy G. Pitre
|
|
13 * Created.
|
|
14 *
|
|
15
|
|
16 ifp1
|
|
17 use defsfile
|
|
18 endc
|
|
19
|
|
20 tylg set Prgrm+Objct
|
|
21 atrv set ReEnt+rev
|
|
22 rev set $00
|
|
23 edition set 1
|
|
24
|
|
25 mod eom,name,tylg,atrv,start,size
|
|
26
|
|
27 pSize equ 12000
|
|
28 dSize equ 3000
|
|
29
|
|
30 org 0
|
|
31 pmem rmb pSize
|
|
32 dmem rmb dSize
|
|
33 stack rmb 200
|
|
34 size equ .
|
|
35
|
|
36 name fcs /bfp/
|
|
37 fcb edition
|
|
38
|
|
39 * initialize interpreter
|
|
40 * clear program memory
|
|
41 start
|
|
42 lda #READ.
|
|
43 os9 I$Open
|
|
44 lbcs error
|
|
45 ldy #pSize
|
|
46 leax pmem,u
|
|
47 os9 I$Read
|
|
48 lbcs error
|
|
49 os9 I$Close
|
|
50 tfr y,d nul terminate program string in memory
|
|
51 clr d,x
|
|
52
|
|
53 leay dmem,u
|
|
54 ldd #dSize
|
|
55 clrloop clr ,y+
|
|
56 subd #$0001
|
|
57 bne clrloop
|
|
58 leay dmem,u
|
|
59 bra parse
|
|
60
|
|
61 ptrInc
|
|
62 leay 1,y
|
|
63 bra parse
|
|
64
|
|
65 ptrDec
|
|
66 leay -1,y
|
|
67 bra parse
|
|
68
|
|
69 dataInc
|
|
70 inc ,y
|
|
71 bra parse
|
|
72
|
|
73 dataDec
|
|
74 dec ,y
|
|
75 bra parse
|
|
76
|
|
77 putChar
|
|
78 pshs d,x,y
|
|
79 lda #1
|
|
80 tfr y,x
|
|
81 ldy #1
|
|
82 os9 I$Write
|
|
83 puls d,x,y,pc
|
|
84
|
|
85 getChar
|
|
86 pshs d,x,y
|
|
87 clra
|
|
88 tfr y,x
|
|
89 ldy #1
|
|
90 os9 I$Read
|
|
91 puls d,x,y,pc
|
|
92
|
|
93
|
|
94 brOpen lda #1
|
|
95 pshs a
|
|
96 tst ,y
|
|
97 bne brOpenBye
|
|
98 brOpenDo
|
|
99 lda ,x+
|
|
100 cmpa #'[
|
|
101 bne brOpenCkClose
|
|
102 inc ,s
|
|
103 bra brOpenDoTest
|
|
104 brOpenCkClose
|
|
105 cmpa #']
|
|
106 bne brOpenDoTest
|
|
107 dec ,s
|
|
108 brOpenDoTest
|
|
109 tst ,s
|
|
110 bne brOpenDo
|
|
111 brOpenBye
|
|
112 puls a,pc
|
|
113
|
|
114 brClose
|
|
115 clr ,-s
|
|
116 brCloseDo
|
|
117 lda ,-x
|
|
118 cmpa #'[
|
|
119 bne brCloseCkClose
|
|
120 inc ,s
|
|
121 bra brCloseDoCont
|
|
122 brCloseCkClose
|
|
123 cmpa #']
|
|
124 bne brCloseDoCont
|
|
125 dec ,s
|
|
126 brCloseDoCont
|
|
127 tst ,s
|
|
128 bne brCloseDo
|
|
129 puls a,pc
|
|
130
|
|
131 * X = Brainfuck program pointer (nul byte terminates)
|
|
132 * Y = Brainfuck data pointer
|
|
133 parse
|
|
134 lda ,x+
|
|
135 beq parseEnd
|
|
136 cmpa #'>
|
|
137 bne a1
|
|
138 leay 1,y
|
|
139 bra parse
|
|
140 a1 cmpa #'<
|
|
141 bne a2
|
|
142 leay -1,y
|
|
143 bra parse
|
|
144 a2 cmpa #'+
|
|
145 bne a3
|
|
146 inc ,y
|
|
147 bra parse
|
|
148 a3 cmpa #'-
|
|
149 bne a4
|
|
150 dec ,y
|
|
151 bra parse
|
|
152 a4 cmpa #'.
|
|
153 bne a5
|
|
154 bsr putChar
|
|
155 bra parse
|
|
156 a5 cmpa #',
|
|
157 bne a6
|
|
158 bsr getChar
|
|
159 bra parse
|
|
160 a6 cmpa #'[
|
|
161 bne a7
|
|
162 bsr brOpen
|
|
163 bra parse
|
|
164 a7 cmpa #']
|
|
165 bne parse unrecognized character -- keep parsing
|
|
166 bsr brClose
|
|
167 bra parse
|
|
168
|
|
169
|
|
170 parseEnd
|
|
171 clrb
|
|
172 error
|
|
173 os9 F$Exit
|
|
174
|
|
175 EMOD
|
|
176 eom EQU *
|
|
177 END
|