-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlist.scm
334 lines (314 loc) · 7.69 KB
/
list.scm
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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;This program is distributed under the terms of the ;;;
;;;GNU General Public License. ;;;
;;;Copyright (C) 2011 David Joseph Stith ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;
;; List Primitives ;;
;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(new-primitive "cons")
(mov ARGL VAL)
(insure-more-args VAL)
(mov (@ 4 ARGL) ARGL)
(insure-one-last-arg ARGL)
(mov (@ ARGL) ARGL)
(mov ARGL (@ 4 VAL))
(ret)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (cxxxxr path)
(define n (- (string-length path) 1))
(if (< n 4)
(begin
(new-primitive (string-append "c" path "r"))
(insure-one-last-arg ARGL)
(mov (@ ARGL) ARGL)
(: (string->symbol (string-append "c" path "r")))
(call 'insure_pair)
(let ((x (string-ref path n))
(r (substring path 0 n)))
(mov
(@ (if (char=? x #\a) 0 4) ARGL)
(if (zero? n) VAL ARGL))
(if (zero? n)
(ret)
(jmpl (string->symbol (string-append "c" r "r"))))
(cxxxxr (next-path path n))))))
(define (next-path path n)
(let ((x (string-ref path n)))
(case x
((#\a)
(string-set! path n #\d)
path)
((#\d)
(string-set! path n #\a)
(if (zero? n)
(string-append "a" path)
(next-path path (- n 1)))))))
(cxxxxr "a")
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (set-cxr offset)
(insure-more-args ARGL)
(mov (@ 4 ARGL) VAL)
(mov (@ ARGL) ARGL)
(insure-pair ARGL)
(insure-one-last-arg VAL)
(mov (@ VAL) VAL)
(mov VAL (@ offset ARGL))
(ret))
(new-primitive "set-car!")
(set-cxr 0)
(new-primitive "set-cdr!")
(set-cxr 4)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(new-primitive "list")
(mov ARGL VAL)
(ret)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(new-primitive "reverse")
(insure-one-last-arg ARGL)
(mov (@ ARGL) ARGL)
(test ARGL ARGL)
(jz 'prim_list)
(insure-object-is-pair ARGL)
(jmpl 'reverse)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(: 'prim_append_null)
(clear VAL)
(ret)
(: 'prim_append_simple)
(mov EXP VAL)
(: 'append_end)
(ret)
(new-primitive "append")
(test ARGL ARGL)
(jz 'prim_append_null)
(: 'prim_append_recurse)
(mov (@ ARGL) EXP)
(mov (@ 4 ARGL) ARGL)
(test ARGL ARGL)
(jz 'prim_append_simple)
(save EXP)
(call 'prim_append_recurse)
(restore EXP)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;Append VAL to end of list in EXP
;;Return list in VAL
(: 'append)
(test EXP EXP)
(jz 'append_end)
(insure-object-is-pair EXP)
(mov (@ EXP) TEMP)
(save TEMP)
(mov (@ 4 EXP) EXP)
(call 'append)
(restore EXP)
(mov (object EXP VAL) VAL)
(jmpl 'advance_free)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(new-primitive "length")
(insure-one-last-arg ARGL)
(mov (@ ARGL) ARGL)
(clear TEMP)
(test ARGL ARGL)
(ifnz
(begin
(insure-object-is-pair ARGL)
(: 'prim_length_loop)
(inc TEMP)
(mov (@ 4 ARGL) ARGL)
(test ARGL ARGL)
(jnz 'prim_length_loop)))
(mov INTEGER (@ FREE))
(mov TEMP (@ 4 FREE))
(mov FREE VAL)
(jmpl 'advance_free)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(new-primitive "list-tail")
(insure-more-args ARGL)
(mov (@ ARGL) VAL)
(mov (@ 4 ARGL) ARGL)
(call 'get_last_exact_natural)
(mov (@ 4 TEMP) TEMP)
(jmp 'prim_list_tail_loop_begin)
(: 'prim_list_tail_loop)
(test VAL VAL)
(jzl 'error_expected_pair)
(insure-object-is-pair VAL)
(mov (@ 4 VAL) VAL)
(: 'prim_list_tail_loop_begin)
(dec TEMP)
(jns 'prim_list_tail_loop)
(ret)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(new-primitive "list-ref")
(call-prim "list-tail")
(test VAL VAL)
(jzl 'error_expected_pair)
(insure-object-is-pair VAL)
(mov (@ VAL) VAL)
(ret)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(new-primitive "memv")
(mov 1 (@ 'thunk))
(jmp 'prim_mem)
(new-primitive "memq")
(mov 0 (@ 'thunk))
(: 'prim_mem)
(insure-more-args ARGL)
(mov (@ ARGL) VAL)
(mov (@ 4 ARGL) ARGL)
(insure-one-last-arg ARGL)
(mov (@ ARGL) ARGL)
(jmp 'prim_mem_begin)
(: 'prim_mem_loop)
(mov (@ 4 ARGL) ARGL)
(: 'prim_mem_begin)
(test ARGL ARGL)
(jzl 'return_false)
(insure-object-is-pair ARGL)
(mov (@ ARGL) EXP)
(cmp EXP VAL)
(je 'prim_mem_found)
(cmp 0 (@ 'thunk))
(je 'prim_mem_loop) ;memq gives up here
(test VAL VAL)
(jz 'prim_mem_loop)
(test EXP EXP)
(jz 'prim_mem_loop)
(mov (@ VAL) TEMP)
(cmp (@ EXP) TEMP)
(jne 'prim_mem_loop)
(test 1 TEMP) ;Pairs are not allowed to be eqv? unless eq?
(jz 'prim_mem_loop)
(cmpb TYPE_VECTOR TEMP) ;Vectors are not allowed to be eqv? unless eq?
(je 'prim_mem_loop)
(cmpb SYMBOL TEMP) ;Symbols are not allowed to be eqv? unless eq?
(je 'prim_mem_loop)
(mov (@ 4 VAL) TEMP)
(cmp (@ 4 EXP) TEMP)
(jne 'prim_mem_loop)
(: 'prim_mem_found)
(mov ARGL VAL)
(ret)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(new-primitive "member")
(insure-more-args ARGL)
(mov (@ ARGL) VAL)
(push ARGL) ;for reuse when calling equal?.
(mov (@ 4 ARGL) ARGL)
(insure-one-last-arg ARGL)
(mov (@ ARGL) ARGL)
(jmp 'prim_member_begin)
(: 'prim_member_loop)
(mov (@ 4 ARGL) ARGL)
(: 'prim_member_begin)
(test ARGL ARGL)
(jzl 'prim_member_false)
(insure-object-is-pair ARGL)
(mov (@ ARGL) EXP)
(cmp EXP VAL)
(je 'prim_member_found)
(push ARGL)
(mov (@ 4 SP) ARGL)
(mov EXP (@ ARGL))
(mov (@ 4 ARGL) TEMP)
(mov VAL (@ TEMP))
(push VAL)
(call-prim "equal?")
(mov VAL TEMP)
(pop VAL)
(pop ARGL)
(cmp 'false TEMP)
(je 'prim_member_loop)
(: 'prim_member_found)
(mov ARGL VAL)
(add 4 SP)
(ret)
(: 'prim_member_false)
(add 4 SP)
(return-false)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(new-primitive "assv")
(mov 1 (@ 'thunk))
(jmp 'prim_ass)
(new-primitive "assq")
(mov 0 (@ 'thunk))
(: 'prim_ass)
(insure-more-args ARGL)
(mov (@ ARGL) VAL)
(mov (@ 4 ARGL) ARGL)
(insure-one-last-arg ARGL)
(mov (@ ARGL) ARGL)
(jmp 'prim_ass_begin)
(: 'prim_ass_loop)
(mov (@ 4 ARGL) ARGL)
(: 'prim_ass_begin)
(test ARGL ARGL)
(jzl 'return_false)
(insure-object-is-pair ARGL)
(mov (@ ARGL) EXP)
(insure-object-is-pair EXP)
(mov (@ EXP) EXP)
(cmp EXP VAL)
(je 'prim_ass_found)
(cmp 0 (@ 'thunk))
(je 'prim_ass_loop) ;assq gives up here
(test VAL VAL)
(jz 'prim_ass_loop)
(test EXP EXP)
(jz 'prim_ass_loop)
(mov (@ VAL) TEMP)
(cmp (@ EXP) TEMP)
(jne 'prim_ass_loop)
(test 1 TEMP) ;Pairs are not allowed to be eqv? unless eq?
(jz 'prim_ass_loop)
(cmpb TYPE_VECTOR TEMP) ;Vectors are not allowed to be eqv? unless eq?
(je 'prim_ass_loop)
(cmpb SYMBOL TEMP) ;Symbols are obviously not eqv? unless eq?
(je 'prim_ass_loop)
(mov (@ 4 VAL) TEMP)
(cmp (@ 4 EXP) TEMP)
(jne 'prim_ass_loop)
(: 'prim_ass_found)
(mov (@ ARGL) VAL)
(ret)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(new-primitive "assoc")
(insure-more-args ARGL)
(mov (@ ARGL) VAL)
(push ARGL) ;for reuse when calling equal?.
(mov (@ 4 ARGL) ARGL)
(insure-one-last-arg ARGL)
(mov (@ ARGL) ARGL)
(jmp 'prim_assoc_begin)
(: 'prim_assoc_loop)
(mov (@ 4 ARGL) ARGL)
(: 'prim_assoc_begin)
(test ARGL ARGL)
(jzl 'prim_assoc_false)
(insure-object-is-pair ARGL)
(mov (@ ARGL) EXP)
(insure-object-is-pair EXP)
(mov (@ EXP) EXP)
(cmp EXP VAL)
(je 'prim_assoc_found)
(push ARGL)
(mov (@ 4 SP) ARGL)
(mov EXP (@ ARGL))
(mov (@ 4 ARGL) TEMP)
(mov VAL (@ TEMP))
(push VAL)
(call-prim "equal?")
(mov VAL TEMP)
(pop VAL)
(pop ARGL)
(cmp 'false TEMP)
(je 'prim_assoc_loop)
(: 'prim_assoc_found)
(add 4 SP)
(mov (@ ARGL) VAL)
(ret)
(: 'prim_assoc_false)
(add 4 SP)
(return-false)