Skip to content

Commit

Permalink
Add support for binary, octal, and underscores in integer notation
Browse files Browse the repository at this point in the history
At the source level, integer literal can now be written in

  - binary with a 0b or 0B prefix: 0b10001111
  - octal with a 0o or 0O prefix: 0o7722
  - decimal without any explicit prefix: 42
  - hexadecimal with a 0x or 0X prefix: 0xabcd

All these notations may contain underscores (except at the beginning),
e.g., 0b1111_0000 or 10_854_736. Caveat _1234 is an indentifier, not a
number.

Co-authored-by: Vincent Laporte <Vincent.Laporte@inria.fr>
  • Loading branch information
MrDaiki and vbgl committed Sep 16, 2024
1 parent 18a8f5d commit 5603a32
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@

# [unreleased]

## New features

- Support more integer notations
([PR#897](https://github.com/jasmin-lang/jasmin/pull/897)):
* Octal: `0O777`, `0o52`
* Binary: `0b11101`, `0B11100`
* `_` characters: `100_000_00___111`

## Bug fixes

- Easycrypt extraction for CT : fix decreasing for loops
Expand Down
12 changes: 8 additions & 4 deletions compiler/src/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@
let blank = [' ' '\t' '\r']
let newline = ['\n']
let digit = ['0'-'9']
let octdigit = ['0'-'7']
let bindigit = ['0'-'1']
let hexdigit = ['0'-'9' 'a'-'f' 'A'-'F']
let lower = ['a'-'z']
let upper = ['A'-'Z']
Expand All @@ -133,11 +135,13 @@ rule main = parse

| '"' (([^'"' '\\']|'\\' _)* as s) '"' { STRING (unescape (L.of_lexbuf lexbuf) s) }

(* Why this is needed *)
| ((*'-'?*) digit+) as s
{INT s}
| (digit+(('_')+ digit+)*) as s

| ('0' ['x' 'X'] hexdigit+) as s
| ('0' ['x' 'X'] hexdigit+(('_')+hexdigit+)*) as s

| ('0' ['b' 'B'] bindigit+(('_')+bindigit+)*) as s

| ('0' ['o' 'O'] octdigit+(('_')+octdigit+)*) as s
{INT s}

| ident as s
Expand Down
5 changes: 4 additions & 1 deletion compiler/src/syntax.ml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
open Annotations
open Utils
(* -------------------------------------------------------------------- *)
module L = Location

Expand All @@ -24,7 +25,9 @@ type castop1 = CSS of sowsize | CVS of svsize
type castop = castop1 L.located option

type int_representation = string
let parse_int = Z.of_string
let parse_int (i: int_representation) : Z.t =
let s = String.filter (( <> ) '_') i in
Z.of_string s

let bits_of_wsize : wsize -> int = Annotations.int_of_ws

Expand Down
22 changes: 22 additions & 0 deletions compiler/tests/success/common/integer_notation.jazz
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
Test for all valid integer syntaxes
*/
export fn test () -> reg u32 {
reg u32 y;
y = 0b11110000;
y = 0b111_111_11;
y = 0B111_00_11;

y = 0o01234567;
y = 0o765_4_321;
y = 0O76543210;

y = 1000000000;
y = 1000_0000_000;

y = 0x01234567;
y = 0x765_b_32aac;
y = 0X76aab3210;

return y;
}

0 comments on commit 5603a32

Please sign in to comment.