about summary refs log tree commit diff stats
path: root/064list.mu
blob: cf7be09c94d424f02e9f84d5c248b2d591a19349 (plain) (blame)
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
339
340
341
342
343
344
345
346
347
348
349
350
# A list links up multiple objects together to make them easier to manage.
#
# The objects must be of the same type. If you want to store multiple types in
# a single list, use an exclusive-container.

container list:_elem [
  value:_elem
  next:&:list:_elem
]

def push x:_elem, in:&:list:_elem -> result:&:list:_elem [
  local-scope
  load-ingredients
  result <- new {(list _elem): type}
  *result <- merge x, in
]

def first in:&:list:_elem -> result:_elem [
  local-scope
  load-ingredients
  result <- get *in, value:offset
]

def rest in:&:list:_elem -> result:&:list:_elem/contained-in:in [
  local-scope
  load-ingredients
  result <- get *in, next:offset
]

scenario list-handling [
  run [
    local-scope
    x:&:list:num <- push 3, 0
    x <- push 4, x
    x <- push 5, x
    10:num/raw <- first x
    x <- rest x
    11:num/raw <- first x
    x <- rest x
    12:num/raw <- first x
    20:&:list:num/raw <- rest x
  ]
  memory-should-contain [
    10 <- 5
    11 <- 4
    12 <- 3
    20 <- 0  # nothing left
  ]
]

def length l:&:list:_elem -> result:num [
  local-scope
  load-ingredients
  return-unless l, 0
  rest:&:list:_elem <- rest l
  length-of-rest:num <- length rest
  result <- add length-of-rest, 1
]

# insert 'x' after 'in'
def insert x:_elem, in:&:list:_elem -> in:&:list:_elem [
  local-scope
  load-ingredients
  new-node:&:list:_elem <- new {(list _elem): type}
  *new-node <- put *new-node, value:offset, x
  next-node:&:list:_elem <- get *in, next:offset
  *in <- put *in, next:offset, new-node
  *new-node <- put *new-node, next:offset, next-node
]

scenario inserting-into-list [
  local-scope
  list:&:list:char <- push 3, 0
  list <- push 4, list
  list <- push 5, list
  run [
    list2:&:list:char <- rest list  # inside list
    list2 <- insert 6, list2
    # check structure
    list2 <- copy list
    10:char/raw <- first list2
    list2 <- rest list2
    11:char/raw <- first list2
    list2 <- rest list2
    12:char/raw <- first list2
    list2 <- rest list2
    13:char/raw <- first list2
  ]
  memory-should-contain [
    10 <- 5  # scanning next
    11 <- 4
    12 <- 6  # inserted element
    13 <- 3
  ]
]

scenario inserting-at-end-of-list [
  local-scope
  list:&:list:char <- push 3, 0
  list <- push 4, list
  list <- push 5, list
  run [
    list2:&:list:char <- rest list  # inside list
    list2 <- rest list2  # now at end of list
    list2 <- insert 6, list2
    # check structure like before
    list2 <- copy list
    10:char/raw <- first list2
    list2 <- rest list2
    11:char/raw <- first list2
    list2 <- rest list2
    12:char/raw <- first list2
    list2 <- rest list2
    13:char/raw <- first list2
  ]
  memory-should-contain [
    10 <- 5  # scanning next
    11 <- 4
    12 <- 3
    13 <- 6  # inserted element
  ]
]

scenario inserting-after-start-of-list [
  local-scope
  list:&:list:char <- push 3, 0
  list <- push 4, list
  list <- push 5, list
  run [
    list <- insert 6, list
    # check structure like before
    list2:&:list:char <- copy list
    10:char/raw <- first list2
    list2 <- rest list2
    11:char/raw <- first list2
    list2 <- rest list2
    12:char/raw <- first list2
    list2 <- rest list2
    13:char/raw <- first list2
  ]
  memory-should-contain [
    10 <- 5  # scanning next
    11 <- 6  # inserted element
    12 <- 4
    13 <- 3
  ]
]

# remove 'x' from its surrounding list 'in'
#
# Returns null if and only if list is empty. Beware: in that case any other
# pointers to the head are now invalid.
def remove x:&:list:_elem/contained-in:in, in:&:list:_elem -> in:&:list:_elem [
  local-scope
  load-ingredients
  # if 'x' is null, return
  return-unless x
  next-node:&:list:_elem <- rest x
  # clear next pointer of 'x'
  *x <- put *x, next:offset, 0
  # if 'x' is at the head of 'in', return the new head
  at-head?:bool <- equal x, in
  return-if at-head?, next-node
  # compute prev-node
  prev-node:&:list:_elem <- copy in
  curr:&:list:_elem <- rest prev-node
  {
    return-unless curr
    found?:bool <- equal curr, x
    break-if found?
    prev-node <- copy curr
    curr <- rest curr
  }
  # set its next pointer to skip 'x'
  *prev-node <- put *prev-node, next:offset, next-node
]

scenario removing-from-list [
  local-scope
  list:&:list:char <- push 3, 0
  list <- push 4, list
  list <- push 5, list
  run [
    list2:&:list:char <- rest list  # second element
    list <- remove list2, list
    10:bool/raw <- equal list2, 0
    # check structure like before
    list2 <- copy list
    11:char/raw <- first list2
    list2 <- rest list2
    12:char/raw <- first list2
    20:&:list:char/raw <- rest list2
  ]
  memory-should-contain [
    10 <- 0  # remove returned non-null
    11 <- 5  # scanning next, skipping deleted element
    12 <- 3
    20 <- 0  # no more elements
  ]
]

scenario removing-from-start-of-list [
  local-scope
  list:&:list:char <- push 3, 0
  list <- push 4, list
  list <- push 5, list
  run [
    list <- remove list, list
    # check structure like before
    list2:&:list:char <- copy list
    10:char/raw <- first list2
    list2 <- rest list2
    11:char/raw <- first list2
    20:&:list:char/raw <- rest list2
  ]
  memory-should-contain [
    10 <- 4  # scanning next, skipping deleted element
    11 <- 3
    20 <- 0  # no more elements
  ]
]

scenario removing-from-end-of-list [
  local-scope
  list:&:list:char <- push 3, 0
  list <- push 4, list
  list <- push 5, list
  run [
    # delete last element
    list2:&:list:char <- rest list
    list2 <- rest list2
    list <- remove list2, list
    10:bool/raw <- equal list2, 0
    # check structure like before
    list2 <- copy list
    11:char/raw <- first list2
    list2 <- rest list2
    12:char/raw <- first list2
    20:&:list:char/raw <- rest list2
  ]
  memory-should-contain [
    10 <- 0  # remove returned non-null
    11 <- 5  # scanning next, skipping deleted element
    12 <- 4
    20 <- 0  # no more elements
  ]
]

scenario removing-from-singleton-list [
  local-scope
  list:&:list:char <- push 3, 0
  run [
    list <- remove list, list
    1:num/raw <- copy list
  ]
  memory-should-contain [
    1 <- 0  # back to an empty list
  ]
]

# reverse the elements of a list
# (contributed by Caleb Couch)
def reverse list:&:list:_elem temp:&:list:_elem -> result:&:list:_elem [
  local-scope
  load-ingredients
  reply-unless list, temp
  object:_elem <- first, list
  list <- rest list
  temp <- push object, temp
  result <- reverse list, temp
]

scenario reverse-list [
  local-scope
  list:&:list:number <- push 1, 0
  list <- push 2, list
  list <- push 3, list
  run [
    stash [list:], list
    list <- reverse list
    stash [reversed:], list
  ]
  trace-should-contain [
    app: list: 3 -> 2 -> 1
    app: reversed: 1 -> 2 -> 3
  ]
]

def to-text in:&:list:_elem -> result:text [
  local-scope
  load-ingredients
  buf:&:buffer <- new-buffer 80
  buf <- to-buffer in, buf
  result <- buffer-to-array buf
]

# variant of 'to-text' which stops printing after a few elements (and so is robust to cycles)
def to-text-line in:&:list:_elem -> result:text [
  local-scope
  load-ingredients
  buf:&:buffer <- new-buffer 80
  buf <- to-buffer in, buf, 6  # max elements to display
  result <- buffer-to-array buf
]

def to-buffer in:&:list:_elem, buf:&:buffer -> buf:&:buffer [
  local-scope
  load-ingredients
  {
    break-if in
    buf <- append buf, [[]]
    return
  }
  # append in.value to buf
  val:_elem <- get *in, value:offset
  buf <- append buf, val
  # now prepare next
  next:&:list:_elem <- rest in
  nextn:num <- copy next
  return-unless next
  buf <- append buf, [ -> ]
  # and recurse
  remaining:num, optional-ingredient-found?:bool <- next-ingredient
  {
    break-if optional-ingredient-found?
    # unlimited recursion
    buf <- to-buffer next, buf
    return
  }
  {
    break-unless remaining
    # limited recursion
    remaining <- subtract remaining, 1
    buf <- to-buffer next, buf, remaining
    return
  }
  # past recursion depth; insert ellipses and stop
  append buf, [...]
]

scenario stash-empty-list [
  local-scope
  x:&:list:num <- copy 0
  run [
    stash x
  ]
  trace-should-contain [
    app: []
  ]
]
ss="p">> <span class="Normal"> . </span> 0/imm32 <span class="subxComment"># compare edx</span> <span id="L22" class="LineNr"> 22 </span> 75/jump-if-!= $new-stream:abort/disp8 <span id="L23" class="LineNr"> 23 </span> <span class="subxS1Comment"># . edx = elemsize*length</span> <span id="L24" class="LineNr"> 24 </span> 89/copy 3/mod/direct 2/rm32/edx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0/r32/eax <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy eax to edx</span> <span id="L25" class="LineNr"> 25 </span> <span class="subxS1Comment"># . eax += 12</span> <span id="L26" class="LineNr"> 26 </span> 05/add-to-eax 0xc/imm32 <span id="L27" class="LineNr"> 27 </span> <span class="subxComment"># var eax: (handle stream _) = allocate(ad, n)</span> <span id="L28" class="LineNr"> 28 </span> <span class="subxS2Comment"># . . push args</span> <span id="L29" class="LineNr"> 29 </span> 50/push-eax <span id="L30" class="LineNr"> 30 </span> ff 6/subop/push 1/mod/*+disp8 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># push *(ebp+8)</span> <span id="L31" class="LineNr"> 31 </span> <span class="subxS2Comment"># . . call</span> <span id="L32" class="LineNr"> 32 </span> e8/call <a href='069allocate.subx.html#L59'>allocate</a>/disp32 <span id="L33" class="LineNr"> 33 </span> <span class="subxS2Comment"># . . discard args</span> <span id="L34" class="LineNr"> 34 </span> 81 0/subop/add 3/mod/direct 4/rm32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/imm32 <span class="subxComment"># add to esp</span> <span id="L35" class="LineNr"> 35 </span> <span class="subxComment"># eax-&gt;length = elemsize*length</span> <span id="L36" class="LineNr"> 36 </span> 89/copy 1/mod/*+disp8 0/rm32/eax <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 2/r32/edx 8/disp8 <span class="Normal"> . </span> <span class="subxComment"># copy edx to *(eax+8)</span> <span id="L37" class="LineNr"> 37 </span> <span class="subxComment"># clear-stream(eax)</span> <span id="L38" class="LineNr"> 38 </span> <span class="subxS2Comment"># . . push args</span> <span id="L39" class="LineNr"> 39 </span> 50/push-eax <span id="L40" class="LineNr"> 40 </span> <span class="subxS2Comment"># . . call</span> <span id="L41" class="LineNr"> 41 </span> e8/call <a href='055stream.subx.html#L17'>clear-stream</a>/disp32 <span id="L42" class="LineNr"> 42 </span> <span class="subxS2Comment"># . . discard args</span> <span id="L43" class="LineNr"> 43 </span> 81 0/subop/add 3/mod/direct 4/rm32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 4/imm32 <span class="subxComment"># add to esp</span> <span id="L44" class="LineNr"> 44 </span><span class="Constant">$new-stream:end</span>: <span id="L45" class="LineNr"> 45 </span> <span class="subxS1Comment"># . restore registers</span> <span id="L46" class="LineNr"> 46 </span> 5a/pop-to-edx <span id="L47" class="LineNr"> 47 </span> <span class="subxS1Comment"># . epilogue</span> <span id="L48" class="LineNr"> 48 </span> 89/copy 3/mod/direct 4/rm32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 5/r32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy ebp to esp</span> <span id="L49" class="LineNr"> 49 </span> 5d/pop-to-ebp <span id="L50" class="LineNr"> 50 </span> c3/return <span id="L51" class="LineNr"> 51 </span> <span id="L52" class="LineNr"> 52 </span><span class="Constant">$new-stream:abort</span>: <span id="L53" class="LineNr"> 53 </span> <span class="subxS1Comment"># . _write(2/stderr, error)</span> <span id="L54" class="LineNr"> 54 </span> <span class="subxS2Comment"># . . push args</span> <span id="L55" class="LineNr"> 55 </span> 68/push <span class="Constant">&quot;new-stream: size too large\n&quot;</span>/imm32 <span id="L56" class="LineNr"> 56 </span> 68/push 2/imm32/stderr <span id="L57" class="LineNr"> 57 </span> <span class="subxS2Comment"># . . call</span> <span id="L58" class="LineNr"> 58 </span> e8/call <a href='050_write.subx.html#L14'>_write</a>/disp32 <span id="L59" class="LineNr"> 59 </span> <span class="subxS2Comment"># . . discard args</span> <span id="L60" class="LineNr"> 60 </span> 81 0/subop/add 3/mod/direct 4/rm32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/imm32 <span class="subxComment"># add to esp</span> <span id="L61" class="LineNr"> 61 </span> <span class="subxS1Comment"># . syscall(exit, 1)</span> <span id="L62" class="LineNr"> 62 </span> bb/copy-to-ebx 1/imm32 <span id="L63" class="LineNr"> 63 </span> b8/copy-to-eax 1/imm32/exit <span id="L64" class="LineNr"> 64 </span> cd/syscall 0x80/imm8 <span id="L65" class="LineNr"> 65 </span> <span class="subxComment"># never gets here</span> <span id="L66" class="LineNr"> 66 </span> <span id="L67" class="LineNr"> 67 </span><span class="subxTest">test-new-stream</span>: <span id="L68" class="LineNr"> 68 </span> <span class="subxS1Comment"># . prologue</span> <span id="L69" class="LineNr"> 69 </span> 55/push-ebp <span id="L70" class="LineNr"> 70 </span> 89/copy 3/mod/direct 5/rm32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 4/r32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy esp to ebp</span> <span id="L71" class="LineNr"> 71 </span> <span class="subxComment"># var heap/ecx: allocation-descriptor</span> <span id="L72" class="LineNr"> 72 </span> 68/push 0/imm32/limit <span id="L73" class="LineNr"> 73 </span> 68/push 0/imm32/curr <span id="L74" class="LineNr"> 74 </span> 89/copy 3/mod/direct 1/rm32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 4/r32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy esp to ecx</span> <span id="L75" class="LineNr"> 75 </span> <span class="subxComment"># heap = new-segment(512)</span> <span id="L76" class="LineNr"> 76 </span> <span class="subxS2Comment"># . . push args</span> <span id="L77" class="LineNr"> 77 </span> 51/push-ecx <span id="L78" class="LineNr"> 78 </span> 68/push 0x200/imm32 <span id="L79" class="LineNr"> 79 </span> <span class="subxS2Comment"># . . call</span> <span id="L80" class="LineNr"> 80 </span> e8/call <a href='053new-segment.subx.html#L41'>new-segment</a>/disp32 <span id="L81" class="LineNr"> 81 </span> <span class="subxS2Comment"># . . discard args</span> <span id="L82" class="LineNr"> 82 </span> 81 0/subop/add 3/mod/direct 4/rm32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/imm32 <span class="subxComment"># add to esp</span> <span id="L83" class="LineNr"> 83 </span> <span class="subxComment"># var start/edx = ad-&gt;curr</span> <span id="L84" class="LineNr"> 84 </span> 8b/copy 0/mod/indirect 1/rm32/ecx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 2/r32/edx <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy *ecx to edx</span> <span id="L85" class="LineNr"> 85 </span> <span class="subxComment"># var eax: (handle stream byte) = new-stream(heap, 3, 2)</span> <span id="L86" class="LineNr"> 86 </span> <span class="subxS2Comment"># . . push args</span> <span id="L87" class="LineNr"> 87 </span> 68/push 2/imm32 <span id="L88" class="LineNr"> 88 </span> 68/push 3/imm32 <span id="L89" class="LineNr"> 89 </span> 51/push-ecx <span id="L90" class="LineNr"> 90 </span> <span class="subxS2Comment"># . . call</span> <span id="L91" class="LineNr"> 91 </span> e8/call <a href='070new-stream.subx.html#L8'>new-stream</a>/disp32 <span id="L92" class="LineNr"> 92 </span> <span class="subxS2Comment"># . . discard args</span> <span id="L93" class="LineNr"> 93 </span> 81 0/subop/add 3/mod/direct 4/rm32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0xc/imm32 <span class="subxComment"># add to esp</span> <span id="L94" class="LineNr"> 94 </span> <span class="subxComment"># check-ints-equal(eax, edx, msg)</span> <span id="L95" class="LineNr"> 95 </span> <span class="subxS2Comment"># . . push args</span> <span id="L96" class="LineNr"> 96 </span> 68/push <span class="Constant">&quot;F - <a href='070new-stream.subx.html#L67'>test-new-stream</a>: returns current pointer of allocation descriptor&quot;</span>/imm32 <span id="L97" class="LineNr"> 97 </span> 52/push-edx <span id="L98" class="LineNr"> 98 </span> 50/push-eax <span id="L99" class="LineNr"> 99 </span> <span class="subxS2Comment"># . . call</span> <span id="L100" class="LineNr">100 </span> e8/call <a href='051test.subx.html#L24'>check-ints-equal</a>/disp32 <span id="L101" class="LineNr">101 </span> <span class="subxS2Comment"># . . discard args</span> <span id="L102" class="LineNr">102 </span> 81 0/subop/add 3/mod/direct 4/rm32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0xc/imm32 <span class="subxComment"># add to esp</span> <span id="L103" class="LineNr">103 </span> <span class="subxComment"># check-ints-equal(eax-&gt;length, 6, msg)</span> <span id="L104" class="LineNr">104 </span> <span class="subxS2Comment"># . . push args</span> <span id="L105" class="LineNr">105 </span> 68/push <span class="Constant">&quot;F - <a href='070new-stream.subx.html#L67'>test-new-stream</a>: sets length correctly&quot;</span>/imm32 <span id="L106" class="LineNr">106 </span> 68/push 6/imm32 <span id="L107" class="LineNr">107 </span> ff 6/subop/push 1/mod/*+disp8 0/rm32/eax <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 8/disp8 <span class="subxComment"># push *(eax+8)</span> <span id="L108" class="LineNr">108 </span> <span class="subxS2Comment"># . . call</span> <span id="L109" class="LineNr">109 </span> e8/call <a href='051test.subx.html#L24'>check-ints-equal</a>/disp32 <span id="L110" class="LineNr">110 </span> <span class="subxS2Comment"># . . discard args</span> <span id="L111" class="LineNr">111 </span> 81 0/subop/add 3/mod/direct 4/rm32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 0xc/imm32 <span class="subxComment"># add to esp</span> <span id="L112" class="LineNr">112 </span> <span class="subxComment"># the rest is delegated to clear-stream() so we won't bother checking it</span> <span id="L113" class="LineNr">113 </span> <span class="subxS1Comment"># . epilogue</span> <span id="L114" class="LineNr">114 </span> 89/copy 3/mod/direct 4/rm32/esp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="Normal"> . </span> 5/r32/ebp <span class="Normal"> . </span> <span class="Normal"> . </span> <span class="subxComment"># copy ebp to esp</span> <span id="L115" class="LineNr">115 </span> 5d/pop-to-ebp <span id="L116" class="LineNr">116 </span> c3/return <span id="L117" class="LineNr">117 </span> <span id="L118" class="LineNr">118 </span><span class="subxS2Comment"># . . vim&#0058;nowrap:textwidth=0</span> </pre> </body> </html> <!-- vim: set foldmethod=manual : -->