From 06bf4c656377572859846767cb9af5db8fc27893 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 9 Oct 2020 11:35:56 +0530 Subject: use mtl to generate errors --- src/Evaluator.hs | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) (limited to 'src/Evaluator.hs') diff --git a/src/Evaluator.hs b/src/Evaluator.hs index f264ee0..28ee79b 100644 --- a/src/Evaluator.hs +++ b/src/Evaluator.hs @@ -2,21 +2,27 @@ module Evaluator ( eval ) where +import Control.Monad.Except +import Error (LispError (..), LispResult (..), + unwrap) import Operators import Parser import Text.ParserCombinators.Parsec -apply :: String -> [Expr] -> Expr +apply :: String -> [Expr] -> LispResult Expr apply fn args = case lookup fn primitives of Just f -> f args - _ -> BoolLiteral False -- TODO: error out instead + _ -> throwError $ UnknownFunction fn -eval :: Expr -> Expr -eval v@(StringLiteral s) = v -eval v@(IntLiteral i) = v -eval v@(BoolLiteral b) = v +eval :: Expr -> LispResult Expr +eval v@(StringLiteral s) = return v +eval v@(IntLiteral i) = return v +eval v@(BoolLiteral b) = return v -- handle quotes as literals -eval (List[Id "quote", val]) = val -eval (List (Id fn : args)) = apply fn $ map eval args +eval (List[Id "quote", val]) = return val +eval (List (Id fn : args)) = mapM eval args >>= apply fn + +-- handle bad forms +eval idk = throwError $ BadForm "lisk can't recognize this form" idk -- cgit v1.2.3