diff options
author | Juan J. Martinez <jjm@usebox.net> | 2022-09-07 16:26:50 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2022-09-07 16:26:50 +0100 |
commit | 65c8beecb14f6d09c49504d74beedd58cc7ddd17 (patch) | |
tree | 0a39cc6fc3f78153272c6528300936c039351d3e /src/Micro/Lexer.hs | |
parent | 48896c56c39344fa429260d3969eccc93ef8035c (diff) | |
download | micro-lang-hs-65c8beecb14f6d09c49504d74beedd58cc7ddd17.tar.gz micro-lang-hs-65c8beecb14f6d09c49504d74beedd58cc7ddd17.zip |
Better project layout, removed warnings
Diffstat (limited to 'src/Micro/Lexer.hs')
-rw-r--r-- | src/Micro/Lexer.hs | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/Micro/Lexer.hs b/src/Micro/Lexer.hs new file mode 100644 index 0000000..5496af1 --- /dev/null +++ b/src/Micro/Lexer.hs @@ -0,0 +1,58 @@ +module Micro.Lexer where + +import Data.Char (digitToInt) +import Text.Parsec +import Text.Parsec.Language (emptyDef) +import Text.Parsec.String (Parser) +import qualified Text.Parsec.Token as T + +scanner :: T.TokenParser () +scanner = T.makeTokenParser style + where + ops = ["+", "*", "-", ";", "="] + names = ["module", "private", "var", "def", "return", "->", "true", "false"] + style = + emptyDef + { T.commentLine = "#", + T.reservedOpNames = ops, + 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 = try binNum <|> T.integer scanner + +parens :: Parser a -> Parser a +parens = T.parens scanner + +braces :: Parser a -> Parser a +braces = T.braces scanner + +commaSep :: Parser a -> Parser [a] +commaSep = T.commaSep scanner + +colonSep :: Parser String +colonSep = T.colon scanner + +identifier :: Parser String +identifier = T.identifier scanner + +reserved :: String -> Parser () +reserved = T.reserved scanner + +reservedOp :: String -> Parser () +reservedOp = T.reservedOp scanner + +scan :: Parser a -> Parser a +scan p = do + T.whiteSpace scanner + r <- p + eof + return r |