calculator-0.2.2.0: src/Calculator/Evaluator/Cmd.hs
module Calculator.Evaluator.Cmd (evalCmd) where
--------------------------------------------------------------------------------
import Calculator.Evaluator.Expr (evalExpr)
import Calculator.Evaluator.Func (execFunc)
import Calculator.Help (help)
import Calculator.Prim.Bindings (Bindings, addFun, addVar, display)
import Calculator.Prim.Cmd (Cmd (..))
import Calculator.Prim.Definitions (defBinds)
import Calculator.Prim.Expr (Expr (Message, Constant))
import Calculator.Prim.Function (testFunc)
--------------------------------------------------------------------------------
evalCmd :: Bindings -> Cmd -> Either [String] Bindings
evalCmd _ Help = Left help
evalCmd b Display = Left $ display b
evalCmd _ Reset = Right defBinds
evalCmd b (Assign s e) =
case evalExpr b e of
Message xxs -> Left xxs
Constant c -> Right $ addVar (s, c) b
_ -> Left [" ~~ Erroneous Result ~~ "]
evalCmd b fun@(Func i _ _) = let f = execFunc b fun
in case testFunc f 0 of
Nothing -> Right $ addFun (i, f) b
Just ms -> Left ms
--------------------------------------------------------------------------------