-
Notifications
You must be signed in to change notification settings - Fork 2
/
cast.ml
213 lines (197 loc) · 7.79 KB
/
cast.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
type vtype =
Int | Char | Double | Bool | String | Any
type expr =
Tree of expr * expr list
| IntLit of string
| ChrLit of string
| FltLit of string
| StrLit of string
| GetBranch of expr * expr
| GetWidth of expr
| Void
| FunCall of string * expr
| Eq of expr * expr
| Neq of expr * expr
| Lt of expr * expr
| Leq of expr * expr
| Gt of expr * expr
| Geq of expr * expr
| Add of expr * expr
| Minus of expr * expr
| Mul of expr * expr
| Div of expr * expr
| Mod of expr * expr
| Cast of vtype * expr
| Id of string
type stmt =
While of expr * stmt * stmt list
| If of expr * stmt * stmt list
| IfElse of expr * stmt * stmt * stmt list
| FuncDec of string * vtype * string * stmt * stmt list
| VarDec of vtype * string * expr
| Assn of string * expr
| Expr of expr
| Return of expr
| Seq of stmt list
type program = stmt list
let string_of_vtype = function
Int -> "int"
| Char -> "char"
| Double -> "double"
| Bool -> "bool"
| String -> "string"
| Any -> "any"
let rec string_tab n v = if n == 0 then v else string_tab (n-1) ("\t" ^ v)
let rec tree_list_from_string =
function s ->
let len = String.length s in
if (len > 0) then
Tree(ChrLit("'" ^ (Char.escaped (String.get s 0)) ^ "'") , [])::
tree_list_from_string (String.sub s 1 (len - 1))
else
[]
let rec gen_c_expr =
let rec gen_c_tree_list = function n -> function
hd::tl -> "" ^ (gen_c_expr n hd) ^ ", \n" ^ string_tab n (gen_c_tree_list n tl)
| [] -> ""
in
function n ->
function
FunCall(x, y) -> ("" ^ x ^ "(\n" ^ string_tab (n + 1) (gen_c_expr (n+1) y) ^ ")" )
| Tree(IntLit(x), children) ->
"int_treemake(" ^ x ^ ", " ^
if List.length children == 0 then
"NULL)"
else
"\n" ^ string_tab (n+1) (gen_c_tree_list (n+1) children ^ "NULL)")
| Tree(ChrLit(x), children) ->
"char_treemake(" ^ x ^ ", " ^
if List.length children == 0 then
"NULL)"
else
"\n" ^ string_tab (n+1) (gen_c_tree_list (n+1) children ^ "NULL)")
| Tree(FltLit(x), children) ->
"double_treemake(" ^ x ^ ", " ^
if List.length children == 0 then
"NULL)"
else
"\n" ^ string_tab (n+1) (gen_c_tree_list (n+1) children ^ "NULL)")
| Tree(Void, children) ->
"void_treemake(" ^
if List.length children == 0 then
"NULL)"
else
"\n" ^ string_tab (n+1) (gen_c_tree_list (n+1) children ^ "NULL)")
| Tree(StrLit(x), []) -> "void_treemake(\n" ^
string_tab (n+1) (gen_c_tree_list (n+1) (tree_list_from_string x) ^ "NULL)")
| Tree(expr, children) ->
"tree_treemake(" ^ gen_c_expr n expr ^ ", " ^
if List.length children == 0 then
"NULL)"
else
"\n" ^ string_tab (n+1) (gen_c_tree_list (n+1) children ^ "NULL)")
| GetBranch(tree, expr) -> "get_branch_t(" ^ gen_c_expr n tree ^ ", " ^ gen_c_expr n expr ^ ")"
| GetWidth(tree) -> "get_width_t(" ^ gen_c_expr n tree ^ ")"
| Eq(v1, v2) -> ("equal(" ^ gen_c_expr n v1 ^ ", " ^ gen_c_expr n v2 ^ ")")
| Neq(v1, v2) -> ("nequal(" ^ gen_c_expr n v1 ^ ", " ^ gen_c_expr n v2 ^ ")")
| Lt(v1, v2) -> ("lt(" ^ gen_c_expr n v1 ^ ", " ^ gen_c_expr n v2 ^ ")")
| Leq(v1, v2) -> ("lte(" ^ gen_c_expr n v1 ^ ", " ^ gen_c_expr n v2 ^ ")")
| Gt(v1, v2) -> ("gt(" ^ gen_c_expr n v1 ^ ", " ^ gen_c_expr n v2 ^ ")")
| Geq(v1, v2) -> ("gte(" ^ gen_c_expr n v1 ^ ", " ^ gen_c_expr n v2 ^ ")")
| Add(v1, v2) -> ("add(" ^ gen_c_expr n v1 ^ ", " ^ gen_c_expr n v2 ^ ")")
| Minus(v1, v2) -> ("sub(" ^ gen_c_expr n v1 ^ ", " ^ gen_c_expr n v2 ^ ")")
| Mul(v1, v2) -> ("mult(" ^ gen_c_expr n v1 ^ ", " ^ gen_c_expr n v2 ^ ")")
| Div(v1, v2) -> ("divd(" ^ gen_c_expr n v1 ^ ", " ^ gen_c_expr n v2 ^ ")")
| Mod(v1, v2) -> ("mod(" ^ gen_c_expr n v1 ^ ", " ^ gen_c_expr n v2 ^ ")")
| Cast(vt,e) -> ("cast(" ^ string_of_vtype vt ^ ", " ^ gen_c_expr n e ^ ")")
| Id(v1) -> "" ^ v1
| IntLit(x) -> x
| ChrLit(x) -> x
| FltLit(x) -> x
| StrLit(x) -> x
| Void -> ""
let rec gen_c = function n -> function
While(x, y, l) -> string_tab n ("while ("^(gen_c_expr n x) ^ ") { \n" ^ (gen_c (n+1) y) ^ "\n" ^ string_tab n "}")
| If(x, y, l) -> string_tab n ("if ("^(gen_c_expr n x) ^ ") { \n" ^ (gen_c (n+1) y) ^ "\n" ^ string_tab n "}")
| IfElse(x, s1,s2, l) ->
string_tab n ("if ("^(gen_c_expr n x) ^ ") { \n" ^ (gen_c (n+1) s1) ^ "\n" ^ string_tab n "} else {\n"
^ (gen_c (n+1) s2) ^ "\n" ^ string_tab n "}")
| VarDec(v1, v2, v3) -> string_tab n ("struct tree * " ^ v2 ^ " = " ^ gen_c_expr n v3 ^ "; inc_refcount(" ^ v2 ^ ");")
| FuncDec(str, vt, vn, stmt, l) -> "" (*str ^ "(){\n" ^ gen_c (n + 1) stmt ^ "} " *)
| Assn(v1, v2) -> string_tab n ("" ^ v1 ^ " = " ^ gen_c_expr n v2 ^ "; inc_refcount(" ^ v1 ^ ");")
| Expr(v1) -> string_tab n (gen_c_expr n v1)
| Return(v1) -> string_tab n ("return " ^ gen_c_expr n v1 ^ ";")
| Seq(v1) -> let rec gen_c_seq = function
hd::tl -> gen_c n hd ^ ";\n" ^ gen_c_seq tl
| [] -> "" in
gen_c_seq v1
let rec eval_expr = function n ->
let rec eval_tree_list = function n -> function
hd::tl -> eval_expr n hd ^ "::\n" ^ string_tab (n) (eval_tree_list n tl)
| [] -> "[]"
in
function
FunCall(x, y) -> ("FunCall(" ^ x ^ ", \n" ^ string_tab (n+1) ((eval_expr (n+1) y) ^ ")"))
| Tree(StrLit(x), []) -> "Tree(Void, \n" ^ string_tab (n+1) (eval_tree_list (n+1) (tree_list_from_string x) ^ ")")
| Tree(expr, children) ->
"Tree(" ^ eval_expr n expr ^
if List.length children == 0 then
", [])"
else
", \n" ^ string_tab (n+1) (eval_tree_list (n+1) children ^ ")")
| GetBranch(tree, expr) -> "" (* TODO *)
| GetWidth(tree) -> ""
| Eq(v1, v2) -> ("Eq(" ^ eval_expr n v1 ^ ", " ^ eval_expr n v2 ^ ")")
| Neq(v1, v2) -> ("Neq(" ^ eval_expr n v1 ^ ", " ^ eval_expr n v2 ^ ")")
| Lt(v1, v2) -> ("Lt(" ^ eval_expr n v1 ^ ", " ^ eval_expr n v2 ^ ")")
| Leq(v1, v2) -> ("Leq(" ^ eval_expr n v1 ^ ", " ^ eval_expr n v2 ^ ")")
| Gt(v1, v2) -> ("Gt(" ^ eval_expr n v1 ^ ", " ^ eval_expr n v2 ^ ")")
| Geq(v1, v2) -> ("Geq(" ^ eval_expr n v1 ^ ", " ^ eval_expr n v2 ^ ")")
| Add(v1, v2) -> ("Add(" ^ eval_expr n v1 ^ ", " ^ eval_expr n v2 ^ ")")
| Minus(v1, v2) -> ("Minus(" ^ eval_expr n v1 ^ ", " ^ eval_expr n v2 ^ ")")
| Mul(v1, v2) -> ("Mul(" ^ eval_expr n v1 ^ ", " ^ eval_expr n v2 ^ ")")
| Div(v1, v2) -> ("Div(" ^ eval_expr n v1 ^ ", " ^ eval_expr n v2 ^ ")")
| Mod(v1, v2) -> ("Mod(" ^ eval_expr n v1 ^ ", " ^ eval_expr n v2 ^ ")")
| Cast(vt,e) -> ("Cast(" ^ string_of_vtype vt ^ ", " ^ eval_expr n e ^ ")")
| Id(v1) -> "Id(" ^ v1 ^ ")"
| IntLit(x) -> x
| ChrLit(x) -> x
| FltLit(x) -> x
| StrLit(x) -> x
| Void -> "Void"
let rec eval = function n -> function
While(x, y, l) -> string_tab n ("While("^(eval_expr n x) ^ ",\n" ^ (eval (n+1) y) ^ "\n" ^ string_tab n ")")
| If(x, y, l) -> string_tab n ("If("^(eval_expr n x) ^ ",\n" ^ (eval (n+1) y) ^ "\n" ^ string_tab n ")")
| IfElse(x, s1, s2, l) -> string_tab n ("IfElse("^(eval_expr n x) ^ ",\n" ^
(eval (n+1) s1) ^ "\n" ^ string_tab n ")\n" ^
(eval (n+1) s2) ^ "\n" ^ string_tab n ")")
| VarDec(v1, v2, v3) -> string_tab n ("VarDec(" ^ v2 ^ ", " ^ eval_expr n v3 ^ ")")
| FuncDec(str, vt, vn, stmt, l) -> "" (* TODO *)
| Assn(v1, v2) -> string_tab n ("Assn(" ^ v1 ^ ", " ^ eval_expr n v2 ^ ")")
| Expr(v1) -> string_tab n (eval_expr n v1)
| Return(v1) -> string_tab n ("Return(" ^ eval_expr n v1 ^ ")")
| Seq(v1) -> let rec eval_seq = function
hd::tl -> eval n hd ^ "\n" ^ eval_seq tl
| [] -> "" in
eval_seq v1
let rec eval_prog = function
hd::tl -> "" ^ eval 0 hd ^ "\n" ^ eval_prog tl
| [] -> ""
let rec gen_c_prog = function
hd::tl -> "" ^ gen_c 1 hd ^ ";\n" ^ gen_c_prog tl
| [] -> ""
let rec gen_c_funcs = function
FuncDec(str, vt, vn, stmt, l)::tl -> "struct tree *" ^ str ^ "(struct tree *" ^ vn ^ "){" ^
"inc_refcount(" ^ vn ^ ");\n" ^ gen_c 1 stmt ^ "return NULL;}\n" ^ gen_c_funcs tl
| _::tl -> gen_c_funcs tl
| [] -> ""
let headers =
"#include <stdio.h>\n#include <stdlib.h>\n" ^
"#include <stdio.h>\n" ^
"#include <stdlib.h>\n" ^
"#include <string.h>\n" ^
"#include <stdarg.h>\n"
let string_of_program stmts =
"int main(int argc, char **argv) {\n" ^
gen_c_prog stmts ^
"\treturn 0;\n}"