Implemented binary equality operator

This commit is contained in:
VegOwOtenks 2024-10-18 10:00:39 +02:00
parent cab7424415
commit c38aa04143

View file

@ -34,7 +34,7 @@ import Data.Functor.Identity
data Expression = Constant Bool | Binary BinaryOperator Expression Expression | Unary UnaryOperator Expression | Variable String
deriving Show
data BinaryOperator = LogicalAnd | LogicalOr
data BinaryOperator = LogicalAnd | LogicalOr | LogicalEquality
deriving Show
data UnaryOperator = LogicalNot
deriving Show
@ -44,9 +44,9 @@ def = emptyDef{ commentStart = ""
, commentEnd = ""
, identStart = letter <|> char '_'
, identLetter = alphaNum <|> char '_'
, opStart = oneOf "&|~"
, opLetter = oneOf "&|~"
, reservedOpNames = ["&", "|", "~"]
, opStart = oneOf "&|~="
, opLetter = oneOf "&|~="
, reservedOpNames = ["&", "|", "~", "="]
, reservedNames = ["true", "false"]
}
@ -61,6 +61,9 @@ exprparser = buildExpressionParser table term <?> "expression"
table :: [[Operator String u Identity Expression]]
table = [
[
Infix (m_reservedOp "=" >> return (Binary LogicalEquality)) AssocLeft
],
[
Prefix (m_reservedOp "~" >> return (Unary LogicalNot))
],
@ -93,4 +96,5 @@ evaluate _ (Constant b) = b
evaluate vs (Unary LogicalNot e) = not (evaluate vs e)
evaluate vs (Binary LogicalAnd l r) = evaluate vs l && evaluate vs r
evaluate vs (Binary LogicalOr l r) = evaluate vs l || evaluate vs r
evaluate vs (Binary LogicalEquality l r) = evaluate vs l == evaluate vs r
evaluate vs (Variable name) = maybe False id (Map.lookup name vs)