-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathoperators.py
501 lines (481 loc) · 19.4 KB
/
operators.py
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
import tokens
class Command(tokens.Token):
def __init__(self, token, function, argtypes):
super().__init__(token)
self.function = function
self.argtypes = argtypes
def __str__(self):
return self._text
def __repr__(self):
return f"Command({self._text},{self.function},{self.argtypes})"
class Operator(tokens.Token):
def __init__(self, token, function, arity, precedence, associativity,
default=None, flags=0):
super().__init__(token)
self.function = function
self.arity = arity
self.precedence = precedence
self.associativity = associativity
self.flags = flags
self.assign = False # Turns + into +: for instance
self.map = False # Turns ! into !* for instance
self.fold = False # Turns + into $+ for instance
self.scan = False # Turns + into \$+ for instance
# The default argument represents the value when folding an empty
# iterable with this operator. Defaults are specified as Python
# objects here and converted to Pip types later.
if (isinstance(default, (int, str))
or default == []
or default is None):
self.default = default
else:
raise ValueError("Unsupported default value for operator "
f"{token} ({function}): {default!r}")
def __str__(self):
opString = self._text
if self.fold:
opString = "$" + opString
elif self.scan:
opString = r"\$" + opString
if self.map:
if self._text + "*" in operators:
# Add a space to mapped versions of operators like @
# to prevent ambiguity with @* operator
opString += " *"
else:
opString += "*"
if self.assign:
opString += ":"
return opString
def __repr__(self):
return (f"Operator({self},{self.function},{self.arity},"
f"{self.precedence},{self.associativity})")
def copy(self):
cpy = Operator(self._text,
self.function,
self.arity,
self.precedence,
self.associativity,
self.default,
self.flags)
cpy.assign = self.assign
cpy.fold = self.fold
cpy.scan = self.scan
cpy.map = self.map
return cpy
# Each entry in the command table contains the command symbol, the function,
# and a list of parsing items that the command expects:
# NAME - a single variable name
# LOOPVAR - a single name, or multiple names in square braces
# EXPR - any expression
# CODE - a single statement, or a block of statements in curly braces
# ELSE - the E token followed by CODE
# WITH - the W token followed by EXPR
cmdTable = [
("F", "FOR", ["LOOPVAR", "EXPR", "CODE"]),
("I", "IF", ["EXPR", "CODE", "ELSE"]),
("L", "LOOP", ["EXPR", "CODE"]),
("LR", "LOOPREGEX", ["EXPR", "EXPR", "CODE"]),
("T", "TILL", ["EXPR", "CODE"]),
("W", "WHILE", ["EXPR", "CODE"]),
("WG", "WIPEGLOBALS", []),
]
commands = {cmdSpecs[0]:Command(*cmdSpecs) for cmdSpecs in cmdTable}
# The precedence table contains a list of precedence levels from lowest to
# highest. The first two entries in each level are the arity and associativity
# of all operators at that level. "C" associativity means "Chaining," used for
# comparison operators (e.g. 1<x<5). Each tuple that follows contains the
# operator symbol, the function, the default value if used to fold an empty
# iterable, and optionally some flags.
# Flags for operator properties:
VALS = 0x01 # Evaluate all arguments (to lvals or rvals)
RVALS = 0x02 # Evaluate all arguments to rvals
LIST_EACH = 0x04 # Perform operation item by item on Lists
RANGE_EACH = 0x08 # Convert Range arguments to equivalent List of ints;
# implies LIST_EACH
IN_LAMBDA = 0x10 # Can be used to build lambda expressions from _
precedenceTable = [
[1, None,
("O", "OUTPUT", None, RVALS),
("P", "PRINT", None, RVALS),
("Y", "YANK", None, RVALS),
("YO", "YANKOUTPUT", None, RVALS),
("YP", "YANKPRINT", None, RVALS),
],
[2, "R",
(":", "ASSIGN", None, VALS),
("::", "SWAP", None, VALS),
],
[3, "R",
("?", "IFTE", None, ),
],
[2, "L",
("|", "OR", 0, ),
],
[2, "L",
("&", "AND", 1, ),
],
[1, None,
("!", "NOT", None, RVALS),
],
[2, "L",
("==", "OBJEQUAL", 1, RVALS), # NB: *not* a chaining operator!
],
[1, None,
("V", "EVAL", None, RVALS),
("FI", "FILTER", None, RVALS),
("FN", "FILTERNOT", None, RVALS),
("FU", "FILTERUNPACK", None, RVALS),
("FX", "FILTERINDEXES", None, RVALS),
("FJ", "FILTERJOIN", None, RVALS),
("RE", "RECURSE", None, RVALS),
],
[2, "R",
("M", "MAP", [], RVALS),
("MC", "MAPCOORDS", [], RVALS),
("ME", "MAPENUMERATE", [], RVALS),
("MJ", "MAPJOIN", "", RVALS),
("MM", "MAPMAP", [], RVALS),
("MP", "MAPPAIRS", [], RVALS),
("MS", "MAPSUM", 0, RVALS),
("MU", "MAPUNPACK", [], RVALS),
("MF", "MAPFLATTEN", [], RVALS),
("FI", "FILTER", [], RVALS),
("FN", "FILTERNOT", [], RVALS),
("FE", "FILTERENUMERATE", [], RVALS),
("FU", "FILTERUNPACK", [], RVALS),
("FX", "FILTERINDEXES", [], RVALS),
("FJ", "FILTERJOIN", [], RVALS),
("SK", "SORTKEYED", [], RVALS),
("DK", "DESCENDINGKEYED", [], RVALS),
("V", "EVAL", None, RVALS),
],
[3, "R",
("MR", "MAPREGEX", [], RVALS),
("MZ", "MAPZIP", [], RVALS),
],
[3, "R",
(r"\?", "RVALIFTE", None, RVALS | IN_LAMBDA),
],
[2, "L",
(r"\|", "RVALOR", 0, RVALS | IN_LAMBDA),
],
[2, "L",
(r"\&", "RVALAND", 1, RVALS | IN_LAMBDA),
],
[1, None,
(r"\!", "NOT", None, RVALS | IN_LAMBDA),
],
[2, "C",
("<", "NUMLESS", 1, RVALS | IN_LAMBDA),
(">", "NUMGREATER", 1, RVALS | IN_LAMBDA),
("=", "NUMEQUAL", 1, RVALS | IN_LAMBDA),
("<=", "NUMLESSEQ", 1, RVALS | IN_LAMBDA),
(">=", "NUMGREATEREQ", 1, RVALS | IN_LAMBDA),
("!=", "NUMNOTEQUAL", 1, RVALS | IN_LAMBDA),
("LT", "STRLESS", 1, RVALS | IN_LAMBDA),
("GT", "STRGREATER", 1, RVALS | IN_LAMBDA),
("Q", "STREQUAL", 1, RVALS | IN_LAMBDA),
("LE", "STRLESSEQ", 1, RVALS | IN_LAMBDA),
("GE", "STRGREATEREQ", 1, RVALS | IN_LAMBDA),
("NE", "STRNOTEQUAL", 1, RVALS | IN_LAMBDA),
("#=", "LENEQUAL", 1, RVALS | IN_LAMBDA),
("#<", "LENLESS", 1, RVALS | IN_LAMBDA),
("#>", "LENGREATER", 1, RVALS | IN_LAMBDA),
("~=", "FULLMATCH", 1, RVALS | IN_LAMBDA),
],
# Note: comparison operators can be used in lambdas because the
# CHAIN pseudo-operator has the IN_LAMBDA flag (see below). The
# presence of the flag in this list has no actual effect.
[2, "L",
("N", "IN", None, RVALS | IN_LAMBDA),
("NI", "NOTIN", None, RVALS | IN_LAMBDA),
],
[1, None,
("RP", "REPR", None, RVALS),
("ST", "STR", None, RVALS),
],
[2, "L",
("CB", "COMBINATIONS", [], RVALS | IN_LAMBDA),
],
[1, None,
("MX", "MAX", None, RVALS | IN_LAMBDA),
("M", "MAX", None, RVALS | IN_LAMBDA), # Synonym for golfiness
("MN", "MIN", None, RVALS | IN_LAMBDA),
("N", "MIN", None, RVALS | IN_LAMBDA), # Synonym for golfiness
("RC", "RANDCHOICE", None, RVALS | IN_LAMBDA),
("SH", "SHUFFLE", None, RVALS | IN_LAMBDA),
("SN", "SORTNUM", None, RVALS | IN_LAMBDA),
("SS", "SORTSTRING", None, RVALS | IN_LAMBDA),
("DN", "DESCENDINGNUM", None, RVALS | IN_LAMBDA),
("DS", "DESCENDINGSTRING", None, RVALS | IN_LAMBDA),
("UQ", "UNIQUE", None, RVALS | IN_LAMBDA),
("EN", "ENUMERATE", None, RVALS | IN_LAMBDA),
("PM", "PERMUTATIONS", None, RVALS | IN_LAMBDA),
("FL", "FLATTEN", None, RVALS | IN_LAMBDA),
("FA", "FLATTENALL", None, RVALS | IN_LAMBDA),
],
[2, "L",
("AE", "APPENDELEM", [], RVALS | IN_LAMBDA),
("AL", "APPENDLIST", [], RVALS | IN_LAMBDA),
("PE", "PREPENDELEM", [], RVALS | IN_LAMBDA),
("PU", "PUSH", [], VALS | IN_LAMBDA),
("PB", "PUSHBACK", [], VALS | IN_LAMBDA),
("PK", "PICK", [], VALS | IN_LAMBDA),
],
[1, None,
("PO", "POP", None, VALS | IN_LAMBDA),
("DQ", "DEQUEUE", None, VALS | IN_LAMBDA),
],
[2, "L",
("^", "SPLIT", [], RVALS | IN_LAMBDA | RANGE_EACH),
("^@", "SPLITAT", [], RVALS | IN_LAMBDA),
("@?", "FIND", None, RVALS | IN_LAMBDA),
("@*", "FINDALL", [], RVALS | IN_LAMBDA),
("<>", "GROUP", [], RVALS | IN_LAMBDA),
("CH", "CHOP", [], RVALS | IN_LAMBDA),
("J", "JOIN", "", RVALS | IN_LAMBDA),
("JW", "JOINWRAP", None, RVALS | IN_LAMBDA),
("RL", "REPEATLIST", [], RVALS | IN_LAMBDA),
("Z", "ZIP", [], RVALS | IN_LAMBDA),
("ZD", "ZIPDEFAULT", [], RVALS | IN_LAMBDA),
("ZJ", "ZIPJOIN", [], RVALS | IN_LAMBDA),
("H", "PREFIX", [], VALS | IN_LAMBDA), # Mnemonic: Head
("S", "SUFFIX", [], VALS | IN_LAMBDA),
("WV", "WEAVE", [], RVALS | IN_LAMBDA),
("UW", "UNWEAVE", [], RVALS | IN_LAMBDA),
("CP", "CARTESIANPRODUCT", [], RVALS | IN_LAMBDA),
("CG", "COORDINATEGRID", None, RVALS | IN_LAMBDA | RANGE_EACH),
("ZG", "ZEROGRID", None, RVALS | IN_LAMBDA | RANGE_EACH),
("OG", "ONEGRID", None, RVALS | IN_LAMBDA | RANGE_EACH),
("TD", "TODIGITS", [], RVALS | IN_LAMBDA ),
("FD", "FROMDIGITS", 0, RVALS | IN_LAMBDA ),
],
[3, "L",
("RA", "REPLACEAT", None, RVALS | IN_LAMBDA),
("TR", "TRANSLITERATE", None, RVALS | IN_LAMBDA),
],
[1, None,
("^", "SPLIT", None, RVALS | IN_LAMBDA | RANGE_EACH),
("<>", "GROUP", None, RVALS | IN_LAMBDA),
("CH", "CHOP", None, RVALS | IN_LAMBDA),
("J", "JOIN", None, RVALS | IN_LAMBDA),
("RV", "REVERSE", None, RVALS | IN_LAMBDA),
("R", "REVERSE", None, RVALS | IN_LAMBDA), # Synonym for golfiness
("RF", "REFLECT", None, RVALS | IN_LAMBDA),
("PZ", "PALINDROMIZE", None, RVALS | IN_LAMBDA),
("QR", "QUADREFLECT", None, RVALS | IN_LAMBDA),
("QP", "QUADPALINDROMIZE", None, RVALS | IN_LAMBDA),
("Z", "ZIP", None, RVALS | IN_LAMBDA),
("ZD", "ZIPDEFAULT", None, RVALS | IN_LAMBDA),
("ZJ", "ZIPJOIN", None, RVALS | IN_LAMBDA),
("H", "PREFIX", None, VALS | IN_LAMBDA), # Mnemonic: Head
("S", "SUFFIX", None, VALS | IN_LAMBDA),
("WV", "WEAVE", None, RVALS | IN_LAMBDA),
("UW", "UNWEAVE", None, RVALS | IN_LAMBDA),
("CP", "CARTESIANPRODUCT", None, RVALS | IN_LAMBDA),
("CG", "COORDINATEGRID", None, RVALS | IN_LAMBDA | RANGE_EACH),
("ZG", "ZEROGRID", None, RVALS | IN_LAMBDA | RANGE_EACH),
("OG", "ONEGRID", None, RVALS | IN_LAMBDA | RANGE_EACH),
("EY", "IDENTITYMATRIX", None, RVALS | IN_LAMBDA | RANGE_EACH),
("TD", "TODIGITS", None, RVALS | IN_LAMBDA ),
("FD", "FROMDIGITS", None, RVALS | IN_LAMBDA ),
],
[3, "L",
("R", "REPLACE", None, RVALS),
],
[2, "L",
("WR", "WRAP", "", RVALS | IN_LAMBDA | RANGE_EACH),
("~", "FIRSTMATCH", "", RVALS | IN_LAMBDA | RANGE_EACH),
],
[2, "L",
(".", "CAT", "", RVALS | IN_LAMBDA | RANGE_EACH),
],
[1, None,
("X", "REGEX", None, RVALS | IN_LAMBDA),
(".", "DOT", None, RVALS | IN_LAMBDA | LIST_EACH),
("K", "KLEENESTAR", None, RVALS | IN_LAMBDA | LIST_EACH),
],
[2, "L",
("RM", "REMOVE", "", RVALS | IN_LAMBDA),
("DC", "DELETECHARS", "", RVALS | IN_LAMBDA | RANGE_EACH),
],
[2, "L",
("X", "STRMUL", "", RVALS | IN_LAMBDA | RANGE_EACH),
],
[2, "L",
("||", "STRIP", "", RVALS | IN_LAMBDA | LIST_EACH),
("|>", "LSTRIP", "", RVALS | IN_LAMBDA | LIST_EACH),
("<|", "RSTRIP", "", RVALS | IN_LAMBDA | LIST_EACH),
("TM", "TRIM", "", RVALS | IN_LAMBDA | LIST_EACH),
],
[1, None,
("||", "STRIP", None, RVALS | IN_LAMBDA | LIST_EACH),
("|>", "LSTRIP", None, RVALS | IN_LAMBDA | LIST_EACH),
("<|", "RSTRIP", None, RVALS | IN_LAMBDA | LIST_EACH),
("TM", "TRIM", None, RVALS | IN_LAMBDA | LIST_EACH),
("LC", "LOWERCASE", None, RVALS | IN_LAMBDA | LIST_EACH),
("UC", "UPPERCASE", None, RVALS | IN_LAMBDA | LIST_EACH),
("SC", "SWAPCASE", None, RVALS | IN_LAMBDA | LIST_EACH),
("TC", "TITLECASE", None, RVALS | IN_LAMBDA | LIST_EACH),
("IC", "INITIALCAPS", None, RVALS | IN_LAMBDA | LIST_EACH),
],
[2, "L",
(",", "RANGE", None, RVALS | IN_LAMBDA | RANGE_EACH),
(r"\,", "INCLRANGE", None, RVALS | IN_LAMBDA | RANGE_EACH),
("RR", "RANDRANGE", 0, RVALS | IN_LAMBDA | RANGE_EACH),
("TB", "TOBASE", 0, RVALS | IN_LAMBDA | RANGE_EACH),
],
[1, None,
(",", "RANGETO", None, RVALS | IN_LAMBDA | RANGE_EACH),
(r"\,", "INCLRANGETO", None, RVALS | IN_LAMBDA | RANGE_EACH),
("RR", "RANDRANGETO", None, RVALS | IN_LAMBDA | RANGE_EACH),
("TB", "TOBASE", None, RVALS | IN_LAMBDA | RANGE_EACH),
# Unary mnemonic: ToBinary
],
[2, "L",
("BA", "BITWISEAND", -1, RVALS | IN_LAMBDA | RANGE_EACH),
("BO", "BITWISEOR", 0, RVALS | IN_LAMBDA | RANGE_EACH),
("BX", "BITWISEXOR", 0, RVALS | IN_LAMBDA | RANGE_EACH),
("AT", "ARCTAN", None, RVALS | IN_LAMBDA | RANGE_EACH),
("CM", "NUMCMP", 0, RVALS | IN_LAMBDA),
("|<", "FLOOR", 0, RVALS | IN_LAMBDA | RANGE_EACH),
(">|", "CEIL", 0, RVALS | IN_LAMBDA | RANGE_EACH),
("RZ", "ROUNDZERO", 0, RVALS | IN_LAMBDA | RANGE_EACH),
("RN", "ROUNDNEAREST", 0, RVALS | IN_LAMBDA | RANGE_EACH),
],
[1, None,
("BN", "BITWISENOT", None, RVALS | IN_LAMBDA | RANGE_EACH),
("SI", "SINE", None, RVALS | IN_LAMBDA | RANGE_EACH),
("CO", "COSINE", None, RVALS | IN_LAMBDA | RANGE_EACH),
("TA", "TANGENT", None, RVALS | IN_LAMBDA | RANGE_EACH),
("SE", "SECANT", None, RVALS | IN_LAMBDA | RANGE_EACH),
("CS", "COSEC", None, RVALS | IN_LAMBDA | RANGE_EACH),
("CT", "COTAN", None, RVALS | IN_LAMBDA | RANGE_EACH),
("AT", "ARCTAN", None, RVALS | IN_LAMBDA | RANGE_EACH),
("RD", "RADIANS", None, RVALS | IN_LAMBDA | RANGE_EACH),
("DG", "DEGREES", None, RVALS | IN_LAMBDA | RANGE_EACH),
("BL", "BITLENGTH", None, RVALS | IN_LAMBDA | RANGE_EACH),
("|<", "FLOOR", None, RVALS | IN_LAMBDA | RANGE_EACH),
(">|", "CEIL", None, RVALS | IN_LAMBDA | RANGE_EACH),
("RZ", "ROUNDZERO", None, RVALS | IN_LAMBDA | RANGE_EACH),
("RN", "ROUNDNEAREST", None, RVALS | IN_LAMBDA | RANGE_EACH),
],
[2, "L",
("+", "ADD", 0, RVALS | IN_LAMBDA | LIST_EACH),
("-", "SUB", 0, RVALS | IN_LAMBDA | LIST_EACH),
("AD", "ABSOLUTEDIFF", 0, RVALS | IN_LAMBDA | RANGE_EACH),
],
[2, "L",
("*", "MUL", 1, RVALS | IN_LAMBDA | RANGE_EACH),
("/", "DIV", 1, RVALS | IN_LAMBDA | RANGE_EACH),
("%", "MOD", 0, RVALS | IN_LAMBDA | RANGE_EACH),
("//", "INTDIV", 1, RVALS | IN_LAMBDA | RANGE_EACH),
],
[1, None,
("+", "POS", None, RVALS | IN_LAMBDA | LIST_EACH),
("-", "NEG", None, RVALS | IN_LAMBDA | RANGE_EACH),
("/", "INVERT", None, RVALS | IN_LAMBDA | RANGE_EACH),
("%", "MOD", None, RVALS | IN_LAMBDA | RANGE_EACH),
("HV", "HALVE", None, RVALS | IN_LAMBDA | RANGE_EACH),
("HU", "HALVEUP", None, RVALS | IN_LAMBDA | RANGE_EACH),
("DB", "DOUBLE", None, RVALS | IN_LAMBDA | RANGE_EACH),
("SQ", "SQUARE", None, RVALS | IN_LAMBDA | RANGE_EACH),
("**", "POW", None, RVALS | IN_LAMBDA | RANGE_EACH),
("E", "POW", None, RVALS | IN_LAMBDA | RANGE_EACH),
("EE", "POWEROFTEN", None, RVALS | IN_LAMBDA | RANGE_EACH),
("RT", "SQRT", None, RVALS | IN_LAMBDA | RANGE_EACH),
("EX", "EXPONENTIAL", None, RVALS | IN_LAMBDA | RANGE_EACH),
("LN", "NATURALLOG", None, RVALS | IN_LAMBDA | RANGE_EACH),
("LB", "BINARYLOG", None, RVALS | IN_LAMBDA | RANGE_EACH),
("LD", "DECIMALLOG", None, RVALS | IN_LAMBDA | RANGE_EACH),
],
[2, "R",
("**", "POW", 1, RVALS | IN_LAMBDA | RANGE_EACH),
("E", "POW", 1, RVALS | IN_LAMBDA | RANGE_EACH),
("EE", "POWEROFTEN", 1, RVALS | IN_LAMBDA | RANGE_EACH),
("RT", "ROOT", 1, RVALS | IN_LAMBDA | RANGE_EACH),
],
[2, "L",
("FB", "FROMBASE", 0, RVALS | IN_LAMBDA | RANGE_EACH),
],
[1, None,
("U", "INC", None, VALS | IN_LAMBDA),
("D", "DEC", None, VALS | IN_LAMBDA),
("#", "LEN", None, RVALS | IN_LAMBDA),
("A", "ASC", None, RVALS | IN_LAMBDA | RANGE_EACH),
("C", "CHR", None, RVALS | IN_LAMBDA | RANGE_EACH),
("AB", "ABS", None, RVALS | IN_LAMBDA | RANGE_EACH),
("SG", "SIGN", None, RVALS | IN_LAMBDA | RANGE_EACH),
("FB", "FROMBASE", None, RVALS | IN_LAMBDA | RANGE_EACH),
# Unary mnemonic: FromBinary
],
[2, "L",
("@", "AT", None, VALS | IN_LAMBDA),
("@<", "LEFTOF", None, VALS | IN_LAMBDA),
("@>", "RIGHTOF", None, VALS | IN_LAMBDA),
],
[1, None,
("@", "AT", None, VALS | IN_LAMBDA),
("@<", "LEFTOF", None, VALS | IN_LAMBDA),
("@>", "RIGHTOF", None, VALS | IN_LAMBDA),
("++", "INC", None, VALS | IN_LAMBDA),
("--", "DEC", None, VALS | IN_LAMBDA),
],
]
# Now take the information from the precedence table and rearrange it into
# structures more useful to the scanner and parser:
opsByArity = {1:{}, 2:{}, 3:{}}
operators = set()
# Some convenience operators for parsing that don't have straightforward
# equivalents in the syntax:
highestPrecedence = len(precedenceTable)
paren = Operator("PAREN", "PARENTHESIZE", 1,
highestPrecedence, "L", None, VALS)
enlist = Operator("LIST", "LIST", 1, highestPrecedence, "L", None, VALS)
block = Operator("BLOCK", "BLOCK", 1, highestPrecedence, "L", None)
send = Operator("SEND", "SEND", 1, highestPrecedence, "L", None, VALS)
chain = None # We define this "operator" later to make sure it gets the same
# precedence as the comparison operators it comprises
for precedence, (arity, associativity, *entries) in enumerate(precedenceTable):
for entry in entries:
text, function, default = entry[:3]
if len(entry) > 3:
flags = entry[3]
else:
flags = 0
if flags & RANGE_EACH:
# RANGE_EACH implies LIST_EACH (though not vice versa)
flags |= LIST_EACH
if arity > 2 and flags & LIST_EACH:
# TODO: proper implementation error message
msg = ("Current implementation cannot handle LIST_EACH or "
"RANGE_EACH for operators\n of arity greater than 2 "
f"(like {text})")
print(msg)
flags = flags & ~LIST_EACH & ~RANGE_EACH
if flags & IN_LAMBDA and not flags & (VALS | RVALS):
# TODO: proper implementation error message
msg = ("IN_LAMBDA may not be set for operators that do not "
f"also set VALS or RVALS\n ({text})")
print(msg)
flags = flags & ~IN_LAMBDA
op = Operator(text,
function,
arity,
precedence,
associativity,
default,
flags)
if associativity == "C" and not chain:
# Define the CHAIN pseudo-operator, which allows parsing of
# chained comparisons like 1<x<5
chain = Operator("CHAIN",
"CHAIN",
2,
precedence,
"C",
None,
RVALS | IN_LAMBDA)
opsByArity[arity][text] = op
operators.add(text)