-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.hs
276 lines (247 loc) · 10.1 KB
/
Parser.hs
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
-- /src/Parser.hs
-- Copyright Joan Montas
-- All rights reserved.
-- License under GNU General Public License v3.0
module Parser
( Atom (..),
Sexpression (..),
parse,
makeGlobalScope,
makeLocalScope,
insertSexpToScope,
insertSexpToGlobalScope,
findSexp,
Scope,
getPrev,
expectSexpression,
evalErrorAtom,
identAtom,
integerAtom,
)
where
import qualified Data.Map as Map
import Lexer
data Sexpression
= A Atom
| Debug [Sexpression]
| CallExpression {callExpressionValue :: Sexpression, callExpressionArgs :: [Sexpression]} -- ( Ident list-of-Sexpression )
| ListExpression [Sexpression]
| ConsExpression Sexpression Sexpression
| IfExpression Sexpression Sexpression Sexpression -- ( if condition body else-body )
| FunctionExpression Sexpression [Sexpression] Sexpression -- name/Ident argument (list of ident), body... own scope too when eval
deriving (Show, Eq)
data Atom
= IntegerAtom {integerAtomValue :: Integer}
| StringAtom {stringAtomValue :: String}
| IdentAtom {identAtomValue :: String}
| NilAtom {nilAtomValue :: String}
| TrueAtom {trueAtomValue :: String}
| ParseErrorAtom {parseErrorAtomValue :: String}
| EvalErrorAtom {evalErrorAtomValue :: String} -- For the future
deriving (Show, Eq)
-- S-expression type
integerAtom :: String
integerAtom = "integerAtom"
stringAtom :: String
stringAtom = "stringAtom"
identAtom :: String
identAtom = "identAtom"
nilAtom :: String
nilAtom = "nilAtom"
trueAtom :: String
trueAtom = "trueAtom"
parseErrorAtom :: String
parseErrorAtom = "parseErrorAtom"
evalErrorAtom :: String
evalErrorAtom = "evalErrorAtom"
-- Makes sure the given token is the desired type
-- TOOO(JoanMontas) just as lexpexceptpeek.... change Sexpression to [Sexpression] this avoid having to check for empty list
-- dry your code
expectSexpression :: Sexpression -> String -> Bool
expectSexpression (A (ParseErrorAtom _)) "parseErrorAtom" = True
expectSexpression (A (EvalErrorAtom _)) "evalErrorAtom" = True
expectSexpression (A (IntegerAtom _)) "integerAtom" = True
expectSexpression (A (StringAtom _)) "stringAtom" = True
expectSexpression (A (IdentAtom _)) "identAtom" = True
expectSexpression (A (TrueAtom _)) "trueAtom" = True
expectSexpression (A (NilAtom _)) "nilAtom" = True
expectSexpression _ _ = False
-- -- -- -- -- -- -- -- -- -- -- Parse
parse :: [Token] -> [Sexpression] -> ([Token], [Sexpression])
parse [] s =
( [],
s
++ [ A
( ParseErrorAtom
"parse's parse Error: Received empmty token" -- TODO(JoanMontas) remove and return nil instead
)
]
)
-- parse atoms
parse ((IntegerToken v) : ts) s = (ts, s ++ [A (IntegerAtom v)])
parse ((StringToken v) : ts) s = (ts, s ++ [A (StringAtom v)])
parse ((IdentToken "T") : ts) s = (ts, s ++ [A (TrueAtom "T")])
parse ((IdentToken "NIL") : ts) s = (ts, s ++ [A (NilAtom "NIL")])
parse ((IdentToken v) : ts) s = (ts, s ++ [A (IdentAtom v)])
parse
((LParenToken c) : (RParenToken c') : ts)
s = (ts, s ++ [A (NilAtom "NIL")])
-- parse S-Expression
parse ((LParenToken c) : ts) s = parseSexpression ts s
parse _ s =
( [],
( A
( ParseErrorAtom
"parse's parse Error: Unkown Parser Condition"
)
)
: s
)
-- parse S-expression, if not one of the define statements(if, for, ...)
parseSexpression :: [Token] -> [Sexpression] -> ([Token], [Sexpression])
parseSexpression ((OperatorToken v) : ts) s = parseCallSexpression v ts s
parseSexpression ((IdentToken v) : ts) s = parseCallSexpression v ts s
parseSexpression _ s = ([], [A (ParseErrorAtom "parse's parseSexpression Error: Unkown Call Condition")])
parseCallSexpression :: String -> [Token] -> [Sexpression] -> ([Token], [Sexpression])
parseCallSexpression _ [] _ =
( [],
[ A
( ParseErrorAtom
"parse's parseCallSexpression Error: S-Expression not closed"
)
]
)
parseCallSexpression "if" t s = parseIfStatement t s
parseCallSexpression "defun" t s = parseDefunStatement t s
parseCallSexpression v ((RParenToken c) : ts) s = (ts, s ++ [CallExpression (A (IdentAtom v)) []])
parseCallSexpression v (t : ts) s =
let (t', s', g) = parseGroupSexpression (t : ts) s []
in if (not (null s')) && expectSexpression (head s') parseErrorAtom
then ([], (s'))
else (t', s' ++ [CallExpression (A (IdentAtom v)) g])
parseGroupSexpression :: [Token] -> [Sexpression] -> [Sexpression] -> ([Token], [Sexpression], [Sexpression])
parseGroupSexpression [] s g =
( [],
( ( A
( ParseErrorAtom
"parse's parseGroupSexpression Error: S-Expression not closed"
)
)
: []
),
g
)
parseGroupSexpression ((RParenToken c) : ts) s g = (ts, s, g)
parseGroupSexpression t s g =
let (t', (s' : ss')) = parse t s
in if expectSexpression s' parseErrorAtom
then ([], ([s']), [])
else parseGroupSexpression (t') (ss') (g ++ [s'])
parseIfStatement :: [Token] -> [Sexpression] -> ([Token], [Sexpression])
parseIfStatement [] s = ([], [A (ParseErrorAtom "parse's parseIfStatement Error: No Condition Body, or Else-Body Found")])
parseIfStatement t s =
let (t', s') = parse t []
in if ((not (null s')) && expectSexpression (head s') parseErrorAtom) || (length s') == 0
then ([], [A (ParseErrorAtom "parse's parseIfStatement Error: Could Not Parse Condition-Statement")])
else
let (t'', s'') = parse t' []
in if ((not (null s'')) && expectSexpression (head s'') parseErrorAtom) || (length s'') == 0
then ([], [A (ParseErrorAtom "parse's parseIfStatement Error: Could Not Parse Body")])
else case t'' of
[] -> ([], [A (ParseErrorAtom "parse's parseIfStatement Error: Did Not Close If-Statement")])
((RParenToken _) : t''s) -> (t''s, [IfExpression (head s') (head s'') (A (NilAtom "NIL"))])
othewise ->
let (t''', s''') = parse t'' []
in if ((not (null s''')) && expectSexpression (head s''') parseErrorAtom) || (length s''') == 0
then ([], [A (ParseErrorAtom "parse's parseIfStatement Error: Could Not Parse Else-Body")])
else case t''' of
((RParenToken _) : t'''s) -> (t'''s, [IfExpression (head s') (head s'') (head s''')])
otherwise -> ([], [A (ParseErrorAtom "parse's parseIfStatement Error: Did Not Close If-Statement")])
parseDefunStatement :: [Token] -> [Sexpression] -> ([Token], [Sexpression])
parseDefunStatement [] s = ([], [A (ParseErrorAtom "parse's parseDefunStatement Error: No Name, Arguments or Body Found")])
parseDefunStatement (t : ts) s = case t of
(IdentToken functionname) ->
if (not (lexExpectPeekToken ts lParenToken))
then ([], [A (ParseErrorAtom "parse's parseDefunStatement Error: Missing Argument")])
else
let (t', s', arg) = parseDefunArguments (tail ts) s []
in if (length s' > 0) && expectSexpression (head s') parseErrorAtom
then ([], s')
else
let (t'', s'') = parse t' []
in if (length s'' > 0) && expectSexpression (head s'') parseErrorAtom
then ([], [A (ParseErrorAtom "parse's parseDefunStatement Error: Error Parsing Body")])
else
if not (lexExpectPeekToken t'' rParenToken)
then ([], [A (ParseErrorAtom "parse's parseDefunStatement Error: Did not Close defun-Statement")])
else ((tail t''), [FunctionExpression (A (IdentAtom functionname)) (arg) (head s'')])
otherwise -> ([], [A (ParseErrorAtom "parse's parseDefunStatement Error: No Name Found")])
parseDefunArguments :: [Token] -> [Sexpression] -> [Sexpression] -> ([Token], [Sexpression], [Sexpression])
parseDefunArguments [] s g =
( [],
( ( A
( ParseErrorAtom
"parse's parseDefunArguments Error: Arguments not closed"
)
)
: []
),
g
)
parseDefunArguments ((RParenToken ')') : ts) s g = (ts, s, g)
parseDefunArguments ((IdentToken c) : ts) s g = parseDefunArguments ts s (g ++ [A (IdentAtom c)])
parseDefunArguments t s g =
( [],
[ ( A
( ParseErrorAtom
"parse's parseDefunArguments Error: Unexpected Value"
)
)
],
[ ( A
( ParseErrorAtom
"parse's parseDefunArguments Error: Unexpected Value"
)
)
]
)
parseWhileStatement :: [Token] -> [Sexpression] -> ([Token], [Sexpression])
parseWhileStatement t s = undefined
-- -- -- -- -- -- -- -- -- -- -- scope -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
-- NOTE(JoanMontas) I need to investigate more about haskell's memory management...
data Scope
= LocalScope {local :: (Map.Map String Sexpression), prev :: Scope}
| EmptyScope
-- make global scope, i.e empty prev
makeGlobalScope :: Scope
makeGlobalScope = LocalScope (Map.empty) EmptyScope
-- make a local scope, i.e non-empty prev
makeLocalScope :: Scope -> Scope
makeLocalScope s = LocalScope (Map.empty) s
-- insert a Sexp to the given scope
insertSexpToScope :: Scope -> String -> Sexpression -> Scope
insertSexpToScope s k se = LocalScope (Map.insert k se (local s)) (prev s)
-- insert a Sexp to the global Scope
insertSexpToGlobalScope :: Scope -> String -> Sexpression -> Scope
insertSexpToGlobalScope s k se = case (prev s) of
EmptyScope -> insertSexpToScope s k se
otherwise -> LocalScope (local s) (insertSexpToGlobalScope (prev s) k se)
-- find a Sexp via lexically scoping, how I like to call it "bubble up"
findSexp :: Scope -> String -> Sexpression
findSexp EmptyScope k = A (EvalErrorAtom "EvalErrorAtom ERROR: Variable not found in any scope")
findSexp s k = case Map.lookup k (local s) of
Just t -> t
Nothing -> findSexp (prev s) k
getPrev :: Scope -> Scope
getPrev s = prev s
-- TODO(JoanMontas) make a helper function so you can add tabs per scope and
-- have it pretty print
instance Show Scope where
show EmptyScope = "Empty\n"
show (LocalScope l p) =
"\n=======\n"
++ "Local: "
++ show l
++ "\nprev:"
++ show p
++ "========\n"