Commit 00f4931f authored by Pietro Abate's avatar Pietro Abate
Browse files

[r2005-05-17 14:26:08 by beppe] Modified int_of so that now it accepts also...

[r2005-05-17 14:26:08 by beppe] Modified int_of so that now it accepts also strings 0x 0X 0b 0B 0o 0O
for hex, octals and binary

Original author: beppe
Date: 2005-05-17 14:26:09+00:00
parent 6a246ebc
......@@ -211,7 +211,12 @@ unary_op_warning "int_of"
string intstr int
(fun v ->
let (s,_) = Value.get_string_utf8 v in
try Value.Integer (Intervals.V.mk (U.get_str s)) (* UTF-8 is ASCII compatible ! *)
(* It was like that *)
(* try Value.Integer (Intervals.V.mk (U.get_str s)) *)
(* UTF-8 is ASCII compatible ! *)
(* modified to allow 0x 0b 0o notations *)
try Value.Integer (Intervals.V.from_int (
try int_of_string(U.get_str s) with Failure _ -> raise exn_int_of))
with Failure _ -> raise exn_int_of);;
register_fun "atom_of"
......
......@@ -16,12 +16,102 @@ let byte_int = mk_interval_type "0" "255"
let non_zero_int = Types.cup pos_int neg_int
let pos_intstr =
let decimal_intstr =
Sequence.plus (Types.char (Chars.char_class
(Chars.V.mk_char '0')
(Chars.V.mk_char '9')
)
)
let octal_intstr =
Sequence.plus (Types.char (Chars.char_class
(Chars.V.mk_char '0')
(Chars.V.mk_char '7')
)
)
let binary_intstr =
Sequence.plus (Types.char (Chars.char_class
(Chars.V.mk_char '0')
(Chars.V.mk_char '1')
)
)
let hex_intstr =
Sequence.plus (
Types.cup
(Types.char (Chars.char_class
(Chars.V.mk_char '0')
(Chars.V.mk_char '9')
)
)
(Types.cup
(Types.char (Chars.char_class
(Chars.V.mk_char 'a')
(Chars.V.mk_char 'f')
)
)
(Types.char (Chars.char_class
(Chars.V.mk_char 'A')
(Chars.V.mk_char 'F')
)
)
)
)
let hex_str =
Types.times
(Types.cons (Types.char (Chars.atom (Chars.V.mk_char '0'))))
(Types.cons(
Types.times
(Types.cons(
Types.cup
(Types.char (Chars.atom (Chars.V.mk_char 'X')))
(Types.char (Chars.atom (Chars.V.mk_char 'x')))
)
)
(Types.cons hex_intstr)
)
) (* [ '0' ('X' | 'x') hex+] *)
let oct_str =
Types.times
(Types.cons (Types.char (Chars.atom (Chars.V.mk_char '0'))))
(Types.cons(
Types.times
(Types.cons(
Types.cup
(Types.char (Chars.atom (Chars.V.mk_char 'O')))
(Types.char (Chars.atom (Chars.V.mk_char 'o')))
)
)
(Types.cons octal_intstr)
)
)
let bin_str =
Types.times
(Types.cons (Types.char (Chars.atom (Chars.V.mk_char '0'))))
(Types.cons(
Types.times
(Types.cons(
Types.cup
(Types.char (Chars.atom (Chars.V.mk_char 'B')))
(Types.char (Chars.atom (Chars.V.mk_char 'b')))
)
)
(Types.cons binary_intstr)
)
)
let pos_intstr =
Types.cup
decimal_intstr (Types.cup
hex_str (Types.cup
bin_str
oct_str))
let neg_intstr =
Types.times
......@@ -30,6 +120,7 @@ let neg_intstr =
let intstr = Types.cup pos_intstr neg_intstr (* [ '-'? '0'--'9'+ ] *)
let true_atom = Atoms.V.mk_ascii "true"
let false_atom = Atoms.V.mk_ascii "false"
let true_type = Types.atom (Atoms.atom true_atom)
......
......@@ -623,10 +623,15 @@ E.g.: <code>atom_of "x"</code> evaluates to <code>`x</code>
<section title="Creating integers from strings">
<p>
The operator <code>int_of</code> converts a string to an integer.
It fails if the string is not a decimal representation of
an integer. There is a type-checking warning when the argument
cannot be proved to be of type <code>[ '-'? '0'--'9'+ ]</code>.
The operator <code>int_of</code> converts a string to an integer. The string
is read in decimal (by default) or in hexadecimal (if it begins with
<code>0x</code> or <code>0X</code>), octal (if it begins with <code>0o</code>
or <code>0O</code>), or binary (if it begins with <code>0b</code> or
<code>0B</code>).It fails if the string is not a decimal representation of an
integer. There is a type-checking warning when the argument cannot be proved
to be of type <code>[ '-'? '0'--'9'+ ] | ['-'? 'O'('b'|'B') '0'--'1'+ ] |
['-'? 'O'('o'|'O') '0'--'7'+ ] | ['-'? 'O'('x'|'X')
('0'--'9'|'a'--'f'|'A'--'F')+] </code>.
</p>
</section>
......
Markdown is supported
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