Skip to content

Commit

Permalink
mod: ArrayAccess by . and Array separator
Browse files Browse the repository at this point in the history
  • Loading branch information
oriollinan committed Jan 10, 2025
1 parent 69cd5b7 commit d8667e8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/Ast/Parser/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ parseCall = CE.Postfix $ do

parseArrayAccess :: CE.Operator PU.Parser AT.Expr
parseArrayAccess = CE.InfixL $ do
srcLoc <- PU.parseSrcLoc <* PU.symbol "#"
srcLoc <- PU.parseSrcLoc <* PU.symbol "."
return $ \value pos -> AT.ArrayAccess srcLoc value pos

-- TODO: rethink order
Expand Down
16 changes: 13 additions & 3 deletions lib/Ast/Parser/Literal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,22 @@ parseInt = AT.LInt <$> ML.signed (pure ()) ML.decimal
-- | Parses a floating-point literal.
-- Returns a `Literal` of type `LFloat`.
parseFloat :: PU.Parser AT.Literal
parseFloat = AT.LFloat <$> ML.signed (pure ()) ML.float
parseFloat =
AT.LFloat
<$> ML.signed
(pure ())
( do
wholePart <- ML.decimal :: (PU.Parser Integer)
fractionalPart <- MC.char ',' *> M.some MC.digitChar
let fractional = read ("0." ++ fractionalPart) :: Double
let value = fromIntegral wholePart + fractional
return value
)

-- | Parses a boolean literal (`true` or `false`).
-- Returns a `Literal` of type `LBool`.
parseBool :: PU.Parser AT.Literal
parseBool = AT.LBool True <$ PU.symbol trueSymbol M.<|> AT.LBool False <$ PU.symbol falseSymbol
parseBool = AT.LBool True <$ MC.string trueSymbol M.<|> AT.LBool False <$ MC.string falseSymbol

-- | Parses a character literal (e.g., 'a').
-- Returns a `Literal` of type `LChar`.
Expand All @@ -45,7 +55,7 @@ parseArray :: PU.Parser AT.Literal
parseArray =
M.choice
[ AT.LArray . map AT.LChar <$> M.between (MC.char '\"') (MC.char '\"') (M.many (M.noneOf ['"'])),
AT.LArray <$> M.between (PU.symbol "[") (PU.symbol "]") (M.sepBy parseLiteral (PU.symbol ","))
AT.LArray <$> M.between (PU.symbol "[") (PU.symbol "]") (M.sepBy parseLiteral PU.sc)
]

-- | Parses a `null` literal.
Expand Down
4 changes: 2 additions & 2 deletions test/Ast/Parser/ExprSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ spec = do
result `shouldBe` expected

it "parses an array access" $ do
let input = "myArray#1"
let input = "myArray.1"
let arrayType = AT.TArray AT.TChar Nothing
let env = PS.insertVar "myArray" arrayType PS.parserState
let result = normalizeExpr <$> fst (S.runState (M.runParserT PE.parseExpr "" input) env)
Expand All @@ -284,7 +284,7 @@ spec = do
result `shouldBe` expected

it "parses an nested array access" $ do
let input = "myArray#1#1"
let input = "myArray.1.1"
let arrayType = AT.TArray (AT.TArray AT.TChar Nothing) Nothing
let env = PS.insertVar "myArray" arrayType PS.parserState
let result = normalizeExpr <$> fst (S.runState (M.runParserT PE.parseExpr "" input) env)
Expand Down
10 changes: 5 additions & 5 deletions test/Ast/Parser/LiteralSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ spec = do

describe "parseFloat" $ do
it "parses positive floats" $ do
parseWithEnv "123.45" `shouldBe` Right (AT.LFloat 123.45)
parseWithEnv "123,45" `shouldBe` Right (AT.LFloat 123.45)

it "parses negative floats" $ do
parseWithEnv "-67.89" `shouldBe` Right (AT.LFloat (-67.89))
parseWithEnv "-67,89" `shouldBe` Right (AT.LFloat (-67.89))

it "fails on non-float input" $ do
E.isLeft (parseWithEnv "abc") `shouldBe` True
Expand All @@ -52,16 +52,16 @@ spec = do

describe "parseArray" $ do
it "parses an array of integers" $ do
parseWithEnv "[1,2,3]" `shouldBe` Right (AT.LArray [AT.LInt 1, AT.LInt 2, AT.LInt 3])
parseWithEnv "[1 2 3]" `shouldBe` Right (AT.LArray [AT.LInt 1, AT.LInt 2, AT.LInt 3])

it "parses an array of mixed literals" $ do
parseWithEnv "[true,'a',123]" `shouldBe` Right (AT.LArray [AT.LBool True, AT.LChar 'a', AT.LInt 123])
parseWithEnv "[true 'a' 123]" `shouldBe` Right (AT.LArray [AT.LBool True, AT.LChar 'a', AT.LInt 123])

it "parses a string literal as an array of characters" $ do
parseWithEnv "\"hello\"" `shouldBe` Right (AT.LArray (map AT.LChar "hello"))

it "fails on invalid input" $ do
E.isLeft (parseWithEnv "[1,true,]") `shouldBe` True
E.isLeft (parseWithEnv "[1 true ]") `shouldBe` True

describe "parseNull" $ do
it "parses null" $ do
Expand Down

0 comments on commit d8667e8

Please # to comment.