- let choose_jump_down a b c d =
- choose_jump a b c d
- (mk_fun (Tree.mk_nil) "Tree.mk_nil")
- (mk_fun (Tree.text_below) "Tree.text_below")
- (mk_fun (fun _ -> Tree.node_child) "[TaggedChild]Tree.node_child") (* !! no tagged_child in Tree.ml *)
- (mk_fun (fun _ -> Tree.node_child) "[SelectChild]Tree.node_child") (* !! no select_child in Tree.ml *)
- (mk_fun (Tree.tagged_desc) "Tree.tagged_desc")
- (mk_fun (fun _ -> Tree.node_child ) "[SelectDesc]Tree.node_child") (* !! no select_desc *)
- (mk_fun (Tree.node_child) "Tree.node_child")
-
- let choose_jump_next a b c d =
- choose_jump a b c d
- (mk_fun (fun t _ -> Tree.mk_nil t) "Tree.mk_nil2")
- (mk_fun (Tree.text_next) "Tree.text_next")
- (mk_fun (fun _ -> Tree.node_sibling_ctx) "[TaggedSibling]Tree.node_sibling_ctx")(* !! no tagged_sibling in Tree.ml *)
- (mk_fun (fun _ -> Tree.node_sibling_ctx) "[SelectSibling]Tree.node_sibling_ctx")(* !! no select_sibling in Tree.ml *)
- (mk_fun (Tree.tagged_foll_below) "Tree.tagged_foll_below")
- (mk_fun (fun _ -> Tree.node_sibling_ctx) "[SelectFoll]Tree.node_sibling_ctx")(* !! no select_foll *)
- (mk_fun (Tree.node_sibling_ctx) "Tree.node_sibling_ctx")
-
- let get_trans slist tag a t =
- try
- Hashtbl.find td_trans (tag,hpl slist)
- with
- | Not_found ->
- let fl_list,llist,rlist,ca,da,sa,fa =
- fold_pl
- (fun set _ (fll_acc,lllacc,rllacc,ca,da,sa,fa) -> (* For each set *)
- let fl,ll,rr,ca,da,sa,fa =
- StateSet.fold
- (fun q acc ->
- List.fold_left
- (fun ((fl_acc,ll_acc,rl_acc,c_acc,d_acc,s_acc,f_acc) as acc)
- (ts,t) ->
- if (TagSet.mem tag ts)
- then
- let _,_,f,_ = Transition.node t in
- let (child,desc,below),(sibl,foll,after) = Formula.st f in
- (Formlist.add t fl_acc,
- StateSet.union ll_acc below,
- StateSet.union rl_acc after,
- StateSet.union child c_acc,
- StateSet.union desc d_acc,
- StateSet.union sibl s_acc,
- StateSet.union foll f_acc)
- else acc ) acc (
- try Hashtbl.find a.trans q
- with
- Not_found -> Printf.eprintf "Looking for state %i, doesn't exist!!!\n%!"
- q;[]
- )
-
- ) set (Formlist.empty,StateSet.empty,StateSet.empty,ca,da,sa,fa)
- in fl::fll_acc, cons ll lllacc, cons rr rllacc,ca,da,sa,fa)
- slist ([],Nil,Nil,StateSet.empty,StateSet.empty,StateSet.empty,StateSet.empty)
- in
- (* Logic to chose the first and next function *)
- let tags_below,tags_after = Tree.tags t tag in
- let first = choose_jump_down tags_below ca da a
- and next = choose_jump_next tags_after sa fa a in
- let v = (fl_list,llist,rlist,first,next) in
- Hashtbl.add td_trans (tag, hpl slist) v; v
+ let is_valid b = (Obj.magic b) != 2
+ let get_replace tab idx default =
+ let e = get tab idx in
+ if e == default then
+ let ne = Array.copy e in (set tab idx ne;ne)
+ else e
+
+ let add h tag fl s1 s2 (data: SList.t*bool*(merge_conf array)) =
+ let l1 = get_replace h tag default_line1 in
+ let l2 = get_replace l1 (Uid.to_int fl.Formlistlist.Node.id) default_line2 in
+ let l3 = get_replace l2 (Uid.to_int s1.SList.Node.id) default_line3 in
+ set l3 (Uid.to_int s2.SList.Node.id) (Obj.repr data)
+ end
+
+
+
+
+ let top_down ?(noright=false) a tree t slist ctx slot_size td_trans h_fold2=
+ let pempty = empty_size slot_size in
+ let rempty = Array.make slot_size RS.empty in
+ (* evaluation starts from the right so we put sl1,res1 at the end *)
+ let eval_fold2_slist fll t tag (sl2,res2) (sl1,res1) =
+ let res = Array.copy rempty in
+ let r,b,btab = Fold2Res.find h_fold2 tag fll sl1 sl2 in
+ if Fold2Res.is_valid b then
+ begin
+ if b then for i=0 to slot_size - 1 do
+ res.(0) <- RS.merge btab.(0) t res1.(0) res2.(0);
+ done;
+ r,res
+ end
+ else
+ begin
+ let btab = Array.make slot_size NO in
+ let rec fold l1 l2 fll i aq ab =
+ match fll.Formlistlist.Node.node,
+ l1.SList.Node.node,
+ l2.SList.Node.node
+ with
+ | Formlistlist.Cons(fl,fll),
+ SList.Cons(s1,ll1),
+ SList.Cons(s2,ll2) ->
+ let r',conf = eval_formlist tag s1 s2 fl in
+ let _ = btab.(i) <- conf
+ in
+ fold ll1 ll2 fll (i+1) (SList.cons r' aq) ((conf!=NO)||ab)
+ | _ -> aq,ab
+ in
+ let r,b = fold sl1 sl2 fll 0 SList.nil false in
+ Fold2Res.add h_fold2 tag fll sl1 sl2 (r,b,btab);
+ if b then for i=0 to slot_size - 1 do
+ res.(i) <- RS.merge btab.(i) t res1.(i) res2.(i);
+ done;
+ r,res;
+ end
+ in
+
+ let null_result = (pempty,Array.copy rempty) in
+ let empty_res = null_result in
+
+ let rec loop t ctx slist _ =
+ if t == Tree.nil then null_result else
+ let tag = Tree.tag tree t in
+ (CodeCache.get_fun td_trans slist tag) t ctx slist tag false
+ (* get_trans t ctx slist tag false
+ (CodeCache.get_opcode td_trans slist tag)
+ *)
+ and loop_tag t ctx slist tag =
+ if t == Tree.nil then null_result else
+ (CodeCache.get_fun td_trans slist tag) t ctx slist tag false
+ (* get_trans t ctx slist tag false
+ (CodeCache.get_opcode td_trans slist tag) *)
+
+ and loop_no_right t ctx slist _ =
+ if t == Tree.nil then null_result else
+ let tag = Tree.tag tree t in
+ (CodeCache.get_fun td_trans slist tag) t ctx slist tag true
+ (* get_trans t ctx slist tag true
+ (CodeCache.get_opcode td_trans slist tag) *)
+ (*
+ and get_trans t ctx slist tag noright opcode =
+ match opcode with
+ | OpCode.K0 fll ->
+ eval_fold2_slist fll t tag empty_res empty_res
+
+ | OpCode.K1 (fll,first,llist,tag1) ->
+ eval_fold2_slist fll t tag empty_res
+ (loop_tag (first t) t llist tag1)
+
+ | OpCode.K2 (fll,first,llist) ->
+ eval_fold2_slist fll t tag empty_res
+ (loop (first t) t llist)
+
+ | OpCode.K3 (fll,next,rlist,tag2) ->
+ eval_fold2_slist fll t tag
+ (loop_tag (next t ctx) ctx rlist tag2)
+ empty_res
+ | OpCode.K4 (fll,next,rlist) ->
+ eval_fold2_slist fll t tag
+ (loop (next t ctx) ctx rlist)
+ empty_res
+
+ | OpCode.K5 (fll,next,rlist,tag2,first,llist,tag1) ->
+ eval_fold2_slist fll t tag
+ (loop_tag (next t ctx) ctx rlist tag2)
+ (loop_tag (first t) t llist tag1)
+
+ | OpCode.K6 (fll,next,rlist,first,llist,tag1) ->
+ eval_fold2_slist fll t tag
+ (loop (next t ctx) ctx rlist)
+ (loop_tag (first t) t llist tag1)
+
+ | OpCode.K7 (fll,next,rlist,tag2,first,llist) ->
+ eval_fold2_slist fll t tag
+ (loop_tag (next t ctx) ctx rlist tag2)
+ (loop (first t) t llist)
+
+ | OpCode.K8 (fll,next,rlist,first,llist) ->
+ eval_fold2_slist fll t tag
+ (loop (next t ctx) ctx rlist)
+ (loop (first t) t llist)
+
+ | OpCode.KDefault _ ->
+ mk_trans t ctx tag slist noright
+ *)
+ and mk_trans t ctx slist tag noright =
+ let fl_list,llist,rlist,ca,da,sa,fa =
+ SList.fold
+ (fun set (fll_acc,lllacc,rllacc,ca,da,sa,fa) -> (* For each set *)
+ let fl,ll,rr,ca,da,sa,fa =
+ StateSet.fold
+ (fun q acc ->
+ List.fold_left
+ (fun ((fl_acc,ll_acc,rl_acc,c_acc,d_acc,s_acc,f_acc) as acc)
+ (ts,t) ->
+ if (TagSet.mem tag ts)
+ then
+ let _,_,_,f,_ = t.Transition.node in
+ let (child,desc,below),(sibl,foll,after) = Formula.st f in
+ (Formlist.cons t fl_acc,
+ StateSet.union ll_acc below,
+ StateSet.union rl_acc after,
+ StateSet.union child c_acc,
+ StateSet.union desc d_acc,
+ StateSet.union sibl s_acc,
+ StateSet.union foll f_acc)
+ else acc ) acc (
+ try Hashtbl.find a.trans q
+ with
+ Not_found -> Printf.eprintf "Looking for state %i, doesn't exist!!!\n%!"
+ q;[]
+ )
+
+ ) set (Formlist.nil,StateSet.empty,StateSet.empty,ca,da,sa,fa)
+ in (Formlistlist.cons fl fll_acc), (SList.cons ll lllacc), (SList.cons rr rllacc),ca,da,sa,fa)
+ slist (Formlistlist.nil,SList.nil,SList.nil,StateSet.empty,StateSet.empty,StateSet.empty,StateSet.empty)
+ in
+ (* Logic to chose the first and next function *)
+ let tags_child,tags_below,tags_siblings,tags_after = Tree.tags tree tag in
+ let d_f = Algebra.decide a tags_child tags_below (StateSet.union ca da) true in
+ let d_n = Algebra.decide a tags_siblings tags_after (StateSet.union sa fa) false in
+ let f_kind,first = choose_jump_down tree d_f
+ and n_kind,next = if noright then (`NIL, fun _ _ -> Tree.nil )
+ else choose_jump_next tree d_n in
+ let empty_res = null_result in
+ let fll = fl_list in
+ let cont =
+ match f_kind,n_kind with
+ | `NIL,`NIL -> (*OpCode.K0(fl_list) *)
+ fun t _ _ tag _ -> eval_fold2_slist fll t tag empty_res empty_res
+
+ | _,`NIL -> (
+ match f_kind with
+ |`TAG(tag1) -> (*OpCode.K1(fl_list,first,llist,tag1) *)
+ fun t _ _ tag _ -> eval_fold2_slist fll t tag empty_res
+ (loop_tag (first t) t llist tag1)
+ | _ -> (* OpCode.K2(fl_list,first,llist) *)
+ fun t _ _ tag _ -> eval_fold2_slist fll t tag empty_res
+ (loop (first t) t llist tag)
+ )
+ | `NIL,_ -> (
+ match n_kind with
+ |`TAG(tag2) -> (*OpCode.K3(fl_list,next,rlist,tag2) *)
+ fun t ctx _ tag _ ->
+ eval_fold2_slist fll t tag
+ (loop_tag (next t ctx) ctx rlist tag2)
+ empty_res
+
+ | _ -> (*OpCode.K4(fl_list,next,rlist) *)
+ fun t ctx _ tag _ ->
+ eval_fold2_slist fll t tag
+ (loop (next t ctx) ctx rlist tag)
+ empty_res
+
+ )