From c38aa041434f34267476b5b4739865d4ae926847 Mon Sep 17 00:00:00 2001 From: VegOwOtenks Date: Fri, 18 Oct 2024 10:00:39 +0200 Subject: [PATCH 1/2] Implemented binary equality operator --- src/Lib.hs | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/Lib.hs b/src/Lib.hs index f081467..e4ecfed 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -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)) ], @@ -89,8 +92,9 @@ collectVariableNames (Unary _ right) = collectVariableNames right collectVariableNames (Variable name) = Set.singleton name evaluate :: Map String Bool -> Expression -> Bool -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 (Variable name) = maybe False id (Map.lookup name vs) +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) From c4860e60b7d6e12e6dbfc402be216ec40734ba72 Mon Sep 17 00:00:00 2001 From: VegOwOtenks Date: Fri, 18 Oct 2024 10:00:54 +0200 Subject: [PATCH 2/2] Changed the separator character '#' --- app/Main.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index a6269da..88e9654 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -28,13 +28,13 @@ parseInputLine :: Int -> String -> Either ParseError Expression parseInputLine n s = parseFullString ("stdin:" ++ show n) s title :: [String] -> String -title ns = " " ++ intercalate " | " ns +title ns = " " ++ intercalate " # " ns header :: [Int] -> String -header ws = intercalate "|" . map (flip replicate '-' . (+2) ) $ ws +header ws = intercalate "#" . map (flip replicate '#' . (+2) ) $ ws row :: [Int] -> [Bool] -> String -row ws rs = intercalate "|" $ (zipWith row' ws rs) +row ws rs = intercalate "#" $ (zipWith row' ws rs) where row' :: Int -> Bool -> String row' w b = replicate (left+1) ' ' ++ (if b then "1" else "0") ++ replicate (right+1) ' '