text
stringlengths
0
601k
let record_backtrace ( ) = if Printexc . backtrace_status ( ) then backtrace := Some ( Printexc . get_backtrace ( ) )
let load_lambda ppf lam = if ! Clflags . dump_rawlambda then fprintf ppf " % a . " @ Printlambda . lambda lam ; let slam = Simplif . simplify_lambda lam in if ! Clflags . dump_lambda then fprintf ppf " % a . " @ Printlambda . lambda slam ; let ( init_code , fun_code ) = Bytegen . compile_phrase slam in if ! Clflags . d...
let pr_item = Printtyp . print_items ( fun env -> function | Sig_value ( id , { val_kind = Val_reg ; val_type } , _ ) -> Some ( outval_of_value env ( getvalue ( Translmod . toplevel_name id ) ) val_type ) | _ -> None )
let toplevel_env = ref Env . empty
let print_out_exception ppf exn outv = ! print_out_phrase ppf ( Ophr_exception ( exn , outv ) )
let print_exception_outcome ppf exn = if exn = Out_of_memory then Gc . full_major ( ) ; let outv = outval_of_value ! toplevel_env ( Obj . repr exn ) Predef . type_exn in print_out_exception ppf exn outv ; if Printexc . backtrace_status ( ) then match ! backtrace with | None -> ( ) | Some b -> print_string b ; backtrace...
let directive_table = ( Hashtbl . create 23 : ( string , directive_fun ) Hashtbl . t )
let directive_info_table = ( Hashtbl . create 23 : ( string , directive_info ) Hashtbl . t )
let add_directive name dir_fun dir_info = Hashtbl . add directive_table name dir_fun ; Hashtbl . add directive_info_table name dir_info
let execute_phrase print_outcome ppf phr = match phr with | Ptop_def sstr -> let oldenv = ! toplevel_env in Typecore . reset_delayed_checks ( ) ; let ( str , sg , sn , newenv ) = Typemod . type_toplevel_phrase oldenv sstr in if ! Clflags . dump_typedtree then Printtyped . implementation ppf str ; let sg ' = Typemod . S...
let execute_phrase print_outcome ppf phr = try execute_phrase print_outcome ppf phr with exn -> Warnings . reset_fatal ( ) ; raise exn
let use_print_results = ref true
let preprocess_phrase ppf phr = let phr = match phr with | Ptop_def str -> let str = Pparse . apply_rewriters_str ~ restore : true ~ tool_name " : ocaml " str in Ptop_def str | phr -> phr in if ! Clflags . dump_parsetree then Printast . top_phrase ppf phr ; if ! Clflags . dump_source then Pprintast . top_phrase ppf phr...
let use_file ppf wrap_mod name = try let ( filename , ic , must_close ) = if name = " " then ( " ( stdin ) " , stdin , false ) else begin let filename = Load_path . find name in let ic = open_in_bin filename in ( filename , ic , true ) end in let lb = Lexing . from_channel ic in Warnings . reset_fatal ( ) ; Location . ...
let mod_use_file ppf name = use_file ppf true name
let use_file ppf name = use_file ppf false name
let use_silently ppf name = protect_refs [ R ( use_print_results , false ) ] ( fun ( ) -> use_file ppf name )
let first_line = ref true
let got_eof = ref false ; ;
let read_input_default prompt buffer len = output_string stdout prompt ; flush stdout ; let i = ref 0 in try while true do if ! i >= len then raise Exit ; let c = input_char stdin in Bytes . set buffer ! i c ; Buffer . add_char phrase_buffer c ; incr i ; if c = ' \ n ' then raise Exit ; done ; ( ! i , false ) with | En...
let read_interactive_input = ref read_input_default
let refill_lexbuf buffer len = if ! got_eof then ( got_eof := false ; 0 ) else begin let prompt = if ! Clflags . noprompt then " " else if ! first_line then " # " else if ! Clflags . nopromptcont then " " else if Lexer . in_comment ( ) then " * " else " " in first_line := false ; let ( len , eof ) = ! read_interactive_...
let _ = if ! Sys . interactive then invalid_arg " The ocamltoplevel . cma library from compiler - libs \ cannot be loaded inside the OCaml toplevel " ; Sys . interactive := true ; let crc_intfs = Symtable . init_toplevel ( ) in Compmisc . init_path ( ) ; Env . import_crcs ~ source : Sys . executable_name crc_intfs ; ( ...
let find_ocamlinit ( ) = let ocamlinit = " . ocamlinit " in if Sys . file_exists ocamlinit then Some ocamlinit else let getenv var = match Sys . getenv var with | exception Not_found -> None | " " -> None | v -> Some v in let exists_in_dir dir file = match dir with | None -> None | Some dir -> let file = Filename . con...
let load_ocamlinit ppf = if ! Clflags . noinit then ( ) else match ! Clflags . init_file with | Some f -> if Sys . file_exists f then ignore ( use_silently ppf f ) else fprintf ppf " Init file not found : " \% s " . . " \@ f | None -> match find_ocamlinit ( ) with | None -> ( ) | Some file -> ignore ( use_silently ppf ...
let set_paths ( ) = let expand = Misc . expand_directory Config . standard_library in let current_load_path = Load_path . get_paths ( ) in let load_path = List . concat [ [ " " ] ; List . map expand ( List . rev ! Compenv . first_include_dirs ) ; List . map expand ( List . rev ! Clflags . include_dirs ) ; List . map ex...
let initialize_toplevel_env ( ) = toplevel_env := Compmisc . initial_env ( )
let loop ppf = Clflags . debug := true ; Location . formatter_for_warnings := ppf ; if not ! Clflags . noversion then fprintf ppf " OCaml version % s . . " @@ Config . version ; begin try initialize_toplevel_env ( ) with Env . Error _ | Typetexp . Error _ as exn -> Location . report_exception ppf exn ; exit 2 end ; let...
let override_sys_argv new_argv = caml_sys_modify_argv new_argv ; Arg . current := 0
let run_script ppf name args = override_sys_argv args ; Compmisc . init_path ~ dir ( : Filename . dirname name ) ( ) ; begin try toplevel_env := Compmisc . initial_env ( ) with Env . Error _ | Typetexp . Error _ as exn -> Location . report_exception ppf exn ; exit 2 end ; Sys . interactive := false ; run_hooks After_se...
module Ppx = struct let ppx_rewriters = ref [ ] let ( ) = Ast_mapper . register_function := ( fun _ f -> ppx_rewriters := f :: ! ppx_rewriters ) let preprocess_structure str = let open Ast_mapper in List . fold_right ( fun ppx_rewriter str -> let mapper = ppx_rewriter [ ] in mapper . structure mapper str ) ! ppx_rewrit...
let warnings = ref [ ]
let ( ) = Location . warning_reporter := ( fun loc w -> match Warnings . report w with | ` Inactive -> None | ` Active { Warnings . id = _ ; message ; is_error = _ ; sub_locs } -> let r = ( loc , message ) , sub_locs in warnings := r :: ! warnings ; None )
let return_success ( e : ' a ) : ' a toplevel_result = Ok ( e , ! warnings )
let return_error e : ' a toplevel_result = Error ( e , ! warnings )
let error_of_exn exn = match Location . error_of_exn exn with | None | Some ` Already_displayed -> let msg = match exn with | Failure msg -> msg | exn -> Printexc . to_string exn in let main = { Location . txt = ( fun fmt -> Format . pp_print_text fmt msg ) ; loc = Location . none } in { Location . main ; sub = [ ] ; k...
let return_exn exn = return_error ( of_report ( error_of_exn exn ) )
let trim_end s = let ws c = c = ' ' || c = ' \ t ' || c = ' \ n ' in let len = String . length s in let stop = ref ( len - 1 ) in while ! stop > 0 && ( ws s . [ ! stop ] ) do decr stop done ; String . sub s 0 ( ! stop + 1 )
let normalize code = let content = trim_end code in let len = String . length content in if content = " " then content else if ( len > 2 && content . [ len - 2 ] = ' ; ' && content . [ len - 1 ] = ' ; ' ) then content ^ " \ n " else content ^ " ; ; \ n "
let refill_lexbuf src ppf = let i = ref 0 in let max_i = String . length src in fun buf len -> if max_i <= ! i then 0 else let ( len , nl ) = try min len ( String . index_from src ! i ' \ n ' - ! i + 1 ) , false with Not_found | Invalid_argument _ -> min len ( max_i - ! i ) , true in String . blit src ! i buf 0 len ; F...
let init_loc lb filename = Location . input_name := filename ; Location . input_lexbuf := Some lb ; Location . init lb filename
let execute ? ppf_code ( ? print_outcome = true ) ~ ppf_answer code = let code = normalize code in let lb = match ppf_code with | Some ppf_code -> Lexing . from_function ( refill_lexbuf code ppf_code ) | None -> Lexing . from_string code in init_loc lb " // toplevel " ; // warnings := [ ] ; let rec loop ( ) = let phr =...
let use_string ( ? filename = " // toplevel " ) // ( ? print_outcome = true ) ~ ppf_answer code = let lb = Lexing . from_string code in init_loc lb filename ; warnings := [ ] ; try List . iter ( fun phr -> if not ( Toploop . execute_phrase print_outcome ppf_answer phr ) then raise Exit else Format . pp_print_flush ppf_...
let parse_mod_string ? filename modname sig_code impl_code = let open Parsetree in let open Ast_helper in let str = let impl_lb = Lexing . from_string impl_code in init_loc impl_lb ( match filename with | None -> String . uncapitalize_ascii modname ^ " . ml " | Some f -> f ) ; Parse . implementation impl_lb in let m = ...
let use_mod_string ? filename ( ? print_outcome = true ) ~ ppf_answer ~ modname ? sig_code impl_code = if String . capitalize_ascii modname <> modname then invalid_arg " Learnocaml_toplevel_toploop . use_mod_string : \ the module name must start with a capital letter . " ; warnings := [ ] ; try let phr = Ppx . preproce...
let check_phrase env = function | Parsetree . Ptop_def sstr -> Typecore . reset_delayed_checks ( ) ; let ( str , sg , sn , newenv ) = Typemod . type_toplevel_phrase env sstr in let sg ' = Typemod . Signature_names . simplify newenv sn sg in ignore ( Includemod . signatures env ~ mark : Includemod . Mark_positive sg sg ...
let check ( ? setenv = false ) code = let lb = Lexing . from_string code in init_loc lb " // toplevel " ; // warnings := [ ] ; try let env = List . fold_left check_phrase ! Toploop . toplevel_env ( List . map Ppx . preprocess_phrase ( ! Toploop . parse_use_file lb ) ) in if setenv then Toploop . toplevel_env := env ; r...
let split_primitives p = let len = String . length p in let rec split beg cur = if cur >= len then [ ] else if p . [ cur ] = ' \ 000 ' then String . sub p beg ( cur - beg ) :: split ( cur + 1 ) ( cur + 1 ) else split beg ( cur + 1 ) in Array . of_list ( split 0 0 )
let setup = lazy ( Hashtbl . add Toploop . directive_table " enable " ( Toploop . Directive_string Config . Flag . enable ) ; Hashtbl . add Toploop . directive_table " disable " ( Toploop . Directive_string Config . Flag . disable ) ; Hashtbl . add Toploop . directive_table " debug_on " ( Toploop . Directive_string Deb...
let initialize cmi_dirs = List . iter Topdirs . dir_directory cmi_dirs ; Lazy . force setup ; Toploop . initialize_toplevel_env ( ) ; Toploop . input_name := " // toplevel " //
type redirection = { channel : out_channel ; name : string ; tee : string -> string -> unit ; callback : string -> unit ; prev : redirection option }
let redirections : ( out_channel * redirection ref ) list ref = ref [ ]
let redirect_channel ( ? tee = ( fun _ _ -> ( ) ) ) name channel callback = try flush channel ; let cur = List . assq channel ! redirections in cur := { channel ; name ; tee ; callback ; prev = Some ! cur } ; ! cur with Not_found -> let cur = ref { channel ; name ; tee ; callback ; prev = None } in redirections := ( ch...
let flush_redirected_channel redir = flush redir . channel
let stop_channel_redirection redir = let fail ( ) = invalid_arg " Toploop_jsoo . stop_channel_redirection " in try let cur = List . assq redir . channel ! redirections in if ! cur != redir then fail ( ) ; flush_redirected_channel redir ; match redir . prev with | Some prev -> cur := prev | None -> redirections := List ...
let map_opt f = function | None -> None | Some x -> Some ( f x )
let iter_opt f = function | None -> ( ) | Some x -> f x
type redirection = { channel : out_channel ; target_fd : Unix . file_descr ; backup_fd : Unix . file_descr ; read_fd : Unix . file_descr ; append : string -> unit }
let redirections = ref [ ]
let redirect_channel ? tee name channel append = flush channel ; let append data = let tee = map_opt ( fun tee -> tee name ) tee in iter_opt ( fun tee -> tee data ) tee ; append data in let target_fd = Unix . descr_of_out_channel channel in let backup_fd = Unix . dup target_fd in let stack = try List . assq target_fd !...
let flush_redirected_channel { read_fd ; append ; channel ; _ } = let buf = Bytes . create 503 in let rec loop ( ) = let len = Unix . read read_fd buf 0 ( Bytes . length buf ) in let data = Bytes . sub_string buf 0 len in append data ; loop ( ) in flush channel ; try loop ( ) with _ -> ( )
let stop_channel_redirection ( { target_fd ; read_fd ; backup_fd ; _ } as redirection ) = let fail ( ) = invalid_arg " Toploop_unix . stop_channel_redirection " in match List . assq target_fd ! redirections with | exception Not_found -> fail ( ) | [ ] -> fail ( ) | redirection ' :: rest -> if redirection ' != redirecti...
let initialize ( ) = Toploop . initialize_toplevel_env ( )
let usage = " Usage : ocaml < options > < object - files > [ script - file [ arguments ] ] \ n \ options are " :
let preload_objects = ref [ ]
let first_nonexpanded_pos = ref 0
let current = ref ( ! Arg . current )
let argv = ref Sys . argv
let is_expanded pos = pos < ! first_nonexpanded_pos
let expand_position pos len = if pos < ! first_nonexpanded_pos then first_nonexpanded_pos := ! first_nonexpanded_pos + len else first_nonexpanded_pos := pos + len + 2
let prepare ppf = Toploop . set_paths ( ) ; try let res = let objects = List . rev ( ! preload_objects @ ! first_objfiles ) in List . for_all ( Topdirs . load_file ppf ) objects in Toploop . run_hooks Toploop . Startup ; res with x -> try Location . report_exception ppf x ; false with x -> Format . fprintf ppf " Uncaug...
let file_argument name = let ppf = Format . err_formatter in if Filename . check_suffix name " . cmo " || Filename . check_suffix name " . cma " then preload_objects := name :: ! preload_objects else if is_expanded ! current then begin Printf . eprintf " For implementation reasons , the toplevel does not support \ \ ha...
let wrap_expand f s = let start = ! current in let arr = f s in expand_position start ( Array . length arr ) ; arr
module Options = Main_args . Make_bytetop_options ( struct include Main_args . Default . Topmain let _stdin ( ) = file_argument " " let _args = wrap_expand Arg . read_arg let _args0 = wrap_expand Arg . read_arg0 let anonymous s = file_argument s end ) ; ;
let ( ) = let extra_paths = match Sys . getenv " OCAMLTOP_INCLUDE_PATH " with | exception Not_found -> [ ] | s -> Misc . split_path_contents s in Clflags . include_dirs := List . rev_append extra_paths ! Clflags . include_dirs
let main ( ) = let ppf = Format . err_formatter in Compenv . readenv ppf Before_args ; let list = ref Options . list in begin try Arg . parse_and_expand_argv_dynamic current argv list file_argument usage ; with | Arg . Bad msg -> Printf . eprintf " % s " msg ; exit 2 | Arg . Help msg -> Printf . printf " % s " msg ; ex...
module type G = sig type t module V : Sig . COMPARABLE val iter_vertex : ( V . t -> unit ) -> t -> unit val iter_succ : ( V . t -> unit ) -> t -> V . t -> unit end
module Make ( G : G ) = struct module Scc = Components . Make ( G ) let fold f g acc = let n , scc = Scc . scc g in let vertices = Array . make n [ ] in let edges = Array . make n [ ] in let degree = Array . make n 0 in let add_vertex x = let ix = scc x in vertices . ( ix ) <- x :: vertices . ( ix ) ; let add_edge y = ...
module Make_stable ( G : sig include G val in_degree : t -> V . t -> int end ) = struct module H = Hashtbl . Make ( G . V ) module C = Path . Check ( G ) let choose ~ old ( v , n : G . V . t * int ) = let l , min = old in if n = min then v :: l , n else if n < min then [ v ] , n else old module Q = struct module S = Se...
let infname = ref None
let outfname = ref None
let pingall = ref false
let prologue = ref " static / mn_prologue . txt "
let epilogue = ref " static / mn_epilogue . txt "
let arg_spec = [ ( " - o " , Arg . String ( fun s -> outfname := Some s ) , " \ tWrite topology to a file " ) ; ( " -- prologue " , Arg . String ( fun s -> prologue := s ) , " \ tPrologue for Mininet scripts " ) ; ( " -- epilogue " , Arg . String ( fun s -> epilogue := s ) , " \ tEpilogue for Mininet scripts " ) ; ( " ...
let usage = Printf . sprintf " usage : % s filename . [ dot | gml ] - o filename . [ dot | py ] " Sys . argv . ( 0 )
let to_mininet ( t : Net . Topology . t ) : string = if ! pingall then Net . Pretty . to_mininet ~ prologue_file " : static / pingall_prologue . txt " ~ epilogue_file " : static / pingall_epilogue . txt " t else Net . Pretty . to_mininet ~ prologue_file :! prologue ~ epilogue_file :! epilogue t
let from_extension ( fname : string ) : Net . Topology . t = if check_suffix fname " . dot " then Net . Parse . from_dotfile fname else if check_suffix fname " . gml " then Net . Parse . from_gmlfile fname else failwith " Cannot parse given file type "
let to_extension fname topo = if check_suffix fname " . dot " then Net . Pretty . to_dot topo else if check_suffix fname " . gml " then failwith " \ nWriting to GML format not supported yet \ n " else if check_suffix fname " . py " then to_mininet topo else failwith " Cannot write to given file type "
let _ = Arg . parse arg_spec ( fun fn -> infname := Some fn ) usage ; let topo = match ! infname with | None -> begin Arg . usage arg_spec usage ; exit 1 end | Some fname -> from_extension fname in match ! outfname with | None -> Printf . printf " \ nMininet script : % s \ n \ n " ( to_mininet topo ) | Some fname -> le...
let rec collect_names acc x = match x with | Repeat x | Repeat1 x | Optional x -> collect_names acc x | Seq l -> List . fold_left ( fun acc x -> collect_names acc x ) acc l | Choice l -> List . fold_left ( fun acc ( _ , x ) -> collect_names acc x ) acc l | Symbol name -> name :: acc | Token _ | Blank -> acc | Alias ( n...
let extract_rule_deps ( rule : rule ) = let deps = collect_names [ ] rule . body in if debug then printf " % s -> % s \ n " %! rule . name ( String . concat " " deps ) ; ( rule . name , deps )
let tsort get_deps elts = let deps_data = List . map ( fun elt -> let id , deps = get_deps elt in let deps = List . sort compare deps in let self_dep = List . mem id deps in ( id , deps , self_dep , elt ) ) elts |> List . sort ( fun ( a , _ , _ , _ ) ( b , _ , _ , _ ) -> compare a b ) in let tbl = Hashtbl . create 100 ...
let sort rules = tsort extract_rule_deps rules
module Make ( Keys : Top_closure_intf . Keys ) ( Monad : Monad_intf . S ) = struct open Monad . O let top_closure ~ key ~ deps elements = let rec loop res visited elt ~ temporarily_marked = let key = key elt in if Keys . mem temporarily_marked key then Monad . return ( Error [ elt ] ) else if not ( Keys . mem visited k...
module Int = Make ( Int . Set ) ( Monad . Id )
module String = Make ( String . Set ) ( Monad . Id )
module C ( F : Cstubs . FOREIGN ) = struct open F let manual_seed = foreign " at_manual_seed " ( int64_t @-> returning void ) let free = foreign " free " ( ptr void @-> returning void ) let get_num_threads = foreign " at_get_num_threads " ( void @-> returning int ) let set_num_threads = foreign " at_set_num_threads " (...
module C0 ( F : Cstubs . FOREIGN ) = struct open F type t = unit ptr let t : t typ = ptr void type scalar = unit ptr let scalar : scalar typ = ptr void let stubs___and__ = foreign " atg___and__ " ( ptr t @-> t @-> scalar @-> returning void ) let stubs___and__tensor_ = foreign " atg___and__tensor_ " ( ptr t @-> t @-> t ...
module C1 ( F : Cstubs . FOREIGN ) = struct open F type t = unit ptr let t : t typ = ptr void type scalar = unit ptr let scalar : scalar typ = ptr void let stubs__index_put_impl_ = foreign " atg__index_put_impl_ " ( ptr t @-> t @-> ptr t @-> int @-> t @-> int @-> int @-> returning void ) let stubs__indices = foreign " ...