Debian Codegraph @

interp/numTok.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 module Stream = Gramlib.Stream
12
13 (** We keep the string to preserve the user representation,
14 e.g. "e"/"E" or the presence of leading 0s, or the presence of a +
15 in the exponent *)
16
17 type num_class = CDec | CHex
18
19 let string_del_head n s = String.sub s n (String.length s - n)
20
21 module UnsignedNat =
22 struct
23 type t = string
24 let of_string s =
25 assert (String.length s > 0);
26 assert (s.[0] >= '0' && s.[0] <= '9');
27 s
28 let to_string s =
29 String.(map Char.lowercase_ascii (concat "" (split_on_char '_' s)))
30
31 let sprint s = s
32 let print s = Pp.str (sprint s)
33
34 let classify s =
35 if String.length s >= 2 && (s.[1] = 'x' || s.[1] = 'X') then CHex else CDec
36
37 (** Comparing two raw numbers (base 10 or 16, big-endian, non-negative).
38 A bit nasty, but not critical: used e.g. to decide when a number
39 is considered as large (see threshold warnings in notation.ml). *)
40
41 exception Comp of int
42
43 let rec compare s s' =
44 let l = String.length s and l' = String.length s' in
45 if l < l' then - compare s' s
46 else
47 let d = l-l' in
48 try
49 for i = 0 to d-1 do if s.[i] != '0' then raise (Comp 1) done;
50 for i = d to l-1 do
51 let c = Stdlib.compare s.[i] s'.[i-d] in
52 if c != 0 then raise (Comp c)
53 done;
54 0
55 with Comp c -> c
56
57 let compare n d =
58 assert (classify d = CDec);
59 match classify n with
60 | CDec -> compare (to_string n) (to_string d)
61 | CHex -> compare (string_del_head 2 (to_string n)) (to_string d)
62
63 let is_zero s =
64 compare s "0" = 0
65 end
66
67 type sign = SPlus | SMinus
68
69 type 'a exp = EDec of 'a | EBin of 'a
70
71 module SignedNat =
72 struct
73 type t = sign * UnsignedNat.t
74 let of_string s =
75 assert (String.length s > 0);
76 let sign,n =
77 match s.[0] with
78 | '-' -> (SMinus,string_del_head 1 s)
79 | '+' -> (SPlus,string_del_head 1 s)
80 | _ -> (SPlus,s) in
81 (sign,UnsignedNat.of_string n)
82 let to_string (sign,n) =
83 (match sign with SPlus -> "" | SMinus -> "-") ^ UnsignedNat.to_string n
84 let classify (_,n) = UnsignedNat.classify n
85 let bigint_of_string (sign,n) = Z.of_string (to_string (sign,n))
86 let to_bigint n = bigint_of_string n
87 let string_of_nonneg_bigint c n =
88 match c with
89 | CDec -> Z.format "%d" n
90 | CHex -> Z.format "%#x" n
91 let of_bigint c n =
92 let sign, n = if Int.equal (-1) (Z.sign n) then (SMinus, Z.neg n) else (SPlus, n) in
93 (sign, string_of_nonneg_bigint c n)
94 end
95
96 module Unsigned =
97 struct
98
99 type t = {
100 int : string;
101 frac : string;
102 exp : string
103 }
104 (**
105 - int: \[0-9\]\[0-9_\]*
106 - frac: empty or \[0-9_\]+
107 - exp: empty or \[eE\]\[+-\]?\[0-9\]\[0-9_\]*
108 or
109 - int: 0\[xX\]\[0-9a-fA-F\]\[0-9a-fA-F_\]*
110 - frac: empty or \[0-9a-fA-F_\]+
111 - exp: empty or \[pP\]\[+-\]?\[0-9\]\[0-9_\]* *)
112
113 let equal n1 n2 =
114 String.(equal n1.int n2.int && equal n1.frac n2.frac && equal n1.exp n2.exp)
115
116 let parse =
117 let buff = ref (Bytes.create 80) in
118 let store len x =
119 let open Bytes in
120 if len >= length !buff then
121 buff := cat !buff (create (length !buff));
122 set !buff len x;
123 succ len in
124 let get_buff len = Bytes.sub_string !buff 0 len in
125 (* reads [0-9_]* *)
126 let rec number len s = match Stream.peek () s with
127 | Some ('0'..'9' as c) -> Stream.junk () s; number (store len c) s
128 | Some ('_' as c) when len > 0 -> Stream.junk () s; number (store len c) s
129 | _ -> len in
130 (* reads [0-9a-fA-F_]* *)
131 let rec hex_number len s = match Stream.peek () s with
132 | Some (('0'..'9' | 'a'..'f' | 'A'..'F') as c) ->
133 Stream.junk () s; hex_number (store len c) s
134 | Some ('_' as c) when len > 0 ->
135 Stream.junk () s; hex_number (store len c) s
136 | _ -> len in
137 fun s ->
138 let hex, i =
139 match Stream.npeek () 3 s with
140 | '0' :: (('x' | 'X') as x) :: (('0'..'9' | 'a'..'f' | 'A'..'F') as c) :: _ ->
141 Stream.junk () s; Stream.junk () s; Stream.junk () s;
142 true, get_buff (hex_number (store (store (store 0 '0') x) c) s)
143 | _ -> false, get_buff (number 0 s) in
144 assert (i <> "");
145 let f =
146 match hex, Stream.npeek () 2 s with
147 | true, '.' :: (('0'..'9' | 'a'..'f' | 'A'..'F' | '_') as c) :: _ ->
148 Stream.junk () s; Stream.junk () s; get_buff (hex_number (store 0 c) s)
149 | false, '.' :: (('0'..'9' | '_') as c) :: _ ->
150 Stream.junk () s; Stream.junk () s; get_buff (number (store 0 c) s)
151 | _ -> "" in
152 let e =
153 match hex, Stream.npeek () 2 s with
154 | true, (('p'|'P') as e) :: ('0'..'9' as c) :: _
155 | false, (('e'|'E') as e) :: ('0'..'9' as c) :: _ ->
156 Stream.junk () s; Stream.junk () s; get_buff (number (store (store 0 e) c) s)
157 | true, (('p'|'P') as e) :: (('+'|'-') as sign) :: _
158 | false, (('e'|'E') as e) :: (('+'|'-') as sign) :: _ ->
159 begin match Stream.npeek () 3 s with
160 | _ :: _ :: ('0'..'9' as c) :: _ ->
161 Stream.junk () s; Stream.junk () s; Stream.junk () s;
162 get_buff (number (store (store (store 0 e) sign) c) s)
163 | _ -> ""
164 end
165 | _ -> "" in
166 { int = i; frac = f; exp = e }
167
168 let sprint n =
169 n.int ^ (if n.frac = "" then "" else "." ^ n.frac) ^ n.exp
170
171 let print n =
172 Pp.str (sprint n)
173
174 let parse_string s =
175 if s = "" || s.[0] < '0' || s.[0] > '9' then None else
176 let strm = Stream.of_string (s ^ " ") in
177 let n = parse strm in
178 if Stream.count strm >= String.length s then Some n else None
179
180 let of_string s =
181 match parse_string s with
182 | None -> assert false
183 | Some s -> s
184
185 let to_string =
186 sprint (* We could remove the '_' but not necessary for float_of_string *)
187
188 let to_nat = function
189 | { int = i; frac = ""; exp = "" } -> Some i
190 | _ -> None
191
192 let is_nat = function
193 | { int = _; frac = ""; exp = "" } -> true
194 | _ -> false
195
196 let classify n = UnsignedNat.classify n.int
197 end
198
199 open Unsigned
200
201 module Signed =
202 struct
203
204 type t = sign * Unsigned.t
205
206 let equal (s1,n1) (s2,n2) =
207 s1 = s2 && equal n1 n2
208
209 let is_zero = function
210 | (SPlus,{int;frac;exp}) -> UnsignedNat.is_zero int && UnsignedNat.is_zero frac
211 | _ -> false
212
213 let of_int_frac_and_exponent (sign,int) f e =
214 assert (match e with None -> true | Some e -> SignedNat.classify e = CDec);
215 let c = UnsignedNat.classify int in
216 let exp = match e with None -> "" | Some e ->
217 let e = SignedNat.to_string e in
218 match c with CDec -> "e" ^ e | CHex -> "p" ^ e in
219 let frac = match f with None -> "" | Some f ->
220 assert (c = UnsignedNat.classify f);
221 let f = UnsignedNat.to_string f in
222 match c with CDec -> f | CHex -> string_del_head 2 f in
223 sign, { int; frac; exp }
224
225 let to_int_frac_and_exponent (sign, { int; frac; exp }) =
226 let e =
227 if exp = "" then None else
228 Some (match exp.[1] with
229 | '-' -> SMinus, string_del_head 2 exp
230 | '+' -> SPlus, string_del_head 2 exp
231 | _ -> SPlus, string_del_head 1 exp) in
232 let f =
233 if frac = "" then None else
234 Some (match UnsignedNat.classify int with
235 | CDec -> frac
236 | CHex -> "0x" ^ frac) in
237 (sign, int), f, e
238
239 let of_nat i =
240 (SPlus,{ int = i; frac = ""; exp = "" })
241
242 let of_int (s,i) =
243 (s,{ int = i; frac = ""; exp = "" })
244
245 let of_int_string s = of_int (SignedNat.of_string s)
246
247 let to_int = function
248 | (s, { int = i; frac = ""; exp = "" }) -> Some (s,i)
249 | _ -> None
250
251 let is_int n = match to_int n with None -> false | Some _ -> true
252
253 let sprint (s,i) =
254 (match s with SPlus -> "" | SMinus -> "-") ^ Unsigned.sprint i
255
256 let print i =
257 Pp.str (sprint i)
258
259 let parse_string s =
260 if s = "" || s = "-" || s = "+" ||
261 (s.[0] < '0' || s.[0] > '9') && ((s.[0] <> '-' && s.[0] <> '+') || s.[1] < '0' || s.[1] > '9') then None else
262 let strm = Stream.of_string (s ^ " ") in
263 let sign = match s.[0] with
264 | '-' -> (Stream.junk () strm; SMinus)
265 | '+' -> (Stream.junk () strm; SPlus)
266 | _ -> SPlus in
267 let n = parse strm in
268 if Stream.count strm >= String.length s then Some (sign,n) else None
269
270 let of_string s =
271 assert (s <> "");
272 let sign,u = match s.[0] with
273 | '-' -> (SMinus, string_del_head 1 s)
274 | '+' -> (SPlus, string_del_head 1 s)
275 | _ -> (SPlus, s) in
276 (sign, Unsigned.of_string u)
277
278 let to_string (sign,u) =
279 (match sign with SPlus -> "" | SMinus -> "-") ^ Unsigned.to_string u
280
281 let to_bigint = function
282 | (sign, { int = n; frac = ""; exp = "" }) ->
283 Some (SignedNat.to_bigint (sign,UnsignedNat.to_string n))
284 | _ -> None
285
286 let of_bigint c n =
287 of_int (SignedNat.of_bigint c n)
288
289 let to_bigint_and_exponent (s, { int; frac; exp }) =
290 let c = UnsignedNat.classify int in
291 let int = UnsignedNat.to_string int in
292 let frac = UnsignedNat.to_string frac in
293 let i = SignedNat.to_bigint (s, int ^ frac) in
294 let e =
295 let e = if exp = "" then Z.zero else match exp.[1] with
296 | '+' -> Z.of_string (UnsignedNat.to_string (string_del_head 2 exp))
297 | '-' -> Z.(neg (of_string (UnsignedNat.to_string (string_del_head 2 exp))))
298 | _ -> Z.of_string (UnsignedNat.to_string (string_del_head 1 exp)) in
299 let l = String.length frac in
300 let l = match c with CDec -> l | CHex -> 4 * l in
301 Z.(sub e (of_int l)) in
302 (i, match c with CDec -> EDec e | CHex -> EBin e)
303
304 let of_bigint_and_exponent i e =
305 let c = match e with EDec _ -> CDec | EBin _ -> CHex in
306 let e = match e with EDec e | EBin e -> Some (SignedNat.of_bigint CDec e) in
307 of_int_frac_and_exponent (SignedNat.of_bigint c i) None e
308
309 let is_bigger_int_than (s, { int; frac; exp }) i =
310 frac = "" && exp = "" && UnsignedNat.compare int i > 0
311
312 let classify (_, n) = Unsigned.classify n
313 end