Added some error handling

This commit is contained in:
Suguivy 2021-02-24 20:23:58 +01:00
parent a02176ee18
commit 6abe90d7cc
2 changed files with 16 additions and 15 deletions

View File

@ -3,17 +3,17 @@ module Evaluator where
import Expression
import Primitives
eval :: Expression -> Expression
eval n@(Number _) = n
eval s@(Symbol _) = s
eval (SExpr es) = apply $ SExpr (map eval es)
eval :: Expression -> Either String Expression
eval n@(Number _) = Right n
eval s@(Symbol _) = Right s
eval (SExpr es) = mapM eval es >>= apply . SExpr
apply :: Expression -> Expression
apply :: Expression -> Either String Expression
apply (SExpr (f:args)) = case f of
Symbol "+" -> pAdd args
Symbol "-" -> pSub args
Symbol "*" -> pMul args
Symbol "/" -> pDiv args
Symbol _ -> error "no primitive functions not supported"
_ -> error "object not applicable"
apply _ = error "Object not applicable"
Symbol "+" -> Right $ pAdd args
Symbol "-" -> Right $ pSub args
Symbol "*" -> Right $ pMul args
Symbol "/" -> Right $ pDiv args
Symbol _ -> Left "no primitive functions not supported (only +, -, * and /)"
_ -> Left "object not applicable"
apply _ = Left "object not applicable"

View File

@ -16,6 +16,7 @@ main = runInputT defaultSettings repl
Left err -> do
outputStrLn $ show err
repl
Right e -> do
outputStrLn . show $ eval e
Right e -> outputStrLn (case eval e of
Right out -> show out
Left err -> err)
repl