Guest User

haxe infer params

a guest
Apr 2nd, 2012
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.84 KB | None | 0 0
  1.   (* this function will receive the original function argument, the applied function argument and the original function parameters. *)
  2.   (* from this info, it will infer the applied tparams for the function *)
  3.   (* this function is used by CastDetection module *)
  4.   let infer_params gen pos (original_args:((string * bool * t) list * t)) (applied_args:((string * bool * t) list * t)) (params:(string * t) list) : tparams =
  5.     let args_list args = ( List.map (fun (_,_,t) -> t) (fst args) ) @ [snd args] in
  6.     let params_tbl = Hashtbl.create (List.length params) in
  7.     (*List.iter (fun (s,t) -> match t with | TInst(cl,[]) -> Hashtbl.add params_tbl cl.cl_path cl | _ -> assert false) params;*)
  8.    
  9.     let rec get_arg is_follow original applied =
  10.       match (original, applied) with
  11.         | TInst( ({ cl_kind = KTypeParameter } as cl ), []), _ ->
  12.           Hashtbl.replace params_tbl cl.cl_path applied
  13.         | TInst(cl, params), TInst(cl2, params2) ->
  14.           iter2 (get_arg is_follow) params params2
  15.         | TEnum(e, params), TEnum(e2, params2) ->
  16.           iter2 (get_arg is_follow) params params2
  17.         | TFun(params, ret), TFun(params2, ret2) ->
  18.           iter2 (get_arg false) ( args_list (params, ret) ) ( args_list (params2, ret2) )
  19.         | _ -> if not(is_follow) then get_arg true (follow original) (follow applied)
  20.     in
  21.    
  22.     let original = args_list original_args in
  23.     let applied = args_list applied_args in
  24.    
  25.     iter2 (fun original applied ->
  26.       get_arg false original applied
  27.     ) original applied;
  28.    
  29.     List.map (fun (_,t) ->
  30.       match follow t with
  31.         | TInst(cl,_) ->
  32.           (try Hashtbl.find params_tbl cl.cl_path with | Not_found -> (gen.gcon.error ("Error: function argument " ^ (snd cl.cl_path) ^ " not applied.") pos); assert false)
  33.         | _ ->
  34.           assert false
  35.     ) params
Advertisement
Add Comment
Please, Sign In to add comment