Skip to content

Commit

Permalink
rename names
Browse files Browse the repository at this point in the history
  • Loading branch information
lijunsong committed Feb 16, 2015
1 parent 9937c9e commit f2266e0
Show file tree
Hide file tree
Showing 19 changed files with 288 additions and 627 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ let rec related_ids id (env : env) (curr_used : IdSet.t) : IdSet.t =
with _ -> IdSet.add id curr_used

(* eliminate unused ids in environment *)
let new_env_clean (exp : exp) : exp =
let rec env_clean_rec (e : exp) (env : env) (used_ids : IdSet.t) : (exp * IdSet.t) =
let clean_env (exp : exp) : exp =
let rec clean_rec (e : exp) (env : env) (used_ids : IdSet.t) : (exp * IdSet.t) =
let rec handle_option (opt : exp option) env used_ids : exp option * IdSet.t =
match opt with
| Some (e) ->
let new_e, used_ids = env_clean_rec e env used_ids in
let new_e, used_ids = clean_rec e env used_ids in
Some (new_e), used_ids
| None -> None, used_ids
in
Expand All @@ -105,12 +105,12 @@ let new_env_clean (exp : exp) : exp =
extensible = attrs.extensible } in
let handle_prop (p : 'a) env used_ids : ('a * IdSet.t) = match p with
| (s, Data(data, enum, config)) ->
let value, used_ids = env_clean_rec data.value env used_ids in
let value, used_ids = clean_rec data.value env used_ids in
(s, Data({value = value; writable = data.writable},
enum, config)), used_ids
| (s, Accessor (acc, enum, config)) ->
let getter, used_ids = env_clean_rec acc.getter env used_ids in
let setter, used_ids = env_clean_rec acc.setter env used_ids in
let getter, used_ids = clean_rec acc.getter env used_ids in
let setter, used_ids = clean_rec acc.setter env used_ids in
(s, Accessor ({getter = getter; setter = setter},
enum, config)), used_ids
in
Expand All @@ -125,33 +125,33 @@ let new_env_clean (exp : exp) : exp =
Object (p, new_attrs, prop_list), used_ids

| GetAttr (p, pattr, obj, field) ->
let o, used_ids= env_clean_rec obj env used_ids in
let fld, used_ids = env_clean_rec field env used_ids in
let o, used_ids= clean_rec obj env used_ids in
let fld, used_ids = clean_rec field env used_ids in
GetAttr (p, pattr, o, fld), used_ids

| SetAttr (p, attr, obj, field, newval) ->
if useless_obj_set obj field env used_ids then begin
dprint "clean SetAttr: %s\n" (EU.ljs_str e);
Undefined Pos.dummy, used_ids
end else
let o, used_ids = env_clean_rec obj env used_ids in
let f, used_ids = env_clean_rec field env used_ids in
let v, used_ids = env_clean_rec newval env used_ids in
let o, used_ids = clean_rec obj env used_ids in
let f, used_ids = clean_rec field env used_ids in
let v, used_ids = clean_rec newval env used_ids in
SetAttr (p, attr, o, f, v), used_ids

| GetObjAttr (p, oattr, obj) ->
let o, used_ids = env_clean_rec obj env used_ids in
let o, used_ids = clean_rec obj env used_ids in
GetObjAttr(p, oattr, o), used_ids

| SetObjAttr (p, oattr, obj, attre) ->
let o, used_ids = env_clean_rec obj env used_ids in
let attr, used_ids = env_clean_rec attre env used_ids in
let o, used_ids = clean_rec obj env used_ids in
let attr, used_ids = clean_rec attre env used_ids in
SetObjAttr (p, oattr, o, attr), used_ids

| GetField (p, obj, fld, args) ->
let o, used_ids = env_clean_rec obj env used_ids in
let f, used_ids = env_clean_rec fld env used_ids in
let a, used_ids = env_clean_rec args env used_ids in
let o, used_ids = clean_rec obj env used_ids in
let f, used_ids = clean_rec fld env used_ids in
let a, used_ids = clean_rec args env used_ids in
let used_ids = match obj, fld with
| Id (_, "%context"), String (_, id) ->
(dprint "use point %s\n" id; IdSet.add id used_ids)
Expand All @@ -165,40 +165,40 @@ let new_env_clean (exp : exp) : exp =
Undefined Pos.dummy, used_ids
end
else
let o, used_ids = env_clean_rec obj env used_ids in
let f, used_ids = env_clean_rec fld env used_ids in
let v, used_ids = env_clean_rec newval env used_ids in
let a, used_ids = env_clean_rec args env used_ids in
let o, used_ids = clean_rec obj env used_ids in
let f, used_ids = clean_rec fld env used_ids in
let v, used_ids = clean_rec newval env used_ids in
let a, used_ids = clean_rec args env used_ids in
SetField (p, o, f, v, a), used_ids

| DeleteField (p, obj, fld) ->
let o, used_ids = env_clean_rec obj env used_ids in
let f, used_ids = env_clean_rec fld env used_ids in
let o, used_ids = clean_rec obj env used_ids in
let f, used_ids = clean_rec fld env used_ids in
DeleteField (p, o, f), used_ids

| OwnFieldNames (p, obj) ->
let o, used_ids = env_clean_rec obj env used_ids in
let o, used_ids = clean_rec obj env used_ids in
OwnFieldNames (p, o), used_ids

| SetBang (p, x, x_v) ->
let x_v, used_ids = env_clean_rec x_v env used_ids in
let x_v, used_ids = clean_rec x_v env used_ids in
let used_ids = IdSet.add x used_ids in
dprint "find usage point at SetBang %s\n" x;
SetBang (p, x, x_v), used_ids

| Op1 (p, op, e) ->
let e, used_ids = env_clean_rec e env used_ids in
let e, used_ids = clean_rec e env used_ids in
Op1 (p, op, e), used_ids

| Op2 (p, op, e1, e2) ->
let e1, used_ids = env_clean_rec e1 env used_ids in
let e2, used_ids = env_clean_rec e2 env used_ids in
let e1, used_ids = clean_rec e1 env used_ids in
let e2, used_ids = clean_rec e2 env used_ids in
Op2 (p, op, e1, e2), used_ids

| If (p, cond, thn, els) -> (* more optimization in constant folding *)
let cond, used_ids = env_clean_rec cond env used_ids in
let thn, used_ids = env_clean_rec thn env used_ids in
let els, used_ids = env_clean_rec els env used_ids in
let cond, used_ids = clean_rec cond env used_ids in
let thn, used_ids = clean_rec thn env used_ids in
let els, used_ids = clean_rec els env used_ids in
If (p, cond, thn, els), used_ids

| App (p, f, args) ->
Expand All @@ -216,20 +216,20 @@ let new_env_clean (exp : exp) : exp =
dprint "clean app: %s\n" (EU.ljs_str e);
Undefined Pos.dummy, used_ids
end else
let f, used_ids = env_clean_rec f env used_ids in
let f, used_ids = clean_rec f env used_ids in
let rec handle_args args env used_ids = match args with
| [] -> args, used_ids
| fst :: rest ->
let v, used_ids = env_clean_rec fst env used_ids in
let v, used_ids = clean_rec fst env used_ids in
let rest_v, used_ids = handle_args rest env used_ids in
v :: rest_v, used_ids
in
let args, used_ids = handle_args args env used_ids in
App (p, f, args), used_ids

| Seq (p, e1, e2) ->
let new_e2, e2_used_ids = env_clean_rec e2 env used_ids in
let new_e1, e1_used_ids = env_clean_rec e1 env e2_used_ids in
let new_e2, e2_used_ids = clean_rec e2 env used_ids in
let new_e1, e1_used_ids = clean_rec e1 env e2_used_ids in
let e1_is_lambda = match new_e1 with Lambda (_,_,_) -> true | _ -> false in
if e1_is_lambda || not (EU.has_side_effect new_e1) then
new_e2, e1_used_ids
Expand All @@ -238,7 +238,7 @@ let new_env_clean (exp : exp) : exp =

| Let (p, x, x_v, body) ->
let new_env = IdMap.add x x_v env in
let new_body, used_ids = env_clean_rec body new_env used_ids in
let new_body, used_ids = clean_rec body new_env used_ids in
if not (IdSet.mem x used_ids) then begin
dprint "Clean Let(%s=..)\n" x;
new_body, used_ids
Expand All @@ -249,7 +249,7 @@ let new_env_clean (exp : exp) : exp =

| Rec (p, x, lambda, body) ->
let new_env = IdMap.add x lambda env in
let new_body, used_ids = env_clean_rec body new_env used_ids in
let new_body, used_ids = clean_rec body new_env used_ids in
if not (IdSet.mem x used_ids) then begin
dprint "Clean Rec(%s=..)\n" x;
new_body, used_ids
Expand All @@ -260,39 +260,39 @@ let new_env_clean (exp : exp) : exp =
Rec (p, x, lambda, new_body), IdSet.union lambda_ids used_ids

| Label (p, l, e) ->
let new_e, used_ids = env_clean_rec e env used_ids in
let new_e, used_ids = clean_rec e env used_ids in
Label (p, l, new_e), used_ids

| Break (p, l, e) ->
let new_e, used_ids = env_clean_rec e env used_ids in
let new_e, used_ids = clean_rec e env used_ids in
Break (p, l, new_e), used_ids

| TryCatch (p, body, catch) ->
let b, used_ids = env_clean_rec body env used_ids in
let c, used_ids = env_clean_rec catch env used_ids in
let b, used_ids = clean_rec body env used_ids in
let c, used_ids = clean_rec catch env used_ids in
TryCatch (p, b, c), used_ids

| TryFinally (p, body, fin) ->
let b, used_ids = env_clean_rec body env used_ids in
let f, used_ids = env_clean_rec fin env used_ids in
let b, used_ids = clean_rec body env used_ids in
let f, used_ids = clean_rec fin env used_ids in
TryFinally (p, b, f), used_ids

| Throw (p, e) ->
let e, used_ids = env_clean_rec e env used_ids in
let e, used_ids = clean_rec e env used_ids in
Throw(p, e), used_ids

| Lambda (p, xs, body) ->
(* lambda is only for collecting free vars. however,
`with` expression will be desugared to fun(e) and the
lambda contains variables like %context['TypeError'] *)
let body, used_ids = env_clean_rec body env used_ids in
let body, used_ids = clean_rec body env used_ids in
let used_ids = IdSet.diff used_ids (IdSet.from_list xs) in
Lambda (p, xs, body), used_ids

| Hint (p, id, e) ->
let new_e, used_ids = env_clean_rec e env used_ids in
let new_e, used_ids = clean_rec e env used_ids in
Hint (p, id, new_e), used_ids

in
let new_exp, new_ids = env_clean_rec exp IdMap.empty IdSet.empty in
let new_exp, new_ids = clean_rec exp IdMap.empty IdSet.empty in
new_exp
Loading

0 comments on commit f2266e0

Please sign in to comment.