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 binary literal #2248

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
44 changes: 44 additions & 0 deletions compiler/src/Parse/Number.hs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ chompZero pos end =
if word == 0x78 {-x-} then
chompHexInt (plusPtr pos 1) end

else if word == 0x62 {-b-} then
chompBinInt (plusPtr pos 1) end

else if word == 0x2E {-.-} then
chompFraction pos end 0

Expand All @@ -246,6 +249,15 @@ chompHexInt pos end =
OkInt newPos answer


chompBinInt :: Ptr Word8 -> Ptr Word8 -> Outcome
chompBinInt pos end =
let (# newPos, answer #) = chompBin pos end in
if answer < 0 then
Err newPos E.NumberBinDigit
else
OkInt newPos answer



-- CHOMP HEX

Expand Down Expand Up @@ -285,6 +297,38 @@ stepHex pos end word acc



-- CHOMP BIN

{-# INLINE chompBin #-}
chompBin :: Ptr Word8 -> Ptr Word8 -> (# Ptr Word8, Int #)
chompBin pos end =
chompBinHelp pos end (-1) 0


chompBinHelp :: Ptr Word8 -> Ptr Word8 -> Int -> Int -> (# Ptr Word8, Int #)
chompBinHelp pos end answer accumulator =
if pos >= end then
(# pos, answer #)
else
let
!newAnswer =
stepBin pos end (P.unsafeIndex pos) accumulator
in
if newAnswer < 0 then
(# pos, if newAnswer == -1 then answer else -2 #)
else
chompBinHelp (plusPtr pos 1) end newAnswer newAnswer


{-# INLINE stepBin #-}
stepBin :: Ptr Word8 -> Ptr Word8 -> Word8 -> Int -> Int
stepBin pos end word acc
| 0x30 {-0-} <= word && word <= 0x31 {-1-} = 2 * acc + fromIntegral (word - 0x30 {-0-})
| isDirtyEnd pos end word = -2
| True = -1



-- PRECEDENCE


Expand Down
18 changes: 17 additions & 1 deletion compiler/src/Reporting/Error/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ data Number
= NumberEnd
| NumberDot Int
| NumberHexDigit
| NumberBinDigit
| NumberNoLeadingZero


Expand Down Expand Up @@ -2912,7 +2913,7 @@ toNumberReport source number row col =
D.stack
[ D.reflow $
"I recognize numbers in the following formats:"
, D.indent 4 $ D.vcat [ "42", "3.14", "6.022e23", "0x002B" ]
, D.indent 4 $ D.vcat [ "42", "3.14", "6.022e23", "0x002B", "0b1010" ]
, D.reflow $
"So is there a way to write it like one of those?"
]
Expand Down Expand Up @@ -2947,6 +2948,21 @@ toNumberReport source number row col =
]
)

NumberBinDigit ->
Report.Report "WEIRD BINARY LITERAL" region [] $
Code.toSnippet source region Nothing
(
D.reflow $
"I thought I was reading a binary literal until I got here:"
,
D.stack
[ D.reflow $
"Valid binary digits include 0 and 1, so I can\
\ only recognize things like this:"
, D.indent 4 $ D.vcat [ "0x10", "0x0010", "0x011011" ]
]
)

NumberNoLeadingZero ->
Report.Report "LEADING ZEROS" region [] $
Code.toSnippet source region Nothing
Expand Down