aboutsummaryrefslogtreecommitdiff
path: root/ProgramParser.hs
diff options
context:
space:
mode:
Diffstat (limited to 'ProgramParser.hs')
-rw-r--r--ProgramParser.hs27
1 files changed, 25 insertions, 2 deletions
diff --git a/ProgramParser.hs b/ProgramParser.hs
index 38da21f..34b5ce7 100644
--- a/ProgramParser.hs
+++ b/ProgramParser.hs
@@ -71,7 +71,7 @@ pBlock = do
return $ Block body
pStatement :: Parser Statement
-pStatement = pSIf <|> pSWhile <|> pSReturn <|> pSBreak <|> pSDecl <|> pSAs <|> pSExpr
+pStatement = pSIf <|> pSWhile <|> pSReturn <|> pSBreak <|> pSDebugger <|> pSDecl <|> pSAs <|> pSExpr
pSDecl :: Parser Statement
pSDecl = do
@@ -124,6 +124,9 @@ pSBreak = do
symbol ";"
return $ SBreak (fromMaybe 0 m)
+pSDebugger :: Parser Statement
+pSDebugger = symbol "debugger" >> symbol ";" >> return SDebugger
+
pSExpr :: Parser Statement
pSExpr = do
e <- pExpression
@@ -138,6 +141,9 @@ pExpression = E.buildExpressionParser optable litparser
[E.Infix (symbol "*" >> return (mkEBin BOMul)) E.AssocLeft,
E.Infix (symbol "/" >> return (mkEBin BODiv)) E.AssocLeft,
E.Infix (symbol "%" >> return (mkEBin BOMod)) E.AssocLeft],
+ [E.Infix (symbol "^" >> return (mkEBin BOBitXor)) E.AssocLeft],
+ [E.Infix (symbol "&" >> return (mkEBin BOBitAnd)) E.AssocLeft],
+ [E.Infix (symbol "|" >> return (mkEBin BOBitOr)) E.AssocLeft],
[E.Infix (symbol "+" >> return (mkEBin BOAdd)) E.AssocLeft,
E.Infix (symbol "-" >> return (mkEBin BOSub)) E.AssocLeft],
[E.Infix (symbol ">=" >> return (mkEBin BOGeq)) E.AssocNone,
@@ -238,7 +244,20 @@ pName = do
return $ c0 : cr
pInteger :: Parser Integer
-pInteger = read <$> many1 (satisfy isDigit) <* pWhiteComment
+pInteger = bareint <* pWhiteComment
+ where
+ bareint =
+ (try (string "0x") >> many1 (satisfy isHexDigit) >>= return . read . ("0x" ++)) <|>
+ (try (string "0b") >> many1 (oneOf "01") >>= return . bin2int) <|>
+ (many1 (satisfy isDigit) >>= return . read)
+
+ bin2int :: String -> Integer
+ bin2int s = go (reverse s)
+ where
+ go "" = 0
+ go ('0':s') = 2 * go s'
+ go ('1':s') = 2 * go s' + 1
+ go (_:_) = undefined
pIntegerInt :: Parser Int
pIntegerInt = do
@@ -255,8 +274,12 @@ symbol s = try $ do
void $ string s
when (isAlpha (last s)) $ void $ notFollowedBy (satisfy isAlpha)
when (isDigit (last s)) $ void $ notFollowedBy (satisfy isDigit)
+ when (isOperatorChar (last s)) $ void $ notFollowedBy (satisfy isOperatorChar)
pWhiteComment
+isOperatorChar :: Char -> Bool
+isOperatorChar = (`elem` "*/%^&|+-><=!")
+
pWhiteComment :: Parser ()
pWhiteComment = void $ pWhite >> endBy pComment pWhite