Skip to content

Commit

Permalink
Add support for binary literal
Browse files Browse the repository at this point in the history
  • Loading branch information
kLabz committed Apr 4, 2024
1 parent 2aa1b52 commit dd9da8f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/texpr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ let type_constant basic c p =
match c with
| Int (s,_) ->
if String.length s > 10 && String.sub s 0 2 = "0x" then raise_typing_error "Invalid hexadecimal integer" p;
if String.length s > 34 && String.sub s 0 2 = "0b" then raise_typing_error "Invalid binary integer" p;
(try mk (TConst (TInt (Int32.of_string s))) basic.tint p
with _ -> mk (TConst (TFloat s)) basic.tfloat p)
| Float (f,_) -> mk (TConst (TFloat f)) basic.tfloat p
Expand Down
5 changes: 5 additions & 0 deletions src/syntax/lexer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ let integer_digits = [%sedlex.regexp? (digit, Star sep_digit)]
let hex_digit = [%sedlex.regexp? '0'..'9'|'a'..'f'|'A'..'F']
let sep_hex_digit = [%sedlex.regexp? Opt '_', hex_digit]
let hex_digits = [%sedlex.regexp? (hex_digit, Star sep_hex_digit)]
let bin_digit = [%sedlex.regexp? '0'|'1']
let sep_bin_digit = [%sedlex.regexp? Opt '_', bin_digit]
let bin_digits = [%sedlex.regexp? (bin_digit, Star sep_bin_digit)]
let integer = [%sedlex.regexp? ('1'..'9', Star sep_digit) | '0']

let integer_suffix = [%sedlex.regexp? Opt '_', ('i'|'u'), Plus integer]
Expand All @@ -386,6 +389,8 @@ let rec token lexbuf =
| '\n' | '\r' -> newline lexbuf; token lexbuf
| "0x", Plus hex_digits, Opt integer_suffix ->
mk lexbuf (split_int_suffix (lexeme lexbuf))
| "0b", Plus bin_digits, Opt integer_suffix ->
mk lexbuf (split_int_suffix (lexeme lexbuf))
| integer, Opt integer_suffix ->
mk lexbuf (split_int_suffix (lexeme lexbuf))
| integer, float_suffix ->
Expand Down

0 comments on commit dd9da8f

Please sign in to comment.