Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more integer notations for parsing #897

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}