aboutsummaryrefslogtreecommitdiffstats
path: root/src/Builtins.hs
blob: 71401d85601abde37da505aa3db6b7caee57054b (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
module Builtins where

import qualified Data.Map as M
import Control.Monad.Except
import Types

builtinEnv :: Env
builtinEnv = M.fromList [
    ("+", builtinAdd2),
    ("-", builtinSubtract2),
    ("head", builtinHead),
    ("tail", builtinTail)
    ]

builtinAdd2 :: AST
builtinAdd2 =
    let outer ast1 = do
            (ASTInteger a) <- assertIsASTInteger ast1
            let inner ast2 = do
                    (ASTInteger b) <- assertIsASTInteger ast2
                    return $ ASTInteger $ a + b
            return $ ASTFunction $ inner
     in ASTFunction outer

builtinSubtract2 :: AST
builtinSubtract2 =
    let outer ast1 = do
            (ASTInteger a) <- assertIsASTInteger ast1
            let inner ast2 = do
                    (ASTInteger b) <- assertIsASTInteger ast2
                    return $ ASTInteger $ a - b
            return $ ASTFunction $ inner
     in ASTFunction outer

builtinHead :: AST
builtinHead =
    let outer ast = do
            (ASTVector vec) <- assertIsASTVector ast
            when (length vec == 0) $ throwError $ LException $ "head of empty vector"
            return $ head vec
     in ASTFunction outer

builtinTail :: AST
builtinTail =
    let outer ast = do
            (ASTVector vec) <- assertIsASTVector ast
            when (length vec == 0) $ throwError $ LException $ "tail of empty vector"
            return $ ASTVector $ tail vec
     in ASTFunction outer