Что-то похожее на правду
This commit is contained in:
@@ -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
|
||||||
|
|||||||
Reference in New Issue
Block a user