Skip to content

Commit

Permalink
feat(parsing): parseCond now handle else if
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuMarques committed Jan 12, 2024
1 parent 695fba1 commit 6b49da8
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions LobsterLang/src/Parse.hs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ module Parse (
parseWhiteSpace,
errorParsing,
parseDefineFn,
parseCond
parseCond,
parseCmpString
) where

import qualified AST
Expand Down Expand Up @@ -343,6 +344,14 @@ parseAnyString s = Parser (f s s)
Right (_, s'', pos') -> f xs str pos' s''
f [] str pos s' = Right (str, s', pos)

parseCmpString :: String -> Parser String
parseCmpString s = parseWhiteSpace *> Parser (f s) <* parseWhiteSpace
where
f :: String -> Position -> String -> Either String (String, String, Position)
f str pos s' = case runParser parseString pos s' of
Left err -> Left err
Right (res, s'', pos') -> if str == res then Right (res, s'', pos') else Left (errorParsing pos')

-- | Return a Parser that parse a Bool (#f or #t)
parseBool :: Parser AST.Ast
parseBool = AST.Boolean <$> (parseTrue <|> parseFalse) <* parseWhiteSpace
Expand Down Expand Up @@ -408,12 +417,8 @@ parseAst = parseWhiteSpace *>
)

parseDefineFn :: Parser AST.Ast
parseDefineFn = (Parser parseFn) *> (Parser defineFn)
parseDefineFn = parseCmpString "fn" *> (Parser defineFn)

Check warning on line 420 in LobsterLang/src/Parse.hs

View workflow job for this annotation

GitHub Actions / check_style

Suggestion in parseDefineFn in module Parse: Redundant bracket ▫︎ Found: "parseCmpString \"fn\" *> (Parser defineFn)" ▫︎ Perhaps: "parseCmpString \"fn\" *> Parser defineFn"
where
parseFn :: Position -> String -> Either String (String, String, Position)
parseFn pos s = case runParser (parseAnyString "fn") pos s of
Left err -> Left err
Right (res, s', pos') -> Right (res, s', pos')
defineFn :: Position -> String -> Either String (AST.Ast, String, Position)
defineFn s pos = case runParser parseString s pos of
Left err -> Left err
Expand All @@ -434,29 +439,29 @@ parseFunctionValue = Parser parseParams
parseBracket :: Parser AST.Ast
parseBracket = parseStart *> parseAst <* parseEnd
where
parseEnd = parseChar '}' <* parseWhiteSpace
parseStart = parseWhiteSpace *> parseChar '{'
parseEnd = parseWhiteSpace *> parseChar '}' <* parseWhiteSpace
parseStart = parseWhiteSpace *> parseChar '{' <* parseWhiteSpace

parseCond :: Parser AST.Ast
parseCond = parseAnyString "if" *> Parser parseIf
parseCond = parseCmpString "if" *> Parser parseIf
where
parseIf :: Position -> String -> Either String (AST.Ast, String, Position)
parseIf pos s = case runParser parseExpr pos s of
Left err -> Left err
Right (res, s', pos') -> case runParser parseBracket pos' s' of
Left err -> Left err
Right (res', s'', pos'') -> case runParser parseElse pos'' s'' of
Left err -> Left err
Right (res'', s''', pos''') -> Right ((AST.Cond res res' res''), s''', pos''')
parseElse :: Parser (Maybe AST.Ast)
parseElse = Parser f
Left _ -> Right ((AST.Cond res res' Nothing), s'', pos'')

Check warning on line 454 in LobsterLang/src/Parse.hs

View workflow job for this annotation

GitHub Actions / check_style

Suggestion in parseCond in module Parse: Redundant bracket ▫︎ Found: "((AST.Cond res res' Nothing), s'', pos'')" ▫︎ Perhaps: "(AST.Cond res res' Nothing, s'', pos'')"
Right (res'', s''', pos''') -> Right ((AST.Cond res res' (Just res'')), s''', pos''')

Check warning on line 455 in LobsterLang/src/Parse.hs

View workflow job for this annotation

GitHub Actions / check_style

Suggestion in parseCond in module Parse: Redundant bracket ▫︎ Found: "((AST.Cond res res' (Just res'')), s''', pos''')" ▫︎ Perhaps: "(AST.Cond res res' (Just res''), s''', pos''')"
parseElse :: Parser AST.Ast
parseElse = parseCmpString "else" *> Parser p
where
f :: Position -> String -> Either String ((Maybe AST.Ast), String, Position)
f pos s = case runParser (parseAnyString "else") pos s of
Left _ -> Right (Nothing, s, pos)
Right (_, s', pos') -> case runParser parseBracket pos' s' of
p :: Position -> String -> Either String (AST.Ast, String, Position)
p pos s = case runParser parseCond pos s of
Left _ -> case runParser parseBracket pos s of
Left err -> Left err
Right (res, s'', pos'') -> Right ((Just res), s'', pos'')
Right (res, s', pos') -> Right (res, s', pos')
Right (res, s', pos') -> Right (res, s', pos')

parseLobster :: Parser [AST.Ast]
parseLobster = parseSome (parseWhiteSpace *> parseAst)

0 comments on commit 6b49da8

Please sign in to comment.