aboutsummaryrefslogtreecommitdiff
path: root/src/Evaluator.hs
blob: c8d8d34f5115685699423cd2f074f1a002598c80 (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
module Evaluator (
    eval
    ) where

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

apply :: String -> [Expr] -> LispResult Expr
apply fn args =
    case lookup fn primitives of
      Just f -> f args
      _      -> throwError $ UnknownFunction fn

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
-- handle quotes as literals
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