Skip to content

Commit

Permalink
Fix multiple line of expression
Browse files Browse the repository at this point in the history
MINOR
  • Loading branch information
Saverio976 committed Dec 29, 2023
1 parent f10900c commit 9a3aef9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
5 changes: 2 additions & 3 deletions lvtc/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@

module Main (main) where

import Lib
import Expression
import Parser

main :: IO ()
-- main = print $ runParser (parseExpresion) "fn main () -> Int \n{\n <- 0;\n};\n"
main = print $ runParser (parseExpresion) "fn main () -> Int \n{\n <- 0;\n};\n"
-- main = print $ runParser (parseExpresion) "alias abc def;\n"
main = print $ runParser (parseExpresion) "// this is a comment\n"
-- main = print $ runParser (parseExpresion) "// this is a comment\n"
31 changes: 29 additions & 2 deletions lvtc/src/Expression.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,29 @@ instance Eq Expression where
(==) (Comment str1) (Comment str2) = str1 == str2
(==) _ _ = False

countBracketsForFunction :: Int -> String -> Int
countBracketsForFunction _ [] = 0
countBracketsForFunction 1 ['\n', '}', ';', '\n'] = 0
countBracketsForFunction n ['\n', '}', ';', '\n'] = n
countBracketsForFunction 1 ('}':_) = 1
countBracketsForFunction n ('{':xs) = countBracketsForFunction (n + 1) xs
countBracketsForFunction n ('}':xs) = countBracketsForFunction (n - 1) xs
countBracketsForFunction n ('\\':_:xs) = countBracketsForFunction n xs
countBracketsForFunction n (_:xs) = countBracketsForFunction n xs

parseFunction :: Parser Expression
parseFunction = Function <$> ((++) <$> (parseString "fn " <|> parseString "export fn") <*> parseAllCharUntil "\n};\n")
parseFunction = Parser f
where
f str = case runParser (
Function <$>
((++) <$>
(parseString "fn " <|> parseString "export fn") <*>
parseAllCharUntil "\n};\n")) str of
Nothing -> Nothing
Just (Function x, xs) -> case countBracketsForFunction 0 x of
0 -> Just (Function x, xs)
_ -> Nothing
Just _ -> Nothing

parseAlias :: Parser Expression
parseAlias = Alias <$> ((++) <$> parseString "alias " <*> parseAllCharUntil ";\n")
Expand All @@ -41,6 +62,12 @@ parseExpresion :: Parser Expression
parseExpresion = parseAlias <|> parseFunction <|> parseComment

parseAllExpression :: Parser [Expression]
parseAllExpression = some p
parseAllExpression = Parser f
where
p = parseExpresion <* many (parseAnyChar "\n")
f [] = Just ([], [])
f str = case runParser p str of
Nothing -> Nothing
Just (x, xs) -> case runParser parseAllExpression xs of
Nothing -> Nothing
Just (y, ys) -> Just (x : y, ys)
2 changes: 1 addition & 1 deletion lvtc/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ testParserHelper str restExpected expressionExpected =
case runParser parseExpresion str of
Just (parsed, rest) -> assertEqual str restExpected rest >>
assertEqual str expressionExpected parsed
Nothing -> assertFailure ("Parsing failed: " ++ str)
Nothing -> assertFailure ("Parsing failed for: `" ++ str ++ "`")

testParserHelperFail :: String -> IO ()
testParserHelperFail str = case runParser parseExpresion str of
Expand Down

0 comments on commit 9a3aef9

Please sign in to comment.