-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.asm
319 lines (276 loc) · 5.15 KB
/
compiler.asm
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
;{{{ The MIT License
;
;Copyright (c) 2008 Johan Kotlinski
;
;Permission is hereby granted, free of charge, to any person obtaining a copy
;of this software and associated documentation files (the "Software"), to deal
;in the Software without restriction, including without limitation the rights
;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
;copies of the Software, and to permit persons to whom the Software is
;furnished to do so, subject to the following conditions:
;
;The above copyright notice and this permission notice shall be included in
;all copies or substantial portions of the Software.
;
;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
;THE SOFTWARE. }}}
; C, , ; IMMEDIATE [ ] STATE : HEADER LIT LITC COMPILE, LITERAL HERE DODOES
curr_word_no_tail_call_elimination
!byte 1
last_word_no_tail_call_elimination
!byte 1
+BACKLINK "c,", 2
CCOMMA
lda HERE_LSB
sta W
lda HERE_MSB
sta W + 1
ldy #0
lda LSB, x
sta (W), y
; update HERE
inc HERE_LSB
bne +
inc HERE_MSB
+ inx
rts
+BACKLINK ",", 1
COMMA
lda HERE_LSB
sta W
lda HERE_MSB
sta W + 1
ldy #0
lda LSB, x
sta (W), y
iny
lda MSB, x
sta (W), y
; update HERE
lda HERE_LSB
clc
adc #2
sta HERE_LSB
bcc +
inc HERE_MSB
+
inx
rts
; -----
+BACKLINK "[", 1 | F_IMMEDIATE
LBRAC
lda #0
sta STATE
rts
; -----
; disable tail call elimination in case of inline assembly
+BACKLINK "]", 1 | F_NO_TAIL_CALL_ELIMINATION
RBRAC
lda #1
sta STATE
rts
+BACKLINK ";", 1 | F_IMMEDIATE
SEMICOLON
jsr EXIT
; Unhides the word.
inx
lda MSB - 1, x
beq +
sta W + 1
lda LSB - 1, x
sta W
ldy #0
lda (W), y
and #!F_HIDDEN ; clear hidden flag
sta (W), y
+
; go back to IMMEDIATE mode.
jmp LBRAC
+BACKLINK "immediate", 9
ldy #0
lda LATEST_LSB
sta W
lda LATEST_MSB
sta W + 1
lda (W), y
ora #F_IMMEDIATE
sta (W), y
rts
; STATE - Is the interpreter executing code (0) or compiling a word (non-zero)?
+BACKLINK "state", 5
+VALUE STATE
STATE
!word 0
+BACKLINK ":", 1 | F_NO_TAIL_CALL_ELIMINATION
COLON
jsr HEADER ; makes the dictionary entry / header
; Hides the word.
dex
lda LATEST_LSB
sta W
sta LSB, x
lda LATEST_MSB
sta W + 1
sta MSB, x
ldy #0
lda (W), y
ora #F_HIDDEN ; sets hidden flag
sta (W), y
jmp RBRAC ; enter compile mode
; --- HEADER ( name -- )
+BACKLINK "header", 6
HEADER
inc last_word_no_tail_call_elimination
; update dictionary
- jsr PARSE_NAME
lda LSB,x
bne +
jsr REFILL
jmp -
+
; update dictionary pointer
lda LSB, x
sta .putlen+1
clc
adc #3
sta W
lda LATEST_LSB
sec
sbc W
sta LATEST_LSB
sta W
bcs +
dec LATEST_MSB
+
lda LATEST_MSB
sta W + 1
ldy #0
; Store length byte.
lda LSB, x
sta (W), y
inx
lda LSB, x
sta W2
lda MSB, x
sta W2 + 1
; copy string
- lda (W2), y
jsr CHAR_TO_LOWERCASE
iny
sta (W), y
.putlen
cpy #0
bne -
; store here
iny
lda HERE_LSB
sta (W), y
iny
lda HERE_MSB
sta (W), y
inx
rts
+BACKLINK "lit", 3
LIT
dex
; load IP
pla
sta W
pla
sta W + 1
; copy literal to stack
ldy #1
lda (W), y
sta LSB, x
iny
lda (W), y
sta MSB, x
lda W
clc
adc #3
sta + + 1
lda W + 1
adc #0
sta + + 2
+ jmp PLACEHOLDER_ADDRESS ; replaced with instruction pointer
+BACKLINK "litc", 4
LITC
dex
; load IP
pla
sta W
pla
sta W + 1
inc W
bne +
inc W + 1
+
; copy literal to stack
ldy #0
lda (W), y
sta LSB, x
sty MSB, x
inc W
bne +
inc W + 1
+ jmp (W)
+BACKLINK "compile,", 8
COMPILE_COMMA
lda #OP_JSR
jsr compile_a
jmp COMMA
+BACKLINK "literal", 7 | F_IMMEDIATE
LITERAL
dex
lda MSB+1,x
bne +
lda #<LITC
sta LSB,x
lda #>LITC
sta MSB,x
jsr COMPILE_COMMA
jmp CCOMMA ; writes byte
+
lda #<LIT
sta LSB, x
lda #>LIT
sta MSB, x
jsr COMPILE_COMMA
jmp COMMA ; writes number
; HERE - points to the next free byte of memory. When compiling, compiled words go here.
+BACKLINK "here", 4
HERE
HERE_LSB = * + 1
HERE_MSB = * + 3
+VALUE load_base
+BACKLINK "dodoes", 6
; behavior pointer address => W
pla
sta W
pla
sta W + 1
inc W
bne +
inc W + 1
+
; push data pointer to param stack
dex
lda W
clc
adc #2
sta LSB,x
lda W + 1
adc #0
sta MSB,x
ldy #0
lda (W),y
sta W2
iny
lda (W),y
sta W2 + 1
jmp (W2)