Skip to content

Commit cca5af9

Browse files
ADD list.splice(other)
1 parent e6f73b8 commit cca5af9

File tree

3 files changed

+46
-5
lines changed

3 files changed

+46
-5
lines changed

std/ds/list.mx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,26 @@
177177
%seg:load(%this, %list_node:value) ; return detached.value
178178
}
179179

180+
; this, other: *list ->
181+
; splice list elements from other to front of this
182+
; doesn't reallocate any nodes
183+
%void_method{ list_splice, 2, 1,
184+
%if { ; other is empty: do nothing
185+
%seg:movptr(%arg, 1)
186+
lmeq 0
187+
,
188+
%returnFunc(list_splice)
189+
}
190+
ldm ; other.first
191+
str 0
192+
%seg:move(%this, %head) ; tail = this.head, this.head = other.first
193+
swap
194+
%seg:store(%local, 0)
195+
%call(__list_index_last)
196+
%seg:load(%local, 0) ; last_spliced.next = tail
197+
%seg:store(%this, %list_node:next)
198+
}
199+
180200
; this ->
181201
%void_method{ list_print, 1, 0,
182202
outc '['

tests/std-list.mx

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,27 @@
123123
%callListMethod(list_size, 2)
124124
%printResult('C', '\n')
125125

126-
; TODO splice
126+
; A.splice(C)
127+
outc 'S'
128+
outc 'P'
129+
outc 'L'
130+
outc 'I'
131+
outc 'C'
132+
outc 'E'
133+
outc ':'
134+
outc ' '
135+
136+
%callListMethodWithArgs(list_splice, 0, %seg:push(%local, 2)) ; empty C
137+
138+
%callListMethodWithArgs(list_insert_after, 2, %stack:push(9))
139+
%callListMethodWithArgs(list_insert_after, 2, %stack:push(8))
140+
141+
%print_spaced( outc 'C' )
142+
%print_spaced( %callListMethod(list_print, 2) )
143+
144+
%callListMethodWithArgs(list_splice, 0, %seg:push(%local, 2)) ; splice [8, 9] up front
145+
146+
%print_endl( %callListMethod(list_print, 0) )
127147

128148
; set elems to their idx, emit node addresses
129149
%print_spaced( outc 'A' )

tests/std-list.txt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
:returncode 0
22

3-
:stdout 310
3+
:stdout 372
44
- VM -
55
[6, 5, 7]
66
IEs
@@ -26,9 +26,10 @@ I:5
2626
A[6, 5, 2, 7] B[] C[]
2727
A F:6 L:7
2828
S A:4 B:0 C:0
29-
A & 2058 2054 2066 2062
30-
A[0, 1, 2, 3] B[] C[]
31-
20: 2057 2053 2065 2061
29+
SPLICE: C [8, 9] [8, 9, 6, 5, 2, 7]
30+
A & 2074 2070 2058 2054 2066 2062
31+
A[0, 1, 2, 3, 4, 5] B[] C[]
32+
20: 2073 2069 2057 2053 2065 2061
3233
D0
3334
21:
3435
2050:

0 commit comments

Comments
 (0)