OSDN Git Service

Support class compile.
authorMIZUNO Hiroki <mzpppp@gmail.com>
Fri, 15 Aug 2008 00:42:30 +0000 (09:42 +0900)
committerMIZUNO Hiroki <mzpppp@gmail.com>
Fri, 15 Aug 2008 00:42:30 +0000 (09:42 +0900)
- We can generate valid abc with class. But cannot run it.

src/abc.ml
src/asm.ml
src/codegen.ml
src/instruction.ml
test/test_asm.ml

index 23184cd..f117d18 100644 (file)
@@ -232,8 +232,8 @@ let bytes_of_abc { cpool=cpool;
     array bytes_of_method_info info;
     bytes_of_list metadata;
     (* todo: instances *)
-    array bytes_of_class classes;
-    HList.concat_map  bytes_of_instance instances;
+    array bytes_of_instance instances;
+    HList.concat_map bytes_of_class classes;
     array bytes_of_script script;
     array bytes_of_method_body body
   ]
index 67f66a9..3f0a71a 100644 (file)
@@ -45,10 +45,12 @@ let fold_instruction f init =
 (**{6 Collecting some information}*)
 
 (** [collect_const meth] returns all constant value which contained by [meth]. *)
-let collect_const =
-  fold_instruction 
+let collect_const meth=
+  Cpool.append
+    (method_const meth)
+    @@ fold_instruction 
     (fun cpool i-> Cpool.append cpool (get_config i).const)
-    Cpool.empty
+    Cpool.empty meth
 
 
 (** [collect_klass meth] returns all class which contained by [meth]. *)
@@ -95,7 +97,7 @@ let asm_method map index m =
   let info =
     { Abc.params=m.params; 
       Abc.return=m.return; 
-      Abc.name=index; 
+      Abc.name=Cpool.multiname_nget m.name map.cpool;
       Abc.flags=m.flags } in
   let body =
     { Abc.method_sig=index;
index aa34cf8..0001209 100644 (file)
@@ -104,7 +104,7 @@ let rec generate_expr expr env =
          cinit     = make_meth "cinit" [];
          iinit     = init;
          interface = [];
-         methods   = List.map snd methods;
+         methods   = List.map snd @@ List.remove_assoc "init" methods;
        } in
          [NewClass klass]
     | Var name ->
index 5454440..13c37c6 100644 (file)
@@ -75,7 +75,6 @@ let klass_const {cname=cname;
         | _ -> failwith "must not happen"
      with Not_found -> Cpool.empty];
     List.map multiname [cname;sname];
-    List.map method_const methods]
+    List.map method_const (cinit::iinit::methods)]
     
-
 #include "match.ml"
index 73c18be..f1b07d1 100644 (file)
@@ -25,21 +25,21 @@ test asm =
     assert_equal {Abc.empty_cpool with
                    Abc.int = [~-42];
                    Abc.uint = [42];
-                   Abc.string=["";"print"];
+                   Abc.string=["";"main";"print"];
                    Abc.namespace = [{Abc.kind=0x08; Abc.ns_name=1}];
-                   Abc.multiname = [Abc.QName (1,2)]} cpool;
-    assert_equal [{Abc.params=[]; Abc.return=0; Abc.name=0; Abc.flags=0}] info;
-    assert_equal [{Abc.method_sig=0; 
+                   Abc.multiname = [Abc.QName (1,2);Abc.QName (1,3)]} cpool;
+    assert_equal [{Abc.params=[]; Abc.return=0; Abc.name=1; Abc.flags=0}] info;
+    assert_equal [{Abc.method_sig=0;
                   Abc.max_stack=3; 
                   Abc.local_count=1; 
                   Abc.init_scope_depth=0;
                   Abc.max_scope_depth=1;
                   Abc.code=[u8 208;
                             u8 48;
-                            u8 93; u30 1;
+                            u8 93; u30 2;
                             u8 45; u30 1; (* pushint *)
                             u8 46; u30 1; (* pushuint *)
-                            u8 76; u30 1; u30 1;
+                            u8 76; u30 2; u30 1;
                             u8 41;
                             u8 71];
                   Abc.exceptions=[];
@@ -50,12 +50,13 @@ test collect_const =
     List.fold_left Cpool.append Cpool.empty 
       [string "hoge";
        int 42;
-       multiname @@ make_qname "f"] in
+       multiname @@ make_qname "f";
+       multiname @@ make_qname "g"] in
   let meth  =
-    make_meth "" [
+    make_meth "g" [
       PushInt 42;
       NewFunction (make_meth "f" [PushString "hoge"])] in
-    OUnit.assert_equal ~printer:to_string cpool (collect_const meth)
+    assert_equal (Cpool.to_abc cpool) (Cpool.to_abc (collect_const meth))
 
 module Set = Core.Std.Set