Commit e728bb92 authored by Guillaume Melquiond's avatar Guillaume Melquiond
Browse files

Do not store integer literals as strings, unless they were typed by the user.

This commit also completes the set of converters.
parent 0dd2c23d
......@@ -337,7 +337,14 @@ let t_compare t1 t2 =
| Tvar v1, Tvar v2 ->
comp_raise (vs_compare v1 v2)
| Tconst c1, Tconst c2 ->
perv_compare c1 c2
let open Number in
begin match c1, c2 with
| ConstInt { ic_negative = s1; ic_abs = IConstRaw b1 },
ConstInt { ic_negative = s2; ic_abs = IConstRaw b2 } ->
perv_compare s1 s2;
comp_raise (BigInt.compare b1 b2)
| _, _ -> perv_compare c1 c2
end
| Tapp (s1,l1), Tapp (s2,l2) ->
comp_raise (ls_compare s1 s2);
List.iter2 (t_compare bnd vml1 vml2) l1 l2
......
......@@ -14,6 +14,7 @@ open Format
(** Construction *)
type integer_literal =
| IConstRaw of BigInt.t
| IConstDec of string
| IConstHex of string
| IConstOct of string
......@@ -73,26 +74,27 @@ let int_const_bin s =
check_integer_literal 2 is_bin s;
IConstBin s
let int_const_of_int n =
let neg,a =
if n >= 0 then
false,n
else
true,-n
in
assert (a >= 0);
let a = int_const_dec (string_of_int a) in
{ic_negative = neg ;ic_abs = a}
let int_literal_raw i =
assert (BigInt.ge i BigInt.zero);
IConstRaw i
let const_of_big_int n =
let neg,a =
let int_const_of_big_int n =
let neg, n =
if BigInt.ge n BigInt.zero then
false,n
false, n
else
true,BigInt.minus n
true, BigInt.minus n
in
let a = int_const_dec (BigInt.to_string a) in
ConstInt {ic_negative = neg ;ic_abs = a}
{ ic_negative = neg; ic_abs = IConstRaw n }
let int_const_of_int n =
int_const_of_big_int (BigInt.of_int n)
let const_of_big_int n =
ConstInt (int_const_of_big_int n)
let const_of_int n =
const_of_big_int (BigInt.of_int n)
let check_exp e =
let e = if e.[0] = '-' then String.sub e 1 (String.length e - 1) else e in
......@@ -130,6 +132,7 @@ let compute_any radix s =
let compute_int_literal c =
match c with
| IConstRaw i -> i
| IConstDec s -> compute_any 10 s
| IConstHex s -> compute_any 16 s
| IConstOct s -> compute_any 8 s
......@@ -141,6 +144,7 @@ let compute_int_constant c =
let to_small_integer i =
match i with
| IConstRaw i -> BigInt.to_int i
| IConstDec s -> int_of_string s
| IConstHex s -> int_of_string ("0x"^s)
| IConstOct s -> int_of_string ("0o"^s)
......@@ -304,6 +308,7 @@ let print_hex_real support fmt =
))
let print_int_literal support fmt = function
| IConstRaw i -> print_dec_int support fmt (BigInt.to_string i)
| IConstDec i -> print_dec_int support fmt i
| IConstHex i -> print_hex_int support fmt i
| IConstOct i -> print_oct_int support fmt i
......
......@@ -16,6 +16,7 @@ open Format
exception InvalidConstantLiteral of int * string
type integer_literal = private
| IConstRaw of BigInt.t
| IConstDec of string
| IConstHex of string
| IConstOct of string
......@@ -50,7 +51,12 @@ val int_const_bin : string -> integer_literal
InvalidConstantLiteral(base,s) is raised if [s] contains invalid
characters for the given base. *)
val int_literal_raw : BigInt.t -> integer_literal
val int_const_of_int : int -> integer_constant
val int_const_of_big_int : BigInt.t -> integer_constant
val const_of_int : int -> constant
val const_of_big_int : BigInt.t -> constant
val real_const_dec : string -> string -> string option -> real_literal
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment