-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.rkt
373 lines (313 loc) · 8.89 KB
/
grammar.rkt
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#lang racket
(require redex)
; Core language grammar definition
(define-language core-lang
;
;
;
;
;
;
; ;;; ;;;; ;;;; ;;;;
; ; ; ;; ;; ;; ; ;; ;;
; ; ; ; ; ; ;
; ; ; ; ; ;;;;;;
; ; ; ; ; ;
; ; ; ;; ;; ; ;; ;
; ;;; ;;;; ; ;;;;
;
;
;
;
[score \;
break
(return e ...)
; stat funcall
($statFunCall prefixexp (e ...))
($statFunCall prefixexp : Name (e ...))
(var_1 var_2 ... = e_1 e_2 ...)
(do s end)
(if e then s else s end)
(while e do s end)
(local Name_1 Name_2 ... = e_1 e_2 ... in s end)
]
; Lua's block of code: it helps to avoid an ambiguity in the grammar, between
; funcalls and concat. of stats
[s score
(score_1 score_2 score_3 ...)]
[v nil Boolean Number String]
; Difference between statements and expressions is present also at a semantics
; level: eg., tuples' semantics is defined taking into account if they appear
; as an expression or as a statement, and the same with funcall
[e v
<<<
var
; To allow function calls in protected mode, in place of expressions.
functioncall
($builtIn builtinserv (e ...))
(\( e \))
tableconstructor
(e binop e)
(unop e)
functiondef
]
; Built-in services' names, for use by the random generator of terms
[builtinserv assert
collectgarbage
error
pcall
print
rawequal
select
tonumber
type
xpcall
setmetatable
rawset
ipairs
next
pairs
load
loadfile
getmetatable
tostring
rawget
rawlen
require
; math
math.abs
math.acos
math.asin
math.atan
math.ceil
math.cos
math.cosh
math.deg
math.exp
math.floor
math.fmod
math.log
math.max
math.modf
math.rad
math.sin
math.sinh
math.sqrt
math.tan
math.tanh
; string
string.len
string.rep
string.reverse
string.sub
; table
table.pack
; string
string.dump
; table
table.concat
table.unpack]
[Boolean true false]
[vlist (v ...)]
; Variables' Identifiers' syntactic category, to ease the definition of the
; substitution function.
[id Name
<<<]
[parameters (Name ...)
(Name ... <<<)]
; This syntactic category is added to ease meta-functions' definitions.
[functiondef (function Name parameters s end)]
[prefixexp var
functioncall
(\( e \))]
[functioncall (prefixexp (e ...))
(prefixexp : Name (e ...))]
[field (\[ e \] = e)
; We need to allow fields like this
e]
[tableconstructor (\{ field ... \})]
[arithop + - * / ^ %]
[relop < <= > >=]
; Not short-circuit binop
[strictbinop arithop relop == ..]
[binop strictbinop and or]
[unop - not \#]
; Name can be anything except a keyword of the language
[Name variable-not-otherwise-mentioned]
[var Name
(e \[ e \])]
; Number represents real (double-precision floating-point) numbers
[Number real]
[String string]
; terms: to specify meta-functions and relations that work on both, s and e
[aterm e s]
[Ce hole
; Function call, method call, built-in services
(Ce (e ...))
(e (e ... Ce e ...))
($builtIn builtinserv (e ... Ce e ...))
(Ce : Name (e ...))
(e : Name (e ... Ce e ...))
; Expressions
(\( Ce \))
(Ce binop e)
(e binop Ce)
(unop Ce)
(\{ field ... (\[ Ce \] = e) field ... \})
(\{ field ... (\[ e \] = Ce) field ... \})
(\{ field ... Ce field ... \})
(Ce \[ e \])
(e \[ Ce \])
; function def.
(function Name_1 (Name_2 ...) C end)
(function Name_1 (Name_2 ... <<<) C end)]
[Ccore hole
Ce
; Statements
(do C end)
(if Ce then s else s end)
(if e then C else s end)
(if e then s else C end)
(local Name ... = e ... Ce e ... in s end)
(local Name ... = e ... in C end)
(e ... Ce e ... = e ...)
(e ... = e ... Ce e ...)
(return e ... Ce e ...)
(while Ce do s end)
(while e do C end)
; Function call, method call, built-in services
($statFunCall Ce (e ...))
($statFunCall e (e ... Ce e ...))
($statFunCall Ce : Name (e ...))
($statFunCall e : Name (e ... Ce e ...))]
[C hole
Ccore
(Ccore score_1 score_2 ...)
(score_1 ... score_2 Ccore score_3 ...)]
; To express the concepto of "absence of while loops" in the context of a
; given point
[Cenw hole
; Function call, method call, built-in services
(Cenw (e ...))
(e (e ... Cenw e ...))
($builtIn builtinserv (e ... Cenw e ...))
(Cenw : Name (e ...))
(e : Name (e ... Cenw e ...))
; Expressions
(\( Cenw \))
(Cenw binop e)
(e binop Cenw)
(unop Cenw)
(\{ field ... (\[ Cenw \] = e) field ... \})
(\{ field ... (\[ e \] = Cenw) field ... \})
(\{ field ... Cenw field ... \})
(Cenw \[ e \])
(e \[ Cenw \])
; function def.
(function Name_1 (Name_2 ...) Cnw end)
(function Name_1 (Name_2 ... <<<) Cnw end)]
[Ccorenw hole
Ce
; Statements
(do Cnw end)
(if Cenw then s else s end)
(if e then Cnw else s end)
(if e then s else Cnw end)
(local Name ... = e ... Cenw e ... in s end)
(local Name ... = e ... in Cnw end)
(e ... Cenw e ... = e ...)
(e ... = e ... Cenw e ...)
(return e ... Cenw e ...)
; Function call, method call, built-in services
($statFunCall Cenw (e ...))
($statFunCall e (e ... Cenw e ...))
($statFunCall Cenwe : Name (e ...))
($statFunCall e : Name (e ... CCenw e ...))]
[Cnw hole
Ccorenw
(Ccorenw score_1 score_2 ...)
(score_1 ... score_2 Ccorenw score_3 ...)]
)
(provide core-lang)
;
;
;
; ;
; ; ;
; ; ;
; ; ; ; ; ;; ;;
; ; ; ; ; ; ;
; ; ; ; ; ; ;
; ; ; ; ; ;
; ;;;;; ; ; ;
; ; ; ; ; ; ;
; ; ; ;; ;; ; ;
; ; ; ;;;; ; ;; ;;
;
;
;
;
(define is_s?
(redex-match? core-lang
s))
(define is_e?
(redex-match? core-lang
e))
(define (is_term? t)
(or (is_s? t)
(is_e? t)))
; values
(define is_v?
(redex-match? core-lang
v))
(define is_number?
(redex-match? core-lang
Number))
(define is_string?
(redex-match? core-lang
String))
(define is_nil?
(redex-match? core-lang
nil))
(define is_false?
(redex-match? core-lang
false))
(define is_true?
(redex-match? core-lang
true))
(define is_fdef?
(redex-match? core-lang
functiondef))
; operators
(define is_strconcat?
(redex-match? core-lang
..))
(define is_arithop?
(redex-match? core-lang
arithop))
; exps
(define is_r?
(redex-match? core-lang
r))
; statements
(define is_skip?
(redex-match? core-lang
\;))
(define is_break?
(redex-match? core-lang
break))
(provide is_s? is_e? is_term?
;values
is_v?
is_number?
is_string?
is_fdef?
is_nil?
is_false?
is_true?
; ops
is_strconcat?
is_arithop?
; stats
is_skip?
is_break?)