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
|
{-# OPTIONS_GHC -Wno-missing-export-lists #-}
module Utils where
import Control.Monad.Except
import Control.Monad.Reader
import qualified Data.Map as M
import qualified Data.List as L
import qualified Data.Char as C
-- TYPES
newtype LException = LException String
data Config = Config {
configScriptFileName :: Maybe String,
configVerboseMode :: Bool,
configShowHelp :: Bool,
configPrintEvaled :: Bool,
configPrintCallStack :: Bool
}
type LContext a = ReaderT Config (ExceptT LException IO) a
runL :: Config -> LContext a -> IO (Either LException a)
runL config lc = runExceptT $ runReaderT lc config
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
type Env = M.Map String AST
type LFunction = (Env -> AST -> LContext AST)
data ASTNode
= ASTInteger Int
| ASTDouble Double
| ASTSymbol String
| ASTBoolean Bool
| ASTString String
| ASTVector [AST]
| ASTFunctionCall [AST]
| ASTHashMap (M.Map AST AST)
| ASTFunction LFunction
| ASTUnit
| ASTHole
data AST = AST {
astNode :: ASTNode,
astRow :: Int,
astColumn :: Int,
astFileName :: String
}
instance (Show AST) where
show (AST { astNode = 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 _) = "<fn>"
show ASTUnit = "<unit>"
show ASTHole = "<hole>"
instance (Eq AST) where
AST { astNode = node1 } == AST { astNode = 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 { astNode = node1 } <= AST { astNode = 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 { astNode = node }) = case node of
(ASTFunction _) -> return ast
_ -> throwL $ show node ++ " is not a function"
assertIsASTInteger :: AST -> LContext AST
assertIsASTInteger ast@(AST { astNode = node }) = case node of
(ASTInteger _) -> return ast
_ -> throwL $ show node ++ " is not an integer"
assertIsASTSymbol :: AST -> LContext AST
assertIsASTSymbol ast@(AST { astNode = node }) = case node of
(ASTSymbol _) -> return ast
_ -> throwL $ show node ++ " is not a symbol"
assertIsASTVector :: AST -> LContext AST
assertIsASTVector ast@(AST { astNode = node }) = case node of
(ASTVector _) -> return ast
_ -> throwL $ show node ++ " is not a vector"
assertIsASTString :: AST -> LContext AST
assertIsASTString ast@(AST { astNode = node }) = case node of
(ASTString _) -> return ast
_ -> throwL $ show node ++ " is not a string"
assertIsASTFunctionCall :: AST -> LContext AST
assertIsASTFunctionCall ast@(AST { astNode = node }) = case node of
(ASTFunctionCall _) -> return ast
_ -> throwL $ 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 -> LContext a
throwL s = throwError $ LException s
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 { astNode = node, astRow = -1, astColumn = -1, astFileName = "nonsense"}
pos :: AST -> String
pos AST { astRow = r, astColumn = c, astFileName = f } = f ++ ":" ++ show r ++ ":" ++ show c
|