From cbedc425b810dd887da2a52d2f33df2bced0f831 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sun, 21 Aug 2022 14:28:30 +0100 Subject: Support for binary numbers witg 0b[0-1] notation --- src/Lexer.hs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/Lexer.hs b/src/Lexer.hs index 4938cfe..848a892 100644 --- a/src/Lexer.hs +++ b/src/Lexer.hs @@ -1,5 +1,6 @@ module Lexer where +import Data.Char (digitToInt) import Text.Parsec import Text.Parsec.Language (emptyDef) import Text.Parsec.String (Parser) @@ -17,8 +18,16 @@ scanner = T.makeTokenParser style T.reservedNames = names } +binNum :: Parser Integer +binNum = do + _ <- char '0' + _ <- oneOf "bB" + digits <- many1 $ oneOf "01" + let n = foldl (\x d -> 2 * x + toInteger (digitToInt d)) 0 digits + seq n $ return n + integer :: Parser Integer -integer = T.integer scanner +integer = binNum <|> T.integer scanner parens :: Parser a -> Parser a parens = T.parens scanner -- cgit v1.2.3