11#[ cfg( test) ]
22mod tests {
3+ use std:: rc:: Rc ;
34 use crate :: compiler_test:: { run_compiler_test, CompilerTestCase } ;
45 use crate :: op_code:: Opcode :: * ;
56 use crate :: op_code:: { concat_instructions, make_instructions} ;
@@ -13,20 +14,20 @@ mod tests {
1314 expected_constants: vec![
1415 Object :: Integer ( 5 ) ,
1516 Object :: Integer ( 10 ) ,
16- Object :: CompiledFunction ( object:: CompiledFunction {
17+ Object :: CompiledFunction ( Rc :: from ( object:: CompiledFunction {
1718 instructions: concat_instructions( & vec![
1819 make_instructions( OpConst , & vec![ 0 ] ) ,
1920 make_instructions( OpConst , & vec![ 1 ] ) ,
2021 make_instructions( OpAdd , & vec![ 0 ] ) ,
2122 make_instructions( OpReturnValue , & vec![ 0 ] ) ,
2223 ] )
23- . data,
24+ . data,
2425 num_locals: 0 ,
2526 num_parameters: 0 ,
26- } ) ,
27+ } ) ) ,
2728 ] ,
2829 expected_instructions: vec![
29- make_instructions( OpConst , & vec![ 2 ] ) ,
30+ make_instructions( OpClosure , & vec![ 2 , 0 ] ) ,
3031 make_instructions( OpPop , & vec![ 0 ] ) ,
3132 ] ,
3233 } ,
@@ -35,20 +36,20 @@ mod tests {
3536 expected_constants: vec![
3637 Object :: Integer ( 5 ) ,
3738 Object :: Integer ( 10 ) ,
38- Object :: CompiledFunction ( object:: CompiledFunction {
39+ Object :: CompiledFunction ( Rc :: from ( object:: CompiledFunction {
3940 instructions: concat_instructions( & vec![
4041 make_instructions( OpConst , & vec![ 0 ] ) ,
4142 make_instructions( OpConst , & vec![ 1 ] ) ,
4243 make_instructions( OpAdd , & vec![ 0 ] ) ,
4344 make_instructions( OpReturnValue , & vec![ 0 ] ) ,
4445 ] )
45- . data,
46+ . data,
4647 num_locals: 0 ,
4748 num_parameters: 0 ,
48- } ) ,
49+ } ) ) ,
4950 ] ,
5051 expected_instructions: vec![
51- make_instructions( OpConst , & vec![ 2 ] ) ,
52+ make_instructions( OpClosure , & vec![ 2 , 0 ] ) ,
5253 make_instructions( OpPop , & vec![ 0 ] ) ,
5354 ] ,
5455 } ,
@@ -57,20 +58,20 @@ mod tests {
5758 expected_constants: vec![
5859 Object :: Integer ( 1 ) ,
5960 Object :: Integer ( 2 ) ,
60- Object :: CompiledFunction ( object:: CompiledFunction {
61+ Object :: CompiledFunction ( Rc :: from ( object:: CompiledFunction {
6162 instructions: concat_instructions( & vec![
6263 make_instructions( OpConst , & vec![ 0 ] ) ,
6364 make_instructions( OpPop , & vec![ 0 ] ) ,
6465 make_instructions( OpConst , & vec![ 1 ] ) ,
6566 make_instructions( OpReturnValue , & vec![ 0 ] ) ,
6667 ] )
67- . data,
68+ . data,
6869 num_locals: 0 ,
6970 num_parameters: 0 ,
70- } ) ,
71+ } ) ) ,
7172 ] ,
7273 expected_instructions: vec![
73- make_instructions( OpConst , & vec![ 2 ] ) ,
74+ make_instructions( OpClosure , & vec![ 2 , 0 ] ) ,
7475 make_instructions( OpPop , & vec![ 0 ] ) ,
7576 ] ,
7677 } ,
@@ -82,14 +83,14 @@ mod tests {
8283 fn test_function_without_return_value ( ) {
8384 let tests = vec ! [ CompilerTestCase {
8485 input: "fn() { }" ,
85- expected_constants: vec![ Object :: CompiledFunction ( object:: CompiledFunction {
86+ expected_constants: vec![ Object :: CompiledFunction ( Rc :: from ( object:: CompiledFunction {
8687 instructions: concat_instructions( & vec![ make_instructions( OpReturn , & vec![ 0 ] ) ] )
8788 . data,
8889 num_locals: 0 ,
8990 num_parameters: 0 ,
90- } ) ] ,
91+ } ) ) ] ,
9192 expected_instructions: vec![
92- make_instructions( OpConst , & vec![ 0 ] ) ,
93+ make_instructions( OpClosure , & vec![ 0 , 0 ] ) ,
9394 make_instructions( OpPop , & vec![ 0 ] ) ,
9495 ] ,
9596 } ] ;
@@ -103,18 +104,18 @@ mod tests {
103104 input: "fn() { 24 }();" ,
104105 expected_constants: vec![
105106 Object :: Integer ( 24 ) ,
106- Object :: CompiledFunction ( object:: CompiledFunction {
107+ Object :: CompiledFunction ( Rc :: from ( object:: CompiledFunction {
107108 instructions: concat_instructions( & vec![
108109 make_instructions( OpConst , & vec![ 0 ] ) ,
109110 make_instructions( OpReturnValue , & vec![ 0 ] ) ,
110111 ] )
111- . data,
112+ . data,
112113 num_locals: 0 ,
113114 num_parameters: 0 ,
114- } ) ,
115+ } ) ) ,
115116 ] ,
116117 expected_instructions: vec![
117- make_instructions( OpConst , & vec![ 1 , 0 ] ) ,
118+ make_instructions( OpClosure , & vec![ 1 , 0 ] ) ,
118119 make_instructions( OpCall , & vec![ 0 ] ) ,
119120 make_instructions( OpPop , & vec![ 0 ] ) ,
120121 ] ,
@@ -123,18 +124,18 @@ mod tests {
123124 input: "let noArg = fn() { 24; }; noArg();" ,
124125 expected_constants: vec![
125126 Object :: Integer ( 24 ) ,
126- Object :: CompiledFunction ( object:: CompiledFunction {
127+ Object :: CompiledFunction ( Rc :: from ( object:: CompiledFunction {
127128 instructions: concat_instructions( & vec![
128129 make_instructions( OpConst , & vec![ 0 ] ) ,
129130 make_instructions( OpReturnValue , & vec![ 0 ] ) ,
130131 ] )
131- . data,
132+ . data,
132133 num_locals: 0 ,
133134 num_parameters: 0 ,
134- } ) ,
135+ } ) ) ,
135136 ] ,
136137 expected_instructions: vec![
137- make_instructions( OpConst , & vec![ 1 ] ) ,
138+ make_instructions( OpClosure , & vec![ 1 , 0 ] ) ,
138139 make_instructions( OpSetGlobal , & vec![ 0 ] ) ,
139140 make_instructions( OpGetGlobal , & vec![ 0 ] ) ,
140141 make_instructions( OpCall , & vec![ 0 ] ) ,
@@ -144,19 +145,19 @@ mod tests {
144145 CompilerTestCase {
145146 input: "let oneArg = fn(a) { a; }; oneArg(24);" ,
146147 expected_constants: vec![
147- Object :: CompiledFunction ( object:: CompiledFunction {
148+ Object :: CompiledFunction ( Rc :: from ( object:: CompiledFunction {
148149 instructions: concat_instructions( & vec![
149150 make_instructions( OpGetLocal , & vec![ 0 ] ) ,
150151 make_instructions( OpReturnValue , & vec![ 0 ] ) ,
151152 ] )
152- . data,
153+ . data,
153154 num_locals: 1 ,
154155 num_parameters: 1 ,
155- } ) ,
156+ } ) ) ,
156157 Object :: Integer ( 24 ) ,
157158 ] ,
158159 expected_instructions: vec![
159- make_instructions( OpConst , & vec![ 0 ] ) ,
160+ make_instructions( OpClosure , & vec![ 0 , 0 ] ) ,
160161 make_instructions( OpSetGlobal , & vec![ 0 ] ) ,
161162 make_instructions( OpGetGlobal , & vec![ 0 ] ) ,
162163 make_instructions( OpConst , & vec![ 1 ] ) ,
@@ -167,7 +168,7 @@ mod tests {
167168 CompilerTestCase {
168169 input: "let manyArg = fn(a, b, c) { a; b; c; }; manyArg(24, 25, 26);" ,
169170 expected_constants: vec![
170- Object :: CompiledFunction ( object:: CompiledFunction {
171+ Object :: CompiledFunction ( Rc :: from ( object:: CompiledFunction {
171172 instructions: concat_instructions( & vec![
172173 make_instructions( OpGetLocal , & vec![ 0 ] ) ,
173174 make_instructions( OpPop , & vec![ 0 ] ) ,
@@ -176,16 +177,16 @@ mod tests {
176177 make_instructions( OpGetLocal , & vec![ 2 ] ) ,
177178 make_instructions( OpReturnValue , & vec![ 0 ] ) ,
178179 ] )
179- . data,
180+ . data,
180181 num_locals: 3 ,
181182 num_parameters: 3 ,
182- } ) ,
183+ } ) ) ,
183184 Object :: Integer ( 24 ) ,
184185 Object :: Integer ( 25 ) ,
185186 Object :: Integer ( 26 ) ,
186187 ] ,
187188 expected_instructions: vec![
188- make_instructions( OpConst , & vec![ 0 ] ) ,
189+ make_instructions( OpClosure , & vec![ 0 , 0 ] ) ,
189190 make_instructions( OpSetGlobal , & vec![ 0 ] ) ,
190191 make_instructions( OpGetGlobal , & vec![ 0 ] ) ,
191192 make_instructions( OpConst , & vec![ 1 ] ) ,
@@ -207,41 +208,41 @@ mod tests {
207208 input: "let num = 55; fn() { num; }" ,
208209 expected_constants: vec![
209210 Object :: Integer ( 55 ) ,
210- Object :: CompiledFunction ( object:: CompiledFunction {
211+ Object :: CompiledFunction ( Rc :: from ( object:: CompiledFunction {
211212 instructions: concat_instructions( & vec![
212213 make_instructions( OpGetGlobal , & vec![ 0 ] ) ,
213214 make_instructions( OpReturnValue , & vec![ 0 ] ) ,
214215 ] )
215- . data,
216+ . data,
216217 num_locals: 0 ,
217218 num_parameters: 0 ,
218- } ) ,
219+ } ) ) ,
219220 ] ,
220221 expected_instructions: vec![
221222 make_instructions( OpConst , & vec![ 0 ] ) ,
222223 make_instructions( OpSetGlobal , & vec![ 0 ] ) ,
223- make_instructions( OpConst , & vec![ 1 ] ) ,
224+ make_instructions( OpClosure , & vec![ 1 , 0 ] ) ,
224225 make_instructions( OpPop , & vec![ 0 ] ) ,
225226 ] ,
226227 } ,
227228 CompilerTestCase {
228229 input: "fn() { let num = 55; num; }" ,
229230 expected_constants: vec![
230231 Object :: Integer ( 55 ) ,
231- Object :: CompiledFunction ( object:: CompiledFunction {
232+ Object :: CompiledFunction ( Rc :: from ( object:: CompiledFunction {
232233 instructions: concat_instructions( & vec![
233234 make_instructions( OpConst , & vec![ 0 ] ) ,
234235 make_instructions( OpSetLocal , & vec![ 0 ] ) ,
235236 make_instructions( OpGetLocal , & vec![ 0 ] ) ,
236237 make_instructions( OpReturnValue , & vec![ 0 ] ) ,
237238 ] )
238- . data,
239+ . data,
239240 num_locals: 1 ,
240241 num_parameters: 0 ,
241- } ) ,
242+ } ) ) ,
242243 ] ,
243244 expected_instructions: vec![
244- make_instructions( OpConst , & vec![ 1 ] ) ,
245+ make_instructions( OpClosure , & vec![ 1 , 0 ] ) ,
245246 make_instructions( OpPop , & vec![ 0 ] ) ,
246247 ] ,
247248 } ,
@@ -250,7 +251,7 @@ mod tests {
250251 expected_constants: vec![
251252 Object :: Integer ( 55 ) ,
252253 Object :: Integer ( 77 ) ,
253- Object :: CompiledFunction ( object:: CompiledFunction {
254+ Object :: CompiledFunction ( Rc :: from ( object:: CompiledFunction {
254255 instructions: concat_instructions( & vec![
255256 make_instructions( OpConst , & vec![ 0 ] ) ,
256257 make_instructions( OpSetLocal , & vec![ 0 ] ) ,
@@ -261,13 +262,13 @@ mod tests {
261262 make_instructions( OpAdd , & vec![ 0 ] ) ,
262263 make_instructions( OpReturnValue , & vec![ 0 ] ) ,
263264 ] )
264- . data,
265+ . data,
265266 num_locals: 2 ,
266267 num_parameters: 0 ,
267- } ) ,
268+ } ) ) ,
268269 ] ,
269270 expected_instructions: vec![
270- make_instructions( OpConst , & vec![ 2 ] ) ,
271+ make_instructions( OpClosure , & vec![ 2 , 0 ] ) ,
271272 make_instructions( OpPop , & vec![ 0 ] ) ,
272273 ] ,
273274 } ,
@@ -295,19 +296,19 @@ mod tests {
295296 } ,
296297 CompilerTestCase {
297298 input: "fn() { len([]) }" ,
298- expected_constants: vec![ Object :: CompiledFunction ( object:: CompiledFunction {
299+ expected_constants: vec![ Object :: CompiledFunction ( Rc :: from ( object:: CompiledFunction {
299300 instructions: concat_instructions( & vec![
300301 make_instructions( OpGetBuiltin , & vec![ 0 ] ) ,
301302 make_instructions( OpArray , & vec![ 0 ] ) ,
302303 make_instructions( OpCall , & vec![ 1 ] ) ,
303304 make_instructions( OpReturnValue , & vec![ 0 ] ) ,
304305 ] )
305- . data,
306+ . data,
306307 num_locals: 0 ,
307308 num_parameters: 0 ,
308- } ) ] ,
309+ } ) ) ] ,
309310 expected_instructions: vec![
310- make_instructions( OpConst , & vec![ 0 ] ) ,
311+ make_instructions( OpClosure , & vec![ 0 , 0 ] ) ,
311312 make_instructions( OpPop , & vec![ 0 ] ) ,
312313 ] ,
313314 } ,
0 commit comments