forked from cpuex2016D/min-caml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inline_nonrec.ml
40 lines (34 loc) · 1.35 KB
/
inline_nonrec.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
open KNormal
(* 非再帰関数を無差別に展開 *)
let rec g env = function (* インライン展開ルーチン本体 (caml2html: inline_g) *)
| IfEq(x, y, e1, e2) -> IfEq(x, y, g env e1, g env e2)
| IfLE(x, y, e1, e2) -> IfLE(x, y, g env e1, g env e2)
| Let(xt, e1, e2) -> Let(xt, g env e1, g env e2)
| Let_Ref(xt,e1,e2) ->Let_Ref(xt,g env e1,g env e2)
| LetRec({ name = (x, t); args = yts; body = e1 }, e2) -> (* 関数定義の場合 (caml2html: inline_letrec) *)
let env = if (S.mem x (fv e1))(*||(loop_exit e1)(*再帰関数か判定*)*)
then env
else M.add x (yts, e1) env in
LetRec({ name = (x, t); args = yts; body = g env e1}, g env e2)
| App(x, ys) when M.mem x env -> (* 関数適用の場合 (caml2html: inline_app) *)
let (zs, e) = M.find x env in
(*Format.eprintf "inlining %s@." x;*)
let env' =
List.fold_left2
(fun env' (z, t) y -> M.add z y env')
M.empty
zs
ys in
Alpha.g env' e
| LetTuple(xts, y, e) -> LetTuple(xts, y, g env e)
|ForLE(cs,e) ->ForLE(cs,g env e)
|LetPara({pargs=xts;
index =cs
;accum=acc
;pbody=e1},e2) ->
LetPara({pargs=xts;
index =cs
;accum=acc
;pbody=g env e1},g env e2)
| e -> e
let f e =(* Printf.printf "\n";*) g M.empty e