text
stringlengths 0
601k
|
---|
let string_of_constructor_arg = function Pcstr_tuple coretypes -> string_of_coretype { ptyp_desc = Ptyp_tuple coretypes ; ptyp_loc = Location . none ; ptyp_attributes = [ ] ; ptyp_loc_stack = [ ] }
|
let rec dots_between = function [ ] -> " "
|
let string_of_longident l = dots_between ( Longident . flatten l )
|
let rec to_string = function Unit -> " Unit " Printf . sprintf " Op ( % s , % s , % s ) " ( to_string_op op ) ( to_string l ) ( to_string r ) Printf . sprintf " And ( % s , % s ) " ( to_string a ) ( to_string b ) Printf . sprintf " Or ( % s , % s ) " ( to_string a ) ( to_string b ) Printf . sprintf " Cmp ( % s , % s , % s ) " ( to_string_cmp cmp ) ( to_string l ) ( to_string r ) Printf . sprintf " If ( % s , % s ) " ( to_string a ) ( to_string b ) Printf . sprintf " If ( % s , % s , % s ) " ( to_string a ) ( to_string b ) ( to_string c ) Printf . sprintf " % s ( % s , % s ) " ( if recflag then " LetRec " else " Let " ) ( to_string_bindings bindings ) ( to_string e ' ) Printf . sprintf " % s ( % s ) " ( if recflag then " LetDefRec " else " LetDef " ) ( to_string_bindings bindings ) Printf . sprintf " Fun ( % s , % s , % s ) " ( to_string_label flabel ) ( to_string_pat fname ) ( to_string fexp ) Printf . sprintf " App ( % s , % s ) " ( to_string e ) ( to_string e ' ) Printf . sprintf " Seq ( % s , % s ) " ( to_string e ) ( to_string e ' ) Printf . sprintf " While ( % s , % s , % s , % s ) " ( to_string e ) ( to_string e ' ) ( to_string copy_e ) ( to_string copy_e ' ) Printf . sprintf " For ( % s , % s , % s , % s , % s , % s ) " var ( to_string s ) ( to_string_forkind forkind ) ( to_string e ) ( to_string expr ) ( to_string copy_expr ) Printf . sprintf " Field ( % s , % s ) " ( to_string e ) y Printf . sprintf " SetField ( % s , % s , % s ) " ( to_string e ) y ( to_string e ' ) Printf . sprintf " Raise % s ( % s ) " n ( match payload with None -> " " | Some x -> to_string x ) Printf . sprintf " Exception ( % s , Some % s ) " e ( string_of_constructor_arg args ) Printf . sprintf " TypeDef ( % b , % s ) " recflag ( to_string_typedecls typedecls ) Printf . sprintf " TryWith ( % s , % s ) " ( to_string e ) ( to_string_patmatch patmatch ) Printf . sprintf " Control ( % s , % s ) " ( to_string_control c ) ( to_string t ) Printf . sprintf " Annot ( % s , % s , % s ) " n ( to_string p ) ( to_string t ) Printf . sprintf " CallBuiltIn % s " name to_string_struct l to_string_sig l Printf . sprintf " % s { % i } " n tag Printf . sprintf " % s { % i } ( % s ) " n tag ( to_string t ) Printf . sprintf " Cons ( % s , % s ) " ( to_string e ) ( to_string e ' ) Printf . sprintf " Append ( % s , % s ) " ( to_string e ) ( to_string e ' ) Printf . sprintf " Tuple ( % s ) " ( List . fold_left ( ^ ) " " ( List . map ( fun x -> to_string x ^ " , " ) xs ) ) Printf . sprintf " Array ( % s ) " ( Array . fold_left ( ^ ) " " ( Array . map ( fun x -> to_string x ^ " , " ) xs ) ) Printf . sprintf " Function % s " ( to_string_patmatch patmatch ) Printf . sprintf " Match ( % s , % s ) " ( to_string e ) ( to_string_patmatch patmatch ) Printf . sprintf " Open % s " x Printf . sprintf " LocalOpen ( % s , % s ) " x ( to_string e ) Printf . sprintf " ModuleBinding ( % s , % s ) " m ( to_string t ) Printf . sprintf " ModuleConstraint ( % s , % s ) " ( to_string_modtype modtype ) ( to_string t ) Printf . sprintf " ModuleApply ( % s , % s ) " ( to_string m1 ) ( to_string m2 ) NoLabel -> " NoLabel " ModTypeSignature t -> Printf . sprintf " ModTypeSignature ( % s ) " ( to_string t ) Printf . sprintf " ModTypeSignature ( % s ) " s Printf . sprintf " ModTypeWith " List . fold_left ( ^ ) " " ( List . map to_string_binding bs ) Printf . sprintf " % s = % s \ n " ( to_string_pat pat ) ( to_string e ) Printf . sprintf " % s = . . . \ n " ( to_string_pat pat ) List . fold_left ( ^ ) " " ( List . map to_string_binding_names bs ) PatAny -> " _ " " PatTuple ( " ^ List . fold_left ( ^ ) " " ( List . map to_string_pat items ) ^ " ) " List . fold_left ( ^ ) " " ( List . map ( fun x -> to_string_case x ^ " , " ) xs ) None -> " None " Printf . sprintf " ( % s , % s , % s ) " ( to_string_pat pat ) ( to_string_guard guard ) ( to_string rhs ) Underline -> " Underline " Add -> " Add " | Sub -> " Sub " | Mul -> " Mul " | Div -> " Div " LT -> " LT " | EQ -> " EQ " | GT -> " GT " | EQLT -> " EQLT " | EQGT -> " EQGT " | NEQ -> " NEQ " " Record [ " ^ List . fold_left ( ^ ) " " ( List . map ( fun ( n , t ) -> Printf . sprintf " ( % s , % s ) ; " n ( to_string ! t ) ) l ) ^ " ] " Printf . sprintf " Struct [ " ^ List . fold_left ( ^ ) " " ( List . map ( fun x -> to_string x ^ " \ n " ) l ) ^ " ] " Printf . sprintf " Sig [ " ^ List . fold_left ( ^ ) " " ( List . map ( fun x -> to_string x ^ " \ n " ) l ) ^ " ] " match ptyp_desc with Ptyp_any -> " _ " | Ptyp_var s -> s | Ptyp_arrow ( _ , a , b ) -> to_string_core_type a ^ " -> " ^ to_string_core_type b | Ptyp_tuple core_types -> " ( " ^ List . fold_left ( ^ ) " " ( List . map ( fun x -> to_string_core_type x ^ " , " ) core_types ) ^ " ) " | Ptyp_constr ( { txt } , _ ) -> string_of_longident txt | Ptyp_object _ -> " Ptyp_object " | Ptyp_class _ -> " Ptyp_class " | Ptyp_alias _ -> " Ptyp_alias " | Ptyp_variant _ -> " Ptyp_variant " | Ptyp_poly _ -> " Ptyp_poly " | Ptyp_package _ -> " Ptyp_package " | Ptyp_extension _ -> " Ptyp_extenstion " Printf . sprintf " % s % s " txt ( match pcd_args with | Pcstr_tuple [ ] -> " " | Pcstr_tuple types -> " of " ^ List . fold_left ( fun x y -> x ^ " " ^ y ) " " ( List . map to_string_core_type types ) | Pcstr_record _ -> " of RECORD " ) Ptype_variant constdecls -> List . fold_left ( fun x y -> x ^ " | " ^ y ) " " ( List . map to_string_constdecl constdecls ) Printf . sprintf " % s " txt ^ to_string_ptype_kind ptype_kind List . fold_left ( ^ ) " " ( List . map ( fun x -> to_string_typedecl x ^ " \ n " ) typedecls ) EnvBinding ( recflag , bs ) -> Printf . sprintf " ( % b , % s ) \ n " recflag ( ( if full then to_string_bindings else to_string_bindings_names ) ! bs ) | EnvFunctor _ -> " EnvFunctor " | EnvType ( recflag , typedecls ) -> Printf . sprintf " EnvType ( % b , % s ) " recflag ( to_string_typedecls typedecls ) let strings = List . map to_string_envitem env in Printf . sprintf " Env [ " ^ List . fold_left ( ^ ) " " strings ^ " ] "
|
let rec bound_in_pattern = function PatAny -> [ ]
|
let bound_in_bindings bindings = List . flatten ( List . map bound_in_pattern ( List . map fst bindings ) )
|
let begins_with n s = String . length n <= String . length s && n = String . sub s 0 ( String . length n )
|
let rec pattern_begins_with n = function PatVar s when begins_with n s -> true
|
let binding_begins_with n ( p , e ) = pattern_begins_with n p
|
let string_loc_begins_with n ( { txt } : string loc ) = begins_with n txt
|
let bindings_beginning_with n env = option_map ( function envitem -> match envitem with EnvFunctor ( func_name , input_module_name , modtype , e , env ) -> if begins_with n func_name then Some envitem else None | EnvBinding ( recflag , bindings ) -> if List . for_all ( binding_begins_with n ) ! bindings then Some envitem else None | EnvType ( recflag , typedecls ) -> if List . for_all ( string_loc_begins_with n ) ( List . map ( fun x -> x . ptype_name ) typedecls ) then ( Some envitem ) else ( None ) ) env
|
let cut n s = String . sub s ( String . length n + 1 ) ( String . length s - String . length n - 1 )
|
let rec strip_pattern n = function PatVar s -> PatVar ( cut n s )
|
let strip_binding n ( p , e ) = ( strip_pattern n p , e )
|
let strip_constructor n c = match c with { pcd_name = ( { txt } as loc ) } -> { c with pcd_name = { loc with txt = cut n txt } }
|
let strip_ptype_kind n = function Ptype_abstract -> Ptype_abstract
|
let strip_typedecl n t = { t with ptype_name = { t . ptype_name with txt = cut n t . ptype_name . txt } ; ptype_kind = strip_ptype_kind n t . ptype_kind }
|
let strip_bindings n = function EnvFunctor ( s , input_module_name , modtype , e , env ) -> EnvFunctor ( cut n s , input_module_name , modtype , e , env ) EnvBinding ( recflag , ref ( List . map ( strip_binding n ) ! bs ) ) EnvType ( recflag , List . map ( strip_typedecl n ) typedecls )
|
let open_module n ( env : env ) = List . map ( strip_bindings n ) ( bindings_beginning_with n env ) @ env
|
let rec prefix_pattern prefix = function PatVar s -> PatVar ( prefix ^ " . " ^ s )
|
let prefix_binding prefix ( p , e ) = ( prefix_pattern prefix p , e )
|
let prefix_bindings p = function EnvBinding ( recflag , bs ) -> EnvBinding ( recflag , ref ( List . map ( prefix_binding p ) ! bs ) ) EnvFunctor ( p ^ n , input_module_name , modtype , e , env )
|
let alias_module current alias ( env : env ) = let replaced = List . map ( prefix_bindings alias ) ( List . map ( strip_bindings current ) ( bindings_beginning_with current env ) ) in replaced @ env
|
let bindings_of_struct_item p = function | LetDef ( b , ld ) -> Some ( prefix_bindings p ( EnvBinding ( b , ref ld ) ) ) | _ -> None
|
let open_struct_as_module name items ( env : env ) = let bindings = option_map ( bindings_of_struct_item name ) items in let top_level_binding = EnvBinding ( false , ref [ ( PatVar name , Struct ( false , items ) ) ] ) in top_level_binding :: bindings @ env
|
let realops = ref false
|
let rec tag_of_constructor_name_constdecls valnum blocknum str = function [ ] -> valnum , blocknum , None if args = [ ] then valnum , blocknum , Some valnum else valnum , blocknum , Some blocknum if args = [ ] then tag_of_constructor_name_constdecls ( valnum + 1 ) blocknum str t else tag_of_constructor_name_constdecls valnum ( blocknum + 1 ) str t valnum , blocknum , Some blocknum tag_of_constructor_name_constdecls valnum ( blocknum + 1 ) str t
|
let rec tag_of_constructor_name_envtype valnum blocknum str ( recflag , typedecls ) = match typedecls with [ ] -> None | { ptype_kind = Ptype_variant constdecls } :: more -> begin match tag_of_constructor_name_constdecls valnum blocknum str constdecls with _ , _ , Some tag -> Some tag | valnum ' , blocknum ' , None -> tag_of_constructor_name_envtype valnum ' blocknum ' str ( recflag , more ) end | { ptype_kind = Ptype_abstract } :: more -> tag_of_constructor_name_envtype valnum blocknum str ( recflag , more ) | { ptype_kind = Ptype_record _ } :: more -> tag_of_constructor_name_envtype valnum blocknum str ( recflag , more ) | { ptype_kind = Ptype_open } :: more -> failwith " tag_of_constructor_name_envtype : Ptype_open "
|
let rec tag_of_constructor_name env str = match env with [ ] -> begin match str with " Invalid_argument " | " Failure " | " None " | " Some " | " Stream . Error " | " Stream . Failure " | " GenHashTable . EDead " | " GenHashTable . EFalse " | " GenHashTable . ETrue " -> 0 | _ -> 42 end | EnvType t :: more -> begin match tag_of_constructor_name_envtype 0 0 str t with None -> tag_of_constructor_name more str | Some tag -> tag end | _ :: t -> tag_of_constructor_name t str
|
let tag_of_constructor_name env str = try tag_of_constructor_name env str with e -> Printf . printf " TAG_OF_CONSTRUCTOR failed . . . . . " ; ! print_endline ( to_string_env env ) ; raise e
|
let rec of_real_ocaml_expression_desc env = function Pexp_constant ( Pconst_integer ( s , None ) ) -> Int ( int_of_string s ) Cons ( of_real_ocaml env e , of_real_ocaml env e ' ) let str = string_of_longident txt in Constr ( tag_of_constructor_name env str , str , None ) let str = string_of_longident txt in Constr ( tag_of_constructor_name env str , str , Some ( of_real_ocaml env e ) ) If ( of_real_ocaml env e , of_real_ocaml env e1 , Some ( of_real_ocaml env e2 ) ) If ( of_real_ocaml env e , of_real_ocaml env e1 , None ) let ocaml_exp = of_real_ocaml env exp in Fun ( of_real_ocaml_label env label opt , of_real_ocaml_pattern env pat . ppat_desc , ocaml_exp , env ) let cases = List . map ( of_real_ocaml_case env ) cases in Function ( cases , env ) let theref = ref [ ] in let recflag = r = Recursive in let bindings ' = List . map ( of_real_ocaml_binding ( EnvBinding ( recflag , theref ) :: env ) ) bindings in theref := bindings ' ; let env ' = EnvBinding ( recflag , ref bindings ' ) :: env in Let ( recflag , bindings ' , of_real_ocaml env ' e ' ) ( { pexp_desc = Pexp_ident { txt = Longident . Lident " raise " } } , [ ( Nolabel , { pexp_desc = Pexp_construct ( { txt = Longident . Lident s } , payload ) } ) ] ) -> begin match payload with None -> Raise ( s , None ) | Some x -> Raise ( s , Some ( of_real_ocaml env x ) ) end ( { pexp_desc = Pexp_ident { txt = Longident . Lident f } } , [ ( Nolabel , l ) ; ( Nolabel , r ) ] ) -> let e = of_real_ocaml env l in let e ' = of_real_ocaml env r in begin match f with " " && -> And ( e , e ' ) | " " || -> Or ( e , e ' ) | " " @ -> Append ( e , e ' ) | " " := when not ! realops -> App ( App ( Var " " , := e ) , e ' ) | ( " " * | " " + | " " - | " " ) / as op -> Op ( op_of_string op , e , e ' ) | ( " " = | " " > | " " < | " " <= | " " >= | " " ) <> as cmp -> Cmp ( cmp_of_string cmp , e , e ' ) | _ -> App ( App ( Var f , e ) , e ' ) end App ( of_real_ocaml env e , of_real_ocaml env e ' ) of_real_ocaml_apps env ( List . rev ( e :: List . map snd apps ) ) Seq ( of_real_ocaml env e , of_real_ocaml env e ' ) While ( of_real_ocaml env e , of_real_ocaml env e ' , of_real_ocaml env e , of_real_ocaml env e ' ) let convert_flag = function Upto -> UpTo | Downto -> DownTo in For ( txt , of_real_ocaml env e , convert_flag flag , of_real_ocaml env e ' , of_real_ocaml env e ' ' , of_real_ocaml env e ' ' ) Record ( List . map ( of_real_ocaml_record_entry env ) items ) Field ( of_real_ocaml env e , n ) SetField ( of_real_ocaml env e , n , of_real_ocaml env e ' ) TryWith ( of_real_ocaml env e , List . map ( of_real_ocaml_case env ) cases ) Tuple ( List . map ( of_real_ocaml env ) xs ) Array ( Array . of_list ( List . map ( of_real_ocaml env ) xs ) ) Match ( of_real_ocaml env e , List . map ( of_real_ocaml_case env ) cases ) Assert ( of_real_ocaml env e ) LocalOpen ( n , of_real_ocaml env e ) match label , opt with Nolabel , None -> NoLabel | Labelled l , None -> Labelled l | Optional l , None -> Optional ( l , None ) | Optional l , Some e -> Optional ( l , Some ( of_real_ocaml env e ) ) | _ -> failwith " bad of_real_ocaml_label " ( of_real_ocaml_pattern env ppat_desc , of_real_ocaml env pvb_expr ) [ ] -> assert false ( { txt } , e ) -> ( string_of_longident txt , ref ( of_real_ocaml env e ) ) ( of_real_ocaml_pattern env pc_lhs . ppat_desc , begin match pc_guard with None -> None | Some x -> Some ( of_real_ocaml env x ) end , of_real_ocaml env pc_rhs ) Ppat_var { txt } -> PatVar txt PatTuple ( List . map ( of_real_ocaml_pattern env ) ( List . map ( fun x -> x . ppat_desc ) patterns ) ) PatArray ( Array . of_list ( List . map ( of_real_ocaml_pattern env ) ( List . map ( fun x -> x . ppat_desc ) patterns ) ) ) PatCons ( of_real_ocaml_pattern env a . ppat_desc , of_real_ocaml_pattern env b . ppat_desc ) PatAlias ( txt , of_real_ocaml_pattern env pattern . ppat_desc ) PatOr ( of_real_ocaml_pattern env p . ppat_desc , of_real_ocaml_pattern env p ' . ppat_desc ) PatConstr ( string_of_longident txt , Some ( of_real_ocaml_pattern env p . ppat_desc ) ) PatConstraint ( of_real_ocaml_pattern env pat . ppat_desc , coretype ) PatRecord ( openflag = Open , List . map ( fun ( n , p ) -> ( string_of_longident n . txt , of_real_ocaml_pattern env p . ppat_desc ) ) items ) PatException ( of_real_ocaml_pattern env p . ppat_desc ) match x . pexp_attributes with [ ] -> of_real_ocaml_expression_desc env x . pexp_desc | { attr_name = { txt = n } ; attr_payload = PStr _ } :: _ -> Annot ( n , Struct ( false , [ ] ) , of_real_ocaml_expression_desc env x . pexp_desc ) | _ -> failwith " unknown annotation " let n = p . pval_name . txt in ( n , Ocamliprim . lookup_primitive ~ typ : p . pval_type ( List . hd p . pval_prim ) ) Sig [ ] match module_type . pmty_desc with Pmty_signature s -> ModTypeSignature ( of_real_ocaml_signature env s ) | Pmty_ident { txt } -> ModTypeIdent ( string_of_longident txt ) | Pmty_functor _ -> failwith " of_real_ocaml_modtype : functor " | Pmty_with ( mt , constraints ) -> ModTypeWith ( of_real_ocaml_module_type env mt , constraints ) | Pmty_typeof _ -> failwith " of_real_ocaml_modtype : typeof " | Pmty_extension _ -> failwith " of_real_ocaml_modtype : extension " | Pmty_alias _ -> failwith " of_real_ocaml_modtype : alias " match module_expr . pmod_desc with Pmod_structure s -> Struct ( true , snd ( of_real_ocaml_structure env [ ] s ) ) | Pmod_constraint ( module_expr , module_type ) -> ModuleConstraint ( of_real_ocaml_module_type env module_type , of_real_ocaml_module_expr env module_expr ) | Pmod_ident { txt } -> ModuleIdentifier ( string_of_longident txt ) | Pmod_functor ( { txt } , mt , me ) -> let mt ' = match mt with | None -> None | Some mt -> Some ( of_real_ocaml_module_type env mt ) in Functor ( txt , mt ' , of_real_ocaml_module_expr env me ) | Pmod_apply ( me1 , me2 ) -> ModuleApply ( of_real_ocaml_module_expr env me1 , of_real_ocaml_module_expr env me2 ) | Pmod_unpack _ -> failwith " of_real_ocaml_module_expr : pmod_unpack " | Pmod_extension _ -> failwith " of_real_ocaml_module_expr : pmod_extension " let name = mb . pmb_name . txt in match of_real_ocaml_module_expr env mb . pmb_expr with ModuleIdentifier original -> ( Some ( ModuleBinding ( name , ModuleIdentifier original ) ) , alias_module original name env ) | x -> ( Some ( ModuleBinding ( name , x ) ) , env ) match o . popen_lid . txt with Longident . Lident x -> x | _ -> failwith " of_real_ocaml_open_description " { pstr_desc = Pstr_eval ( e , _ ) } -> ( Some ( of_real_ocaml env e ) , env ) let theref = ref [ ] in let recflag ' = recflag = Recursive in let bindings ' = List . map ( of_real_ocaml_binding ( EnvBinding ( recflag ' , theref ) :: env ) ) bindings in theref := bindings ' ; let env ' = EnvBinding ( recflag ' , ref bindings ' ) :: env in let evalled = ( LetDef ( recflag ' , bindings ' ) ) in ( Some evalled , env ' ) ( Some ( ExceptionDef ( txt , t ) ) , env ) ( None , env ) let n , primitive = of_real_ocaml_primitive value_description in let bindings = [ ( PatVar n , primitive ) ] in let env ' = EnvBinding ( false , ref bindings ) :: env in ( Some ( LetDef ( false , bindings ) ) , env ' ) let t = ( recflag == Recursive , typedecls ) in ( Some ( TypeDef t ) , EnvType t :: env ) of_real_ocaml_module_binding env module_binding let n = of_real_ocaml_open_description open_description in ( Some ( Open n ) , open_module n env ) ( None , env ) ( Some ( Include ( of_real_ocaml_module_expr env include_declaration . pincl_mod ) ) , env ) match items with | [ ] -> env , List . rev acc | s :: ss -> match of_real_ocaml_structure_item env s with ( None , _ ) -> of_real_ocaml_structure env acc ss | ( Some s , env ' ) -> of_real_ocaml_structure env ' ( s :: acc ) ss
|
let of_real_ocaml env x = let env ' , str = of_real_ocaml_structure env [ ] x in ( env ' , Struct ( false , str ) )
|
let _ = Ocamliprim . of_real_ocaml := of_real_ocaml
|
let rec to_real_ocaml_expression_desc = function | Control ( _ , x ) -> to_real_ocaml_expression_desc x | Unit -> Pexp_construct ( { txt = Longident . Lident " ( ) " ; loc = Location . none } , None ) | Int i -> Pexp_constant ( Pconst_integer ( string_of_int i , None ) ) | String s -> Pexp_constant ( Pconst_string ( Bytes . to_string s , None ) ) | Bool b -> Pexp_construct ( { txt = Longident . Lident ( string_of_bool b ) ; loc = Location . none } , None ) | Var v -> Pexp_ident ( { txt = Longident . parse v ; loc = Location . none } ) | Op ( op , l , r ) -> to_real_ocaml_apply l r ( string_of_op op ) | And ( l , r ) -> to_real_ocaml_apply l r " " && | Or ( l , r ) -> to_real_ocaml_apply l r " " || | Cmp ( cmp , l , r ) -> to_real_ocaml_apply l r ( string_of_cmp cmp ) | If ( e , e1 , None ) -> Pexp_ifthenelse ( to_real_ocaml e , to_real_ocaml e1 , None ) | If ( e , e1 , Some e2 ) -> Pexp_ifthenelse ( to_real_ocaml e , to_real_ocaml e1 , Some ( to_real_ocaml e2 ) ) | Let ( flag , bindings , e ) -> to_real_ocaml_let flag bindings e | Fun ( _ , pat , e , _ ) -> Pexp_fun ( Nolabel , None , to_real_ocaml_pattern pat , to_real_ocaml e ) | App ( e , e ' ) -> Pexp_apply ( to_real_ocaml e , [ ( Nolabel , to_real_ocaml e ' ) ] ) | Seq ( e , e ' ) -> Pexp_sequence ( to_real_ocaml e , to_real_ocaml e ' ) | e -> Printf . printf " Unknown thing in to_real_ocaml_expression_desc : % s \ n " ( to_string e ) ; failwith " fix to_real_ocaml_expression_desc " PatInt i -> { ppat_desc = Ppat_constant ( Pconst_integer ( string_of_int i , None ) ) ; ppat_loc = Location . none ; ppat_attributes = [ ] ; ppat_loc_stack = [ ] } { ppat_desc = Ppat_any ; ppat_loc = Location . none ; ppat_attributes = [ ] ; ppat_loc_stack = [ ] } { ppat_desc = Ppat_var { txt = v ; loc = Location . none } ; ppat_loc = Location . none ; ppat_attributes = [ ] ; ppat_loc_stack = [ ] } { pvb_pat = to_real_ocaml_pattern pat ; pvb_expr = to_real_ocaml t ; pvb_attributes = [ ] ; pvb_loc = Location . none } let bindings = List . map to_real_ocaml_binding bs in Pexp_let ( ( if r then Recursive else Nonrecursive ) , bindings , to_real_ocaml e ) let exprs = [ ( Nolabel , to_real_ocaml l ) ; ( Nolabel , to_real_ocaml r ) ] in let expr = Ocamliutil . with_desc ( Pexp_ident { txt = Longident . Lident n ; loc = Location . none } ) in Pexp_apply ( expr , exprs ) Ocamliutil . with_desc ( to_real_ocaml_expression_desc x )
|
let to_real_ocaml = function | Struct ( _ , xs ) -> List . map ( function LetDef ( recflag , bindings ) -> { pstr_desc = Pstr_value ( ( if recflag then Recursive else Nonrecursive ) , List . map to_real_ocaml_binding bindings ) ; pstr_loc = Location . none } | x -> { pstr_desc = Pstr_eval ( to_real_ocaml x , [ ] ) ; pstr_loc = Location . none } ) xs | x -> [ { pstr_desc = Pstr_eval ( to_real_ocaml x , [ ] ) ; pstr_loc = Location . none } ]
|
let extract_from_let = function LetDef ( _ , [ ( _ , e ) ] ) -> e
|
let of_string s = match of_real_ocaml [ ] ( ast s ) with env , Struct ( _ , ( _ :: _ as l ) ) -> ( env , extract_from_let ( List . hd ( List . rev l ) ) ) | _ -> failwith " Tinyocaml . of_string "
|
let calc = function Add -> ( ) + | Sub -> ( ) - | Mul -> ( * ) | Div -> ( ) /
|
let comp = function LT -> ( ) < | EQ -> ( ) = | GT -> ( ) > | EQLT -> ( ) <= | EQGT -> ( ) >= | NEQ -> ( ) <>
|
let rec is_value e = match e with Unit | Int _ | Bool _ | Fun _ | Function _ List . for_all is_value ( List . map ( fun ( _ , { contents = e } ) -> e ) items ) -> true List . for_all is_value items -> true List . for_all is_value items -> true Array . for_all is_value items -> true is_value e && is_value e ' -> true List . for_all ( fun ( _ , e ) -> is_value e ) bindings -> true List . for_all ( fun ( _ , e ) -> is_value e ) bindings && is_value e -> true
|
let bold , ul , code_end = ( " \ x1b [ 1m " , " \ x1b [ 4m " , " \ x1b [ 0m " )
|
let underline x = Control ( Underline , x )
|
let fastcurry = ref false
|
let rec underline_redex e = try match e with Control ( c , x ) -> Control ( c , underline_redex x ) | Op ( _ , ( Int _ | Var _ ) , ( Int _ | Var _ ) ) -> underline e | Op ( op , ( ( Int _ | Var _ ) as a ) , b ) -> Op ( op , a , underline_redex b ) | Op ( op , a , b ) -> Op ( op , underline_redex a , b ) | And ( Bool false , _ ) -> underline e | And ( Bool true , Bool _ ) -> underline e | And ( Bool true , b ) -> And ( Bool true , underline_redex b ) | And ( a , b ) -> And ( underline_redex a , b ) | Or ( Bool true , _ ) -> underline e | Or ( Bool false , Bool b ) -> underline e | Or ( Bool false , b ) -> Or ( Bool false , underline_redex b ) | Cmp ( _ , ( Int _ | Var _ ) , ( Int _ | Var _ ) ) -> underline e | Cmp ( op , a , ( ( Int _ | Var _ ) as b ) ) -> Cmp ( op , underline_redex a , b ) | Cmp ( op , a , b ) -> Cmp ( op , a , underline_redex b ) | If ( Bool _ , _ , _ ) -> underline e | If ( cond , a , b ) -> If ( underline_redex cond , a , b ) | Let ( recflag , bindings , e ' ) -> if List . for_all ( fun ( _ , v ) -> is_value v ) bindings then Let ( recflag , bindings , underline_redex e ' ) else Let ( recflag , underline_first_non_value_binding bindings , e ' ) | LetDef ( recflag , bindings ) -> if List . for_all ( fun ( _ , v ) -> is_value v ) bindings then failwith " letdef already a value " else LetDef ( recflag , underline_first_non_value_binding bindings ) | App ( Fun f , x ) -> if is_value x then underline e else App ( Fun f , underline_redex x ) | App ( Function f , x ) -> if is_value x then underline e else App ( Function f , underline_redex x ) | App ( Var v , x ) -> App ( underline ( Var v ) , x ) | App ( App _ , _ ) when ! fastcurry -> underline_curry e | App ( f , x ) -> App ( underline_redex f , x ) | Seq ( a , b ) -> if is_value a then underline e else Seq ( underline_redex a , b ) | While ( we , we ' , copy_we , copy_we ' ) -> if not ( is_value we ) then While ( underline_redex we , we ' , copy_we , copy_we ' ) else if not ( is_value we ' ) then While ( we , underline_redex we ' , copy_we , copy_we ' ) else underline e | For ( n , fe , updown , fe ' , fe ' ' , copy_fe ' ' ) -> if not ( is_value fe ) then For ( n , underline_redex fe , updown , fe ' , fe ' ' , copy_fe ' ' ) else if not ( is_value fe ' ) then For ( n , fe , updown , underline_redex fe ' , fe ' ' , copy_fe ' ' ) else if not ( is_value fe ' ' ) then For ( n , fe , updown , fe ' , underline_redex fe ' ' , copy_fe ' ' ) else underline e | Var _ -> underline e | Record items -> underline_first_non_value_ref items ; Record items | Field ( a , n ) -> if is_value a then underline e else Field ( underline a , n ) | SetField ( a , n , b ) -> if is_value a then if is_value b then underline e else SetField ( a , n , underline b ) else SetField ( underline a , n , b ) | Raise _ -> underline e | TryWith ( a , cases ) -> if is_value a then underline e else TryWith ( underline_redex a , cases ) | CallBuiltIn ( typ , name , args , fn ) -> if List . for_all is_value args then underline e else CallBuiltIn ( typ , name , underline_first_non_value args , fn ) | Struct ( b , ls ) -> if List . for_all is_value ls then failwith " module already a value " else Struct ( b , underline_first_non_value ls ) | Tuple ls -> if List . for_all is_value ls then failwith " tuple already a value " else Tuple ( underline_first_non_value ls ) | Array items -> if Array . for_all is_value items then failwith " tuple already a value " else begin underline_first_non_value_array items ; Array items end | Constr ( tag , n , Some t ) -> if is_value t then failwith " constr already a value " else Constr ( tag , n , Some ( underline_redex t ) ) | Cons ( x , y ) -> if is_value x then Cons ( x , underline_redex y ) else Cons ( underline x , y ) | Match ( x , patmatch ) -> if is_value x then underline e else Match ( underline_redex x , patmatch ) | LocalOpen ( n , e ) -> LocalOpen ( n , underline_redex e ) | _ -> raise UnderlineValueUnderLets with UnderlineValueUnderLets -> raise UnderlineValueUnderLets2 | UnderlineValueUnderLets2 -> underline e match e with | App ( f , x ) when not ( is_value x ) -> Some ( App ( f , underline_redex x ) ) | App ( App _ as f ' , x ' ) -> begin match underline_curry_inner f ' with None -> None | Some f ' ' -> Some ( App ( f ' ' , x ' ) ) end | App ( f , x ) when not ( is_value f ) -> Some ( App ( underline_redex f , x ) ) | _ -> None match underline_curry_inner e with None -> underline e | Some x -> x [ ] -> [ ] if is_value h then h :: underline_first_non_value t else underline_redex h :: t let rec loop p = if p >= Array . length x then ( ) else if is_value x . ( p ) then loop ( p + 1 ) else x . ( p ) <- underline_redex x . ( p ) in loop 0 [ ] -> [ ] if is_value v then ( k , v ) :: underline_first_non_value_binding t else ( k , underline_redex v ) :: t try List . iter ( fun ( _ , v ) -> if not ( is_value ! v ) then begin v := underline_redex ! v ; raise Exit end ) items with Exit -> ( )
|
let underline_redex e = if is_value e then e else underline_redex e
|
let rec strip_control = function Control ( _ , e ) -> strip_control e
|
let rec remove_named_recursive_functions all fns = function Let ( true , [ ( PatVar n , v ) ] , e ) -> let r = Tinyocaml . recurse ( remove_named_recursive_functions all fns ) e in if all || List . mem n fns then r else Let ( true , [ ( PatVar n , v ) ] , r )
|
type state = { tokens : token array ; pos : int }
|
let choice xs s = let rec loop xs = match xs with [ ] -> fail ( ) | x :: xs -> try x s with Failed -> loop xs in loop xs
|
let skip x s = try let _ , s = x s in ( ) , s with Failed -> ( ) , s
|
let skip_many x s = let s = ref s in try while true do s := snd ( x ! s ) ; done ; ( ) , ! s with Failed -> ( ) , ! s
|
let token t s = if current s = t then ( ) , next s else fail ( )
|
let token_map f s = f ( current s ) , next s
|
let sep sep x = let return acc s = List . rev acc , s in let rec loop acc s = try let x , s = x s in let acc = x :: acc in ( try let _ , s = sep s in loop acc s with Failed -> return acc s ) with Failed -> return acc s in loop [ ]
|
let sep_empty sep x empty = let return acc s = List . rev acc , s in let rec empties acc s = try let _ , s = sep s in empties ( empty :: acc ) s with Failed -> loop acc s and loop acc s = try let x , s = x s in let acc = x :: acc in ( try let _ , s = sep s in empties acc s with Failed -> return acc s ) with Failed -> ( let acc = empty :: acc in try let _ , s = sep s in empties acc s with Failed -> return acc s ) in loop [ ]
|
let sep_token t x = let return acc s = List . rev acc , s in let rec loop acc s = try let x , s = x s in let acc = x :: acc in ( try if current s = t then loop acc ( next s ) else return acc s with Failed -> return acc s ) with Failed -> return acc s in loop [ ]
|
let sep_token1 t x s = match sep_token t x s with [ ] , _ -> fail ( ) | ok -> ok
|
let seq x = let rec loop acc s = try let x , s = x s in loop ( x :: acc ) s with Failed -> List . rev acc , s in loop [ ]
|
let seq1 x s = match seq x s with [ ] , _ -> fail ( ) | ok -> ok
|
let seq_fold x ~ f ~ init = let rec loop acc s = try let x , s = x s in loop ( f acc x ) s with Failed -> acc , s in loop init
|
let seq_fold_token ~ f ~ init = let rec loop acc s = try loop ( f acc ( current s ) ) ( next s ) with Failed -> acc , s in loop init
|
type t = Empty | Concat of t * int * t * int * int | Leaf of string
|
let height = function Empty | Leaf _ -> 0 | Concat ( _ , _ , _ , _ , h ) -> h
|
let print = let rec print prefix = function | Empty -> print_string " \ n " | Leaf s -> print_string prefix ; print_string s ; print_string " \ n " | Concat ( l , _ , r , _ , _ ) -> let prefixl = prefix ^ if height l = 0 then " " / else " " in let prefixr = prefix ^ if height r = 0 then " " \\ else " " in print prefixl l ; print prefixr r ; in print " "
|
type forest_element = { mutable c : t ; mutable len : int }
|
let string_of_string_list l = String . concat " " l
|
let is_empty = function Empty -> true | _ -> false
|
let length = function Empty -> 0 | Leaf s -> STRING . length s | Concat ( _ , cl , _ , cr , _ ) -> cl + cr
|
let make_concat l r = let hl = height l and hr = height r in let cl = length l and cr = length r in Concat ( l , cl , r , cr , if hl >= hr then hl + 1 else hr + 1 )
|
let min_len = let fib_tbl = Array . make max_height 0 in let rec fib n = match fib_tbl . ( n ) with 0 -> let last = fib ( n - 1 ) and prev = fib ( n - 2 ) in let r = last + prev in let r = if r > last then r else last in fib_tbl . ( n ) <- r ; r | n -> n in fib_tbl . ( 0 ) <- leaf_size + 1 ; fib_tbl . ( 1 ) <- 3 * leaf_size / 2 + 1 ; Array . init max_height ( fun i -> if i = 0 then 1 else fib ( i - 1 ) )
|
let max_length = min_len . ( Array . length min_len - 1 )
|
let concat_fast l r = match l with Empty -> r | Leaf _ | Concat ( _ , _ , _ , _ , _ ) -> match r with Empty -> l | Leaf _ | Concat ( _ , _ , _ , _ , _ ) -> make_concat l r
|
let add_forest forest rope len = let i = ref 0 in let sum = ref empty in while len > min_len . ( ! i + 1 ) do if forest . ( ! i ) . c <> Empty then begin sum := concat_fast forest . ( ! i ) . c ! sum ; forest . ( ! i ) . c <- Empty end ; incr i done ; sum := concat_fast ! sum rope ; let sum_len = ref ( length ! sum ) in while ! sum_len >= min_len . ( ! i ) do if forest . ( ! i ) . c <> Empty then begin sum := concat_fast forest . ( ! i ) . c ! sum ; sum_len := ! sum_len + forest . ( ! i ) . len ; forest . ( ! i ) . c <- Empty ; end ; incr i done ; decr i ; forest . ( ! i ) . c <- ! sum ; forest . ( ! i ) . len <- ! sum_len
|
let concat_forest forest = Array . fold_left ( fun s x -> concat_fast x . c s ) Empty forest
|
let rec balance_insert rope len forest = match rope with Empty -> ( ) | Leaf _ -> add_forest forest rope len | Concat ( l , cl , r , cr , h ) when h >= max_height || len < min_len . ( h ) -> balance_insert l cl forest ; balance_insert r cr forest | x -> add_forest forest x len
|
let balance r = match r with Empty -> Empty | Leaf _ -> r | _ -> let forest = Array . init max_height ( fun _ -> { c = Empty ; len = 0 } ) in balance_insert r ( length r ) forest ; concat_forest forest
|
let bal_if_needed l r = let r = make_concat l r in if height r < max_height then r else balance r
|
let concat_str l = function Empty | Concat ( _ , _ , _ , _ , _ ) -> invalid_arg " concat_str " | Leaf rs as r -> let lenr = STRING . length rs in match l with | Empty -> r | Leaf ls -> let slen = lenr + STRING . length ls in if slen <= leaf_size then Leaf ( str_append ls rs ) else make_concat l r | Concat ( ll , cll , Leaf lrs , clr , h ) -> let slen = clr + lenr in if clr + lenr <= leaf_size then Concat ( ll , cll , Leaf ( str_append lrs rs ) , slen , h ) else bal_if_needed l r | _ -> bal_if_needed l r
|
let append_char c r = concat_str r ( Leaf ( STRING . make 1 c ) )
|
let concat l = function Empty -> l | Leaf _ as r -> concat_str l r | Concat ( Leaf rls , rlc , rr , rc , h ) as r -> ( match l with Empty -> r | Concat ( _ , _ , _ , _ , _ ) -> bal_if_needed l r | Leaf ls -> let slen = rlc + STRING . length ls in if slen <= leaf_size then Concat ( Leaf ( str_append ls rls ) , slen , rr , rc , h ) else bal_if_needed l r ) | r -> ( match l with Empty -> r | _ -> bal_if_needed l r )
|
let prepend_char c r = concat ( Leaf ( STRING . make 1 c ) ) r
|
let rec get i = function Empty -> raise Out_of_bounds | Leaf s -> if i >= 0 && i < STRING . length s then STRING . unsafe_get s i else raise Out_of_bounds | Concat ( l , cl , r , _ , _ ) -> if i < cl then get i l else get ( i - cl ) r
|
let rec getn i = function Empty -> raise Out_of_bounds | Leaf _ -> 0 | Concat ( l , cl , r , _ , _ ) -> if i < cl then 1 + getn i l else 1 + getn ( i - cl ) r
|
let rec set i v = function Empty -> raise Out_of_bounds | Leaf s -> if i >= 0 && i < STRING . length s then let s = Bytes . of_string s in Bytes . unsafe_set s i v ; Leaf ( Bytes . unsafe_to_string s ) else raise Out_of_bounds | Concat ( l , cl , r , _ , _ ) -> if i < cl then concat ( set i v l ) r else concat l ( set ( i - cl ) v r )
|
let of_string = function s when STRING . length s = 0 -> Empty | s -> let min ( x : int ) ( y : int ) = if x <= y then x else y in let rec loop r s len i = if i < len then loop ( concat r ( Leaf ( STRING . sub s i ( min ( len - i ) leaf_size ) ) ) ) s len ( i + leaf_size ) else r in loop Empty s ( STRING . length s ) 0
|
let rec make len c = let rec concatloop len i r = if i <= len then concatloop len ( i * 2 ) ( concat r r ) else r in if len = 0 then Empty else if len <= leaf_size then Leaf ( STRING . make len c ) else let rope = concatloop len 2 ( of_string ( STRING . make 1 c ) ) in concat rope ( make ( len - length rope ) c )
|
let rec sub start len = function Empty -> if start <> 0 || len <> 0 then raise Out_of_bounds else Empty | Leaf s -> if len > 0 then ( try Leaf ( STRING . sub s start len ) with _ -> raise Out_of_bounds ) else if len < 0 || start < 0 || start > STRING . length s then raise Out_of_bounds else Empty | Concat ( l , cl , r , cr , _ ) -> if start < 0 || len < 0 || start + len > cl + cr then raise Out_of_bounds ; let left = if start = 0 then if len >= cl then l else sub 0 len l else if start > cl then Empty else if start + len >= cl then sub start ( cl - start ) l else sub start len l in let right = if start <= cl then let upto = start + len in if upto = cl + cr then r else if upto < cl then Empty else sub 0 ( upto - cl ) r else sub ( start - cl ) len r in concat left right
|
let insert start rope r = concat ( concat ( sub 0 start r ) rope ) ( sub start ( length r - start ) r )
|
let remove start len r = concat ( sub 0 start r ) ( sub ( start + len ) ( length r - start - len ) r )
|
let to_string r = let rec strings l = function Empty -> l | Leaf s -> s :: l | Concat ( left , _ , right , _ , _ ) -> strings ( strings l right ) left in string_of_string_list ( strings [ ] r )
|
let rec iter f = function Empty -> ( ) | Leaf s -> STRING . iter f s | Concat ( l , _ , r , _ , _ ) -> iter f l ; iter f r
|
let iteri f r = let rec aux f i = function Empty -> ( ) | Leaf s -> for j = 0 to STRING . length s - 1 do f ( i + j ) ( STRING . unsafe_get s j ) done | Concat ( l , cl , r , _ , _ ) -> aux f i l ; aux f ( i + cl ) r in aux f 0 r
|
let rec rangeiter f start len = function Empty -> if start <> 0 || len <> 0 then raise Out_of_bounds | Leaf s -> let n = start + len in let lens = STRING . length s in if start >= 0 && len >= 0 && n <= lens then for i = start to n - 1 do f ( STRING . unsafe_get s i ) done else raise Out_of_bounds | Concat ( l , cl , r , cr , _ ) -> if start < 0 || len < 0 || start + len > cl + cr then raise Out_of_bounds ; if start < cl then begin let upto = start + len in if upto <= cl then rangeiter f start len l else begin rangeiter f start ( cl - start ) l ; rangeiter f 0 ( upto - cl ) r end end else begin rangeiter f ( start - cl ) len r end
|
let rec fold f a = function Empty -> a | Leaf s -> let acc = ref a in for i = 0 to STRING . length s - 1 do acc := f ! acc ( STRING . unsafe_get s i ) done ; ! acc | Concat ( l , _ , r , _ , _ ) -> fold f ( fold f a l ) r
|
let set_title _ config soup = let make_title_string default prepend append title_opt = match title_opt with | None -> default | Some title -> let title = Printf . sprintf " % s % s " prepend title in let title = Printf . sprintf " % s % s " title append in title in let valid_options = List . append Config . common_widget_options [ " selector " ; " default " ; " prepend " ; " append " ; " force " ; " keep " ] in let ( ) = Config . check_options valid_options config " widget " \ title " " \ in let selectors = Config . find_strings_or ~ default [ " : h1 " ] config [ " selector " ] in let prepend = Config . find_string_or ~ default " " : config [ " prepend " ] in let append = Config . find_string_or ~ default " " : config [ " append " ] in let default_title = Config . find_string_or ~ default " " : config [ " default " ] in let force = Config . find_bool_or ~ default : false config [ " force " ] in let keep = Config . find_bool_or ~ default : false config [ " keep " ] in let ( ) = if force then begin let head = Soup . select_one " head " soup |> Option . get in let title_elem = Soup . create_element " title " in Soup . append_child head title_elem end in let title_node = Soup . select_one " head title " soup in match title_node with | None -> let ( ) = Logs . debug @@ fun m -> m " Page does not have a < title > element , assuming you don ' t want to set it " in Ok ( ) | Some title_node -> if ( not ( Html_utils . is_empty title_node ) ) && keep then Ok ( ) else let title_string = Html_utils . select_any_of selectors soup >>= Html_utils . get_element_text |> make_title_string default_title prepend append in let new_title_node = Printf . sprintf " < title >% s </ title " > title_string |> Soup . parse in let ( ) = Soup . replace title_node new_title_node in Ok ( )
|
type gifFrame = { imagephoto : imagePhoto ; frameWidth : int ; frameHeight : int ; left : int ; top : int ; delay : int }
|
type animatedGif = { frames : gifFrame list ; animWidth : int ; animHeight : int ; loop : int }
|
type imageType = | Still of Tk . options | Animated of animatedGif
|
let debug = ref false
|
let cTKtoCAMLgifFrame s = match splitlist s with | [ photo ; width ; height ; left ; top ; delay ] -> { imagephoto = cTKtoCAMLimagePhoto photo ; frameWidth = int_of_string width ; frameHeight = int_of_string height ; left = int_of_string left ; top = int_of_string top ; delay = int_of_string delay } | _ -> raise ( Invalid_argument ( " cTKtoCAMLgifFrame : " ^ s ) )
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.