calculator-0.1.3.0: src/Calculator/Evaluator/Base.hs
module Calculator.Evaluator.Base (evaluate) where
--------------------------------------------------------------------------------
import Calculator.Prim.Expr (Bindings, Expr(..))
import Calculator.Parser.Statement (parseStat)
import Calculator.Evaluator.Statement (evalStat)
--------------------------------------------------------------------------------
import Text.ParserCombinators.Parsec (parse, eof)
import Control.Applicative ((<*))
--------------------------------------------------------------------------------
eval :: Bindings -> String -> Either Expr Bindings
eval b inp = case parse (parseStat <* eof) "Statement" inp of
Left e -> Left $ InvalidError $ show e
Right s -> evalStat b s
--------------------------------------------------------------------------------
result :: Either Expr Bindings -> Either String Bindings
result e = case e of
Left (Constant c) -> Left $ show c
Left (InvalidError x) -> Left x
Right b -> Right b
--------------------------------------------------------------------------------
evaluate :: Bindings -> String -> Either String Bindings
evaluate b s = result $ eval b s
--------------------------------------------------------------------------------