-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.ml
163 lines (137 loc) · 6.91 KB
/
compile.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
open Ast
open Str
open Printf
open Parser
open Helper
module StringMap = Map.Make(String);;
let string_of_type = function
IntType -> "int"
| BooleanType -> "Boolean"
| StringType -> "String"
| DoubleType -> "double"
| _ -> ""
let string_of_var = function
Molecule(s) -> s
let string_of_element = function
Element(e) -> e
let string_of_molecule = function
Molecule(m) -> m
let string_of_op = function
Add -> "+"
| Sub -> "-"
| Mul -> "*"
| Div -> "/"
| Mod -> "%"
let string_of_rop = function
| Gt -> ">"
| Geq -> ">="
| Lt -> "<"
| Leq -> "<="
| Eq -> "=="
| Neq -> "!="
let string_of_re = function
And -> "&&"
| Or -> "||"
let string_of_boolean = function
True -> string_of_bool true
| False -> string_of_bool false
let string_of_element = function
Element(e)-> e
let string_of_molecule = function
Molecule(m)-> m
let string_of_mdecl_balance mdecl = mdecl.mname
let rec string_of_expr = function
Int(i) -> string_of_int i
| Double(d) -> string_of_float d
| Boolean(e1, rop, e2) -> string_of_expr e1 ^ string_of_rop rop ^ string_of_expr e2
| String (s) -> s
| Asn(id, left) -> id ^ " = " ^ (string_of_expr left)
| Call(s,l) -> s ^ "(" ^ String.concat "" (List.map string_of_expr l) ^ ")"
| Access(o,m) -> (string_of_expr o) ^ "." ^ m ^"();"
| Binop (e1, op, e2) ->
(string_of_expr e1) ^ " " ^ (string_of_op op)
^ " " ^ (string_of_expr e2)
| Brela (e1, op, e2) ->
(string_of_expr e1) ^ " " ^ (string_of_re op)
^ " " ^ (string_of_expr e2)
| Noexpr -> ""
| Null -> "NULL"
| Concat(s1, s2) -> string_of_expr s1 ^ "+" ^ string_of_expr s2
| List(elist) -> "[" ^ String.concat ", " (List.map string_of_expr elist) ^ "]"
| Print(s) -> "System.out.println(" ^ string_of_expr s ^ ");"
| Equation(name, rlist, plist) -> "equation " ^ name ^ "{" ^ String.concat "," (List.map string_of_element rlist) ^ "--" ^ String.concat "," (List.map string_of_element plist) ^ "}"
| Mass(num) -> num ^ ".mass()"
| Charge(num) -> num ^ ".charge()"
| Electrons(num) -> num ^ ".electrons()"
| Bracket(e) -> "(" ^ string_of_expr e ^ ")"
| Balance(llist, rlist) -> "Balance(\"" ^ String.concat " , " (List.map string_of_molecule llist) ^ " == " ^ String.concat " , " (List.map string_of_molecule rlist) ^ "\")"
let string_of_edecl edecl = "Element " ^ edecl.name ^ "= new Element(" ^ (string_of_int edecl.mass) ^ "," ^ (string_of_int edecl.electrons) ^ "," ^ (string_of_int edecl.charge) ^ ");\n"
let string_of_mdecl mdecl = "ArrayList<Element> " ^ mdecl.mname ^ "1 = new ArrayList<Element>(Arrays.asList(" ^ String.concat "," (List.map string_of_element mdecl.elements) ^ "));\n" ^
"Molecule " ^ mdecl.mname ^ "= new Molecule("^ mdecl.mname ^ "1);\n"
let string_of_pdecl pdecl = string_of_type pdecl.paramtype ^ " " ^ pdecl.paramname
let string_of_pdecl_list pdecl_list = String.concat "" (List.map string_of_pdecl pdecl_list)
let string_of_vdecl vdecl = string_of_type vdecl.vtype ^ " " ^ vdecl.vname ^ ";\n"
let rec string_of_stmt = function
Block(stmts) ->
"{\n" ^ String.concat "" (List.map string_of_stmt stmts) ^ "}\n"
| Expr(expr) -> string_of_expr expr ^ ";\n"
| Return(expr) -> "return " ^ string_of_expr expr ^ ";\n"
| If(e, s1, s2) -> "if (" ^ string_of_expr e ^ ")\n" ^ (string_of_stmt s1) ^ "\n" ^ "else\n" ^ (string_of_stmt s2) ^ "\n"
| For(e1, e2, e3, s) ->
"for (" ^ string_of_expr e1 ^ " ; " ^ string_of_expr e2 ^ " ; " ^
string_of_expr e3 ^ ") " ^ string_of_stmt s ^ "\n"
| While(e, s) -> "while (" ^ string_of_expr e ^ ") {" ^ (string_of_stmt s) ^ "}\n"
| Print(s) -> "System.out.println(" ^ string_of_expr s ^ ");\n"
| Draw(s, e1, e2, e3, e4, e5, e6, e7, e8) -> "randx = (int) (Math.random()*400); randy = (int) (Math.random()*400); scene.add(new AtomShape(randx, randy," ^ s ^ "," ^
(string_of_int e1) ^ "," ^
(string_of_int e2) ^ "," ^
(string_of_int e3) ^ "," ^
(string_of_int e4) ^ "," ^
(string_of_int e5) ^ "," ^
(string_of_int e6) ^ "," ^
(string_of_int e7) ^ "," ^
(string_of_int e8) ^ "));"
let string_of_vdecl vdecl=
string_of_type vdecl.vtype ^ " " ^ vdecl.vname ^ ";"
let string_of_fdecl fdecl =
if fdecl.fname = "main" then "public static void main(String args[])\n{\n" ^
String.concat "" (List.map string_of_vdecl fdecl.locals) ^
String.concat "" (List.map string_of_edecl fdecl.elements) ^
String.concat "" (List.map string_of_mdecl fdecl.molecules) ^
String.concat "" (List.map string_of_stmt fdecl.body) ^
"}\n"
else
"public static void " ^ fdecl.fname ^ "(" ^ String.concat ", " (List.map string_of_pdecl fdecl.formals) ^ ")\n{\n" ^
String.concat "" (List.map string_of_vdecl fdecl.locals) ^
String.concat "" (List.map string_of_edecl fdecl.elements) ^
String.concat "" (List.map string_of_mdecl fdecl.molecules) ^
String.concat "" (List.map string_of_stmt fdecl.body) ^
"}\n"
let string_of_fdecl_list fdecl_list =
String.concat "" (List.map string_of_fdecl fdecl_list)
let string_of_program (vars, funcs) =
String.concat "" (List.map string_of_vdecl (List.rev vars) ) ^ "\n" ^
String.concat "\n" (List.map string_of_fdecl (List.rev funcs) ) ^ "\n"
let rec charge_sum molecule = match molecule with
| [] -> 0
| hd :: tl -> hd.charge + charge_sum tl;;
let contains s1 s2 =
let re = Str.regexp_string s2
in
try ignore (Str.search_forward re s1 0); true
with Not_found -> false
let program program prog_name =
let graphic_boolean a b =
if (contains (string_of_fdecl_list program) "graphics") then a else b in
let prog_string = Helper.balance_head ^ prog_name ^ Helper.balance_mid ^ (graphic_boolean "public static final SceneComponent scene = new SceneComponent();" "") ^ Helper.balance_mid1 ^ prog_name ^ Helper.balance_mid15 ^ Helper.balance_mid2 ^ (string_of_fdecl_list program) ^ Helper.balance_end in
let out_chan = open_out (Printf.sprintf "%s.java" prog_name) in
ignore(Printf.fprintf out_chan "%s" prog_string);
close_out out_chan;
ignore(Sys.command(Printf.sprintf "javac %s.java" prog_name));
ignore(Sys.command(Printf.sprintf "java %s" prog_name));
if (contains (string_of_fdecl_list program) "graphics") then
let graphics_string = Helper.balance_head ^ "ChemGRAPH extends JFrame" ^ Helper.balance_mid ^"public static final SceneComponent scene = new SceneComponent();" ^ Helper.balance_mid1 ^ "ChemGRAPH" ^ Helper.balance_mid15 ^ "setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(500, 500); add(scene, BorderLayout.CENTER);" ^ Helper.balance_mid2 ^ (string_of_fdecl_list program) ^ Helper.balance_end in
let out_chan = open_out ("ChemGRAPH.java") in
ignore(Printf.fprintf out_chan "%s" graphics_string); close_out out_chan;
(ignore(Sys.command ("javac ChemGRAPH.java SceneEditor.java"));
if (contains (string_of_fdecl_list program) "graphics") then ignore(Sys.command("java SceneEditor")));