Что-то похожее на правду

This commit is contained in:
2024-12-08 19:17:29 +03:00
parent c66352d500
commit 70d1742368

View File

@@ -48,3 +48,39 @@ char c = satisfy (== c)
digit :: Parser Char Char digit :: Parser Char Char
digit = satisfy isDigit 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