9 let string_of_insts xs =
11 String.concat "; \n\t" @@ List.map string_of_instruction xs in
12 Printf.sprintf "[\n\t%s ]\n" ys
14 let assert_equal lhs rhs =
15 OUnit.assert_equal ~printer:Std.dump ~msg:"name"
17 OUnit.assert_equal ~printer:Std.dump ~msg:"params"
18 lhs.params rhs.params;
19 OUnit.assert_equal ~printer:Std.dump ~msg:"return"
20 lhs.return rhs.return;
21 OUnit.assert_equal ~printer:Std.dump ~msg:"flags"
23 OUnit.assert_equal ~printer:string_of_insts ~msg:"instructions"
24 lhs.instructions rhs.instructions;
25 OUnit.assert_equal ~printer:Std.dump ~msg:"traits"
26 lhs.traits rhs.traits;
27 OUnit.assert_equal ~printer:Std.dump ~msg:"exceptions"
28 lhs.exceptions rhs.exceptions
31 {Asm.empty_method with
35 [GetLocal_0;PushScope]@inst@[Pop;ReturnVoid]}
38 {Asm.empty_method with
42 [GetLocal_0;PushScope]@inst@[ReturnVoid]}
45 {Asm.empty_method with
54 QName ((Namespace ""),name)
57 (generate_script [Expr x])
62 (expr [FindPropStrict (qname "print");
64 CallPropLex ((qname "print"),1)])
65 (compile (Call [Var "print";String "Hello"]))
83 (expr [PushString "Thanks for All the Fish"])
84 (compile (String "Thanks for All the Fish"))
86 (* builtin operator *)
89 (expr [PushByte 1;PushByte 2;Add_i;])
90 (compile (Call [Var "+";Int 1;Int 2]))
94 (expr [PushByte 1;PushByte 2;Equals])
95 (compile (Call [Var "=";Int 1;Int 2]))
97 (* complex expression *)
100 (expr [PushByte 1;Pop;PushByte 2])
101 (compile (Block [Int 1;Int 2]))
106 let b = Label.peek 1 in
108 (expr [PushByte 10; PushByte 20;
109 IfNe a; PushByte 0; Jump b;
110 Label a;PushByte 1; Label b])
111 (compile (If ((Call [Var "=";Int 10;Int 20]),Int 0,Int 1)))
116 (expr [PushString "x"; PushByte 1;
117 PushString "y"; PushByte 2;
121 GetProperty (qname "x");
124 GetProperty (qname "y");
126 (compile (Let (["x",Int 1;"y",Int 2],
127 Block [Var "x";Var "y"])))
135 SetProperty (qname "x");
138 (compile (LetRec (["x",Int 42],Block [Int 10])))
147 GetProperty (qname "x");
149 SetProperty (qname "x");
153 (compile (LetRec (["x",Var "x"],Block [Int 42])))
157 (toplevel [NewFunction (inner [] [PushByte 42]);
160 SetProperty (qname "f")])
161 (generate_script @@ compile_string "(define (f) 42)")
163 test define_not_hidden =
165 (toplevel [NewFunction (inner [] [PushByte 42]);GetScopeObject 0;Swap;SetProperty (qname "f");
166 NewFunction (inner [] [PushByte 30]);GetScopeObject 0;Swap;SetProperty (qname "g")])
167 (generate_script @@ compile_string "(define (f) 42) (define (g) 30)")
171 (toplevel [NewFunction (inner [] [PushByte 42]);GetScopeObject 0;Swap;SetProperty (qname "f");
172 NewObject 0;PushWith;
173 NewFunction (inner [] [PushByte 30]);GetScopeObject 1;Swap;SetProperty (qname "f")])
174 (generate_script @@ compile_string "(define (f) 42) (define (f) 30)")
178 (toplevel [NewFunction (inner [] [NewFunction (inner [] [GetLex (qname "x")])]);
181 SetProperty (qname "f")])
182 (generate_script @@ compile_string "(define (f) (lambda () x))")
187 (expr [NewFunction (inner [] [PushByte 42]) ])
188 (compile (Lambda ([],Block [Int 42])))
190 test call_with_args =
192 (expr [NewFunction (inner [0;0] [GetLocal 2])])
193 (compile (Lambda (["x";"y"],Block [Var "y"])))
195 test closure_lambda =
197 (expr [PushString "z"; PushByte 42;
200 NewFunction (inner [] [GetLex (qname "z")]);
202 (compile (Let (["z",Int 42],
203 Lambda ([],Block [Var "z"]))))
208 (expr [FindPropStrict (make_qname "Foo");ConstructProp (make_qname "Foo",0)])
209 (generate_script @@ compile_string "(new Foo)")
213 (expr [FindPropStrict (make_qname "Foo");PushByte 42;ConstructProp (make_qname "Foo",1)])
214 (generate_script @@ compile_string "(new Foo 42)")
218 (expr [GetLex (make_qname "x");PushByte 10;CallProperty (make_qname "foo",1)])
219 (generate_script @@ compile_string "(. x (foo 10))")
223 (expr [GetLex (make_qname "obj");GetProperty (make_qname "x")])
224 (generate_script @@ compile_string "(slot-ref obj x)")
229 GetLex (make_qname "obj");
231 SetProperty (make_qname "x");
233 (generate_script @@ compile_string "(slot-set! obj x 42)")
236 let new_class klass =
238 GetLex klass.Asm.sname;
240 GetLex klass.Asm.sname;
245 InitProperty klass.Asm.cname])
247 let prefix= [GetLocal_0;
251 {Asm.empty_method with
255 Asm.Class (make_qname "Foo");
257 prefix @ [ReturnVoid] }
260 {Asm.empty_method with
264 Asm.Class (make_qname "Foo");
271 {Asm.cname = make_qname "Foo";
272 sname = make_qname "Object";
273 flags_k = [Asm.Sealed];
277 instructions = prefix@[PushByte 10;Pop]@[ReturnVoid] };
280 (generate_script @@ compile_string
281 "(define-class Foo (Object) ())
282 (define-method init ((self Foo)) 10)")
287 {Asm.cname = make_qname "Foo";
288 sname = make_qname "Object";
289 flags_k = [Asm.Sealed];
295 (generate_script @@ compile_string "(define-class Foo (Object) ())")
300 {Asm.cname = make_qname "Foo";
301 sname = make_qname "Object";
302 flags_k = [Asm.Sealed];
307 methods = [{ Asm.empty_method with
308 name = make_qname "f";
309 fun_scope = Asm.Class (make_qname "Foo");
310 instructions = [PushByte 42;ReturnValue] }]})
311 (generate_script @@ compile_string
312 "(define-class Foo (Object) ())
313 (define-method f ((self Foo)) 42)")
317 QName ((Namespace ns),x) in
323 make "flash.text" "Object";
332 instructions = prefix @ [PushByte 42; Pop; ReturnVoid]};
335 (generate_script @@ compile_string
336 "(define-class Foo (flash.text.Object) ())
337 (define-method init ((self Foo)) 42)")
342 {Asm.cname = make_qname "Foo";
343 sname = make_qname "Object";
344 flags_k = [Asm.Sealed];
349 instructions = List.concat [
351 [GetLocal 1; Pop;ReturnVoid] ] };
354 (generate_script @@ compile_string
355 "(define-class Foo (Object) ())
356 (define-method init ((self Foo) x) x)")
361 {Asm.cname = make_qname "Foo";
362 sname = make_qname "Object";
363 flags_k = [Asm.Sealed];
367 instructions = prefix @ [GetLocal 0;Pop;ReturnVoid] };
370 (generate_script @@ compile_string
371 "(define-class Foo (Object) ())
372 (define-method init ((self Foo)) self)")
378 {Asm.cname = make_qname "Foo";
379 sname = make_qname "Object";
380 flags_k = [Asm.Sealed];
385 methods = [{Asm.empty_method with
389 Asm.Class (make_qname "Foo");
393 [GetLocal 1;ReturnValue] }]})
394 (generate_script @@ compile_string "(define-class Foo (Object) ())
395 (define-method f ((self Foo) x) x)")
400 {Asm.cname = make_qname "Foo";
401 sname = make_qname "Object";
402 flags_k = [Asm.Sealed];
406 attributes = [Cpool.make_qname "x";Cpool.make_qname "y"];
408 (generate_script @@ compile_string "(define-class Foo (Object) (x y))")