aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJuan J. Martinez <jjm@usebox.net>2022-08-21 14:28:30 +0100
committerJuan J. Martinez <jjm@usebox.net>2022-08-21 14:28:30 +0100
commitcbedc425b810dd887da2a52d2f33df2bced0f831 (patch)
tree41ed83870edfca9f2bcfacce9cdf8bb767067275 /src
parentae86ec5788c8f5e69032e7cc550434c73faf33a2 (diff)
downloadmicro-lang-hs-cbedc425b810dd887da2a52d2f33df2bced0f831.tar.gz
micro-lang-hs-cbedc425b810dd887da2a52d2f33df2bced0f831.zip
Support for binary numbers witg 0b[0-1] notation
Diffstat (limited to 'src')
-rw-r--r--src/Lexer.hs11
1 files changed, 10 insertions, 1 deletions
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