-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.asm
342 lines (204 loc) · 4.72 KB
/
parse.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
SECTION .text
parse:
; string pointer comes into rsi
; type of output comes out of rax
; value of output comes out of rbx
; string pointer to rest of the string stays in rsi
; I do this hack where I replace whitespace and parentheses with '\0' so that symbols can be just pointers into source code
call skipSpaces
mov al, [rsi]
.checkIfList:
cmp al, '('
jne .checkIfNum
mov byte [rsi], 0
inc rsi
call parseRestOfList
cmp byte [rsi], ')'
errorNe "Unclosed parenthesis"
mov byte [rsi], 0
inc rsi
ret
.checkIfNum:
cmp al, '0'
jb .checkIfSpecial
cmp al, '9'
ja .checkIfSpecial
call parseNum
mov rbx, rax
mov rax, int_t
ret
.checkIfSpecial:
cmp al, '#'
jne .checkIfQuote
inc rsi
mov al, [rsi]
cmp al, 't'
jne .maybeFalse
call findEndOfSymbol ; #titititi counts as true...
mov rax, bool_t
mov rbx, 1
ret
.maybeFalse:
cmp al, 'f'
errorNe "'#' must be followed by 't' or 'f'"
call findEndOfSymbol ; #titititi counts as true...
mov rax, bool_t
mov rbx, 0
ret
.checkIfQuote:
cmp al, "'"
jne .mustBeSymbol
inc rsi
call parse
; write the values to heap
mov rdi, [alloc_ptr]
mov qword [rdi], rax
mov qword [rdi+8], rbx
mov qword [rdi+16], null_t
mov qword [rdi+24], 0
mov rax, pair_t_full
mov rbx, [alloc_ptr]
add qword [alloc_ptr], 32
mov rdi, [alloc_ptr]
mov qword [rdi], symbol_t
mov qword [rdi+8], quoteSymbol
mov [rdi+16], rax
mov [rdi+24], rbx
mov rax, pair_t_full
mov rbx, [alloc_ptr]
add qword [alloc_ptr], 32
ret
.mustBeSymbol:
cmp al, ')'
je .error
mov rax, symbol_t
mov rbx, rsi
call findEndOfSymbol
ret
.error:
errorMsg "Error parsing"
findEndOfSymbol:
; string pointer comes into rsi
; string pointer of string after symbol comes out of rsi
.start:
mov r8b, [rsi]
cmp r8b, ' '
je .end
cmp r8b, ')'
je .end
cmp r8b, '('
je .end
cmp r8b, `\t`
je .end
cmp r8b, `\n` ; something something line break carriage return XXX
je .end
inc rsi
jmp .start
.end:
ret
parseNum:
; string pointer comes into rsi
; number comes out of rax
; pointer to rest of string will stay in rsi
push rbx
push rcx
push rdx
push rdi
mov rcx, 0
.getDigits:
mov rax, 0
mov al, [rsi]
cmp al, '0'
jb .startMakingNumber
cmp al, '9'
ja .startMakingNumber
sub rax, '0'
push rax
inc rcx
inc rsi
jmp .getDigits
.startMakingNumber:
mov rbx, 1
mov rdi, 0
.makingNumbersLoop:
cmp rcx,0
je .end
pop rax
mul rbx
add rdi, rax
mov rax, 10
mul rbx
mov rbx, rax
dec rcx
jmp .makingNumbersLoop
.end:
mov rax, rdi
pop rdi
pop rdx
pop rcx
pop rbx
ret
;;;;;;;;;;;;;;
parseRestOfList:
; string pointer comes into rsi
; type of outcome comes out of rax (is a list or null)
; value of outcome comes out of rbx
; TODO: make this tail-call recursive by passing the place to write the value to as a parameter
push rcx
push rdx
push rdi
call skipSpaces
mov al, [rsi]
cmp al, ')'
je .returnNull
cmp al, 0
je .returnNull
call parse
push rax
push rbx
call parseRestOfList
pop rdx
pop rcx
; now the car is in rcx:rdx
; cdr is in rax:rbx
; write the values to heap
mov rdi, [alloc_ptr]
mov [rdi], rcx
mov [rdi+8], rdx
mov [rdi+16], rax
mov [rdi+24], rbx
mov rax, pair_t_full
mov rbx, rdi
add qword [alloc_ptr], 32
jmp .return
.returnNull:
mov rax, null_t
mov rbx, 0
jmp .return
.noClosingParen:
; TODO: print error message
jmp exitError
.return:
pop rdi
pop rdx
pop rcx
ret
skipSpaces:
; buffer pointer comes into esi
push rax
.start:
mov al, [rsi]
cmp al, ' '
je .continue
cmp al, `\t`
je .continue
cmp al, `\n` ; something something line break carriage return XXX
je .continue
jmp .end
.continue:
mov byte [rsi], 0
inc rsi
jmp .start
.end:
pop rax
ret