-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyntax.hs
164 lines (118 loc) · 5.22 KB
/
Syntax.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
{- Benutzt das Parser-Modul um Strings in Sprachelemente des LangDef-Moduls zu konvertieren-}
module Syntax where
import Prelude hiding (negate)
import LangDef
import Parser
parseExpr :: Parser Expression
parseExpr = parseExprLvl1
--parses one statement, or multiple connected by operators.
parseExprLvl1 = next parseExprLvl2
(maybeSome (opNexpr))
assocl
where opNexpr = next4 maybespace pTwoOp maybespace parseExprLvl2
(\_ op _ e2 e1
-> op e1 e2)
assocl = foldl (\e op -> op e)
--Parser with mapping of operators.
pTwoOp :: Parser (Expression -> Expression -> Expression)
pTwoOp = alts (map c twoOps )
where c (s, f) = convert (const f) (atoms s)
twoOps = [
("+", plus),
("-", minus),
("*", mult),
("==", equal),
("same", equal)
]
-- parse "smaller" things.
parseExprLvl2 = next (alts [pConcrete,
pRef,
pOneOpExpr,
pBraces,
pLambdaE
]) pCall
(\b c ->
c b)
where pLambdaE = (convert LambdaExpr pLambda)
pConcrete = convert Concrete parseNumber
pRef = convert Ref parseIdent
pOneOpExpr = next3 pOneOp maybespace parseExprLvl2
(\op _ expr -> op expr)
pOneOp = alts (map c oneOps)
where c (s, f) = convert (const f) (atoms s)
oneOps = [
("~" ,negate),
("X", square),
("not", notop)
]
pBraces = next5 (atom '(') maybespace parseExprLvl1 maybespace (atom ')')
(\_ _ e _ _ -> e)
--Any Expression may be a call of that expression, if followed by braces.
pCall = opt id (next4 maybespace (atom '(') pParams (atom ')')
(\_ _ params _ foo ->
Call foo params))
-- TODO: use pSep here
pParams :: Parser [Expression]
pParams = alt (convert (const []) maybespace)
(next3 maybespace (pKommaSep parseExpr) maybespace
(\_ exprs _ ->exprs))
pLambda = next7
(atoms "function") maybespace (atom '(') vars (atom ')') somespace parseStmt
(\_ _ _ vars _ _ stmt ->
(vars, stmt))
where vars = alt (convert (const []) maybespace)
(next3 maybespace (pKommaSep parseIdent) maybespace
(\_ vars _ ->vars))
parseStmt :: Parser Statement
parseStmt = alts [pAssign,
pReturn,
pObjAssign,
pOption,
pAlt,
pLoop,
pSequence,
pLet]
pAssign = next5 parseIdent maybespace (atom '=') maybespace parseExpr
(\ident _ _ _ expr ->
Assignment ident expr)
pReturn = next3 (atoms "return") somespace parseExpr
(\_ _ e ->
Return e)
pObjAssign = next7
parseExpr (atom '.') pKey maybespace (atom '=') (maybespace) parseExpr
(\obj _ key _ _ _ val ->
ObjAssignment obj key val )
pOption = next7
(atoms "if") somespace parseExpr somespace (atoms "then") somespace parseStmt
(\_ _ e _ _ _ stmt ->
Option e stmt)
pAlt = next5 pOption somespace (atoms "else") somespace parseStmt
(\(Option e stmtif)
_ _ _ stmtelse ->
Alternative e stmtif stmtelse)
pLoop = next7
(atoms "while") somespace parseExpr somespace (atoms "do") somespace parseStmt
(\_ _ cond _ _ _ stmt ->
Loop cond stmt)
pSequence = convert Sequence (pBlock parseStmt)
pLet = next3 (atoms "let") somespace (pBlock pLetSingle)
(\_ _ lets -> Let lets)
pLetSingle = next5 parseIdent maybespace (atom '=') maybespace pLambda
(\id _ _ _ lambda -> (id, lambda))
keywords = map fst oneOps ++ map fst twoOps ++ [
"while", "do", "return", "if", "else", "let", "function" ]
--other parsers
parseIdent = pFilter noKeyword (some (alts chars))
where noKeyword s = not (elem s keywords)
chars = map atom (['A'..'Z']++['a'..'z'])
pKey = parseIdent
{- parses a curly braces block. Separated using ";"
--->( { )---[maybespace]-->[something]--->[maybespace]---->( } )------>
| |
\-----------( ; )<----------------------------/
-}
pBlock :: Parser a -> Parser [a]
pBlock pa = next3 (atom '{') elems (atom '}')
(\_ elems _ -> elems)
where elems = pSep (next3 maybespace pa maybespace (\_ elem _ -> elem))
(atom ';')