-
Notifications
You must be signed in to change notification settings - Fork 0
/
rockstar.peg
396 lines (303 loc) · 12.2 KB
/
rockstar.peg
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
PEG grammar for Rockstar (https://codewithrockstar.com)
Created using PEG.js https://pegjs.org
After updating, run pegjs -o rockstar-parser.js rockstar.peg
*/
{
/* initialiser code - this is JS that runs before the parser is generated */
const keywords = [
'mysterious',
'stronger','continue',
'between','greater','nothing','nowhere','smaller','whisper','without',
'ain\'t','around','bigger','listen','nobody','return','scream','taking','weaker','higher', 'strong',
'break','build','empty','false','great','knock','lower','right','round','shout',
'small','take','takes','times','until','unite','while','wrong','minus',
'aint','back','cast','burn','join','down','else','give','gone','high','into','less','lies','null','plus','says','than','them','they','true','weak','were','your','over','with',
'and','big','her','him','hir','it ','low','nor','not','put','say','she','the','top','ver','was','xem','yes','zie','zir',
'an','as','at','he','if','is','it','my','no','of','ok','or','to','up','ve', 'xe','ze',
'a'
]
function isKeyword(string) {
return keywords.includes(string.toLowerCase());
}
}
program = p:line * { return { list: p.filter(item => item) } }
line = _* s:statement (EOL+ / EOF) { return s }
/
EOL { return null }
whitespace = [ \t]
comment = '(' [^)]* ')'
_ = (whitespace / comment)+
noise = (_ / [;,?!&.])
EOL = noise* '\n'
EOF = !.
ignore_rest_of_line = (_[^\n]*)?
statement = _* s:(break / continue / function / function_call
/ function_return / loop / conditional / operation / expression) { return s }
break = 'break'i ignore_rest_of_line {
return { 'break' : {} }
}
continue = ('continue'i ignore_rest_of_line / 'take it to the top'i) {
return { 'continue' : {} }
}
function = name:variable _ 'takes'i _ args:variable_list EOL body:block EOL
{ return { 'function': {
name: name,
args: args.map(arg => arg),
body: body
} } }
expression_list_separator = (_? ', and' _ / _?('&' / ',' / "'n'")_?)
variable_list_separator = expression_list_separator / _ 'and' _
variable_list = head:variable variable_list_separator tail:variable_list
{ return [head].concat(tail) }
/ arg:variable { return [arg] }
function_call = name:variable _ 'taking'i _ args:expression_list
{ return { 'call': { name: name, args: Array.isArray(args) ? args : [args] } } }
expression_list = head:simple_expression expression_list_separator tail:expression_list
{ return [head].concat(tail) }
/ arg:simple_expression { return arg }
return = 'return'i / 'give back'i
function_return = return _ e:expression
{ return { 'return': { 'expression' : e } } }
operation = readline / output / crement / mutation / assignment / rounding
readline
= 'listen to'i _ target:assignable
{ return { assign: { expression: { listen : ''}, target: target } } }
/ 'listen'i
{ return { 'listen' : ''} }
continuation = EOL _* s:statement { return s };
block = head:statement tail:(continuation)+
{
return { list : [head].concat(tail) }
}
/ s:statement { return s }
consequent = _ s:statement { return s }
/ EOL s:block { return s }
alternate = _ 'else'i _ a:statement { return a }
/ EOL+ 'else'i _ a:statement { return a }
/ EOL+ 'else'i EOL a:block { return a }
/ EOL { return null }
conditional = 'if'i _ e:expression c:consequent? a:alternate? {
return {
'conditional': {
'condition' : e,
'consequent' : c,
'alternate' : a
}
};
}
loopable = _ s:statement { return s }
/ EOL s:block EOL { return s }
loop = 'while'i _ e:expression c:loopable
{ return { 'while_loop': {
'condition': e,
'consequent': c
} }; }
/
'until'i _ e:expression c:loopable
{ return { 'until_loop': {
'condition': e,
'consequent': c
} }; }
output = ('say'i/'shout'i/'whisper'i/'scream'i) _ e:expression
{return {'output': e}}
simple_expression = function_call / constant / lookup / literal / pronoun
literal = constant / number / string
constant = null / true / false / mysterious
true = ('true'i / 'ok'i / 'right'i / 'yes'i) !letter { return { constant: true } }
false = ('false'i / 'lies'i / 'wrong'i / 'no'i) !letter { return { constant: false } }
null = ('null'i / 'nothing'i / 'nowhere'i / 'nobody'i / 'empty'i / 'gone'i) { return { constant: null } }
mysterious = 'mysterious' { return '__MYSTERIOUS__' }
number = n:$('-'?[0-9]+ ('.' [0-9]+)?) '.'?
{ return {number: parseFloat(n)} }
/
n:$('.' [0-9]+)
{ return {number: parseFloat(n) } }
string = '"' s:$[^"]* '"'
{ return {string: s}}
expression = boolean
boolean = nor
nor = lhs:or _ 'nor' _ rhs:nor {
return { 'binary' : { op: 'nor', lhs: lhs, rhs: rhs } } }
/ or
or = lhs:and _ 'or' _ rhs:or {
return { 'binary': {
op: 'or',
lhs: lhs,
rhs: rhs
} }
}
/and
and = lhs:equality_check _ 'and' _ rhs:and {
return { 'binary': {
op: 'and',
lhs: lhs,
rhs: rhs
} }
}
/
equality_check
eq = ('aint'i / 'ain\'t'i) { return 'ne' }
/ 'is'i { return 'eq' }
equality_check = lhs:not _ c:eq _ rhs:equality_check
{
return {
comparison: {
comparator: c,
lhs: lhs,
rhs: rhs
}
};
}
/
not
not = 'not' _ e:not { return { 'not': { expression: e} } }
/ comparison
comparison = lhs:arithmetic _ c:comparator _ rhs:comparison
{
return {
comparison: {
comparator: c,
lhs: lhs,
rhs: rhs
}
};
}
/
arithmetic
greater = ('higher'i /'greater'i / 'bigger'i / 'stronger'i)
smaller = ('lower'i /'less'i / 'smaller'i / 'weaker'i)
great = ('high'i / 'great'i / 'big'i / 'strong'i)
small = ('low'i / 'small'i / 'weak'i)
comparator = 'is'i _ greater _ 'than'i { return 'gt' }
/ 'is'i _ smaller _ 'than'i { return 'lt' }
/ 'is'i _ 'as'i _ great _ 'as'i { return 'ge' }
/ 'is'i _ 'as'i _ small _ 'as'i { return 'le' }
arithmetic = first:product rest:((add / subtract) product)+
{ return rest.reduce(function(memo, curr) {
return { binary: { op: curr[0], lhs: memo, rhs: curr[1]} };
}, first); }
/ product
product = first:simple_expression rest:((multiply / divide) expression_list)+
{ return rest.reduce(function(memo, curr) {
return { binary: { op: curr[0], lhs: memo, rhs: curr[1]} };
}, first); }
/ expression_list
/ simple_expression
// Note that operator aliases explicitly include a trailing space,
// otherwise 'with' is a prefix code for 'without' and confuses the parser.
add = _* ('+' / 'plus ' / 'with ') _* { return '+' }
subtract = _* ('-' / 'minus ' / 'without ') _* { return '-' }
multiply = _* ('*' / 'times ' / 'of ') _* { return '*' }
divide = _* ('/' / 'over ' / 'between ') _* { return '/' }
compoundable_operator = add / subtract / multiply / divide
pronoun = pronoun:(
// Longest pronouns first to avoid prefix coding errors.
// 4-letter pronouns
'they'i / 'them'i
// 3-letter pronouns
/ 'she'i / 'him'i / 'her'i / 'hir'i / 'zie'i / 'zir'i / 'xem'i / 'ver'i
// 2-letter pronouns
/ 'ze'i / 've'i / 'xe'i / 'it'i / 'he'i
)
{ return { pronoun: pronoun.toLowerCase() } }
lookup
= v:variable _ 'at'i _ i:expression
{ return { lookup: { variable: v, index: i } }; }
/ v:variable
{ return { lookup: { variable: v } }; }
common_prefix = ( 'an'i / 'a'i / 'the'i / 'my'i / 'your'i)
uppercase_letter = [A-ZÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞĀĂĄĆĈĊČĎĐĒĔĖĘĚĜĞĠĢĤĦĨĪĬĮİIJĴĶĸĹĻĽĿŁŃŅŇŊŌŎŐŒŔŖŘŚŜŞŠŢŤŦŨŪŬŮŰŲŴŶŸŹŻŽ]
lowercase_letter = [a-zàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþāăąćĉċčďđēĕėęěĝğġģĥħĩīĭįıijĵķĸĺļľŀłńņňŋōŏőœŕŗřśŝşšţťŧũūŭůűųŵŷÿźżžʼnß]
letter = uppercase_letter / lowercase_letter
common_variable = prefix:common_prefix _+ name:$(letter+)
{ return (prefix + '_' + name).toLowerCase() };
is = ('\'s' / (_* ('=' / 'is 'i / 'was 'i / 'are 'i / 'were 'i)))
target = _ 'into'i _ t:assignable { return t }
indexer = _ 'at'i _ i:expression { return i };
assignable
= v:variable i:indexer?
{ return { variable: v, index: i }; }
assignment = target:assignable is _* e:(literal / poetic_number)
{ return { assign: { target: target, expression: e} }; }
/ target:assignable _+ 'says 'i e:poetic_string
{ return { assign: { target: target, expression: e} }; }
/ 'put'i _+ e:expression target:target
{ return { assign: { target: target, expression: e} }; }
/ 'let'i _+ target:assignable _+ 'be'i o:compoundable_operator e:expression {
return { assign: {
target: target,
expression: { binary: { op: o, lhs: { lookup: target }, rhs: e } }
} };
}
/ 'let'i _+ target:assignable _+ 'be'i _+ e:expression
{ return { assign: { target: target, expression: e} }; }
poetic_string = s:$[^\n]*
{ return { string: s} }
poetic_number = poetic_digit_separator* n:poetic_digits poetic_digit_separator* d:poetic_decimal? poetic_digit_separator*
{ return { number: parseFloat(d?n+'.'+d:n)}}
poetic_decimal = '.' poetic_decimal_digit_separator* d:poetic_decimal_digits poetic_decimal_digit_separator* {return d}
/
'.' poetic_decimal_digit_separator*
poetic_digit_separator = ( _ / [0-9\',;:?!+_/] )
poetic_digits =
poetic_digit_separator* head:poetic_digit poetic_digit_separator+ tail:poetic_digits
{ return head + tail }
/
d: poetic_digit
{ return d }
poetic_decimal_digit_separator = ( _ / poetic_digit_separator / '.')
poetic_decimal_digits =
poetic_decimal_digit_separator* head:poetic_digit poetic_decimal_digit_separator+ tail:poetic_decimal_digits
{ return head + tail }
/
d: poetic_digit
{ return d }
poetic_digit = t:[A-Za-z\-']+
{ return (t.filter(c => /[A-Za-z\-]/.test(c)).length%10).toString()}
variable = common_variable / proper_variable / pronoun / simple_variable
simple_variable = name:$(letter letter*) !{ return isKeyword(name) } { return name }
proper_noun = noun:$(uppercase_letter letter*) !{ return isKeyword(noun) } { return noun }
proper_variable = head:$(proper_noun (' ' $proper_noun)*)
{ return head.replace(/ /g, '_').toLowerCase() }
crement = increment / decrement
increment = 'build'i _ v:variable _ t:('up'i noise*)+
{ return {
increment: {
variable: v,
multiple: t.length
}
}; }
decrement = 'knock'i _ v:variable _ t:('down'i noise*)+
{ return {
decrement: {
variable: v,
multiple: t.length
}
}; }
split = ('cut'i / 'split'i / 'shatter'i) { return 'split' }
cast = ('cast'i / 'burn'i) { return 'cast' }
join = ('join'i / 'unite'i) { return 'join' }
mutator = split / cast / join
modifier = _ ('with'i / 'using'i) _ m:expression { return m }
mutation
= op:mutator _ s:expression t:target m:modifier?
{ return { assign: { target: t, expression: { mutation: { type: op, source: s, modifier: m } } } } ; }
/ op:mutator _ s:assignable m:modifier?
{ return { assign: { target: s, expression: { mutation: { type: op, source: { lookup: s }, modifier: m } } } } ; }
rounding = floor / ceil / math_round
floor
= 'turn'i _ v:variable _ 'down'i
{ return { rounding: { variable: v, direction: 'down' } }; }
/ 'turn'i _ 'down'i _ v:variable
{ return { rounding: { variable: v, direction: 'down' } }; }
ceil
= 'turn'i _ v:variable _ 'up'i
{ return { rounding: { variable: v, direction: 'up' } }; }
/ 'turn'i _ 'up'i _ v:variable
{ return { rounding: { variable: v, direction: 'up' } }; }
math_round
= 'turn'i _ v:variable _ ('round'i/'around'i)
{ return { rounding: { variable: v, direction: 'nearest' } }; }
/ 'turn'i _ ('round'i/'around'i) _ v:variable
{ return { rounding: { variable: v, direction: 'nearest' } }; }