calculator 0.1.5.2 → 0.1.5.4
raw patch · 7 files changed
+40/−39 lines, 7 files
Files
- calculator.cabal +2/−1
- src/Calculator/Evaluator/Cmd.hs +11/−6
- src/Calculator/Evaluator/Expr.hs +7/−14
- src/Calculator/Evaluator/Func.hs +18/−0
- src/Calculator/Parser/Expr.hs +2/−3
- src/Calculator/Prim/Cmd.hs +0/−5
- src/Calculator/Prim/Expr.hs +0/−10
calculator.cabal view
@@ -1,5 +1,5 @@ name: calculator-version: 0.1.5.2+version: 0.1.5.4 synopsis: A calculator repl. description: A calculator repl that processes mathematical expressions. Does basic arithmetic, and provides pre-defined basic mathematical functions.@@ -21,6 +21,7 @@ , Calculator.Evaluator.Cmd , Calculator.Evaluator.Expr , Calculator.Evaluator.Statement+ , Calculator.Evaluator.Func , Calculator.Parser.Base , Calculator.Parser.Cmd , Calculator.Parser.Expr
src/Calculator/Evaluator/Cmd.hs view
@@ -2,18 +2,23 @@ -------------------------------------------------------------------------------- -import Calculator.Prim.Expr (Bindings, defBinds, fromConst, addVar, addFun)-import Calculator.Prim.Cmd (Cmd(..))-import Calculator.Evaluator.Func (mkFun)-import Calculator.Evaluator.Expr (evalExpr)-import Calculator.Help (help)+import Calculator.Evaluator.Expr (evalExpr)+import Calculator.Evaluator.Func (mkFun)+import Calculator.Help (help)+import Calculator.Prim.Cmd (Cmd (..))+import Calculator.Prim.Expr (Bindings, Expr (Message, Constant),+ addFun, addVar, defBinds) -------------------------------------------------------------------------------- evalCmd :: Bindings -> Cmd -> Either [String] Bindings evalCmd _ (Help) = Left help evalCmd _ (Reset) = Right defBinds-evalCmd b (Assign s e) = Right $ addVar (s, fromConst $ evalExpr b e) b+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, mkFun b f) b --------------------------------------------------------------------------------
src/Calculator/Evaluator/Expr.hs view
@@ -4,11 +4,7 @@ import Calculator.Prim.Base (Number) import Calculator.Prim.Expr (Bindings, Expr (..), Operator (..),- fromConst, isConst, joinMessage)------------------------------------------------------------------------------------import Data.Function (on)+ joinMessage) -------------------------------------------------------------------------------- @@ -41,15 +37,12 @@ evalPart :: Bindings -> Expr -> (Operator, Expr) -> Expr evalPart b e1 ((BinaryOp op), e2) =- let val1 = evalExpr b e1- val2 = evalExpr b e2- in if ((&&) `on` isConst) val1 val2- then Constant $ (op `on` fromConst) val1 val2- else case (val1, val2) of- (e@(Message _), f@(Message _)) -> joinMessage e f- (_, e@(Message _)) -> e- (e@(Message _), _) -> e- _ -> Message ["Could not find matching pairs for evalPart"]+ case (evalExpr b e1, evalExpr b e2) of+ (Constant n1, Constant n2) -> Constant $ op n1 n2+ (e@(Message _), f@(Message _)) -> joinMessage e f+ (_, e@(Message _)) -> e+ (e@(Message _), _) -> e+ _ -> Message ["Could not find matching pairs for evalPart"] evalPart _ _ _ = Message ["Could not find suitable pattern for evalPart"] --------------------------------------------------------------------------------
+ src/Calculator/Evaluator/Func.hs view
@@ -0,0 +1,18 @@+module Calculator.Evaluator.Func where++--------------------------------------------------------------------------------++import Calculator.Prim.Base (Number)+import Calculator.Prim.Cmd (Cmd(Func))+import Calculator.Prim.Expr (addVar, Bindings, Expr(Constant))+import Calculator.Evaluator.Expr (evalExpr)++--------------------------------------------------------------------------------++mkFun :: Bindings -> Cmd -> (Number -> Number)+mkFun b (Func _ x e) = \v -> case evalExpr (addVar (x, v) b) e of+ Constant z -> z+ _ -> error "Invalid Result"+mkFun _ _ = error "Making function from invalid cmd"++--------------------------------------------------------------------------------
src/Calculator/Parser/Expr.hs view
@@ -8,7 +8,7 @@ -------------------------------------------------------------------------------- -import Control.Applicative ((<$>))+import Control.Applicative ((<$>), (<*)) import Text.ParserCombinators.Parsec --------------------------------------------------------------------------------@@ -81,8 +81,7 @@ parseCall :: Parser Expr parseCall = do- ident <- optionMaybe parseId- _ <- char '('+ ident <- try (optionMaybe parseId <* char '(') expr <- parseExpr _ <- char ')' return $ case ident of
src/Calculator/Prim/Cmd.hs view
@@ -2,12 +2,7 @@ -------------------------------------------------------------------------------- -import Calculator.Prim.Base (Number) import Calculator.Prim.Expr (Expr)------------------------------------------------------------------------------------type Bindings = [(String, Number)] --------------------------------------------------------------------------------
src/Calculator/Prim/Expr.hs view
@@ -74,16 +74,6 @@ -------------------------------------------------------------------------------- -fromConst :: Expr -> Number-fromConst (Constant v) = v-fromConst (Function _ _) = error "fromConst Function"-fromConst (UnOp _ _) = error "fromConst UnOp"-fromConst (BinOp _) = error "fromConst BinOp"-fromConst (Variable _) = error "fromConst Variable"-fromConst (Message _) = error "fromConst Message"----------------------------------------------------------------------------------- isConst :: Expr -> Bool isConst (Constant _) = True isConst _ = False