|
7 | 7 | %include "debug" |
8 | 8 | %include "array" |
9 | 9 |
|
| 10 | +%vm_runtime_delim() |
| 11 | + |
10 | 12 | %proc{ print_lists, ; uses main's local segment |
11 | 13 | outc 'A' |
12 | 14 | %seg:push_addr(%local, 0) |
|
19 | 21 | %print_endl( %call(list_print) ) |
20 | 22 | } |
21 | 23 |
|
22 | | -%macro callListMethodWithArgs(methodName, listIdx, pushArguemts) { |
| 24 | +%macro callListMethodWithArgs(methodName, listIdx, pushArguments) { |
23 | 25 | %if_else { ; lives list in main:locals? |
24 | 26 | ld %listIdx |
25 | 27 | lle 1 |
26 | 28 | , |
27 | 29 | %seg:push_addr(%local, %listIdx), |
28 | 30 | %seg:push(%local, %listIdx) |
29 | 31 | } |
30 | | - %pushArguemts |
| 32 | + %pushArguments |
31 | 33 | %call(%methodName) |
32 | 34 | } |
33 | 35 | %macro callListMethod(methodName, listIdx) { ; no arguments |
|
40 | 42 | outum |
41 | 43 | outc %delim |
42 | 44 | } |
43 | | -%macro populateFirstArray() { ; initialize array at local:0 |
| 45 | +%macro populateFirstList() { ; initialize array at local:0 |
44 | 46 | %callListMethodWithArgs(list_insert_after, 0, %stack:push(5)) |
45 | 47 | %callListMethodWithArgs(list_insert_after, 0, %stack:push(6)) |
46 | 48 | %callListMethodWithArgs(list_insert_back, 0, %stack:push(7)) |
|
52 | 54 | %seg:local(2, %list:construct_here()) ; third lives in heap (here is ptr to that struct) |
53 | 55 | %seg:local(3, %array:init()) |
54 | 56 |
|
55 | | - %populateFirstArray() |
| 57 | + %populateFirstList() |
56 | 58 |
|
57 | 59 | ; A.insert_after(list[1], 2) |
58 | 60 | %callListMethodWithArgs(list_index, 0, %stack:push(1)) |
|
77 | 79 | %printResult('B', ' ') |
78 | 80 | %callListMethod(list_size, 2) |
79 | 81 | %printResult('C', '\n') |
| 82 | + |
| 83 | + ; TODO splice |
| 84 | + |
| 85 | + ; set elems to their idx |
| 86 | + %seg:point(%this, %local, 0) |
| 87 | + %list:foreach { |
| 88 | + %stack:top() |
| 89 | + %seg:store(%this, %list_node:value) |
| 90 | + } |
| 91 | + %call(print_lists) |
80 | 92 | } |
81 | 93 |
|
82 | 94 | %call(main) |
| 95 | + |
| 96 | +; testing for errors in ctime ---------------------------------------------------- |
| 97 | +%macro setLocalsToTemp() { |
| 98 | + %nop { ; point locals to temp (because we're not in any function) |
| 99 | + !store(%local, %temp_addr) |
| 100 | + } |
| 101 | +} |
| 102 | +%macro initCtimeTesting() { |
| 103 | + %setLocalsToTemp() |
| 104 | + %nop { |
| 105 | + !seg:temp(0, %list:init()) ; clear all lists |
| 106 | + !seg:temp(1, %list:init()) |
| 107 | + ; DONT USE third - passed via ptr |
| 108 | + !populateFirstList() |
| 109 | + !stack:push(%temp_addr) |
| 110 | + !print_endl( |
| 111 | + %call(list_print) |
| 112 | + ) |
| 113 | + } |
| 114 | +} |
| 115 | +%macro _implErrorneousTest(methodName, listIdx, pushArguments, preDesc, desc) { |
| 116 | + %print_spaced( outc %preDesc ) |
| 117 | + %callListMethodWithArgs(%methodName, %listIdx, %pushArguments) |
| 118 | + %printResult(%desc, '\n') |
| 119 | +} |
| 120 | +%macro testPossiblyErrorneous(methodName, listIdx, pushArguments, preDesc, desc) { |
| 121 | + %nop { |
| 122 | + !_implErrorneousTest(%methodName, %listIdx, %pushArguments, %preDesc, %desc) |
| 123 | + } |
| 124 | + %setLocalsToTemp() ; need to be repeated after error in function causes local segment to not return to original state |
| 125 | +} |
| 126 | +; test indexing bounds |
| 127 | +%initCtimeTesting() |
| 128 | +%nop(!print_endl( |
| 129 | + outc 'I' |
| 130 | + outc 'E' |
| 131 | + outc 's' |
| 132 | +)) |
| 133 | +%testPossiblyErrorneous(list_index, 0, %stack:push(0), '0', 'I') |
| 134 | +%testPossiblyErrorneous(list_index, 0, %stack:push(2), '2', 'I') |
| 135 | +%testPossiblyErrorneous(list_index, 0, %stack:push(3), '3', 'I') ; out of bounds |
| 136 | +%nop(!print_spaced( outc 'B' )) |
| 137 | +%testPossiblyErrorneous(list_index, 1, %stack:push(0), '0', 'I') ; empty[0] |
| 138 | + |
| 139 | +; popping |
| 140 | +%nop(!print_endl( |
| 141 | + outc 'P' |
| 142 | + outc 'O' |
| 143 | + outc 'P' |
| 144 | + outc ' ' |
| 145 | + outc '*' |
| 146 | + %load(%temp_addr) ; first node in first list |
| 147 | + outum |
| 148 | +)) |
| 149 | +; A = [6, 5, 7] |
| 150 | +%testPossiblyErrorneous(list_pop_after, 0, ld 0, '0', 'F') ; pop front |
| 151 | +%nop { ; pop after 5 -> 7 |
| 152 | + !load(%temp_addr) |
| 153 | + !storer(%local) |
| 154 | +} |
| 155 | +%testPossiblyErrorneous(list_pop_after, 0, ld 0, '0', 'A') ; ^^ |
| 156 | +%testPossiblyErrorneous(list_pop_back, 0, ld 0, '0', 'B') ; pop back |
| 157 | +%testPossiblyErrorneous(list_size, 0, ld 0, '0', 'S') ; size = 0 |
| 158 | + |
| 159 | +; popping empty |
| 160 | +%testPossiblyErrorneous(list_pop_after, 0, ld 0, 'A', '@') ; pop after |
| 161 | +%testPossiblyErrorneous(list_pop_back, 1, ld 0, 'B', '@') ; pop back |
| 162 | +%nop { ; !this.has_next |
| 163 | + !print_spaced( outc 'N' ) |
| 164 | + !store(12, 0) |
| 165 | + !stack:push(12) |
| 166 | + !call(list_pop_after) |
| 167 | +} |
0 commit comments