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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
{-# OPTIONS_GHC -Wno-missing-export-lists #-}
module Utils where
import Control.Monad.Except
import Control.Exception (IOException, catch)
import Control.Monad.State
import qualified Data.Map as M
import qualified Data.List as L
import qualified Data.Char as C
import qualified Data.Text as T
-- TYPES
type PositionString = String
type ErrorString = String
data LException = LException (Maybe PositionString) ErrorString
data PrintEvaled
= PrintEvaledOff
| PrintEvaledAll
| PrintEvaledNonUnit
deriving Show
data Config = Config {
configScriptFileName :: Maybe String,
configVerboseMode :: Bool,
configShowHelp :: Bool,
configPrintEvaled :: PrintEvaled,
configPrintCallStack :: Bool,
configUseREPL :: Bool
}
data Binding a
= Regular a
| Memoized (M.Map [a] a) a
deriving Show
type Env = M.Map String (Binding AST)
type Scope = [(String, AST)]
data LState = LState {
stateConfig :: Config,
stateEnv :: Env,
stateDepth :: Int,
statePure :: Purity
}
type LineNo = Int
type LContext a = StateT LState (ExceptT LException IO) a
runL :: LState -> LContext a -> IO (Either LException (a, LState))
runL s lc = runExceptT $ (flip runStateT) s lc
getEnv :: LContext Env
getEnv = do
s <- get
return $ stateEnv s
getConfig :: LContext Config
getConfig = do
s <- get
return $ stateConfig s
putEnv :: Env -> LContext ()
putEnv env = do
modify (\s -> s { stateEnv = env })
insertEnv :: String -> Binding AST -> LContext ()
insertEnv k v = do
env <- getEnv
putEnv $ M.insert k v env
incrementDepth :: LContext ()
incrementDepth =
modify (\s -> s { stateDepth = stateDepth s + 1 })
decrementDepth :: LContext ()
decrementDepth =
modify (\s -> s { stateDepth = stateDepth s - 1 })
getDepth :: LContext Int
getDepth = do
s <- get
return $ stateDepth s
getPurity :: LContext Purity
getPurity = do
s <- get
return $ statePure s
isAllowedPurity :: Purity -> LContext Bool
isAllowedPurity purity = do
s <- get
let currentPurity = statePure s
return $ case currentPurity of
Impure -> True -- if currently in impure context (false), all calls are ok
Pure -> purity == Pure -- but if in pure context (true), only pure calls are ok
updatePurity :: Purity -> LContext ()
updatePurity purity = do
modify (\s -> s { statePure = purity })
checkPurity :: Purity -> LContext ()
checkPurity purity = do
purityOk <- isAllowedPurity purity
when (not purityOk) $ throwL "" $ "cannot call impure function in pure context"
data Token = Token {
tokenContent :: String,
tokenRow :: Int,
tokenColumn :: Int,
tokenFileName :: String
}
instance (Eq Token) where
Token { tokenContent = tc1 } == Token { tokenContent = tc2 } = tc1 == tc2
instance (Show Token) where
show token = show $ tokenContent token
data Purity = Pure | Impure deriving Eq
type LFunction = AST -> LContext AST
type LRecord = M.Map String AST
data ASTNode
= ASTInteger Int
| ASTDouble Double
| ASTSymbol String
| ASTBoolean Bool
| ASTString String
| ASTVector [AST]
| ASTFunctionCall [AST]
| ASTHashMap (M.Map AST AST)
| ASTFunction Purity LFunction
| ASTRecord String LRecord
| ASTUnit
| ASTHole
data AST = AST {
an :: ASTNode,
astRow :: Int,
astColumn :: Int,
astFileName :: String
}
instance (Show AST) where
show (AST { an = node }) = show node
instance (Show ASTNode) where
show (ASTInteger n) = show n
show (ASTDouble n) = show n
show (ASTSymbol s) = s
show (ASTBoolean b) = show b $> map C.toLower
show (ASTString s) = show s
show (ASTVector v) = "[" ++ L.intercalate " " (map show v) ++ "]"
show (ASTFunctionCall v) = "(" ++ L.intercalate " " (map show v) ++ ")"
show (ASTHashMap m) =
let flattenMap = M.assocs .> L.concatMap (\(k, v) -> [k, v])
in "{" ++ L.intercalate " " (map show $ flattenMap m) ++ "}"
show (ASTFunction isPure _) = case isPure of
Pure -> "<pure fn>"
Impure -> "<impure fn>"
show (ASTRecord identifier record) =
let assocsStrList = map (\(k, v) -> k ++ ":" ++ show v) (M.assocs record)
in "(" ++ identifier ++ " " ++ L.intercalate " " assocsStrList ++ ")"
show ASTUnit = "<unit>"
show ASTHole = "<hole>"
instance (Eq AST) where
AST { an = node1 } == AST { an = node2 } = node1 == node2
instance (Eq ASTNode) where
ASTInteger a == ASTInteger b = a == b
ASTDouble a == ASTDouble b = a == b
ASTSymbol a == ASTSymbol b = a == b
ASTBoolean a == ASTBoolean b = a == b
ASTString a == ASTString b = a == b
ASTVector a == ASTVector b = a == b
ASTFunctionCall a == ASTFunctionCall b = a == b
ASTHashMap a == ASTHashMap b = a == b
ASTUnit == ASTUnit = True
ASTHole == _ = True
_ == ASTHole = True
_ == _ = False
instance (Ord AST) where
AST { an = node1 } <= AST { an = node2 } = node1 <= node2
instance (Ord ASTNode) where
ASTInteger a <= ASTInteger b = a <= b
ASTDouble a <= ASTDouble b = a <= b
ASTSymbol a <= ASTSymbol b = a <= b
ASTBoolean a <= ASTBoolean b = a <= b
ASTString a <= ASTString b = a <= b
ASTVector a <= ASTVector b = a <= b
ASTFunctionCall a <= ASTFunctionCall b = a <= b
ASTHashMap a <= ASTHashMap b = a <= b
ASTUnit <= ASTUnit = True
ASTHole <= _ = True
_ <= ASTHole = True
_ <= _ = False
assertIsASTFunction :: AST -> LContext AST
assertIsASTFunction ast@(AST { an = node }) = case node of
(ASTFunction _ _) -> return ast
_ -> throwL (astPos ast) $ show node ++ " is not a function"
assertIsASTInteger :: AST -> LContext AST
assertIsASTInteger ast@(AST { an = node }) = case node of
(ASTInteger _) -> return ast
_ -> throwL (astPos ast) $ show node ++ " is not an integer"
assertIsASTSymbol :: AST -> LContext AST
assertIsASTSymbol ast@(AST { an = node }) = case node of
(ASTSymbol _) -> return ast
_ -> throwL (astPos ast) $ show node ++ " is not a symbol"
assertIsASTVector :: AST -> LContext AST
assertIsASTVector ast@(AST { an = node }) = case node of
(ASTVector _) -> return ast
_ -> throwL (astPos ast) $ show node ++ " is not a vector"
assertIsASTString :: AST -> LContext AST
assertIsASTString ast@(AST { an = node }) = case node of
(ASTString _) -> return ast
_ -> throwL (astPos ast) $ show node ++ " is not a string"
assertIsASTFunctionCall :: AST -> LContext AST
assertIsASTFunctionCall ast@(AST { an = node }) = case node of
(ASTFunctionCall _) -> return ast
_ -> throwL (astPos ast) $ show node ++ " is not a function call or body"
-- UTILS
(.>) :: (a -> b) -> (b -> c) -> a -> c
(.>) = flip (.)
($>) :: b -> (b -> c) -> c
($>) = flip ($)
infixr 6 $>
oddElems :: [a] -> [a]
oddElems [] = []
oddElems (x:xs) = x:evenElems xs
evenElems :: [a] -> [a]
evenElems [] = []
evenElems (_:xs) = oddElems xs
throwL :: String -> String -> LContext a
throwL p s = throwError $ LException mp s
where mp = if p == "" then Nothing else Just p
appendError :: String -> LException -> LContext a
appendError as (LException psM es) =
throwError $ LException psM $ es ++ "\n " ++ as
asPairsM :: [a] -> LContext [(a, a)]
asPairsM [] = return []
asPairsM (a:b:rest) = do
restPaired <- asPairsM rest
return $ (a, b) : restPaired
asPairsM _ = throwL "" "odd number of elements to pair up"
asPairs :: [a] -> [(a, a)]
asPairs [] = []
asPairs (a:b:rest) =
let restPaired = asPairs rest
in (a, b) : restPaired
asPairs _ = error "odd number of elements to pair up"
makeNonsenseToken :: String -> Token
makeNonsenseToken content =
Token { tokenContent = content, tokenRow = -1, tokenColumn = -1, tokenFileName = "nonsense" }
makeNonsenseAST :: ASTNode -> AST
makeNonsenseAST node =
AST { an = node, astRow = -1, astColumn = -1, astFileName = "nonsense"}
astPos :: AST -> String
astPos AST { astRow = r, astColumn = c, astFileName = f } = f ++ ":" ++ show r ++ ":" ++ show c
tokenPos :: Token -> String
tokenPos Token { tokenRow = r, tokenColumn = c, tokenFileName = f } = f ++ ":" ++ show r ++ ":" ++ show c
separateNsIdPart :: String -> (String, String)
separateNsIdPart identifier =
let t = T.pack identifier
parts = T.splitOn (T.pack "/") t
nsPartText = T.concat $ L.init parts
idPartText = L.last parts
in (T.unpack nsPartText, T.unpack idPartText)
safeReadFile :: FilePath -> IO (Maybe String)
safeReadFile p = (Just <$> readFile p) `catch` handler
where
handler :: IOException -> IO (Maybe String)
handler _ = pure Nothing
safeWriteFile :: FilePath -> String -> IO (Maybe ())
safeWriteFile p content = (Just <$> writeFile p content) `catch` handler
where
handler :: IOException -> IO (Maybe ())
handler _ = pure Nothing
safeAppendFile :: FilePath -> String -> IO (Maybe ())
safeAppendFile p content = (Just <$> appendFile p content) `catch` handler
where
handler :: IOException -> IO (Maybe ())
handler _ = pure Nothing
|