Skip to content

Commit

Permalink
add: parse Cast
Browse files Browse the repository at this point in the history
  • Loading branch information
oriollinan committed Jan 8, 2025
1 parent 7cbd131 commit 58c6c29
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/Ast/Parser/Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ parseExpr =
parseBreak,
parseContinue,
parseBlock,
parseCast,
parseLit,
M.try parseFunction,
M.try parseDeclaration,
Expand Down Expand Up @@ -184,6 +185,13 @@ parseArrayAccess = do
pos <- PU.symbol "." *> parseExpr
return $ AT.ArrayAccess srcLoc value pos

parseCast :: PU.Parser AT.Expr
parseCast = do
srcLoc <- parseSrcLoc
type' <- PU.symbol "@" *> PT.parseType
expr <- M.between (PU.symbol "(") (PU.symbol ")") parseExpr
return $ AT.Cast srcLoc type' expr

parseSrcLoc :: PU.Parser AT.SrcLoc
parseSrcLoc = do
(MP.SourcePos {MP.sourceName = _sourceName, MP.sourceLine = _sourceLine, MP.sourceColumn = _sourceColumn}) <- M.getSourcePos
Expand Down
11 changes: 11 additions & 0 deletions test/Ast/Parser/ExprSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,17 @@ spec = do
(AT.Lit normalizeLoc $ AT.LInt 1)
result `shouldBe` expected

it "parses an type cast" $ do
let input = "@int('a')"
let result = normalizeExpr <$> parseWithEnv input
let expected =
Right $
AT.Cast
normalizeLoc
(AT.TInt 32)
(AT.Lit normalizeLoc $ AT.LChar 'a')
result `shouldBe` expected

normalizeLoc :: AT.SrcLoc
normalizeLoc = AT.SrcLoc "" 0 0

Expand Down

0 comments on commit 58c6c29

Please # to comment.