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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
{-# OPTIONS_GHC -Wno-missing-export-lists #-}
module Builtins where
import qualified Data.Map as M
import qualified Data.Text as T
import Control.Monad.Except
import Utils
builtinEnv :: EnvMap
builtinEnv = M.fromList [
-- arithmetic
builtinAdd2,
builtinSubtract2,
builtinMultiply2,
builtinDivide2,
-- comparisons
builtinEq2,
builtinLt2,
-- conversions
builtinFmt,
builtinFloor,
builtinToDouble,
-- vector operations
builtinHead,
builtinTail,
builtinPrepend,
-- string operations
builtinSubstr,
-- vector & string operations
builtinConcat,
-- special
builtinPrint,
("unit", makeNonsenseAST ASTUnit),
("_", makeNonsenseAST ASTHole),
("otherwise", makeNonsenseAST ASTHole),
builtinFatal,
-- reserved keywords
reservedKeyword "\\",
reservedKeyword "let!",
reservedKeyword "match",
reservedKeyword "env!"
]
argError1 :: String -> AST -> String
argError1 fn arg1 =
"invalid argument to " ++ fn ++ ": " ++ show arg1
argError2 :: String -> AST -> AST -> String
argError2 fn arg1 arg2 =
"invalid arguments to " ++ fn ++ ": " ++ show arg1 ++ ", " ++ show arg2
argError3 :: String -> AST -> AST -> AST -> String
argError3 fn arg1 arg2 arg3 =
"invalid arguments to " ++ fn ++ ": " ++ show arg1 ++ ", " ++ show arg2 ++ ", " ++ show arg3
reservedKeyword :: String -> (String, AST)
reservedKeyword name = (name, makeNonsenseAST $ ASTFunction True fn1) where
fn1 ast1 = throwL (astPos ast1) $ "unreachable: " ++ name ++ " is a reserved word"
-- BUILTINS
builtinAdd2 :: (String, AST)
builtinAdd2 = (name, makeNonsenseAST $ ASTFunction True fn1) where
name = "+"
fn1 ast1@AST { astNode = ASTInteger a } =
return $ makeNonsenseAST $ ASTFunction True $ fn2 where
fn2 AST { astNode = ASTInteger b } =
return $ makeNonsenseAST $ ASTInteger $ a + b
fn2 ast2 = throwL (astPos ast2) $ argError2 name ast1 ast2
fn1 ast1@AST { astNode = ASTDouble a } =
return $ makeNonsenseAST $ ASTFunction True $ fn2 where
fn2 AST { astNode = ASTDouble b } =
return $ makeNonsenseAST $ ASTDouble $ a + b
fn2 ast2 = throwL (astPos ast2) $ argError2 name ast1 ast2
fn1 ast1 = throwL (astPos ast1) $ argError1 name ast1
builtinSubtract2 :: (String, AST)
builtinSubtract2 = (name, makeNonsenseAST $ ASTFunction True fn1) where
name = "-"
fn1 ast1@AST { astNode = ASTInteger a } =
return $ makeNonsenseAST $ ASTFunction True $ fn2 where
fn2 AST { astNode = ASTInteger b } =
return $ makeNonsenseAST $ ASTInteger $ a - b
fn2 ast2 = throwL (astPos ast2) $ argError2 name ast1 ast2
fn1 ast1@AST { astNode = ASTDouble a } =
return $ makeNonsenseAST $ ASTFunction True $ fn2 where
fn2 AST { astNode = ASTDouble b } =
return $ makeNonsenseAST $ ASTDouble $ a - b
fn2 ast2 = throwL (astPos ast2) $ argError2 name ast1 ast2
fn1 ast1 = throwL (astPos ast1) $ argError1 name ast1
builtinMultiply2 :: (String, AST)
builtinMultiply2 = (name, makeNonsenseAST $ ASTFunction True fn1) where
name = "*"
fn1 ast1@AST { astNode = ASTInteger a } =
return $ makeNonsenseAST $ ASTFunction True $ fn2 where
fn2 AST { astNode = ASTInteger b } =
return $ makeNonsenseAST $ ASTInteger $ a * b
fn2 ast2 = throwL (astPos ast2) $ argError2 name ast1 ast2
fn1 ast1@AST { astNode = ASTDouble a } =
return $ makeNonsenseAST $ ASTFunction True $ fn2 where
fn2 AST { astNode = ASTDouble b } =
return $ makeNonsenseAST $ ASTDouble $ a * b
fn2 ast2 = throwL (astPos ast2) $ argError2 name ast1 ast2
fn1 ast1 = throwL (astPos ast1) $ argError1 name ast1
builtinDivide2 :: (String, AST)
builtinDivide2 = (name, makeNonsenseAST $ ASTFunction True fn1) where
name = "/"
fn1 ast1@AST { astNode = ASTInteger a } =
return $ makeNonsenseAST $ ASTFunction True $ fn2 where
fn2 ast2@AST { astNode = ASTInteger b } =
do when (b == 0) $ throwL (astPos ast2) $ "division by zero"
return $ makeNonsenseAST $ ASTInteger $ a `div` b
fn2 ast2 = throwL (astPos ast2) $ argError2 name ast1 ast2
fn1 ast1@AST { astNode = ASTDouble a } =
return $ makeNonsenseAST $ ASTFunction True $ fn2 where
fn2 ast2@AST { astNode = ASTDouble b } =
do when (b == 0) $ throwL (astPos ast2) $ "division by zero"
return $ makeNonsenseAST $ ASTDouble $ a / b
fn2 ast2 = throwL (astPos ast2) $ argError2 name ast1 ast2
fn1 ast1 = throwL (astPos ast1) $ argError1 name ast1
builtinEq2 :: (String, AST)
builtinEq2 = (name, makeNonsenseAST $ ASTFunction True fn1) where
name = "eq?"
fn1 ast1 =
return $ makeNonsenseAST $ ASTFunction True $ fn2 where
fn2 ast2 = return $ makeNonsenseAST $ ASTBoolean $ ast1 == ast2
builtinLt2 :: (String, AST)
builtinLt2 = (name, makeNonsenseAST $ ASTFunction True fn1) where
name = "lt?"
fn1 ast1 =
return $ makeNonsenseAST $ ASTFunction True $ fn2 where
fn2 ast2 = return $ makeNonsenseAST $ ASTBoolean $ ast1 <= ast2
builtinFloor :: (String, AST)
builtinFloor = (name, makeNonsenseAST $ ASTFunction True fn1) where
name = "floor"
fn1 AST { astNode = ASTDouble dbl } = return $ makeNonsenseAST $ ASTInteger $ floor dbl
fn1 ast1 = throwL (astPos ast1) $ argError1 name ast1
builtinToDouble :: (String, AST)
builtinToDouble = (name, makeNonsenseAST $ ASTFunction True fn1) where
name = "to-double"
fn1 AST { astNode = ASTInteger int } = return $ makeNonsenseAST $ ASTDouble $ fromIntegral int
fn1 ast1 = throwL (astPos ast1) $ argError1 name ast1
builtinFmt :: (String, AST)
builtinFmt = (name, makeNonsenseAST $ ASTFunction True fn1) where
name = "fmt"
fn1 ast1@AST { astNode = ASTString str } =
return $ makeNonsenseAST $ ASTFunction True $ fn2 where
fn2 AST { astNode = ASTVector replacements } =
return $ makeNonsenseAST $ ASTString $
T.unpack $ replaceAll (0 :: Int) replacements (T.pack str)
fn2 ast2 = throwL (astPos ast2) $ argError2 name ast1 ast2
fn1 ast1 = throwL (astPos ast1) $ argError1 name ast1
replaceAll :: Int -> [AST] -> T.Text -> T.Text
replaceAll _ [] text = text
replaceAll n (x:xs) text =
let xRepr = case x of
AST { astNode = ASTString s } -> s
_ -> show x
text' = T.replace (T.pack $ "{" ++ show n ++ "}") (T.pack $ xRepr) text
in replaceAll (n + 1) xs text'
builtinHead :: (String, AST)
builtinHead = (name, makeNonsenseAST $ ASTFunction True fn1) where
name = "head"
fn1 ast1@AST { astNode = ASTVector vec } =
do when (length vec == 0) $ throwL (astPos ast1) $ name ++ " of empty vector"
return $ head vec
fn1 ast1 = throwL (astPos ast1) $ argError1 name ast1
builtinTail :: (String, AST)
builtinTail = (name, makeNonsenseAST $ ASTFunction True fn1) where
name = "tail"
fn1 ast1@AST { astNode = ASTVector vec } =
do when (length vec == 0) $ throwL (astPos ast1) $ name ++ " of empty vector"
return $ makeNonsenseAST $ ASTVector $ tail vec
fn1 ast1 = throwL (astPos ast1) $ argError1 name ast1
builtinSubstr :: (String, AST)
builtinSubstr = (name, makeNonsenseAST $ ASTFunction True fn1) where
name = "substr"
fn1 ast1@AST { astNode = ASTInteger at } =
return $ makeNonsenseAST $ ASTFunction True $ fn2 where
fn2 ast2@AST { astNode = ASTInteger len } =
return $ makeNonsenseAST $ ASTFunction True $ fn3 where
fn3 AST { astNode = ASTString str } =
return $ makeNonsenseAST $ ASTString $ drop at .> take len $ str
fn3 ast3 = throwL (astPos ast3) $ argError3 name ast1 ast2 ast3
fn2 ast2 = throwL (astPos ast2) $ argError2 name ast1 ast2
fn1 ast1 = throwL (astPos ast1) $ argError1 name ast1
builtinPrepend :: (String, AST)
builtinPrepend = (name, makeNonsenseAST $ ASTFunction True fn1) where
name = "prepend"
fn1 ast1 =
return $ makeNonsenseAST $ ASTFunction True $ fn2 where
fn2 AST { astNode = ASTVector vec } =
return $ makeNonsenseAST $ ASTVector $ ast1 : vec
fn2 ast2 = throwL (astPos ast2) $ argError2 name ast1 ast2
builtinPrint :: (String, AST)
builtinPrint = (name, makeNonsenseAST $ ASTFunction False fn1) where
name = "print!"
fn1 AST { astNode = ASTString str } =
do liftIO $ putStr $ str
return $ makeNonsenseAST ASTUnit
fn1 ast1 = throwL (astPos ast1) $ argError1 name ast1
builtinConcat :: (String, AST)
builtinConcat = (name, makeNonsenseAST $ ASTFunction True fn1) where
name = "concat"
fn1 ast1@AST { astNode = ASTString str1 } =
return $ makeNonsenseAST $ ASTFunction True $ fn2 where
fn2 AST { astNode = ASTString str2 } =
return $ makeNonsenseAST $ ASTString $ str1 ++ str2
fn2 ast2 = throwL (astPos ast2) $ argError2 name ast1 ast2
fn1 ast1 = throwL (astPos ast1) $ argError1 name ast1
builtinFatal :: (String, AST)
builtinFatal = (name, makeNonsenseAST $ ASTFunction False fn1) where
name = "fatal!"
fn1 ast1@AST { astNode = ASTString str } =
throwL (astPos ast1) $ str
fn1 ast1 =
throwL (astPos ast1) $ argError1 name ast1
|