1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* under the terms of the GNU Library General Public License, with *)
|
|
|
|
(* the special exception on linking described in file ../LICENSE. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
1999-11-30 08:07:38 -08:00
|
|
|
(* type 'a option = None | Some of 'a *)
|
1995-10-23 09:58:50 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Exceptions *)
|
|
|
|
|
2013-11-08 08:18:21 -08:00
|
|
|
external register_named_value : string -> 'a -> unit
|
|
|
|
= "caml_register_named_value"
|
|
|
|
|
|
|
|
let () =
|
|
|
|
(* for asmrun/fail.c *)
|
|
|
|
register_named_value "Pervasives.array_bound_error"
|
|
|
|
(Invalid_argument "index out of bounds")
|
|
|
|
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
external raise : exn -> 'a = "%raise"
|
2013-10-14 06:52:14 -07:00
|
|
|
external raise_notrace : exn -> 'a = "%raise_notrace"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let failwith s = raise(Failure s)
|
|
|
|
let invalid_arg s = raise(Invalid_argument s)
|
|
|
|
|
|
|
|
exception Exit
|
|
|
|
|
2013-06-03 11:22:31 -07:00
|
|
|
(* Composition operators *)
|
|
|
|
|
2013-09-04 08:12:37 -07:00
|
|
|
external ( |> ) : 'a -> ('a -> 'b) -> 'b = "%revapply"
|
2013-06-03 11:22:31 -07:00
|
|
|
external ( @@ ) : ('a -> 'b) -> 'a -> 'b = "%apply"
|
|
|
|
|
2014-04-10 07:11:25 -07:00
|
|
|
(* Debugging *)
|
|
|
|
|
|
|
|
external __LOC__ : string = "%loc_LOC"
|
|
|
|
external __FILE__ : string = "%loc_FILE"
|
|
|
|
external __LINE__ : int = "%loc_LINE"
|
|
|
|
external __MODULE__ : string = "%loc_MODULE"
|
|
|
|
external __POS__ : string * int * int * int = "%loc_POS"
|
|
|
|
|
|
|
|
external __LOC_OF__ : 'a -> string * 'a = "%loc_LOC"
|
|
|
|
external __LINE_OF__ : 'a -> int * 'a = "%loc_LINE"
|
|
|
|
external __POS_OF__ : 'a -> (string * int * int * int) * 'a = "%loc_POS"
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Comparisons *)
|
|
|
|
|
2010-06-09 04:00:56 -07:00
|
|
|
external ( = ) : 'a -> 'a -> bool = "%equal"
|
|
|
|
external ( <> ) : 'a -> 'a -> bool = "%notequal"
|
|
|
|
external ( < ) : 'a -> 'a -> bool = "%lessthan"
|
|
|
|
external ( > ) : 'a -> 'a -> bool = "%greaterthan"
|
|
|
|
external ( <= ) : 'a -> 'a -> bool = "%lessequal"
|
|
|
|
external ( >= ) : 'a -> 'a -> bool = "%greaterequal"
|
|
|
|
external compare : 'a -> 'a -> int = "%compare"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let min x y = if x <= y then x else y
|
|
|
|
let max x y = if x >= y then x else y
|
|
|
|
|
2010-06-09 04:00:56 -07:00
|
|
|
external ( == ) : 'a -> 'a -> bool = "%eq"
|
|
|
|
external ( != ) : 'a -> 'a -> bool = "%noteq"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Boolean operations *)
|
|
|
|
|
|
|
|
external not : bool -> bool = "%boolnot"
|
2010-06-09 04:00:56 -07:00
|
|
|
external ( & ) : bool -> bool -> bool = "%sequand"
|
|
|
|
external ( && ) : bool -> bool -> bool = "%sequand"
|
|
|
|
external ( or ) : bool -> bool -> bool = "%sequor"
|
|
|
|
external ( || ) : bool -> bool -> bool = "%sequor"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Integer operations *)
|
|
|
|
|
2010-06-09 04:00:56 -07:00
|
|
|
external ( ~- ) : int -> int = "%negint"
|
|
|
|
external ( ~+ ) : int -> int = "%identity"
|
1995-05-04 03:15:53 -07:00
|
|
|
external succ : int -> int = "%succint"
|
|
|
|
external pred : int -> int = "%predint"
|
2010-06-09 04:00:56 -07:00
|
|
|
external ( + ) : int -> int -> int = "%addint"
|
|
|
|
external ( - ) : int -> int -> int = "%subint"
|
Use a nominal datatype for CamlinternalFormat.format6
This should make the type-checking of formats simpler and more robust:
instead of trying to find a pair as previously, we can now use the
path of the format6 type directly.
A nice side-effect of the change is that the internal definition of
formats (as a pair) is not printed in error messages anymore.
Because format6 is in fact defined in the CamlinternalFormatBasics
submodule of Pervasives, and has an alias at the toplevel of
Pervasives, error messages still expand the definition:
> Error: This expression has type
> ('a, 'b, 'c, 'd, 'd, 'a) format6 =
> ('a, 'b, 'c, 'd, 'd, 'a) CamlinternalFormatBasics.format6
> but an expression was expected of type ...
Passing the option `-short-paths` does avoid this expansion and
returns exactly the same error message as 4.01:
> Error: This expression has type ('a, 'b, 'c, 'd, 'd, 'a) format6
> but an expression was expected of type ...
(To get this error message without -short-paths, one would need to
define format6 directly in Pervasives; but this type is mutually
recursive with several GADT types that we don't want to add in the
Pervasives namespace unqualified. This is why I'll keep the alias
for now.)
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@14868 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
2014-05-14 08:07:13 -07:00
|
|
|
external ( * ) : int -> int -> int = "%mulint"
|
2010-06-09 04:00:56 -07:00
|
|
|
external ( / ) : int -> int -> int = "%divint"
|
|
|
|
external ( mod ) : int -> int -> int = "%modint"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let abs x = if x >= 0 then x else -x
|
|
|
|
|
2010-06-09 04:00:56 -07:00
|
|
|
external ( land ) : int -> int -> int = "%andint"
|
|
|
|
external ( lor ) : int -> int -> int = "%orint"
|
|
|
|
external ( lxor ) : int -> int -> int = "%xorint"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let lnot x = x lxor (-1)
|
|
|
|
|
2010-06-09 04:00:56 -07:00
|
|
|
external ( lsl ) : int -> int -> int = "%lslint"
|
|
|
|
external ( lsr ) : int -> int -> int = "%lsrint"
|
|
|
|
external ( asr ) : int -> int -> int = "%asrint"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2014-03-20 09:09:33 -07:00
|
|
|
let max_int = (-1) lsr 1
|
|
|
|
let min_int = max_int + 1
|
1995-09-05 02:21:25 -07:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Floating-point operations *)
|
|
|
|
|
2010-06-09 04:00:56 -07:00
|
|
|
external ( ~-. ) : float -> float = "%negfloat"
|
|
|
|
external ( ~+. ) : float -> float = "%identity"
|
|
|
|
external ( +. ) : float -> float -> float = "%addfloat"
|
|
|
|
external ( -. ) : float -> float -> float = "%subfloat"
|
1995-07-02 09:46:44 -07:00
|
|
|
external ( *. ) : float -> float -> float = "%mulfloat"
|
2010-06-09 04:00:56 -07:00
|
|
|
external ( /. ) : float -> float -> float = "%divfloat"
|
2015-10-06 03:58:24 -07:00
|
|
|
external ( ** ) : float -> float -> float = "caml_power_float" "pow"
|
|
|
|
[@@unboxed] [@@noalloc]
|
|
|
|
external exp : float -> float = "caml_exp_float" "exp" [@@unboxed] [@@noalloc]
|
|
|
|
external expm1 : float -> float = "caml_expm1_float" "caml_expm1"
|
|
|
|
[@@unboxed] [@@noalloc]
|
|
|
|
external acos : float -> float = "caml_acos_float" "acos"
|
|
|
|
[@@unboxed] [@@noalloc]
|
|
|
|
external asin : float -> float = "caml_asin_float" "asin"
|
|
|
|
[@@unboxed] [@@noalloc]
|
|
|
|
external atan : float -> float = "caml_atan_float" "atan"
|
|
|
|
[@@unboxed] [@@noalloc]
|
|
|
|
external atan2 : float -> float -> float = "caml_atan2_float" "atan2"
|
|
|
|
[@@unboxed] [@@noalloc]
|
2012-01-12 07:46:51 -08:00
|
|
|
external hypot : float -> float -> float
|
2015-10-06 03:58:24 -07:00
|
|
|
= "caml_hypot_float" "caml_hypot" [@@unboxed] [@@noalloc]
|
|
|
|
external cos : float -> float = "caml_cos_float" "cos" [@@unboxed] [@@noalloc]
|
|
|
|
external cosh : float -> float = "caml_cosh_float" "cosh"
|
|
|
|
[@@unboxed] [@@noalloc]
|
|
|
|
external log : float -> float = "caml_log_float" "log" [@@unboxed] [@@noalloc]
|
|
|
|
external log10 : float -> float = "caml_log10_float" "log10"
|
|
|
|
[@@unboxed] [@@noalloc]
|
|
|
|
external log1p : float -> float = "caml_log1p_float" "caml_log1p"
|
|
|
|
[@@unboxed] [@@noalloc]
|
|
|
|
external sin : float -> float = "caml_sin_float" "sin" [@@unboxed] [@@noalloc]
|
|
|
|
external sinh : float -> float = "caml_sinh_float" "sinh"
|
|
|
|
[@@unboxed] [@@noalloc]
|
|
|
|
external sqrt : float -> float = "caml_sqrt_float" "sqrt"
|
|
|
|
[@@unboxed] [@@noalloc]
|
|
|
|
external tan : float -> float = "caml_tan_float" "tan" [@@unboxed] [@@noalloc]
|
|
|
|
external tanh : float -> float = "caml_tanh_float" "tanh"
|
|
|
|
[@@unboxed] [@@noalloc]
|
|
|
|
external ceil : float -> float = "caml_ceil_float" "ceil"
|
|
|
|
[@@unboxed] [@@noalloc]
|
|
|
|
external floor : float -> float = "caml_floor_float" "floor"
|
|
|
|
[@@unboxed] [@@noalloc]
|
1996-03-07 05:46:46 -08:00
|
|
|
external abs_float : float -> float = "%absfloat"
|
2012-01-12 07:46:51 -08:00
|
|
|
external copysign : float -> float -> float
|
2015-10-06 03:58:24 -07:00
|
|
|
= "caml_copysign_float" "caml_copysign"
|
|
|
|
[@@unboxed] [@@noalloc]
|
|
|
|
external mod_float : float -> float -> float = "caml_fmod_float" "fmod"
|
|
|
|
[@@unboxed] [@@noalloc]
|
2004-01-02 11:23:29 -08:00
|
|
|
external frexp : float -> float * int = "caml_frexp_float"
|
|
|
|
external ldexp : float -> int -> float = "caml_ldexp_float"
|
|
|
|
external modf : float -> float * float = "caml_modf_float"
|
1995-07-11 01:54:13 -07:00
|
|
|
external float : int -> float = "%floatofint"
|
1998-12-02 02:40:33 -08:00
|
|
|
external float_of_int : int -> float = "%floatofint"
|
1995-07-11 01:54:13 -07:00
|
|
|
external truncate : float -> int = "%intoffloat"
|
1998-12-02 02:40:33 -08:00
|
|
|
external int_of_float : float -> int = "%intoffloat"
|
2004-01-01 08:42:43 -08:00
|
|
|
external float_of_bits : int64 -> float = "caml_int64_float_of_bits"
|
2001-02-05 00:55:25 -08:00
|
|
|
let infinity =
|
2003-12-20 08:24:35 -08:00
|
|
|
float_of_bits 0x7F_F0_00_00_00_00_00_00L
|
2001-02-05 00:55:25 -08:00
|
|
|
let neg_infinity =
|
2003-12-20 08:24:35 -08:00
|
|
|
float_of_bits 0xFF_F0_00_00_00_00_00_00L
|
2001-02-05 00:55:25 -08:00
|
|
|
let nan =
|
2003-12-20 08:24:35 -08:00
|
|
|
float_of_bits 0x7F_F0_00_00_00_00_00_01L
|
2001-10-30 06:29:29 -08:00
|
|
|
let max_float =
|
2003-12-20 08:24:35 -08:00
|
|
|
float_of_bits 0x7F_EF_FF_FF_FF_FF_FF_FFL
|
2001-10-30 06:29:29 -08:00
|
|
|
let min_float =
|
2003-12-20 08:24:35 -08:00
|
|
|
float_of_bits 0x00_10_00_00_00_00_00_00L
|
2001-10-30 06:29:29 -08:00
|
|
|
let epsilon_float =
|
2003-12-20 08:24:35 -08:00
|
|
|
float_of_bits 0x3C_B0_00_00_00_00_00_00L
|
2005-10-25 11:34:07 -07:00
|
|
|
|
2001-02-05 00:55:25 -08:00
|
|
|
type fpclass =
|
|
|
|
FP_normal
|
|
|
|
| FP_subnormal
|
|
|
|
| FP_zero
|
|
|
|
| FP_infinite
|
|
|
|
| FP_nan
|
2010-06-09 04:00:56 -07:00
|
|
|
external classify_float : float -> fpclass = "caml_classify_float"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2014-04-29 04:56:17 -07:00
|
|
|
(* String and byte sequence operations -- more in modules String and Bytes *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1998-10-01 05:33:55 -07:00
|
|
|
external string_length : string -> int = "%string_length"
|
2014-04-29 04:56:17 -07:00
|
|
|
external bytes_length : bytes -> int = "%string_length"
|
|
|
|
external bytes_create : int -> bytes = "caml_create_string"
|
|
|
|
external string_blit : string -> int -> bytes -> int -> int -> unit
|
2015-10-06 03:58:22 -07:00
|
|
|
= "caml_blit_string" [@@noalloc]
|
2014-04-29 04:56:17 -07:00
|
|
|
external bytes_blit : bytes -> int -> bytes -> int -> int -> unit
|
2015-10-06 03:58:22 -07:00
|
|
|
= "caml_blit_string" [@@noalloc]
|
2014-04-29 04:56:17 -07:00
|
|
|
external bytes_unsafe_to_string : bytes -> string = "%identity"
|
|
|
|
external bytes_unsafe_of_string : string -> bytes = "%identity"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2010-06-09 04:00:56 -07:00
|
|
|
let ( ^ ) s1 s2 =
|
1995-05-04 03:15:53 -07:00
|
|
|
let l1 = string_length s1 and l2 = string_length s2 in
|
2014-04-29 04:56:17 -07:00
|
|
|
let s = bytes_create (l1 + l2) in
|
1995-05-04 03:15:53 -07:00
|
|
|
string_blit s1 0 s 0 l1;
|
|
|
|
string_blit s2 0 s l1 l2;
|
2014-04-29 04:56:17 -07:00
|
|
|
bytes_unsafe_to_string s
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1998-12-08 06:53:55 -08:00
|
|
|
(* Character operations -- more in module Char *)
|
|
|
|
|
|
|
|
external int_of_char : char -> int = "%identity"
|
|
|
|
external unsafe_char_of_int : int -> char = "%identity"
|
|
|
|
let char_of_int n =
|
2002-07-12 02:47:54 -07:00
|
|
|
if n < 0 || n > 255 then invalid_arg "char_of_int" else unsafe_char_of_int n
|
1998-12-08 06:53:55 -08:00
|
|
|
|
1999-02-24 07:21:50 -08:00
|
|
|
(* Unit operations *)
|
|
|
|
|
|
|
|
external ignore : 'a -> unit = "%ignore"
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Pair operations *)
|
|
|
|
|
|
|
|
external fst : 'a * 'b -> 'a = "%field0"
|
|
|
|
external snd : 'a * 'b -> 'b = "%field1"
|
|
|
|
|
2014-04-15 06:10:33 -07:00
|
|
|
(* References *)
|
|
|
|
|
|
|
|
type 'a ref = { mutable contents : 'a }
|
|
|
|
external ref : 'a -> 'a ref = "%makemutable"
|
|
|
|
external ( ! ) : 'a ref -> 'a = "%field0"
|
|
|
|
external ( := ) : 'a ref -> 'a -> unit = "%setfield0"
|
|
|
|
external incr : int ref -> unit = "%incr"
|
|
|
|
external decr : int ref -> unit = "%decr"
|
|
|
|
|
2015-04-12 11:44:59 -07:00
|
|
|
(* Result type *)
|
|
|
|
|
|
|
|
type ('a,'b) result = Ok of 'a | Error of 'b
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* String conversion functions *)
|
|
|
|
|
2010-06-09 04:00:56 -07:00
|
|
|
external format_int : string -> int -> string = "caml_format_int"
|
|
|
|
external format_float : string -> float -> string = "caml_format_float"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let string_of_bool b =
|
|
|
|
if b then "true" else "false"
|
1998-12-02 02:40:33 -08:00
|
|
|
let bool_of_string = function
|
|
|
|
| "true" -> true
|
|
|
|
| "false" -> false
|
1999-10-02 05:09:43 -07:00
|
|
|
| _ -> invalid_arg "bool_of_string"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let string_of_int n =
|
|
|
|
format_int "%d" n
|
|
|
|
|
2004-01-01 08:42:43 -08:00
|
|
|
external int_of_string : string -> int = "caml_int_of_string"
|
2014-05-12 08:37:31 -07:00
|
|
|
external string_get : string -> int -> char = "%string_safe_get"
|
2002-01-24 23:07:49 -08:00
|
|
|
|
2002-09-05 03:31:18 -07:00
|
|
|
let valid_float_lexem s =
|
2002-01-24 09:47:21 -08:00
|
|
|
let l = string_length s in
|
|
|
|
let rec loop i =
|
|
|
|
if i >= l then s ^ "." else
|
2014-04-29 04:56:17 -07:00
|
|
|
match string_get s i with
|
2010-06-09 04:00:56 -07:00
|
|
|
| '0' .. '9' | '-' -> loop (i + 1)
|
2003-07-26 11:50:52 -07:00
|
|
|
| _ -> s
|
2002-01-24 09:47:21 -08:00
|
|
|
in
|
|
|
|
loop 0
|
|
|
|
;;
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2003-07-29 02:08:42 -07:00
|
|
|
let string_of_float f = valid_float_lexem (format_float "%.12g" f);;
|
2002-09-05 03:31:18 -07:00
|
|
|
|
2004-01-02 11:23:29 -08:00
|
|
|
external float_of_string : string -> float = "caml_float_of_string"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* List operations -- more in module List *)
|
|
|
|
|
2010-06-09 04:00:56 -07:00
|
|
|
let rec ( @ ) l1 l2 =
|
1998-07-31 05:49:52 -07:00
|
|
|
match l1 with
|
|
|
|
[] -> l2
|
|
|
|
| hd :: tl -> hd :: (tl @ l2)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2014-12-13 14:13:34 -08:00
|
|
|
(* Array index operators *)
|
|
|
|
external ( .() ) : 'a array -> int -> 'a = "%array_opt_get"
|
|
|
|
external ( .() <- ) : 'a array -> int -> 'a -> unit = "%array_opt_set"
|
|
|
|
|
|
|
|
(* String index operators *)
|
|
|
|
external ( .[] ) : string -> int -> char = "%string_opt_get"
|
|
|
|
external ( .[] <- ) : bytes -> int -> char -> unit = "%string_opt_set"
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* I/O operations *)
|
|
|
|
|
|
|
|
type in_channel
|
|
|
|
type out_channel
|
|
|
|
|
2012-01-12 07:46:51 -08:00
|
|
|
external open_descriptor_out : int -> out_channel
|
|
|
|
= "caml_ml_open_descriptor_out"
|
2010-06-09 04:00:56 -07:00
|
|
|
external open_descriptor_in : int -> in_channel = "caml_ml_open_descriptor_in"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let stdin = open_descriptor_in 0
|
|
|
|
let stdout = open_descriptor_out 1
|
|
|
|
let stderr = open_descriptor_out 2
|
|
|
|
|
|
|
|
(* General output functions *)
|
|
|
|
|
1995-05-04 05:48:07 -07:00
|
|
|
type open_flag =
|
1995-08-10 05:18:40 -07:00
|
|
|
Open_rdonly | Open_wronly | Open_append
|
|
|
|
| Open_creat | Open_trunc | Open_excl
|
1996-04-29 06:23:25 -07:00
|
|
|
| Open_binary | Open_text | Open_nonblock
|
1995-05-04 05:48:07 -07:00
|
|
|
|
2010-06-09 04:00:56 -07:00
|
|
|
external open_desc : string -> open_flag list -> int -> int = "caml_sys_open"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2015-07-24 06:11:26 -07:00
|
|
|
external set_out_channel_name: out_channel -> string -> unit =
|
|
|
|
"caml_ml_set_channel_name"
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let open_out_gen mode perm name =
|
2015-07-24 06:11:26 -07:00
|
|
|
let c = open_descriptor_out(open_desc name mode perm) in
|
|
|
|
set_out_channel_name c name;
|
|
|
|
c
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let open_out name =
|
|
|
|
open_out_gen [Open_wronly; Open_creat; Open_trunc; Open_text] 0o666 name
|
|
|
|
|
|
|
|
let open_out_bin name =
|
|
|
|
open_out_gen [Open_wronly; Open_creat; Open_trunc; Open_binary] 0o666 name
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
external flush : out_channel -> unit = "caml_ml_flush"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2005-10-25 11:34:07 -07:00
|
|
|
external out_channels_list : unit -> out_channel list
|
2003-12-29 14:15:02 -08:00
|
|
|
= "caml_ml_out_channels_list"
|
2001-10-09 08:14:01 -07:00
|
|
|
|
2005-10-25 11:34:07 -07:00
|
|
|
let flush_all () =
|
2001-10-09 08:14:01 -07:00
|
|
|
let rec iter = function
|
|
|
|
[] -> ()
|
2010-06-09 04:00:56 -07:00
|
|
|
| a :: l -> (try flush a with _ -> ()); iter l
|
2001-10-09 08:14:01 -07:00
|
|
|
in iter (out_channels_list ())
|
|
|
|
|
2014-04-29 04:56:17 -07:00
|
|
|
external unsafe_output : out_channel -> bytes -> int -> int -> unit
|
2003-12-29 14:15:02 -08:00
|
|
|
= "caml_ml_output"
|
2014-08-22 06:45:02 -07:00
|
|
|
external unsafe_output_string : out_channel -> string -> int -> int -> unit
|
|
|
|
= "caml_ml_output"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
external output_char : out_channel -> char -> unit = "caml_ml_output_char"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2014-04-29 04:56:17 -07:00
|
|
|
let output_bytes oc s =
|
|
|
|
unsafe_output oc s 0 (bytes_length s)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let output_string oc s =
|
2014-08-22 06:45:02 -07:00
|
|
|
unsafe_output_string oc s 0 (string_length s)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let output oc s ofs len =
|
2014-04-29 04:56:17 -07:00
|
|
|
if ofs < 0 || len < 0 || ofs > bytes_length s - len
|
1995-05-04 03:15:53 -07:00
|
|
|
then invalid_arg "output"
|
|
|
|
else unsafe_output oc s ofs len
|
|
|
|
|
2014-04-29 04:56:17 -07:00
|
|
|
let output_substring oc s ofs len =
|
2014-08-22 06:45:02 -07:00
|
|
|
if ofs < 0 || len < 0 || ofs > string_length s - len
|
|
|
|
then invalid_arg "output_substring"
|
|
|
|
else unsafe_output_string oc s ofs len
|
2014-04-29 04:56:17 -07:00
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
external output_byte : out_channel -> int -> unit = "caml_ml_output_char"
|
|
|
|
external output_binary_int : out_channel -> int -> unit = "caml_ml_output_int"
|
1997-07-02 11:16:15 -07:00
|
|
|
|
|
|
|
external marshal_to_channel : out_channel -> 'a -> unit list -> unit
|
2004-01-01 08:42:43 -08:00
|
|
|
= "caml_output_value"
|
1997-07-02 11:16:15 -07:00
|
|
|
let output_value chan v = marshal_to_channel chan v []
|
1995-08-09 06:15:01 -07:00
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
external seek_out : out_channel -> int -> unit = "caml_ml_seek_out"
|
|
|
|
external pos_out : out_channel -> int = "caml_ml_pos_out"
|
|
|
|
external out_channel_length : out_channel -> int = "caml_ml_channel_size"
|
|
|
|
external close_out_channel : out_channel -> unit = "caml_ml_close_channel"
|
2002-10-31 02:37:53 -08:00
|
|
|
let close_out oc = flush oc; close_out_channel oc
|
2002-10-29 05:55:33 -08:00
|
|
|
let close_out_noerr oc =
|
|
|
|
(try flush oc with _ -> ());
|
|
|
|
(try close_out_channel oc with _ -> ())
|
1998-07-02 02:51:50 -07:00
|
|
|
external set_binary_mode_out : out_channel -> bool -> unit
|
2003-12-29 14:15:02 -08:00
|
|
|
= "caml_ml_set_binary_mode"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* General input functions *)
|
|
|
|
|
2015-07-24 06:11:26 -07:00
|
|
|
external set_in_channel_name: in_channel -> string -> unit =
|
|
|
|
"caml_ml_set_channel_name"
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let open_in_gen mode perm name =
|
2015-07-24 06:11:26 -07:00
|
|
|
let c = open_descriptor_in(open_desc name mode perm) in
|
|
|
|
set_in_channel_name c name;
|
|
|
|
c
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let open_in name =
|
|
|
|
open_in_gen [Open_rdonly; Open_text] 0 name
|
|
|
|
|
|
|
|
let open_in_bin name =
|
|
|
|
open_in_gen [Open_rdonly; Open_binary] 0 name
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
external input_char : in_channel -> char = "caml_ml_input_char"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2014-04-29 04:56:17 -07:00
|
|
|
external unsafe_input : in_channel -> bytes -> int -> int -> int
|
2003-12-29 14:15:02 -08:00
|
|
|
= "caml_ml_input"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
let input ic s ofs len =
|
2014-04-29 04:56:17 -07:00
|
|
|
if ofs < 0 || len < 0 || ofs > bytes_length s - len
|
1995-05-04 03:15:53 -07:00
|
|
|
then invalid_arg "input"
|
|
|
|
else unsafe_input ic s ofs len
|
|
|
|
|
|
|
|
let rec unsafe_really_input ic s ofs len =
|
|
|
|
if len <= 0 then () else begin
|
|
|
|
let r = unsafe_input ic s ofs len in
|
|
|
|
if r = 0
|
|
|
|
then raise End_of_file
|
2010-06-09 04:00:56 -07:00
|
|
|
else unsafe_really_input ic s (ofs + r) (len - r)
|
1995-05-04 03:15:53 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
let really_input ic s ofs len =
|
2014-04-29 04:56:17 -07:00
|
|
|
if ofs < 0 || len < 0 || ofs > bytes_length s - len
|
1995-05-04 03:15:53 -07:00
|
|
|
then invalid_arg "really_input"
|
|
|
|
else unsafe_really_input ic s ofs len
|
|
|
|
|
2014-04-29 04:56:17 -07:00
|
|
|
let really_input_string ic len =
|
|
|
|
let s = bytes_create len in
|
|
|
|
really_input ic s 0 len;
|
|
|
|
bytes_unsafe_to_string s
|
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
external input_scan_line : in_channel -> int = "caml_ml_input_scan_line"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2002-02-11 05:49:36 -08:00
|
|
|
let input_line chan =
|
|
|
|
let rec build_result buf pos = function
|
|
|
|
[] -> buf
|
|
|
|
| hd :: tl ->
|
2014-04-29 04:56:17 -07:00
|
|
|
let len = bytes_length hd in
|
|
|
|
bytes_blit hd 0 buf (pos - len) len;
|
2002-02-11 05:49:36 -08:00
|
|
|
build_result buf (pos - len) tl in
|
|
|
|
let rec scan accu len =
|
|
|
|
let n = input_scan_line chan in
|
|
|
|
if n = 0 then begin (* n = 0: we are at EOF *)
|
|
|
|
match accu with
|
|
|
|
[] -> raise End_of_file
|
2014-04-29 04:56:17 -07:00
|
|
|
| _ -> build_result (bytes_create len) len accu
|
2002-02-11 05:49:36 -08:00
|
|
|
end else if n > 0 then begin (* n > 0: newline found in buffer *)
|
2014-04-29 04:56:17 -07:00
|
|
|
let res = bytes_create (n - 1) in
|
2010-06-09 04:00:56 -07:00
|
|
|
ignore (unsafe_input chan res 0 (n - 1));
|
2002-02-11 05:49:36 -08:00
|
|
|
ignore (input_char chan); (* skip the newline *)
|
|
|
|
match accu with
|
|
|
|
[] -> res
|
2005-10-25 11:34:07 -07:00
|
|
|
| _ -> let len = len + n - 1 in
|
2014-04-29 04:56:17 -07:00
|
|
|
build_result (bytes_create len) len (res :: accu)
|
2002-02-11 05:49:36 -08:00
|
|
|
end else begin (* n < 0: newline not found *)
|
2014-04-29 04:56:17 -07:00
|
|
|
let beg = bytes_create (-n) in
|
2002-02-11 05:49:36 -08:00
|
|
|
ignore(unsafe_input chan beg 0 (-n));
|
|
|
|
scan (beg :: accu) (len - n)
|
|
|
|
end
|
2014-04-29 04:56:17 -07:00
|
|
|
in bytes_unsafe_to_string (scan [] 0)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2003-12-29 14:15:02 -08:00
|
|
|
external input_byte : in_channel -> int = "caml_ml_input_char"
|
|
|
|
external input_binary_int : in_channel -> int = "caml_ml_input_int"
|
2004-01-01 08:42:43 -08:00
|
|
|
external input_value : in_channel -> 'a = "caml_input_value"
|
2003-12-29 14:15:02 -08:00
|
|
|
external seek_in : in_channel -> int -> unit = "caml_ml_seek_in"
|
|
|
|
external pos_in : in_channel -> int = "caml_ml_pos_in"
|
|
|
|
external in_channel_length : in_channel -> int = "caml_ml_channel_size"
|
|
|
|
external close_in : in_channel -> unit = "caml_ml_close_channel"
|
2002-10-29 05:55:33 -08:00
|
|
|
let close_in_noerr ic = (try close_in ic with _ -> ());;
|
1998-07-02 02:51:50 -07:00
|
|
|
external set_binary_mode_in : in_channel -> bool -> unit
|
2003-12-29 14:15:02 -08:00
|
|
|
= "caml_ml_set_binary_mode"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
|
|
|
(* Output functions on standard output *)
|
|
|
|
|
|
|
|
let print_char c = output_char stdout c
|
|
|
|
let print_string s = output_string stdout s
|
2014-04-29 04:56:17 -07:00
|
|
|
let print_bytes s = output_bytes stdout s
|
1995-05-04 03:15:53 -07:00
|
|
|
let print_int i = output_string stdout (string_of_int i)
|
|
|
|
let print_float f = output_string stdout (string_of_float f)
|
2003-01-22 08:32:57 -08:00
|
|
|
let print_endline s =
|
|
|
|
output_string stdout s; output_char stdout '\n'; flush stdout
|
1995-05-04 03:15:53 -07:00
|
|
|
let print_newline () = output_char stdout '\n'; flush stdout
|
|
|
|
|
|
|
|
(* Output functions on standard error *)
|
|
|
|
|
|
|
|
let prerr_char c = output_char stderr c
|
|
|
|
let prerr_string s = output_string stderr s
|
2014-05-01 14:54:15 -07:00
|
|
|
let prerr_bytes s = output_bytes stderr s
|
1995-05-04 03:15:53 -07:00
|
|
|
let prerr_int i = output_string stderr (string_of_int i)
|
|
|
|
let prerr_float f = output_string stderr (string_of_float f)
|
|
|
|
let prerr_endline s =
|
|
|
|
output_string stderr s; output_char stderr '\n'; flush stderr
|
|
|
|
let prerr_newline () = output_char stderr '\n'; flush stderr
|
|
|
|
|
|
|
|
(* Input functions on standard input *)
|
|
|
|
|
|
|
|
let read_line () = flush stdout; input_line stdin
|
|
|
|
let read_int () = int_of_string(read_line())
|
|
|
|
let read_float () = float_of_string(read_line())
|
|
|
|
|
2002-03-02 01:16:39 -08:00
|
|
|
(* Operations on large files *)
|
|
|
|
|
|
|
|
module LargeFile =
|
|
|
|
struct
|
2003-12-29 14:15:02 -08:00
|
|
|
external seek_out : out_channel -> int64 -> unit = "caml_ml_seek_out_64"
|
|
|
|
external pos_out : out_channel -> int64 = "caml_ml_pos_out_64"
|
|
|
|
external out_channel_length : out_channel -> int64
|
|
|
|
= "caml_ml_channel_size_64"
|
|
|
|
external seek_in : in_channel -> int64 -> unit = "caml_ml_seek_in_64"
|
|
|
|
external pos_in : in_channel -> int64 = "caml_ml_pos_in_64"
|
|
|
|
external in_channel_length : in_channel -> int64 = "caml_ml_channel_size_64"
|
2002-03-02 01:16:39 -08:00
|
|
|
end
|
|
|
|
|
2002-10-31 02:00:02 -08:00
|
|
|
(* Formats *)
|
2006-10-24 13:42:41 -07:00
|
|
|
|
Use a nominal datatype for CamlinternalFormat.format6
This should make the type-checking of formats simpler and more robust:
instead of trying to find a pair as previously, we can now use the
path of the format6 type directly.
A nice side-effect of the change is that the internal definition of
formats (as a pair) is not printed in error messages anymore.
Because format6 is in fact defined in the CamlinternalFormatBasics
submodule of Pervasives, and has an alias at the toplevel of
Pervasives, error messages still expand the definition:
> Error: This expression has type
> ('a, 'b, 'c, 'd, 'd, 'a) format6 =
> ('a, 'b, 'c, 'd, 'd, 'a) CamlinternalFormatBasics.format6
> but an expression was expected of type ...
Passing the option `-short-paths` does avoid this expansion and
returns exactly the same error message as 4.01:
> Error: This expression has type ('a, 'b, 'c, 'd, 'd, 'a) format6
> but an expression was expected of type ...
(To get this error message without -short-paths, one would need to
define format6 directly in Pervasives; but this type is mutually
recursive with several GADT types that we don't want to add in the
Pervasives namespace unqualified. This is why I'll keep the alias
for now.)
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@14868 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
2014-05-14 08:07:13 -07:00
|
|
|
type ('a, 'b, 'c, 'd, 'e, 'f) format6
|
|
|
|
= ('a, 'b, 'c, 'd, 'e, 'f) CamlinternalFormatBasics.format6
|
|
|
|
= Format of ('a, 'b, 'c, 'd, 'e, 'f) CamlinternalFormatBasics.fmt
|
|
|
|
* string
|
2014-05-12 08:37:29 -07:00
|
|
|
|
|
|
|
type ('a, 'b, 'c, 'd) format4 = ('a, 'b, 'c, 'c, 'c, 'd) format6
|
2014-05-12 08:37:37 -07:00
|
|
|
|
2003-07-05 04:13:24 -07:00
|
|
|
type ('a, 'b, 'c) format = ('a, 'b, 'c, 'c) format4
|
2006-10-24 13:42:41 -07:00
|
|
|
|
Use a nominal datatype for CamlinternalFormat.format6
This should make the type-checking of formats simpler and more robust:
instead of trying to find a pair as previously, we can now use the
path of the format6 type directly.
A nice side-effect of the change is that the internal definition of
formats (as a pair) is not printed in error messages anymore.
Because format6 is in fact defined in the CamlinternalFormatBasics
submodule of Pervasives, and has an alias at the toplevel of
Pervasives, error messages still expand the definition:
> Error: This expression has type
> ('a, 'b, 'c, 'd, 'd, 'a) format6 =
> ('a, 'b, 'c, 'd, 'd, 'a) CamlinternalFormatBasics.format6
> but an expression was expected of type ...
Passing the option `-short-paths` does avoid this expansion and
returns exactly the same error message as 4.01:
> Error: This expression has type ('a, 'b, 'c, 'd, 'd, 'a) format6
> but an expression was expected of type ...
(To get this error message without -short-paths, one would need to
define format6 directly in Pervasives; but this type is mutually
recursive with several GADT types that we don't want to add in the
Pervasives namespace unqualified. This is why I'll keep the alias
for now.)
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@14868 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
2014-05-14 08:07:13 -07:00
|
|
|
let string_of_format (Format (fmt, str)) = str
|
2014-05-12 08:37:37 -07:00
|
|
|
|
2002-10-31 02:00:02 -08:00
|
|
|
external format_of_string :
|
2006-10-24 13:42:41 -07:00
|
|
|
('a, 'b, 'c, 'd, 'e, 'f) format6 ->
|
|
|
|
('a, 'b, 'c, 'd, 'e, 'f) format6 = "%identity"
|
2004-07-13 05:25:21 -07:00
|
|
|
|
Use a nominal datatype for CamlinternalFormat.format6
This should make the type-checking of formats simpler and more robust:
instead of trying to find a pair as previously, we can now use the
path of the format6 type directly.
A nice side-effect of the change is that the internal definition of
formats (as a pair) is not printed in error messages anymore.
Because format6 is in fact defined in the CamlinternalFormatBasics
submodule of Pervasives, and has an alias at the toplevel of
Pervasives, error messages still expand the definition:
> Error: This expression has type
> ('a, 'b, 'c, 'd, 'd, 'a) format6 =
> ('a, 'b, 'c, 'd, 'd, 'a) CamlinternalFormatBasics.format6
> but an expression was expected of type ...
Passing the option `-short-paths` does avoid this expansion and
returns exactly the same error message as 4.01:
> Error: This expression has type ('a, 'b, 'c, 'd, 'd, 'a) format6
> but an expression was expected of type ...
(To get this error message without -short-paths, one would need to
define format6 directly in Pervasives; but this type is mutually
recursive with several GADT types that we don't want to add in the
Pervasives namespace unqualified. This is why I'll keep the alias
for now.)
git-svn-id: http://caml.inria.fr/svn/ocaml/trunk@14868 f963ae5c-01c2-4b8c-9fe0-0dff7051ff02
2014-05-14 08:07:13 -07:00
|
|
|
let (^^) (Format (fmt1, str1)) (Format (fmt2, str2)) =
|
|
|
|
Format (CamlinternalFormatBasics.concat_fmt fmt1 fmt2,
|
|
|
|
str1 ^ "%," ^ str2)
|
2002-10-31 02:00:02 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Miscellaneous *)
|
|
|
|
|
2003-12-15 10:37:24 -08:00
|
|
|
external sys_exit : int -> 'a = "caml_sys_exit"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2001-10-09 08:14:01 -07:00
|
|
|
let exit_function = ref flush_all
|
1995-10-23 09:58:50 -07:00
|
|
|
|
|
|
|
let at_exit f =
|
|
|
|
let g = !exit_function in
|
|
|
|
exit_function := (fun () -> f(); g())
|
1996-10-09 04:15:13 -07:00
|
|
|
|
|
|
|
let do_at_exit () = (!exit_function) ()
|
|
|
|
|
|
|
|
let exit retcode =
|
|
|
|
do_at_exit ();
|
|
|
|
sys_exit retcode
|
|
|
|
|
2001-10-30 01:25:09 -08:00
|
|
|
let _ = register_named_value "Pervasives.do_at_exit" do_at_exit
|