Skip to content

Commit e6f73b8

Browse files
ADD list.clear(), proper deinit() + robust testing
1 parent d2ced83 commit e6f73b8

File tree

3 files changed

+115
-8
lines changed

3 files changed

+115
-8
lines changed

std/ds/list.mx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
%macro deinit() {
2020
str 0
2121
}
22-
; TODO remove?
2322
; @list_node -> bool
2423
%macro has_next() {
2524
lmne 0
@@ -62,8 +61,26 @@
6261
; @list
6362
; free all list nodes
6463
%macro deinit() {
65-
; TODO proper free
66-
str 0
64+
%stack:pushh()
65+
%call(list_clear)
66+
}
67+
68+
; this ->
69+
; clears list by deallocating all it's nodes
70+
; node values should be deconstructed in advance
71+
%void_method{ list_clear, 1, 1, ; next=null
72+
%seg:set(%local, 0, 0)
73+
%while { %load(%this), ; THIS != null
74+
%seg:load(%this, %list_node:next) ; new_next <- THIS.next <- null
75+
str 0
76+
%stack:pushr() ; new_next
77+
%if { %seg:load(%local, 0), ; old_next != null
78+
%stack:dup_over(1)
79+
%call(free)
80+
}
81+
%seg:pop(%local, 0) ; next = new_next
82+
%storer(%this) ; THIS = new_next
83+
}
6784
}
6885

6986
; this -> value (THIS)

tests/std-list.mx

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,61 @@
4242
outum
4343
outc %delim
4444
}
45+
46+
; copies of list members, with debug information printing
47+
%void_method{ list_clear_debug, 1, 1, ; next=null
48+
%print_spaced( ; list struct idx
49+
outur
50+
outc ':'
51+
)
52+
%seg:set(%local, 0, 0)
53+
%while { %load(%this), ; THIS != null
54+
%seg:load(%this, %list_node:next) ; new_next <- THIS.next <- null
55+
str 0
56+
%stack:pushr() ; new_next
57+
%if { %seg:load(%local, 0), ; old_next != null
58+
%stack:dup_over(1)
59+
outur ; what free called at
60+
outc 32
61+
%call(free)
62+
}
63+
%seg:pop(%local, 0) ; next = new_next
64+
%storer(%this) ; THIS = new_next
65+
}
66+
outc '\n'
67+
}
68+
%macro list_debug_deinit() {
69+
%stack:pushh()
70+
%call(list_clear_debug)
71+
}
72+
%macro list_debug_destructor() {
73+
ldm
74+
str 0
75+
%stack:pushr()
76+
movr
77+
%list_debug_deinit()
78+
%print_endl {
79+
%stack:top() ; emit free argument
80+
outc 'F'
81+
outum
82+
}
83+
%list:dealloc()
84+
}
85+
; ------------------------------------
86+
4587
%macro populateFirstList() { ; initialize array at local:0
4688
%callListMethodWithArgs(list_insert_after, 0, %stack:push(5))
4789
%callListMethodWithArgs(list_insert_after, 0, %stack:push(6))
4890
%callListMethodWithArgs(list_insert_back, 0, %stack:push(7))
4991
}
50-
51-
%void_func{ main, 0, 6, ; 2 * sizeof(list) + sizeof(ptr) + sizeof(array)
92+
%macro initLocalLists() { ; initialize local arrays
5293
%seg:local(0, %list:init()) ; first 2 lists live in locals
5394
%seg:local(1, %list:init()) ; NOTE: testing of different schemes
5495
%seg:local(2, %list:construct_here()) ; third lives in heap (here is ptr to that struct)
5596
%seg:local(3, %array:init())
56-
97+
}
98+
%void_func{ main, 0, 6, ; 2 * sizeof(list) + sizeof(ptr) + sizeof(array)
99+
%initLocalLists()
57100
%populateFirstList()
58101

59102
; A.insert_after(list[1], 2)
@@ -82,13 +125,49 @@
82125

83126
; TODO splice
84127

85-
; set elems to their idx
128+
; set elems to their idx, emit node addresses
129+
%print_spaced( outc 'A' )
130+
%print_spaced( outc '&' )
86131
%seg:point(%this, %local, 0)
87132
%list:foreach {
133+
%print_spaced( outuh )
88134
%stack:top()
89135
%seg:store(%this, %list_node:value)
90136
}
137+
outc '\n'
91138
%call(print_lists)
139+
140+
; list destructing
141+
%callListMethod(list_clear_debug, 0) ; A.clear()
142+
%seg:load(%local, 0)
143+
%print_endl( ; see list was deinitialized
144+
outc 'D'
145+
outum
146+
)
147+
%callListMethod(list_clear_debug, 1) ; B.clear()
148+
%seg:move(%local, 2) ; empty C.destructor()
149+
%list_debug_destructor()
150+
%seg:load(%local, 2)
151+
%print_endl(
152+
outc 'C'
153+
outum
154+
)
155+
%callWith1Arg(malloc, 1) ; change the new location
156+
%callWith1Arg(malloc, 1)
157+
158+
%initLocalLists()
159+
%callListMethodWithArgs(list_insert_after, 2, %stack:push(69))
160+
%seg:move(%local, 2) ; nonempty C.destructor()
161+
%print_endl(
162+
outc 'V'
163+
outum
164+
)
165+
%list_debug_destructor()
166+
%seg:load(%local, 2)
167+
%print_endl(
168+
outc 'D'
169+
outum
170+
)
92171
}
93172

94173
%call(main)

tests/std-list.txt

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

3-
:stdout 209
3+
:stdout 310
44
- VM -
55
[6, 5, 7]
66
IEs
@@ -26,6 +26,17 @@ 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
2930
A[0, 1, 2, 3] B[] C[]
31+
20: 2057 2053 2065 2061
32+
D0
33+
21:
34+
2050:
35+
F2050
36+
C0
37+
V2057
38+
2057: 2061
39+
F2057
40+
D0
3041

3142

0 commit comments

Comments
 (0)