aboutsummaryrefslogtreecommitdiff
path: root/src/Evaluator.hs
blob: 8a5274f5c05198e9f96b7779d744c0607c295e15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
module Evaluator (
    eval
    ) where

import           Control.Monad.Except
import           Error.Base                    (LispError (..), LispResult (..),
                                                unwrap)
import           Operators
import           Parser
import           Text.ParserCombinators.Parsec

apply :: String -> [Expr] -> LispResult Expr
apply fn args = maybe
    (throwError $ UnknownFunction fn)
    ($ args)
    (lookup fn primitives)

eval :: Expr -> LispResult Expr
eval v@(StringLiteral s) = return v
eval v@(IntLiteral i)    = return v
eval v@(BoolLiteral b)   = return v
eval v@(FloatLiteral f)  = return v
eval v@(Vector xs)       = liftM Vector $ mapM eval xs
-- handle quotes as literals
eval (List[Id "quote", val])      = return val
eval (List[Id "quasiquote", val]) = undefined
eval (List[Id "unquote", val])    = undefined
eval (List (Id fn : args))        = mapM eval args >>= apply fn

-- handle bad forms
eval invalidForm = throwError $ BadForm "lisk can't recognize this form" invalidForm