aboutsummaryrefslogtreecommitdiff
path: root/ProgramParser.hs
diff options
context:
space:
mode:
Diffstat (limited to 'ProgramParser.hs')
-rw-r--r--ProgramParser.hs23
1 files changed, 20 insertions, 3 deletions
diff --git a/ProgramParser.hs b/ProgramParser.hs
index 2cacaf5..38da21f 100644
--- a/ProgramParser.hs
+++ b/ProgramParser.hs
@@ -2,6 +2,7 @@ module ProgramParser(parseProgram) where
import Control.Monad
import Data.Char
+import Data.Maybe
import Text.Parsec
import qualified Text.Parsec.Expr as E
@@ -70,7 +71,7 @@ pBlock = do
return $ Block body
pStatement :: Parser Statement
-pStatement = pSIf <|> pSWhile <|> pSReturn <|> pSDecl <|> pSAs <|> pSExpr
+pStatement = pSIf <|> pSWhile <|> pSReturn <|> pSBreak <|> pSDecl <|> pSAs <|> pSExpr
pSDecl :: Parser Statement
pSDecl = do
@@ -112,8 +113,16 @@ pSWhile = do
pSReturn :: Parser Statement
pSReturn = do
symbol "return"
- SReturn <$> ((symbol ";" >> return Nothing) <|>
- ((Just <$> pExpression) <* symbol ";"))
+ m <- optionMaybe pExpression
+ symbol ";"
+ return $ SReturn m
+
+pSBreak :: Parser Statement
+pSBreak = do
+ symbol "break"
+ m <- optionMaybe pIntegerInt
+ symbol ";"
+ return $ SBreak (fromMaybe 0 m)
pSExpr :: Parser Statement
pSExpr = do
@@ -231,6 +240,14 @@ pName = do
pInteger :: Parser Integer
pInteger = read <$> many1 (satisfy isDigit) <* pWhiteComment
+pIntegerInt :: Parser Int
+pIntegerInt = do
+ i <- pInteger
+ when (i > (fromIntegral (maxBound :: Int) :: Integer) ||
+ i < (fromIntegral (minBound :: Int) :: Integer)) $
+ unexpected $ "Integer literal " ++ show i ++ " does not fit in an Int"
+ return $ fromIntegral i
+
symbol :: String -> Parser ()
symbol "" = error "symbol \"\""