Skip to content

Commit 8b6de7b

Browse files
author
Hongbo Zhang
committed
[refact] using smart constructors for all lambda construction
1 parent 97aef33 commit 8b6de7b

14 files changed

+262
-148
lines changed

.travis-ci-before-script.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ cd ocaml && ./configure -prefix $(dirname $(pwd)) -no-ocamldoc -no-ocamlbuild
66

77
export PATH=$(pwd)/bin:$PATH
88

9-
cd jscomp && make world-test
9+
cd jscomp && make travis-world-test

jscomp/Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,14 @@ world-test:
120120
@echo "Making test"
121121
cd test && make all
122122
@echo "Making test finsihed"
123-
123+
124+
travis-world-test:./bin/ocaml_pack
125+
@echo "Generating the compiler"
126+
rm -f bin/compiler.ml
127+
./bin/ocaml_pack ./compiler.mllib > bin/compiler.ml
128+
@echo "Generating the compiler finished"
129+
make world-test
130+
124131
.PHONY: stdlib
125132
.PHONY: test quicktest release snapshot snapshotcmj
126133

jscomp/lam_beta_reduce.ml

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,17 +109,17 @@ let rewrite (map : (Ident.t, _) Hashtbl.t)
109109
let l1 = aux l1 in
110110
let xs = List.map rebind xs in
111111
let l2 = aux l2 in
112-
Lstaticcatch(l1, (i,xs), l2)
112+
Lam_comb.staticcatch l1 (i,xs) l2
113113
| Lfor(ident, l1, l2, dir, l3) ->
114114
let ident = rebind ident in
115115
let l1 = aux l1 in
116116
let l2 = aux l2 in
117117
let l3 = aux l3 in
118-
Lfor(ident,aux l1, l2, dir, l3)
118+
Lam_comb.for_ ident (aux l1) l2 dir l3
119119
| Lconst _ -> lam
120120
| Lprim(prim, ll) ->
121121
(* here it makes sure that global vars are not rebound *)
122-
Lprim(prim, List.map aux ll)
122+
Lam_comb.prim prim (List.map aux ll)
123123
| Lapply(fn, args, info) ->
124124
let fn = aux fn in
125125
let args = List.map aux args in
@@ -131,25 +131,26 @@ let rewrite (map : (Ident.t, _) Hashtbl.t)
131131
sw_numconsts;
132132
}) ->
133133
let l = aux l in
134-
Lswitch(l,
134+
Lam_comb.switch l
135135
{sw_consts =
136136
List.map (fun (v, l) -> v, aux l) sw_consts;
137137
sw_blocks = List.map (fun (v, l) -> v, aux l) sw_blocks;
138138
sw_numconsts = sw_numconsts;
139139
sw_numblocks = sw_numblocks;
140140
sw_failaction = option_map sw_failaction
141-
})
141+
}
142142
| Lstringswitch(l, sw, d) ->
143143
let l = aux l in
144144
Lam_comb.stringswitch l
145145
(List.map (fun (i, l) -> i,aux l) sw)
146146
(option_map d)
147-
| Lstaticraise (i,ls) -> Lstaticraise(i, List.map aux ls)
147+
| Lstaticraise (i,ls)
148+
-> Lam_comb.staticraise i (List.map aux ls)
148149
| Ltrywith(l1, v, l2) ->
149150
let l1 = aux l1 in
150151
let v = rebind v in
151152
let l2 = aux l2 in
152-
Ltrywith(l1,v, l2)
153+
Lam_comb.try_ l1 v l2
153154
| Lifthenelse(l1, l2, l3) ->
154155
let l1 = aux l1 in
155156
let l2 = aux l2 in
@@ -158,23 +159,25 @@ let rewrite (map : (Ident.t, _) Hashtbl.t)
158159
| Lsequence(l1, l2) ->
159160
let l1 = aux l1 in
160161
let l2 = aux l2 in
161-
Lsequence( l1, l2)
162+
Lam_comb.seq l1 l2
162163
| Lwhile(l1, l2) ->
163164
let l1 = aux l1 in
164165
let l2 = aux l2 in
165-
Lwhile( l1, l2)
166-
| Lassign(v, l) -> Lassign(v,aux l)
166+
Lam_comb.while_ l1 l2
167+
| Lassign(v, l)
168+
-> Lam_comb.assign v (aux l)
167169
| Lsend(u, m, o, ll, v) ->
168170
let m = aux m in
169171
let o = aux o in
170172
let ll = List.map aux ll in
171-
Lsend(u, m, o, ll,v)
173+
Lam_comb.send u m o ll v
172174
| Levent(l, event) ->
173175
let l = aux l in
174-
Levent( l, event)
176+
Lam_comb.event l event
175177
| Lifused(v, l) ->
176178
let l = aux l in
177-
Lifused(v, l) in
179+
Lam_comb.ifused v l
180+
in
178181
aux lam
179182

180183

jscomp/lam_beta_reduce_util.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ let simple_beta_reduce params body args =
7979
let result =
8080
Hashtbl.fold (fun _param {lambda; used} code ->
8181
if not used then
82-
Lambda.Lsequence(lambda, code)
83-
else code) param_hash (Lambda.Lprim (primitive, us)) in
82+
Lam_comb.seq lambda code
83+
else code) param_hash (Lam_comb.prim primitive us) in
8484
Hashtbl.clear param_hash;
8585
Some result
8686
| exception _ ->
@@ -104,7 +104,7 @@ let simple_beta_reduce params body args =
104104
Hashtbl.fold
105105
(fun _param {lambda; used} code ->
106106
if not used then
107-
Lambda.Lsequence(lambda, code)
107+
Lam_comb.seq lambda code
108108
else code )
109109
param_hash (Lambda.Lapply ( f, us , info)) in
110110
Hashtbl.clear param_hash;

jscomp/lam_comb.ml

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,18 @@
2727

2828

2929
type t = Lambda.lambda
30+
3031
type binop = t -> t -> t
32+
3133
type triop = t -> t -> t -> t
34+
3235
type unop = t -> t
33-
let if_ a (b : t) c =
36+
37+
let if_ (a : t) (b : t) c =
3438
match a with
35-
| Lambda.Lconst v ->
39+
| Lconst v ->
3640
begin match v with
37-
| Const_pointer (x, _) | Lambda.Const_base(Const_int x)
41+
| Const_pointer (x, _) | Const_base(Const_int x)
3842
->
3943
if x <> 0 then b else c
4044
| Const_base (Const_char x) ->
@@ -50,14 +54,14 @@ let if_ a (b : t) c =
5054
| Const_float_array _
5155
| Const_immstring _ -> b
5256
end
53-
| _ -> Lambda.Lifthenelse (a,b,c)
57+
| _ -> Lifthenelse (a,b,c)
5458

55-
let switch lam lam_switch =
56-
Lambda.Lswitch(lam,lam_switch)
59+
let switch lam lam_switch : t =
60+
Lswitch(lam,lam_switch)
5761

58-
let stringswitch lam cases default =
62+
let stringswitch (lam : t) cases default : t =
5963
match lam with
60-
| Lambda.Lconst (Lambda.Const_base (Const_string (a,_))) ->
64+
| Lconst (Const_base (Const_string (a,_))) ->
6165
begin
6266
try List.assoc a cases with Not_found ->
6367
begin
@@ -66,27 +70,56 @@ let stringswitch lam cases default =
6670
| None -> assert false
6771
end
6872
end
69-
| _ -> Lambda.Lstringswitch(lam, cases, default)
73+
| _ -> Lstringswitch(lam, cases, default)
7074

7175

72-
let true_ : Lambda.lambda =
76+
let true_ : t =
7377
Lconst (Const_pointer ( 1, Pt_constructor "true"))
7478

75-
let false_ : Lambda.lambda =
79+
let false_ : t =
7680
Lconst (Const_pointer( 0, Pt_constructor "false"))
7781

78-
let unit : Lambda.lambda =
82+
let unit : t =
7983
Lconst (Const_pointer( 0, Pt_constructor "()"))
8084

81-
let not x : t =
82-
Lambda.Lprim (Pnot, [x])
8385

8486
(** [l || r ] *)
8587
let sequor l r = if_ l true_ r
8688

8789
(** [l && r ] *)
8890
let sequand l r = if_ l r false_
8991

92+
let seq a b : t =
93+
Lsequence (a, b)
94+
95+
let while_ a b : t =
96+
Lwhile(a,b)
97+
98+
let try_ body id handler : t =
99+
Ltrywith(body,id,handler)
100+
101+
let for_ v e1 e2 dir e3 : t =
102+
Lfor(v,e1,e2,dir,e3)
103+
104+
let event l (_event : Lambda.lambda_event) = l
105+
106+
let ifused v l : t =
107+
Lifused (v,l)
108+
109+
let assign v l : t = Lassign(v,l)
110+
111+
let send u m o ll v : t =
112+
Lsend(u, m, o, ll, v)
113+
114+
let staticcatch a b c : t = Lstaticcatch(a,b,c)
115+
116+
let staticraise a b : t = Lstaticraise(a,b)
117+
118+
let prim prim ll : t = Lprim(prim,ll)
119+
120+
let not x : t =
121+
prim Pnot [x]
122+
90123
module Prim = struct
91124
type t = Lambda.primitive
92125
let js_is_nil : t =

jscomp/lam_comb.mli

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,28 @@ val unit : t
4242
val sequor : binop
4343
val sequand : binop
4444
val not : unop
45+
val seq : binop
46+
val while_ : binop
47+
val event : t -> Lambda.lambda_event -> t
48+
val try_ : t -> Ident.t -> t -> t
49+
val ifused : Ident.t -> t -> t
50+
val assign : Ident.t -> t -> t
51+
52+
val send :
53+
Lambda.meth_kind ->
54+
t -> t -> t list ->
55+
Location.t -> t
56+
val prim : Lambda.primitive -> t list -> t
57+
val staticcatch :
58+
t -> int * Ident.t list -> t -> t
59+
60+
val staticraise :
61+
int -> t list -> t
62+
63+
val for_ :
64+
Ident.t ->
65+
t ->
66+
t -> Asttypes.direction_flag -> t -> t
4567

4668
module Prim : sig
4769
type t = Lambda.primitive

jscomp/lam_compile_global.ml

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,22 @@ open Js_output.Ops
3838
Make(S), S can not be an exception
3939
*)
4040

41+
42+
(* TODO: add module into taginfo*)
43+
(* let len = List.length sigs in *)
44+
(* TODO: could be optimized *)
45+
4146
let query_lambda id env =
4247
Lam_compile_env.query_and_add_if_not_exist (Lam_module_ident.of_ml id)
4348
(Has_env env)
4449
~not_found:(fun id -> assert false)
4550
~found:(fun {signature = sigs; _} ->
46-
(* TODO: add module into taginfo*)
47-
(* let len = List.length sigs in *)
48-
(* TODO: could be optimized *)
49-
Lambda.Lprim (Pmakeblock(0, Blk_na, Immutable) ,
51+
Lam_comb.prim (Pmakeblock(0, Blk_na, Immutable))
5052
(List.mapi (fun i _ ->
51-
Lambda.Lprim(Pfield (i, Lambda.Fld_na), [Lprim(Pgetglobal id,[])])))
52-
sigs))
53+
Lam_comb.prim (Pfield (i, Lambda.Fld_na))
54+
[Lam_comb.prim (Pgetglobal id) [] ])
55+
sigs)
56+
)
5357

5458

5559
(* Given an module name and position, find its corresponding name *)

jscomp/lam_group.ml

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ let rec flatten
100100
let lambda_of_groups result groups =
101101
List.fold_left (fun acc x ->
102102
match x with
103-
| Nop l -> Lambda.Lsequence(l,acc)
103+
| Nop l -> Lam_comb.seq l acc
104104
| Single(kind,ident,lam) -> Lam_util.refine_let ~kind ident lam acc
105105
| Recursive bindings -> Lletrec (bindings,acc))
106106
result groups
@@ -138,7 +138,9 @@ let deep_flatten
138138
[arg]), body)
139139
->
140140
let id' = Ident.rename id in
141-
flatten acc (Llet (str, id', arg, Llet(Alias, id, Lprim(Pccall p , [Lvar id']), body)))
141+
flatten acc (Llet (str, id', arg,
142+
Llet(Alias, id, Lam_comb.prim (Pccall p) [Lvar id'], body)
143+
))
142144
| Llet (str,id,arg,body) ->
143145
let (res,l) = flatten acc arg in
144146
flatten (Single(str, id, res ) :: l) body
@@ -268,7 +270,7 @@ let deep_flatten
268270
result
269271
(* List.map (fun (id,lam) -> (id, aux lam )) bind_args *),
270272
aux body)) (List.rev wrap)
271-
| Lsequence (l,r) -> Lsequence(aux l, aux r)
273+
| Lsequence (l,r) -> Lam_comb.seq (aux l) (aux r)
272274
| Lconst _ -> lam
273275
| Lvar _ -> lam
274276
(* | Lapply(Lfunction(Curried, params, body), args, _) *)
@@ -310,7 +312,7 @@ let deep_flatten
310312
| Pdirapply loc, [Levent (Lapply (f, args, _),_); x] ->
311313
Lapply (f, args@[x], Lambda.default_apply_info ~loc ())
312314
| Pdirapply loc, [f; x] -> Lapply (f, [x], Lambda.default_apply_info ~loc ())
313-
| _ -> Lprim(p,ll)
315+
| _ -> Lam_comb.prim p ll
314316
end
315317
| Lfunction(kind, params, l) -> Lfunction (kind, params , aux l)
316318
| Lswitch(l, {sw_failaction;
@@ -319,7 +321,7 @@ let deep_flatten
319321
sw_numblocks;
320322
sw_numconsts;
321323
}) ->
322-
Lswitch(aux l,
324+
Lam_comb.switch (aux l)
323325
{sw_consts =
324326
List.map (fun (v, l) -> v, aux l) sw_consts;
325327
sw_blocks = List.map (fun (v, l) -> v, aux l) sw_blocks;
@@ -330,26 +332,36 @@ let deep_flatten
330332
match sw_failaction with
331333
| None -> None
332334
| Some x -> Some (aux x)
333-
end})
335+
end}
334336
| Lstringswitch(l, sw, d) ->
335337
Lam_comb.stringswitch (aux l)
336338
(List.map (fun (i, l) -> i,aux l) sw)
337339
(match d with
338340
| Some d -> Some (aux d )
339341
| None -> None)
340342

341-
| Lstaticraise (i,ls) -> Lstaticraise(i, List.map (aux ) ls)
342-
| Lstaticcatch(l1, (i,x), l2) -> Lstaticcatch(aux l1, (i,x), aux l2)
343-
| Ltrywith(l1, v, l2) -> Ltrywith(aux l1,v, aux l2)
344-
| Lifthenelse(l1, l2, l3) ->
343+
| Lstaticraise (i,ls)
344+
-> Lam_comb.staticraise i (List.map aux ls)
345+
| Lstaticcatch(l1, ids, l2)
346+
->
347+
Lam_comb.staticcatch (aux l1) ids (aux l2)
348+
| Ltrywith(l1, v, l2) ->
349+
Lam_comb.try_ (aux l1) v (aux l2)
350+
| Lifthenelse(l1, l2, l3)
351+
->
345352
Lam_comb.if_ (aux l1) (aux l2) (aux l3)
346-
| Lwhile(l1, l2) -> Lwhile(aux l1, aux l2)
347-
| Lfor(flag, l1, l2, dir, l3) -> Lfor(flag,aux l1, aux l2, dir, aux l3)
353+
| Lwhile(l1, l2)
354+
->
355+
Lam_comb.while_ (aux l1) (aux l2)
356+
| Lfor(flag, l1, l2, dir, l3)
357+
->
358+
Lam_comb.for_ flag (aux l1) (aux l2) dir (aux l3)
348359
| Lassign(v, l) ->
349360
(* Lalias-bound variables are never assigned, so don't increase
350361
v's refaux *)
351-
Lassign(v,aux l)
352-
| Lsend(u, m, o, ll, v) -> Lsend(u, aux m, aux o, List.map aux ll,v)
362+
Lam_comb.assign v (aux l)
363+
| Lsend(u, m, o, ll, v) ->
364+
Lam_comb.send u (aux m) (aux o) (List.map aux ll) v
353365

354366
(* Levent(aux l, event) *)
355367
| Lifused(v, l) -> Lifused(v,aux l)

0 commit comments

Comments
 (0)