packages feed

calculator-0.2.0.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))

--------------------------------------------------------------------------------

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 f@(Func i _ _) = Right $ addFun (i, execFunc b f) b

--------------------------------------------------------------------------------