Skip to content

Commit

Permalink
Merge pull request #124 from EpitechPromo2027/123-incorrect-precedenc…
Browse files Browse the repository at this point in the history
…e-with-comparison

add: operator hierarchy with comparisons
  • Loading branch information
oriollinan authored Jan 9, 2025
2 parents 162c128 + 457f71d commit ec446dd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
11 changes: 6 additions & 5 deletions lib/Ast/Parser/Operation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,19 @@ operationTable =
binary "<<" (`AT.Op` AT.BitShl),
binary ">>" (`AT.Op` AT.BitShr)
],
[ binary "&&" (`AT.Op` AT.And),
binary "and" (`AT.Op` AT.And),
binary "||" (`AT.Op` AT.Or),
binary "or" (`AT.Op` AT.Or),
binary "==" (`AT.Op` AT.Eq),
[ binary "==" (`AT.Op` AT.Eq),
binary "is" (`AT.Op` AT.Eq),
binary "!=" (`AT.Op` AT.Ne),
binary "<=" (`AT.Op` AT.Lte),
binary ">=" (`AT.Op` AT.Gte),
binary "<" (`AT.Op` AT.Lt),
binary ">" (`AT.Op` AT.Gt)
],
[ binary "&&" (`AT.Op` AT.And),
binary "and" (`AT.Op` AT.And),
binary "||" (`AT.Op` AT.Or),
binary "or" (`AT.Op` AT.Or)
],
[ postfix "." (`AT.UnaryOp` AT.Deref),
postfix "++" (`AT.UnaryOp` AT.PostInc),
postfix "--" (`AT.UnaryOp` AT.PostDec)
Expand Down
23 changes: 23 additions & 0 deletions test/Ast/Parser/ExprSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,29 @@ spec = do
)
result `shouldBe` expected

it "parses an operator with hierarchy and comparisons" $ do
let input = "n is 0 or n is 1"
let env = E.insertVar "n" (AT.TInt 32) E.emptyEnv
let result = normalizeExpr <$> fst (S.runState (M.runParserT PE.parseExpr "" input) env)
let expected =
Right $
AT.Op
normalizeLoc
AT.Or
( AT.Op
normalizeLoc
AT.Eq
(AT.Var normalizeLoc "n" $ AT.TInt 32)
(AT.Lit normalizeLoc $ AT.LInt 0)
)
( AT.Op
normalizeLoc
AT.Eq
(AT.Var normalizeLoc "n" $ AT.TInt 32)
(AT.Lit normalizeLoc $ AT.LInt 1)
)
result `shouldBe` expected

it "parses a unary operator" $ do
let input = "not 1"
let result = normalizeExpr <$> parseWithEnv input
Expand Down

0 comments on commit ec446dd

Please # to comment.