Skip to content

Commit ae3b361

Browse files
authored
Merge pull request #82 from AStenbaek/integer-literal-frontend
Integer literal frontend
2 parents d6eb1cd + aeb5bf4 commit ae3b361

23 files changed

+51
-6
lines changed

compiler/src/Lexer.x

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@ import Control.Monad.Except
1616
import Data.Char (isSpace, toLower)
1717
import Data.List (dropWhileEnd)
1818
import Data.Char ( chr )
19-
import Numeric ( readDec )
19+
import Numeric ( readDec, readBin, readOct, readHex )
2020
import Control.Monad (when)
2121
}
2222

2323
%wrapper "monadUserState"
2424

25+
$bindigit = [01]
26+
$octdigit = 0-7
27+
$hexdigit = [0-9A-Fa-f]
2528
$digit = 0-9
2629
$alpha = [a-zA-Z]
2730
$alpha_ = [$alpha \_]
@@ -30,7 +33,10 @@ $graphic = $printable # $white
3033
@sym = $alpha_ [$alpha $digit \_ \']*
3134
@string = \" ($printable # \")* \"
3235
@label = \`\{ ($printable # \})* \}\`
33-
36+
@declit = $digit[\_$digit]*
37+
@binlit = 0[bB]$bindigit[\_$bindigit]*
38+
@octlit = 0[oO]$octdigit[\_$octdigit]*
39+
@hexlit = 0[xX]$hexdigit[\_$hexdigit]*
3440

3541
tokens:-
3642
-- Whitespace insensitive
@@ -109,7 +115,12 @@ tokens:-
109115
<state_dclabel> "#root-integrity" { mkL TokenDCRootInteg }
110116
<state_dclabel> "#null-confidentiality" { mkL TokenDCNullConf }
111117
<state_dclabel> "#null-integrity" { mkL TokenDCNullInteg }
112-
<0> $digit+ { mkLs (\s -> TokenNum (read s)) }
118+
-- Integer literal parsing inspired by https://github.com/ocaml/ocaml/blob/trunk/parsing/lexer.mll
119+
<0> @declit { mkLs (\s -> TokenNum (read (filter (/='_') s))) }
120+
<0> @binlit { mkLs (\s -> TokenNum (fst (head (readBin (filter (/='_') (drop 2 s)))))) }
121+
<0> @octlit { mkLs (\s -> TokenNum (fst (head (readOct (filter (/='_') (drop 2 s)))))) }
122+
<0> @hexlit { mkLs (\s -> TokenNum (fst (head (readHex (filter (/='_') (drop 2 s)))))) }
123+
<0> (@declit|@binlit|@octlit|@hexlit)@sym { \(_, _, _, s) _ -> lexerError ("Invalid literal " ++ s) }
113124
<0> [\<][\<] { mkL TokenBinShiftLeft }
114125
<0> [\>][\>] { mkL TokenBinShiftRight }
115126
<0> [\~][\>][\>] { mkL TokenBinZeroShiftRight }
@@ -386,9 +397,6 @@ lexerError msg =
386397
where
387398
trim = reverse . dropWhile (== ' ') . reverse . dropWhile (== ' ')
388399

389-
390-
391-
392400
-- we use a custom version of monadScan so that we have full
393401
-- control over the error reporting; this one is based on
394402
-- the built-in alexMonadScan

tests/cmp/int-lit1.golden

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Parse Error:
2+
Invalid literal 0x_A
3+
at 1:5 before end of line

tests/cmp/int-lit1.trp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0x_A

tests/cmp/int-lit2.golden

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Parse Error:
2+
Invalid literal 1_x_0
3+
at 1:6 before end of line

tests/cmp/int-lit2.trp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1_x_0

tests/cmp/int-lit3.golden

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Parse Error:
2+
Invalid literal 0b12
3+
at 1:5 before end of line

tests/cmp/int-lit3.trp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0b12

tests/cmp/int-lit4.golden

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Parse Error:
2+
Invalid literal 0o8
3+
at 1:4 before end of line

tests/cmp/int-lit4.trp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0o8

tests/rt/pos/core/int-lit1.golden

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2025-08-24T19:08:18.468Z [RTM] info: Skipping network creation. Observe that all external IO operations will yield a runtime error.
2+
>>> Main thread finished with value: 1000000@{}%{}

0 commit comments

Comments
 (0)