diff options
Diffstat (limited to 'src/Evaluator.hs')
-rw-r--r-- | src/Evaluator.hs | 38 |
1 files changed, 23 insertions, 15 deletions
diff --git a/src/Evaluator.hs b/src/Evaluator.hs index 8a5274f..e3bf697 100644 --- a/src/Evaluator.hs +++ b/src/Evaluator.hs | |||
@@ -1,8 +1,7 @@ | |||
1 | module Evaluator ( | 1 | module Evaluator (eval) where |
2 | eval | ||
3 | ) where | ||
4 | 2 | ||
5 | import Control.Monad.Except | 3 | import Control.Monad.Except |
4 | import Environment | ||
6 | import Error.Base (LispError (..), LispResult (..), | 5 | import Error.Base (LispError (..), LispResult (..), |
7 | unwrap) | 6 | unwrap) |
8 | import Operators | 7 | import Operators |
@@ -15,18 +14,27 @@ apply fn args = maybe | |||
15 | ($ args) | 14 | ($ args) |
16 | (lookup fn primitives) | 15 | (lookup fn primitives) |
17 | 16 | ||
18 | eval :: Expr -> LispResult Expr | 17 | eval :: Env -> Expr -> IOResult Expr |
19 | eval v@(StringLiteral s) = return v | 18 | eval _ v@(StringLiteral s) = return v |
20 | eval v@(IntLiteral i) = return v | 19 | eval _ v@(IntLiteral i) = return v |
21 | eval v@(BoolLiteral b) = return v | 20 | eval _ v@(BoolLiteral b) = return v |
22 | eval v@(FloatLiteral f) = return v | 21 | eval env (Id l) = getVar env l |
23 | eval v@(Vector xs) = liftM Vector $ mapM eval xs | 22 | eval _ v@(FloatLiteral f) = return v |
24 | -- handle quotes as literals | 23 | eval env v@(Vector xs) = liftM Vector $ mapM (eval env) xs |
25 | eval (List[Id "quote", val]) = return val | 24 | eval env (List[Id "quote", val]) = return val |
26 | eval (List[Id "quasiquote", val]) = undefined | 25 | eval env (List[Id "quasiquote", val]) = undefined |
27 | eval (List[Id "unquote", val]) = undefined | 26 | eval env (List[Id "unquote", val]) = eval env val |
28 | eval (List (Id fn : args)) = mapM eval args >>= apply fn | 27 | eval env (List [Id "set!", Id var, val]) = do |
28 | e <- eval env val | ||
29 | setVar env var e | ||
30 | return e | ||
31 | eval env (List [Id "define", Id var, val]) = do | ||
32 | e <- eval env val | ||
33 | defineVar env var e | ||
34 | return e | ||
35 | eval env (List (Id fn : args)) = mapM (eval env) args >>= liftLispResult . apply fn | ||
36 | eval env NoReturn = throwError $ BadForm "Invalid usage of non-returning expression" NoReturn | ||
29 | 37 | ||
30 | -- handle bad forms | 38 | -- handle bad forms |
31 | eval invalidForm = throwError $ BadForm "lisk can't recognize this form" invalidForm | 39 | eval env invalidForm = throwError $ BadForm "lisk can't recognize this form" invalidForm |
32 | 40 | ||