-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.ml
295 lines (246 loc) · 9.3 KB
/
utils.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
(* Pretty Printer *)
open Ast
open Sast
open Parser
open Processor
let save file string =
let channel = open_out file in
output_string channel string;
close_out channel
let string_of_primitive = function
Int_t -> "int"
| Matrix_t(r, c) -> "mat["^(string_of_int r)^","^(string_of_int c)^"]"
| Float_t -> "float"
| Bool_t -> "bool"
let rec print_brackets = function
1 -> "[]"
| a -> "[]" ^ print_brackets (a - 1)
(*let string_of_primitive = function *)
(*(p) -> (string_of_primitive p)*)
(*| Any -> "Any"*)
(* Print expressions *)
let string_of_op = function
Add -> "+"
| Sub -> "-"
| Inc -> "++"
| Dec -> "--"
| Mult -> "*"
| MMult -> ".*"
| Transpose -> ".~"
| Div -> "/"
| MDiv -> "./"
| Equal -> "=="
| Neq -> "!="
| Less -> "<"
| Leq -> "<="
| Greater -> ">"
| Geq -> ">="
| And -> "and"
| Not -> "not"
| Or -> "or"
| Mod -> "%"
let rec string_of_bracket_expr = function
[] -> ""
| head :: tail -> "[" ^ (string_of_expr head) ^ "]" ^ (string_of_bracket_expr tail)
and string_of_array_primitive = function
[] -> ""
| [last] -> (string_of_expr last)
| head :: tail -> (string_of_expr head) ^ ", " ^ (string_of_array_primitive tail)
and string_of_expr = function
Int_Lit(i) -> string_of_int i
| Boolean_Lit(b) -> if b then "true" else "false"
| Float_Lit(f) -> string_of_float f
| Id(s) -> s
| Binop(e1, o, e2) -> (string_of_expr e1) ^ " " ^ (string_of_op o) ^ " " ^ (string_of_expr e2)
| Assign(e1, e2) -> (string_of_expr e1) ^ " = " ^ (string_of_expr e2)
| Noexpr -> ""
| Call(f, el) -> f ^ "(" ^ String.concat ", " (List.map string_of_expr el) ^ ")"
| Mat_Lit(el) -> "[" ^ (string_of_array_primitive el) ^ "]"
| Unop(op, e) -> (string_of_op op) ^ "(" ^ string_of_expr e ^ ")"
| MIndex(e, i, j) -> string_of_expr e ^ "["^string_of_expr i^","^string_of_expr j^"]"
;;
let string_of_local_expr = function
Noexpr -> ""
| e -> " = " ^ string_of_expr e
(*let string_of_mdecl indent = function*)
(*let indent_string = String.make indent '\t' in*)
(*mdecl(n, r, c, e) -> indent_string ^ "mat "^n^"["^string_of_int r^","^string_of_int c^"]"^ string_of_local_expr e^";\n"*)
let rec string_of_stmt indent =
let indent_string = String.make indent '\t' in
let get_stmt_string = function
Block(stmts) ->
indent_string ^ "{\n" ^
String.concat "" (List.map (string_of_stmt (indent+1)) stmts) ^
indent_string ^ "}\n"
| Expr(expr) ->
indent_string ^ string_of_expr expr ^ ";\n";
| Return(expr) ->
indent_string ^ "return " ^ string_of_expr expr ^ ";\n";
| If(e, s, Block([Expr(Noexpr)])) ->
indent_string ^ "if (" ^ string_of_expr e ^ ")\n" ^
(string_of_stmt (indent+1) s)
| If(e, s1, s2) ->
indent_string ^ "if (" ^ string_of_expr e ^ ")\n" ^
string_of_stmt (indent+1) s1 ^
indent_string ^ "else\n" ^
string_of_stmt (indent+1) s2
| For(e1, e2, e3, s) ->
indent_string ^ "for (" ^ string_of_expr e1 ^ " ; " ^ string_of_expr e2 ^ " ; " ^ string_of_expr e3 ^ ")\n" ^
string_of_stmt (indent) s
| While(e, s) ->
indent_string ^ "while (" ^ string_of_expr e ^ ")\n" ^
string_of_stmt (indent) s
| Break -> indent_string ^ "break;\n"
| Continue -> indent_string ^ "continue;\n"
| Local(d, s, e) -> indent_string ^ string_of_primitive d ^ " " ^ s ^ string_of_local_expr e ^ ";\n"
| MatrixDecl(md) -> "mat decl"
in get_stmt_string
let string_of_fname = function
FName(s) -> s
let string_of_formal = function
(d, s) -> (string_of_primitive d) ^ " " ^ s
| _ -> ""
let string_of_formal_name = function
Formal(_, s) -> s
| _ -> ""
let string_of_func_decl fdecl =
(string_of_primitive fdecl.returnType) ^ " " ^ fdecl.fname ^ " " ^
(*(string_of_primitive fdecl.returnType) ^ " " ^ (string_of_fname fdecl.fname) ^ " " ^ *)
(* Formals *)
"(" ^ String.concat "," (List.map string_of_formal fdecl.formals) ^ ") {\n" ^
String.concat "" (List.map (string_of_stmt 2) fdecl.body) ^
"\t}\n\n"
let rec string_of_include = function
Include(s) -> "include(" ^ s ^ ");\n"
(* Print whole program *)
let string_of_program = function
Program(includes, (*cdecls*)cbody) ->
String.concat "" (List.map string_of_include includes) ^ "\n"
(* Print AST tree representation *)
let includes_tree includes =
`List (List.map (function Include s -> `String s) includes)
(*let map_fields_to_json fields = *)
(*`List (List.map (function Field((*scope,*) datatype, s) -> *)
(*`Assoc [*)
(*("name", `String s);*)
(*("datatype", `String (string_of_primitive datatype));*)
(*]) fields)*)
let map_formals_to_json formals =
`List (List.map (function(d, s) -> `Assoc [
("name", `String s);
("type", `String (string_of_primitive d));
]
) formals)
let rec map_expr_to_json = function
Int_Lit(i) -> `Assoc [("int_lit", `Int i)]
| Boolean_Lit(b) -> `Assoc [("bool_lit", `Bool b)]
| Float_Lit(f) -> `Assoc [("float_lit", `Float f)]
| Id(s) -> `Assoc [("id", `String s)]
| Binop(e1, o, e2) -> `Assoc [("binop", `Assoc [("lhs", map_expr_to_json e1); ("op", `String (string_of_op o)); ("rhs", map_expr_to_json e2)])]
| Assign(e1, e2) -> `Assoc [("assign", `Assoc [("lhs", map_expr_to_json e1); ("op", `String "="); ("rhs", map_expr_to_json e2)])]
| Noexpr -> `String "noexpr"
| Call(f, el) -> `Assoc [("call", `Assoc ([("name", `String f); ("params", `List (List.map map_expr_to_json el)); ]) )]
| Unop(op, e) -> `Assoc [("Unop", `Assoc [("op", `String (string_of_op op)); ("operand", map_expr_to_json e)])]
let rec map_stmt_to_json = function
Block(stmts) -> `Assoc [("block", `List (List.map (map_stmt_to_json) stmts))]
| Expr(expr) -> `Assoc [("expr", map_expr_to_json expr)]
| Return(expr) -> `Assoc [("return", map_expr_to_json expr)]
| If(e, s1, s2) -> `Assoc [("if", `Assoc [("cond", map_expr_to_json e); ("ifbody", map_stmt_to_json s1)]); ("else", map_stmt_to_json s2)]
| For(e1, e2, e3, s) -> `Assoc [("for", `Assoc [("init", map_expr_to_json e1); ("cond", map_expr_to_json e2); ("inc", map_expr_to_json e3); ("body", map_stmt_to_json s)])]
| While(e, s) -> `Assoc [("while", `Assoc [("cond", map_expr_to_json e); ("body", map_stmt_to_json s)])]
| Break -> `String "break"
| Continue -> `String "continue"
| Local(d, s, e) -> `Assoc [("local", `Assoc [("datatype", `String (string_of_primitive d)); ("name", `String s); ("val", map_expr_to_json e)])]
let map_methods_to_json methods =
`List (List.map (fun (fdecl:Ast.fdecl) ->
`Assoc [
("name", `String (fdecl.fname));
(*("name", `String (string_of_fname fdecl.fname));*)
(*("scope", `String (string_of_scope fdecl.scope));*)
("returnType", `String (string_of_primitive fdecl.returnType));
("formals", map_formals_to_json fdecl.formals);
("body", `List (List.map (map_stmt_to_json) fdecl.body));
]) methods)
let map_top_stmt_to_json = function
Function(fdecl) -> `Assoc [("fdecl", `String (string_of_func_decl fdecl))]
| Statement(stmt) -> `Assoc [("stmt", `String ((string_of_stmt 0) stmt))]
let top_statements_list top_statements =
`List (List.map (map_top_stmt_to_json) top_statements)
let print_tree = function
Program(includes, top_statements) ->
`Assoc [("program",
`Assoc([
("includes", includes_tree includes);
("top_statements", top_statements_list top_statements);
(*("classes", cdecls_tree cdecls)*)
])
)]
let string_of_token_no_id = function
LPAREN -> "LPAREN"
| RPAREN -> "RPAREN"
| LBRACE -> "LBRACE"
| RBRACE -> "RBRACE"
| SEMI -> "SEMI"
| COMMA -> "COMMA"
| PLUS -> "PLUS"
| PLUSPLUS -> "PLUSPLUS"
| MINUS -> "MINUS"
| MINUSMINUS -> "MINUSMINUS"
| TRANSPOSE -> "TRANSPOSE"
| TIMES -> "TIMES"
| MTIMES -> "MTIMES"
| DIVIDE -> "DIVIDE"
| MDIVIDE -> "MDIVIDE"
| ASSIGN -> "ASSIGN"
| EQ -> "EQ"
| NEQ -> "NEQ"
| LT -> "LT"
| LEQ -> "LEQ"
| GT -> "GT"
| GEQ -> "GEQ"
| AND -> "AND"
| OR -> "OR"
| NOT -> "NOT"
| DOT -> "DOT"
| LBRACKET -> "LBRACKET"
| RBRACKET -> "RBRACKET"
| BAR -> "BAR"
| IF -> "IF"
| ELSE -> "ELSE"
| FOR -> "FOR"
| WHILE -> "WHILE"
| RETURN -> "RETURN"
| INT -> "INT"
| MATRIX -> "MATRIX"
| FLOAT -> "FLOAT"
| BOOL -> "BOOL"
| CHAR -> "CHAR"
| VOID -> "VOID"
| NULL -> "NULL"
| TRUE -> "TRUE"
| FALSE -> "FALSE"
| CLASS -> "CLASS"
| CONSTRUCTOR -> "CONSTRUCTOR"
| PUBLIC -> "PUBLIC"
| PRIVATE -> "PRIVATE"
| EXTENDS -> "EXTENDS"
| INCLUDE -> "INCLUDE"
| THIS -> "THIS"
| BREAK -> "BREAK"
| CONTINUE -> "CONTINUE"
| NEW -> "NEW"
| INT_LITERAL(i) -> "INT_LITERAL"
| FLOAT_LITERAL(f) -> "FLOAT_LITERAL"
| CHAR_LITERAL(c) -> "CHAR_LITERAL"
| STRING_LITERAL(s) -> "STRING_LITERAL"
| ID(s) -> "ID"
| DELETE -> "DELETE"
| EOF -> "EOF"
let token_list_to_string token_list =
let rec helper = function
(token, line)::tail ->
string_of_token_no_id token ^ " " ^ helper tail
| [] -> "\n"
in helper token_list
let check_sprogram = function
SProgram(includes, stop_stmts) -> ()