Java main pretty print

This commit is contained in:
Léonard Gérard 2011-03-23 20:31:26 +01:00
parent 9714cf0be9
commit 0fc0c3ba5b
3 changed files with 156 additions and 123 deletions

View file

@ -72,3 +72,4 @@ let rec field_assoc f = function
else field_assoc f l

View file

@ -1,7 +1,9 @@
open Misc
open Signature
open Java
open Java_printer
(** 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
@ -28,24 +30,31 @@ let program p =
let vd_step, pat_step = mk_var Tint "step" in
let vd_args, pat_args = mk_var (Tarray (Tclass (Names.pervasives_qn "String"), (Sint 0))) "args" in
let body =
let vd_main, e_main, q_main =
let q_main = !Compiler_options.simulation_node |> Modules.qualify_value |> Obc2java.qualname_to_package_classe
in let id = Idents.gen_var "java_main" "main" in
mk_var_dec id (Tclass q_main), Eval (Pvar id), q_main
let vd_main, e_main, q_main, ty_main =
let q_main = !Compiler_options.simulation_node |> Modules.qualify_value in (*qual*)
let ty_main = (Modules.find_value q_main).node_outputs |> types_of_arg_list |> Types.prod in
let q_main = Obc2java.qualname_to_package_classe q_main in (*java qual*)
let id = Idents.gen_var "java_main" "main" in
mk_var_dec id (Tclass q_main), Eval (Pvar id), q_main, ty_main
in
let acts =
let integer = Eval(Pclass(Names.pervasives_qn "Integer")) in
let args1 = Eval(Parray_elem(pat_args, Sint 1)) in
let out = Eval(Pclass(Names.qualname_of_string "java.lang.System.out")) in
let jarrays = Eval(Pclass(Names.qualname_of_string "java.util.Arrays")) in
let ret = Emethod_call(e_main, "step", []) in
let print_ret = match ty_main with
| Types.Tarray (Types.Tarray _, _) -> Emethod_call(jarrays, "deepToString", [ret])
| Types.Tarray _ -> Emethod_call(jarrays, "toString", [ret])
| _ -> Emethod_call(ret, "toString", [])
in
[ Anewvar(vd_main, Enew (Tclass q_main, []));
Aifelse( Efun(Names.pervasives_qn ">", [Eval (Pfield (pat_args, "length")); Sint 1])
, mk_block [Aassgn(pat_step, Emethod_call(integer, "parseInt", [args1]))]
, mk_block [Aassgn(pat_step, Eval (Pvar id_step_dnb))]);
Obc2java.fresh_for (Eval pat_step)
(fun i ->
[ Amethod_call(out, "printf", [ Sstring "%d => %s\\n";
Eval (Pvar i);
Emethod_call(Emethod_call(e_main, "step", []), "toString", [])])]
[ Amethod_call(out, "printf", [ Sstring "%d => %s\\n"; Eval (Pvar i); print_ret]) ]
)
]
in

View file

@ -13,124 +13,147 @@ public class Pervasives {
else return i;
}
public static final ExecutorService executor_cached = Executors.newCachedThreadPool();
public static final ExecutorService executor_cached = Executors.newCachedThreadPool();
public static class StaticFuture<V> implements Future<V> {
V v;
public StaticFuture(V v) { this.v = v; }
public boolean cancel(boolean mayInterruptIfRunning) { return false; }
public boolean isCancelled() { return false; }
public boolean isDone() { return true; }
public V get() { return v; }
public V get(long timeout, TimeUnit unit) { return v; }
}
public static class Tuple1 {
public final Object c0;
public Tuple1(Object v) {
c0 = v;
}
}
public static class Tuple2 {
public final Object c0;
public final Object c1;
public Tuple2(Object v0, Object v1) {
c0 = v0;
c1 = v1;
}
}
public static class Tuple3 {
public final Object c0;
public final Object c1;
public final Object c2;
public Tuple3(Object v0, Object v1, Object v2) {
c0 = v0;
c1 = v1;
c2 = v2;
}
}
public static class Tuple4 {
public final Object c0;
public final Object c1;
public final Object c2;
public final Object c3;
public Tuple4(Object v0, Object v1, Object v2, Object v3) {
c0 = v0;
c1 = v1;
c2 = v2;
c3 = v3;
}
}
public static class Tuple5 {
public final Object c0;
public final Object c1;
public final Object c2;
public final Object c3;
public final Object c4;
public Tuple5(Object v0, Object v1, Object v2, Object v3, Object v4) {
c0 = v0;
c1 = v1;
c2 = v2;
c3 = v3;
c4 = v4;
}
}
public static class Tuple6 {
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 Tuple6(Object v0, Object v1, Object v2, Object v3, Object v4, Object v5) {
c0 = v0;
c1 = v1;
c2 = v2;
c3 = v3;
c4 = v4;
c5 = v5;
}
}
public static class Tuple7 {
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 Tuple7(Object v0, Object v1, Object v2, Object v3, Object v4, Object v5, Object v6) {
c0 = v0;
c1 = v1;
c2 = v2;
c3 = v3;
c4 = v4;
c5 = v5;
c6 = v6;
}
}
public static class StaticFuture<V> implements Future<V> {
V v;
public static int do_stuff(int coeff) {
int x = 13;
for (int i = 0; i < coeff; i++) {
for (int j = 0; j < 1000000; j++) {
x = (x + j) % (x + j/x) + 13;
}
}
return x;
}
public StaticFuture(V v) { this.v = v; }
public boolean cancel(boolean mayInterruptIfRunning) { return false; }
public boolean isCancelled() { return false; }
public boolean isDone() { return true; }
public V get() { return v; }
public V get(long timeout, TimeUnit unit) { return v; }
}
public static class Tuple1 {
public final Object c0;
public Tuple1(Object v) {
c0 = v;
}
}
public static class Tuple2 {
public final Object c0;
public final Object c1;
public Tuple2(Object v0, Object v1) {
c0 = v0;
c1 = v1;
}
public String toString() {
return "(" + c0.toString() + ", " + c1.toString() + ")";
}
}
public static class Tuple3 {
public final Object c0;
public final Object c1;
public final Object c2;
public Tuple3(Object v0, Object v1, Object v2) {
c0 = v0;
c1 = v1;
c2 = v2;
}
public String toString() {
return "(" + c0.toString() + ", " + c1.toString() + ", " + c2.toString() + ")";
}
}
public static class Tuple4 {
public final Object c0;
public final Object c1;
public final Object c2;
public final Object c3;
public Tuple4(Object v0, Object v1, Object v2, Object v3) {
c0 = v0;
c1 = v1;
c2 = v2;
c3 = v3;
}
public String toString() {
return "(" + c0.toString() + ", " + c1.toString()
+ ", " + c2.toString() + ", " + c3.toString() + ")";
}
}
public static class Tuple5 {
public final Object c0;
public final Object c1;
public final Object c2;
public final Object c3;
public final Object c4;
public Tuple5(Object v0, Object v1, Object v2, Object v3, Object v4) {
c0 = v0;
c1 = v1;
c2 = v2;
c3 = v3;
c4 = v4;
}
public String toString() {
return "(" + c0.toString() + ", " + c1.toString() + ", " + c2.toString()
+ ", " + c3.toString() + ", " + c4.toString() + ")";
}
}
public static class Tuple6 {
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 Tuple6(Object v0, Object v1, Object v2, Object v3, Object v4, Object v5) {
c0 = v0;
c1 = v1;
c2 = v2;
c3 = v3;
c4 = v4;
c5 = v5;
}
public String toString() {
return "(" + c0.toString() + ", " + c1.toString() + ", " + c2.toString()
+ ", " + c3.toString() + ", " + c4.toString() + ", " + c5.toString() + ")";
}
}
public static class Tuple7 {
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 Tuple7(Object v0, Object v1, Object v2, Object v3, Object v4, Object v5, Object v6) {
c0 = v0;
c1 = v1;
c2 = v2;
c3 = v3;
c4 = v4;
c5 = v5;
c6 = v6;
}
public String toString() {
return "(" + c0.toString() + ", " + c1.toString() + ", " + c2.toString()
+ ", " + c3.toString() + ", " + c4.toString() + ", " + c5.toString()
+ ", " + c6.toString() + ")";
}
}
public static int do_stuff(int coeff) {
int x = 13;
for (int i = 0; i < coeff; i++) {
for (int j = 0; j < 1000000; j++) {
x = (x + j) % (x + j/x) + 13;
}
}
return x;
}
}