Improve Java printing and main.

genToString is a fully generic to string function
dealing with arrays, primitive arrays, etc.
This commit is contained in:
Léonard Gérard 2011-11-20 23:09:12 +01:00
parent 25ce5edbd0
commit adc47c536a
3 changed files with 133 additions and 32 deletions

View file

@ -9,7 +9,8 @@
type class_name = Names.qualname (** [qual] is the package name, [Name] is the class name *)
type obj_ident = Idents.var_ident
type constructor_name = Names.qualname (** [Qual] is the enum class name (type), [NAME] is the constructor name *)
(** [Qual] is the enum class name (type), [NAME] is the constructor name *)
type constructor_name = Names.qualname
type const_name = Names.qualname
type method_name = Names.name
type field_name = Names.name
@ -115,7 +116,8 @@ let rec default_value ty = match ty with
let java_pervasive_class c = Names.qualname_of_string ("jeptagon.Pervasives."^c)
let the_java_pervasives = Names.qualname_of_string "jeptagon.Pervasives"
let java_pervasives_name = Names.qualname_of_string "jeptagon.Pervasives"
let java_pervasives = Eclass java_pervasives_name
let mk_var x = Evar x
@ -135,18 +137,21 @@ let mk_methode ?(protection=Ppublic) ?(static=false) ?(args=[]) ?(returns=Tunit)
let mk_classe ?(imports=[]) ?(protection=Ppublic) ?(static=false) ?(fields=[])
?(classes=[]) ?(constrs=[]) ?(methodes=[]) ?(implements=[])
class_name =
{ c_protection = protection; c_static = static; c_name = class_name; c_imports = imports; c_implements = implements;
c_kind = Cgeneric { cd_fields = fields; cd_classs = classes; cd_constructors = constrs; cd_methodes = methodes; } }
{ c_protection = protection; c_static = static; c_name = class_name;
c_imports = imports; c_implements = implements;
c_kind = Cgeneric { cd_fields = fields; cd_classs = classes;
cd_constructors = constrs; cd_methodes = methodes; } }
let mk_enum ?(protection=Ppublic) ?(static=false) ?(imports=[]) ?(implements=[])
constructor_names class_name =
{ c_protection = protection; c_static = static; c_name = class_name; c_imports = imports; c_implements = implements;
{ c_protection = protection; c_static = static; c_name = class_name;
c_imports = imports; c_implements = implements;
c_kind = Cenum(constructor_names) }
let mk_field ?(protection = Ppublic) ?(static = false) ?(final = false) ?(value = None)
ty ident =
{ f_protection = protection; f_static = static; f_final = final; f_type = ty; f_ident = ident; f_value = value }
let mk_field ?(protection = Ppublic) ?(static = false) ?(final = false) ?(value = None) ty ident =
{ f_protection = protection; f_static = static; f_final = final;
f_type = ty; f_ident = ident; f_value = value }
let vds_to_exps vd_l = List.map (fun { vd_ident = x } -> mk_var x) vd_l

View file

@ -5,6 +5,11 @@ open Signature
open Java
open Java_printer
let load_conf () =
(*TODO spill normalize_mem := false ! *)
Compiler_options.do_scalarize := true;
()
(** returns the vd and the pat of a fresh ident from [name] *)
let mk_var ty name =
let id = Idents.gen_var "java_main" name in
@ -61,7 +66,6 @@ let program p =
in
let acts =
let out = Eclass(Names.qualname_of_string "java.lang.System.out") in
let jarrays = Eclass(Names.qualname_of_string "java.util.Arrays") in
let jint = Eclass(Names.qualname_of_string "Integer") in
let jfloat = Eclass(Names.qualname_of_string "Float") in
let jbool = Eclass(Names.qualname_of_string "Boolean") in
@ -100,15 +104,7 @@ let program p =
| Tunit -> Aexp(Emethod_call(e_main, "step", []))
| _ -> Anewvar (vd_ret, Emethod_call(e_main, "step", []))
in
let print_ret = match ty_ret with
| Tunit -> Sstring ""
| Tarray (Tarray _, _) -> Emethod_call(jarrays, "deepToString", [exp_ret])
| Tarray _ -> Emethod_call(jarrays, "toString", [exp_ret])
| Tint -> Emethod_call(jint, "toString", [exp_ret])
| Tfloat -> Emethod_call(jfloat, "toString", [exp_ret])
| Tbool -> Emethod_call(jbool, "toString", [exp_ret])
| _ -> Emethod_call(exp_ret, "toString", [])
in
let print_ret = Emethod_call(java_pervasives, "genToString", [exp_ret]) in
let main_for_loop i =
[call_main; Aexp (Emethod_call(out, "printf",
[Sstring "%d => %s\\n"; Evar i; print_ret]))]

View file

@ -1,5 +1,7 @@
package jeptagon;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.concurrent.Executors;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
@ -8,8 +10,14 @@ import java.util.concurrent.TimeUnit;
public class Pervasives {
public static int between(int i, int m) {
if (i<0) return 0;
else if (i>=m) return m-1;
if (i<0) {
java.lang.System.err.printf("%d ! >= 0\n",i);
return 0;
}
else if (i>=m) {
java.lang.System.err.printf("%d ! < %d\n",i,m);
return m-1;
}
else return i;
}
@ -31,11 +39,50 @@ public class Pervasives {
public V get(long timeout, TimeUnit unit) { return v; }
}
//faster version for primitive arrays using serializing
public static Object copyNd(Object arr) {
if (arr.getClass().isArray()) {
int innerArrayLength = Array.getLength(arr);
Class component = arr.getClass().getComponentType();
Object newInnerArray = Array.newInstance(component, innerArrayLength);
//copy each elem of the array
for (int i = 0; i < innerArrayLength; i++) {
Object elem = copyNd(Array.get(arr, i));
Array.set(newInnerArray, i, elem);
}
return newInnerArray;
} else {
return arr;//cant deep copy an opac object??
}
}
public static String genToString(Object c) {
Class<?> cClass = c.getClass();
if (cClass.isArray()) {
switch (cClass.getName().charAt(1)) { //charAt(0) is '['
case 'B' : return Arrays.toString((byte[]) c);
case 'C' : return Arrays.toString((char[]) c);
case 'D' : return Arrays.toString((double[]) c);
case 'F' : return Arrays.toString((float[]) c);
case 'I' : return Arrays.toString((int[]) c);
case 'J' : return Arrays.toString((long[]) c);
case 'S' : return Arrays.toString((short[]) c);
case 'Z' : return Arrays.toString((boolean[]) c);
default : //L or [
return Arrays.deepToString((Object [])c);
}
} else
return c.toString();
}
public static class Tuple1 {
public final Object c0;
public Tuple1(Object v) {
c0 = v;
}
public String toString() {
return "(" + genToString(c0) + ")";
}
}
public static class Tuple2 {
@ -46,7 +93,7 @@ public class Pervasives {
c1 = v1;
}
public String toString() {
return "(" + c0.toString() + ", " + c1.toString() + ")";
return "(" + genToString(c0) + ", " + genToString(c1) + ")";
}
}
@ -60,7 +107,7 @@ public class Pervasives {
c2 = v2;
}
public String toString() {
return "(" + c0.toString() + ", " + c1.toString() + ", " + c2.toString() + ")";
return "(" + genToString(c0) + ", " + genToString(c1) + ", " + genToString(c2) + ")";
}
}
@ -76,8 +123,8 @@ public class Pervasives {
c3 = v3;
}
public String toString() {
return "(" + c0.toString() + ", " + c1.toString()
+ ", " + c2.toString() + ", " + c3.toString() + ")";
return "(" + genToString(c0) + ", " + genToString(c1)
+ ", " + genToString(c2) + ", " + genToString(c3) + ")";
}
}
@ -95,8 +142,8 @@ public class Pervasives {
c4 = v4;
}
public String toString() {
return "(" + c0.toString() + ", " + c1.toString() + ", " + c2.toString()
+ ", " + c3.toString() + ", " + c4.toString() + ")";
return "(" + genToString(c0) + ", " + genToString(c1) + ", " + genToString(c2)
+ ", " + genToString(c3) + ", " + genToString(c4) + ")";
}
}
@ -116,8 +163,8 @@ public class Pervasives {
c5 = v5;
}
public String toString() {
return "(" + c0.toString() + ", " + c1.toString() + ", " + c2.toString()
+ ", " + c3.toString() + ", " + c4.toString() + ", " + c5.toString() + ")";
return "(" + genToString(c0) + ", " + genToString(c1) + ", " + genToString(c2)
+ ", " + genToString(c3) + ", " + genToString(c4) + ", " + genToString(c5) + ")";
}
}
@ -139,9 +186,63 @@ public class Pervasives {
c6 = v6;
}
public String toString() {
return "(" + c0.toString() + ", " + c1.toString() + ", " + c2.toString()
+ ", " + c3.toString() + ", " + c4.toString() + ", " + c5.toString()
+ ", " + c6.toString() + ")";
return "(" + genToString(c0) + ", " + genToString(c1) + ", " + genToString(c2)
+ ", " + genToString(c3) + ", " + genToString(c4) + ", " + genToString(c5)
+ ", " + genToString(c6) + ")";
}
}
public static class Tuple8 {
public final Object c0;
public final Object c1;
public final Object c2;
public final Object c3;
public final Object c4;
public final Object c5;
public final Object c6;
public final Object c7;
public Tuple8(Object v0, Object v1, Object v2, Object v3, Object v4, Object v5, Object v6, Object v7) {
c0 = v0;
c1 = v1;
c2 = v2;
c3 = v3;
c4 = v4;
c5 = v5;
c6 = v6;
c7 = v7;
}
public String toString() {
return "(" + genToString(c0) + ", " + genToString(c1) + ", " + genToString(c2)
+ ", " + genToString(c3) + ", " + genToString(c4) + ", " + genToString(c5)
+ ", " + genToString(c6) + ", " + genToString(c7) + ")";
}
}
public static class Tuple9 {
public final Object c0;
public final Object c1;
public final Object c2;
public final Object c3;
public final Object c4;
public final Object c5;
public final Object c6;
public final Object c7;
public final Object c8;
public Tuple9(Object v0, Object v1, Object v2, Object v3, Object v4, Object v5, Object v6, Object v7, Object v8) {
c0 = v0;
c1 = v1;
c2 = v2;
c3 = v3;
c4 = v4;
c5 = v5;
c6 = v6;
c7 = v7;
c8 = v8;
}
public String toString() {
return "(" + genToString(c0) + ", " + genToString(c1) + ", " + genToString(c2)
+ ", " + genToString(c3) + ", " + genToString(c4) + ", " + genToString(c5)
+ ", " + genToString(c6) + ", " + genToString(c7) + ", " + genToString(c8) + ")";
}
}
@ -155,5 +256,4 @@ public class Pervasives {
return x;
}
}