Skip to content

Commit

Permalink
group extractors to avoid code explosion (closes #6322)
Browse files Browse the repository at this point in the history
  • Loading branch information
Simn committed Jun 2, 2017
1 parent 36edba4 commit e533c12
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/typing/matcher.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1038,30 +1038,33 @@ module Compile = struct
let pat_any = (PatAny,null_pos) in
let _,_,ex_subjects,cases,bindings = List.fold_left2 (fun (left,right,subjects,cases,ex_bindings) (case,bindings,patterns) extractor -> match extractor,patterns with
| Some(v,e1,pat,vars), _ :: patterns ->
let patterns = make_offset_list (left + 1) (right - 1) pat pat_any @ patterns in
let rec loop e = match e.eexpr with
| TLocal v' when v' == v -> subject
| _ -> Type.map_expr loop e
in
let e1 = loop e1 in
let bindings = List.map (fun v -> v,subject.epos,subject) vars @ bindings in
let v,ex_bindings = try
let v,_,_ = List.find (fun (_,_,e2) -> Texpr.equal e1 e2) ex_bindings in
v,ex_bindings
begin try
let v,_,_,left,right = List.find (fun (_,_,e2,_,_) -> Texpr.equal e1 e2) ex_bindings in
let ev = mk (TLocal v) v.v_type e1.epos in
let patterns = make_offset_list (left + 1) (right - 1) pat pat_any @ patterns in
(left + 1, right - 1,ev :: subjects,((case,bindings,patterns) :: cases),ex_bindings)
with Not_found ->
let v = alloc_var "_hx_tmp" e1.etype e1.epos in
v.v_meta <- (Meta.Custom ":extractorVariable",[],v.v_pos) :: v.v_meta;
v,(v,e1.epos,e1) :: ex_bindings
in
let ev = mk (TLocal v) v.v_type e1.epos in
(left + 1, right - 1,ev :: subjects,((case,bindings,patterns) :: cases),ex_bindings)
let ex_bindings = (v,e1.epos,e1,left,right) :: ex_bindings in
let patterns = make_offset_list (left + 1) (right - 1) pat pat_any @ patterns in
let ev = mk (TLocal v) v.v_type e1.epos in
(left + 1, right - 1,ev :: subjects,((case,bindings,patterns) :: cases),ex_bindings)
end
| None,pat :: patterns ->
let patterns = make_offset_list 0 num_extractors pat pat_any @ patterns in
(left,right,subjects,((case,bindings,patterns) :: cases),ex_bindings)
| _,[] ->
assert false
) (0,num_extractors,[],[],[]) cases (List.rev extractors) in
let dt = compile mctx ((subject :: List.rev ex_subjects) @ subjects) (List.rev cases) in
let bindings = List.map (fun (a,b,c,_,_) -> (a,b,c)) bindings in
bind mctx bindings dt

let compile ctx match_debug subjects cases p =
Expand Down

0 comments on commit e533c12

Please sign in to comment.