module Calculator.Evaluator.Base (evaluate) where -------------------------------------------------------------------------------- import Calculator.Evaluator.Statement (evalStat) import Calculator.Parser.Statement (parseStat) import Calculator.Prim.Expr (Bindings, Expr (..)) -------------------------------------------------------------------------------- import Control.Applicative ((<*)) -- import Data.List (nub) import Text.ParserCombinators.Parsec (eof, parse) -------------------------------------------------------------------------------- eval :: Bindings -> String -> Either Expr Bindings eval b inp = case parse (parseStat <* eof) "Statement" inp of Left e -> Left . Message . tail . lines $ 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 (Message x) -> Left . init . unlines . map (" !! " ++ ) . nub $ x Left (Message x) -> Left . init . unlines . map (" -!- " ++ ) $ x Right b -> Right b Left _ -> Left " ~~ Erroneous Result ~~" -------------------------------------------------------------------------------- evaluate :: Bindings -> String -> Either String Bindings evaluate b s = result $ eval b s --------------------------------------------------------------------------------