-
Notifications
You must be signed in to change notification settings - Fork 0
/
Oodle.y
291 lines (251 loc) · 9.68 KB
/
Oodle.y
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
-- Filename: oodle.y
-- Contents: The directies for the Happy compiler-compiler.
-- Notes: This code probably does not work for Phase 1, as it is not needed.
{
module Oodle.Parser (parser) where
import Oodle.Token
import Oodle.ParseTree
import Data.List.Split (splitOn)
import Oodle.Error
}
%name parser
%tokentype { Token }
%error { parseError }
%monad { Error }
%left or
%left and
%nonassoc '=' '>' '>='
%left '&'
%left '+' '-'
%left '*' '/'
%right UNARY
%right '.' new
%attributetype { Attrs a }
%attribute value { a }
%attribute tk { Token }
%token
-- Newline
newline { Token TokenNewline _ }
-- Literals
intLit { Token (TokenIntLiteral _) _ }
strLit { Token (TokenStringLiteral _) _ }
id { Token (TokenIdentifier _) _ }
-- Keywords
boolean { Token TokenBoolean _ }
begin { Token TokenBegin _ }
class { Token TokenClass _ }
else { Token TokenElse _ }
end { Token TokenEnd _ }
false { Token TokenFalse _ }
from { Token TokenFrom _ }
if { Token TokenIf _ }
inherits { Token TokenInherits _ }
int { Token TokenInt _ }
is { Token TokenIs _ }
loop { Token TokenLoop _ }
me { Token TokenMe _ }
new { Token TokenNew _ }
not { Token TokenNot _ }
null { Token TokenNull _ }
string { Token TokenString _ }
then { Token TokenThen _ }
true { Token TokenTrue _ }
while { Token TokenWhile _ }
and { Token TokenAnd _ }
or { Token TokenOr _ }
-- Operators
'&' { Token TokenStringConcat _ }
'+' { Token TokenPlus _ }
'-' { Token TokenMinus _ }
'*' { Token TokenTimes _ }
'/' { Token TokenDiv _ }
'>' { Token TokenGT _ }
'>=' { Token TokenGTEq _ }
'=' { Token TokenEq _ }
-- Miscellaneous
':=' { Token TokenAssign _ }
'(' { Token TokenOP _ }
')' { Token TokenCP _ }
'[' { Token TokenOB _ }
']' { Token TokenCB _ }
',' { Token TokenComma _ }
';' { Token TokenSemicolon _ }
':' { Token TokenColon _ }
'.' { Token TokenPeriod _ }
%%
Start : nullable_cr ClassList { $$ = Start $2 }
nullable_cr : {- empty -} { }
| cr { }
-- Classes
ClassList : Class nullable_cr { $$ = [$1] }
| Class cr ClassList { $$ = $1 : $3 }
Class
: class Id InheritsExpr is cr
VarList
MethodList
Id {
$$ = Class $1 $2 $3 (reverse $6) $7;
where if $2 == $8 then return $2
else failWithToken $1
"Class closing name not equal to beginning name."
}
Id : id {
$$ = (Id (getTokenStr $1)); $$.tk = $1
}
InheritsExpr
: inherits from Id { $$ = $3 }
| {- empty -} { $$ = Id "" }
-- Methods
MethodList
: Method MethodList { $$ = $1 : $2 }
| end { $$ = [] }
Method : Id '(' ArgumentList ')' TypeExpression is cr
VarList
begin cr
StatementList
end Id cr {
$$ = Method $1.tk $1 $5 $3 (reverse $8) $11
; where if $1 == $13 then return $1
else failWithToken $2
"Method closing name not equal to beginning name."
}
-- Variables
VarList
: VarList VarDecl { $$ = ($2 : $1) }
| {- empty -} { $$ = [] }
VarDecl : Id TypeExpression NullableInit cr
{ $$ = Var $1.tk $1 $2 $3}
TypeExpression
: ':' Type { $$ = $2 }
| {- empty -} { $$ = typeNull }
NullableInit :
InitExpression { $$ = $1 }
| {- empty -} { $$ = ExpressionNoop }
InitExpression
: ':=' Expression { $$ = $2 }
-- Arguments
ArgumentList
: ArgumentList ';' Argument { $$ = $1 ++ [$3] }
| Argument { $$ = [$1] }
| {- empty -} { $$ = [] }
Argument : Id ':' Type { $$ = Argument $1.tk $1 $3 }
-- Statements
StatementList
: {- empty -} { $$ = [] }
| Statement cr StatementList { $$ = $1 : $3 }
Statement
: AssignStatement { $$ = $1 }
| IfStatement { $$ = $1 }
| LoopStatement { $$ = $1 }
| CallStatement { $$ = $1 }
-- Assign
AssignStatement
: Id ArrayIndexList InitExpression{
$$ = if $2 == [] then
AssignStatement $1.tk $1 $3
else
AssignStatement $1.tk (IdArray (getIdString $1) $2) $3
}
-- If
IfStatement
: if Expression then cr
StatementList
ElseClause
end if {
$$ = IfStatement $1 $2 $5 $6
; $2.tk = $1
}
ElseClause
: else cr
StatementList { $$ = $3 }
| {- empty -} { $$ = [] }
-- Loop
LoopStatement
: loop while Expression cr
StatementList
end loop { $$ = LoopStatement $1 $3 $5 }
-- Call
CallStatement
: CallScope '(' CallExprList ')' {
$$ = CallStatement $1.tk (fst $1) (snd $1) $3
}
CallScope
: Expression '.' Id { $$ = ($1, $3); $$.tk = $2 }
| Id {
$$ = (ExpressionNoop, $1)
; $$.tk = $1.tk
}
CallExprList
: ExpressionList { $$ = $1; $1.tk = $$.tk }
| {- empty -} { $$ = [] }
-- Array Indexing
ArrayIndexList
: {- empty -} { $$ = [] }
| ArrayIndex ArrayIndexList { $$ = $1 : $2 }
ArrayIndex
: '[' Expression ']' { $$ = $2 }
ArrayIndexOptExpr
: '[' Expression ']' { $$ = $2 }
| '[' ']' { $$ = ExpressionNoop }
-- Type
Type : int { $$ = TypeInt }
| string { $$ = typeString }
| boolean { $$ = TypeBoolean }
| Id { $$ = TypeId $1 }
| Type ArrayIndexOptExpr { $$ = TypeExp $1 $2 }
-- Expression
Expression
: Id ArrayIndexList {
$$ = if null $2 then ExpressionId $1.tk $1
else ExpressionIdArray $1.tk (IdArray (getIdString $1) $2)
}
| strLit {
$$ = ExpressionStr $1 (getTokenStr $1)
}
| intLit {
$$ = ExpressionInt $1 (getInt (getToken $1))
}
| true { $$ = ExpressionTrue $1 }
| false { $$ = ExpressionFalse $1 }
| null { $$ = ExpressionNull $1 }
| me { $$ = ExpressionMe $1 }
| new Type { $$ = ExpressionNew $1 $2 }
| '(' Expression ')' { $$ = $2 }
| CallScope '(' CallExprList ')' {
$$ = ExpressionCall $2 (fst $1) (snd $1) $3
}
-- Unary Operators
| not Expression %prec UNARY { $$ = ExpressionNot $1 $2 }
| '-' Expression %prec UNARY { $$ = ExpressionNeg $1 $2 }
| '+' Expression %prec UNARY { $$ = ExpressionPos $1 $2 }
-- Binary Operators
| Expression '*' Expression { $$ = ExpressionMul $2 $1 $3 }
| Expression '/' Expression { $$ = ExpressionDiv $2 $1 $3 }
| Expression '+' Expression { $$ = ExpressionAdd $2 $1 $3 }
| Expression '-' Expression { $$ = ExpressionSub $2 $1 $3 }
| Expression '&' Expression {
$$ = ExpressionStrCat $2 $1 $3
}
| Expression '=' Expression { $$ = ExpressionEq $2 $1 $3 }
| Expression '>' Expression { $$ = ExpressionGt $2 $1 $3 }
| Expression '>=' Expression {
$$ = ExpressionGtEq $2 $1 $3
}
| Expression and Expression { $$ = ExpressionAnd $2 $1 $3 }
| Expression or Expression { $$ = ExpressionOr $2 $1 $3 }
ExpressionList
: Expression { $$ = [$1] }
| Expression ',' ExpressionList { $$ = $1 : $3 }
-- one or more newlines
cr : newline { }
| cr newline { }
{
failWithToken t err =
fail $ (concatMap (\s -> s ++ ":") . init $ (splitOn ":" (printToken t)))
++ " " ++ err
parseError tokenStream = fail $ "Parse error at: " ++ (
if (length tokenStream) > 0
then (printToken (head tokenStream))
else "EOF"
)
}