From 70d1742368f951e7e29398c38cb3768e1501e442 Mon Sep 17 00:00:00 2001 From: Arity-T Date: Sun, 8 Dec 2024 19:17:29 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A7=D1=82=D0=BE-=D1=82=D0=BE=20=D0=BF=D0=BE?= =?UTF-8?q?=D1=85=D0=BE=D0=B6=D0=B5=D0=B5=20=D0=BD=D0=B0=20=D0=BF=D1=80?= =?UTF-8?q?=D0=B0=D0=B2=D0=B4=D1=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- coursework/part1/src/Lib.hs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/coursework/part1/src/Lib.hs b/coursework/part1/src/Lib.hs index 48bd5ac..943d290 100644 --- a/coursework/part1/src/Lib.hs +++ b/coursework/part1/src/Lib.hs @@ -48,3 +48,39 @@ char c = satisfy (== c) digit :: Parser Char Char digit = satisfy isDigit + +skipSpaces :: Parser Char String +skipSpaces = many (char ' ') + +number :: Parser Char Int +number = skipSpaces *> (strToInt <$> some digit) + where + strToInt = foldl (\acc x -> acc * 10 + digitToInt x) 0 + +data Operation = Add | Sub | Mul | Div deriving Show + +operationToString :: Operation -> String +operationToString op = case op of + Add -> " + " + Sub -> " - " + Mul -> " * " + Div -> " / " + +operationToOperator :: Operation -> (Int -> Int -> Int) +operationToOperator op = case op of + Add -> (+) + Sub -> (-) + Mul -> (*) + Div -> div + + +operation :: Parser Char Operation +operation = skipSpaces *> ( + char '+' *> pure Add <|> + char '-' *> pure Sub <|> + char '*' *> pure Mul <|> + char '/' *> pure Div + ) + +expression :: Parser Char (Int, Operation, Int) +expression = (,,) <$> number <*> operation <*> number <* skipSpaces