-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
606 lines (528 loc) · 17.7 KB
/
parser.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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
from __future__ import annotations
import dataclasses
import functools
import json
import operator
import random
import sys
# from watchdog.events import FileSystemEventHandler
# from watchdog.observers import Observer
import time
from functools import partial, reduce, wraps
from itertools import product
from typing import Any, Callable, Generic, Protocol, TypeAlias, TypeVar, Union, overload
import sly # type: ignore[import]
from sly import Lexer, Parser
import terms
import typed
from algorithm_j import Context, Scheme, fresh_ty_var, type_infer
def _(fn: str, *args: str) -> Callable[[R], R]:
raise Exception
A = TypeVar("A")
R = TypeVar("R")
class UwuLexer(Lexer):
tokens = {
"INT",
"FLOAT",
"STRING",
"IDENTIFIER",
"DEF",
"DO",
"END",
"IF",
"ELSE",
"CASE",
"OF",
"CONCAT",
"ELIF",
"ENUM",
"THEN",
"TYPE_IDENTIFIER",
"EXTERNAL",
"FLOAT_SUM",
"FLOAT_SUB",
"FLOAT_DIV",
"FLOAT_MUL",
"NOT_EQUAL",
"EQUAL",
"NEWLINE",
"STRICT_OR",
"STRICT_AND",
"STRICT_NOT",
"OR",
"AND",
"TEXT_MATCH",
"LESS_OR_EQ",
"MORE_OR_EQ",
"ARRAY_CONCAT",
"ARRAY_SUB",
"POW",
"FLOAT_POW",
"BIT_OR",
"BIT_AND",
"BIT_SHIFT_LEFT",
# "BIT_SHIFT_RIGHT", TODO 1: fix A<A<A<B>>> +bug
"DOUBLE_ARROW_LEFT",
"DOUBLE_ARROW_RIGHT",
"ARROW_LEFT",
"ARROW_RIGHT",
"ARROW_BOTH",
"SOME_CONCAT",
"SOME_SUB",
"FLOAT_LESS_OR_EQ",
"FLOAT_LESS",
"FLOAT_MORE_OR_EQ",
"FLOAT_MORE",
}
literals = {
"=",
".",
"[",
"]",
",",
"{",
"}",
"(",
")",
":",
"+",
"-",
">",
"<",
"*",
"/",
"!",
}
OR = r"\|\|"
AND = r"&&"
TEXT_MATCH = r"=~"
FLOAT_LESS_OR_EQ = r"<=\."
FLOAT_LESS = r"<\."
FLOAT_MORE_OR_EQ = r">=\."
FLOAT_MORE = r">\."
LESS_OR_EQ = r"<="
MORE_OR_EQ = r">="
ARRAY_CONCAT = r"\+\+"
ARRAY_SUB = r"--"
FLOAT_POW = r"\*\*\."
POW = r"\*\*"
BIT_OR = r"\|\|\|"
BIT_AND = r"&&&"
BIT_SHIFT_LEFT = r"<<<"
# BIT_SHIFT_RIGHT = r">>>"
DOUBLE_ARROW_LEFT = r"<<~"
DOUBLE_ARROW_RIGHT = r"~>>"
ARROW_LEFT = r"<~"
ARROW_RIGHT = r"~>"
ARROW_BOTH = r"<~>"
SOME_CONCAT = r"\+\+\+"
SOME_SUB = r"---"
NOT_EQUAL = r"!="
EQUAL = r"=="
STRING = r"'[^']*'"
FLOAT = r"\d[\d_]*\.[\d_]*"
INT = r"\d[\d_]*"
CONCAT = r"<>"
FLOAT_SUM = r"\+\."
FLOAT_SUB = r"-\."
FLOAT_DIV = r"/\."
FLOAT_MUL = r"\*\."
TYPE_IDENTIFIER = r"[A-Z\d][\w\d]*"
IDENTIFIER = r"[a-z_][\w\d]*"
IDENTIFIER["def"] = "DEF" # type: ignore[index]
IDENTIFIER["do"] = "DO" # type: ignore[index]
IDENTIFIER["end"] = "END" # type: ignore[index]
IDENTIFIER["if"] = "IF" # type: ignore[index]
IDENTIFIER["else"] = "ELSE" # type: ignore[index]
IDENTIFIER["elif"] = "ELIF" # type: ignore[index]
IDENTIFIER["case"] = "CASE" # type: ignore[index]
IDENTIFIER["enum"] = "ENUM" # type: ignore[index]
IDENTIFIER["then"] = "THEN" # type: ignore[index]
IDENTIFIER["of"] = "OF" # type: ignore[index]
IDENTIFIER["or"] = "STRICT_OR" # type: ignore[index]
IDENTIFIER["and"] = "STRICT_AND" # type: ignore[index]
IDENTIFIER["not"] = "STRICT_NOT" # type: ignore[index]
EXTERNAL = r"`[^`]*`"
ignore_comment = r"\#.*"
ignore = " \t"
@_(r"\n([\s\t\n]|\#.*)*")
def NEWLINE(self, t):
self.lineno += t.value.count("\n")
return t
def concat(v: A | None, l1: list[A] | None) -> list[A]:
l0 = [] if v is None else [v]
l2 = l1 if isinstance(l1, list) else []
return l0 + l2
class UwuParser(Parser):
tokens = UwuLexer.tokens
debugfile = "parser.out"
@_("[ NEWLINE ] [ do_exprs ]")
def program(self, p: sly.yacc.YaccProduction) -> terms.EProgram:
return terms.EProgram(p.do_exprs or [])
@_("expr NEWLINE do_exprs")
def do_exprs(self, p: sly.yacc.YaccProduction) -> list[terms.EExpr]:
return [p.expr] + p.do_exprs
@_("expr [ NEWLINE ]") # type: ignore[no-redef]
def do_exprs(self, p: sly.yacc.YaccProduction) -> list[terms.EExpr]:
return [p.expr]
precedence = (
("right", "="),
("left", "OR", "STRICT_OR", "BIT_OR"),
("left", "AND", "STRICT_AND", "BIT_AND"),
("left", "NOT_EQUAL", "EQUAL", "TEXT_MATCH"),
(
"left",
"<",
">",
"LESS_OR_EQ",
"MORE_OR_EQ",
"FLOAT_LESS",
"FLOAT_MORE",
"FLOAT_LESS_OR_EQ",
"FLOAT_MORE_OR_EQ",
),
(
"left",
# "BIT_SHIFT_RIGHT",
"BIT_SHIFT_LEFT",
"DOUBLE_ARROW_RIGHT",
"DOUBLE_ARROW_LEFT",
"ARROW_RIGHT",
"ARROW_LEFT",
"ARROW_BOTH",
),
("right", "CONCAT", "ARRAY_CONCAT", "ARRAY_SUB", "SOME_CONCAT", "SOME_SUB"),
("left", "+", "-", "FLOAT_SUM", "FLOAT_SUB"),
("left", "*", "/", "FLOAT_DIV", "FLOAT_MUL"),
("left", "POW", "FLOAT_POW"),
("right", "UNARY"),
("left", "(", ")"),
)
@_(
"enum",
"external",
"do",
"def_expr",
"if_expr",
"binary_expr",
"case_of",
"call",
"let",
"identifier",
"variant_call",
"array",
"int_literal",
"float_literal",
"str_literal",
"unary_expr",
"binary_op_def",
)
def expr(self, p: sly.yacc.YaccProduction) -> terms.EExpr:
return terms.EExpr(p[0])
@_( # type: ignore[no-redef]
"'(' expr ')'",
)
def expr(self, p: sly.yacc.YaccProduction) -> terms.EExpr:
return p[1]
@_( # type: ignore[no-redef]
"'-' expr %prec UNARY",
"STRICT_NOT expr %prec UNARY",
"'!' expr %prec UNARY",
"'+' expr %prec UNARY",
)
def unary_expr(self, p: sly.yacc.YaccProduction) -> terms.EUnaryExpr:
return terms.EUnaryExpr(p[0], p.expr)
@_("EXTERNAL")
def external(self, p: sly.yacc.YaccProduction) -> terms.EExternal:
return terms.EExternal(p.EXTERNAL[1:-1])
@_(
"expr CONCAT expr",
"expr '+' expr",
"expr '-' expr",
"expr '/' expr",
"expr '*' expr",
"expr '<' expr",
"expr FLOAT_SUM expr",
"expr FLOAT_SUB expr",
"expr FLOAT_DIV expr",
"expr FLOAT_MUL expr",
"expr '>' expr",
"expr NOT_EQUAL expr",
"expr EQUAL expr",
"expr OR expr",
"expr STRICT_OR expr",
"expr AND expr",
"expr STRICT_AND expr",
"expr TEXT_MATCH expr",
"expr LESS_OR_EQ expr",
"expr MORE_OR_EQ expr",
"expr ARRAY_CONCAT expr",
"expr ARRAY_SUB expr",
"expr POW expr",
"expr FLOAT_POW expr",
"expr BIT_OR expr",
"expr BIT_AND expr",
"expr BIT_SHIFT_LEFT expr",
# "expr BIT_SHIFT_RIGHT expr",
"expr DOUBLE_ARROW_LEFT expr",
"expr DOUBLE_ARROW_RIGHT expr",
"expr ARROW_LEFT expr",
"expr ARROW_RIGHT expr",
"expr ARROW_BOTH expr",
"expr SOME_CONCAT expr",
"expr SOME_SUB expr",
"expr FLOAT_LESS_OR_EQ expr",
"expr FLOAT_LESS expr",
"expr FLOAT_MORE_OR_EQ expr",
"expr FLOAT_MORE expr",
)
def binary_expr(self, p: sly.yacc.YaccProduction):
return terms.EBinaryExpr(p[1], p[0], p[2])
@_(
"CONCAT",
"'+'",
"'-'",
"'/'",
"'*'",
"'<'",
"FLOAT_SUM",
"FLOAT_SUB",
"FLOAT_DIV",
"FLOAT_MUL",
"'>'",
"NOT_EQUAL",
"EQUAL",
"OR",
"STRICT_OR",
"AND",
"STRICT_AND",
"TEXT_MATCH",
"LESS_OR_EQ",
"MORE_OR_EQ",
"ARRAY_CONCAT",
"ARRAY_SUB",
"POW",
"FLOAT_POW",
"BIT_OR",
"BIT_AND",
"BIT_SHIFT_LEFT",
# "BIT_SHIFT_RIGHT",
"DOUBLE_ARROW_LEFT",
"DOUBLE_ARROW_RIGHT",
"ARROW_LEFT",
"ARROW_RIGHT",
"ARROW_BOTH",
"SOME_CONCAT",
"SOME_SUB",
"FLOAT_LESS_OR_EQ",
"FLOAT_LESS",
"FLOAT_MORE_OR_EQ",
"FLOAT_MORE",
)
def binary_op(self, p: sly.yacc.YaccProduction) -> str:
return p[0]
@_(
"DEF binary_op '<' type_identifier { ',' type_identifier } '>' '(' [ NEWLINE ] param ',' [ NEWLINE ] param [ NEWLINE ] ')' [ ':' type ] do"
)
def binary_op_def(self, p: sly.yacc.YaccProduction):
return terms.EBinaryOpDef(
p.binary_op,
[p.param0, p.param1],
body=p.do,
hint=terms.MaybeEHint(p.type or terms.MaybeEHintNothing()),
generics=concat(p.type_identifier0, p.type_identifier1),
)
@_("DEF binary_op '(' [ NEWLINE ] param ',' [ NEWLINE ] param [ NEWLINE ] ')' [ ':' type ] do") # type: ignore[no-redef]
def binary_op_def(self, p: sly.yacc.YaccProduction):
return terms.EBinaryOpDef(
p.binary_op,
[p.param0, p.param1],
body=p.do,
hint=terms.MaybeEHint(p.type or terms.MaybeEHintNothing()),
)
@_(
"DO [ ':' type ] block_statement END",
)
def do(self, p: sly.yacc.YaccProduction):
return terms.EDo(
p.block_statement,
hint=terms.MaybeEHint(p.type or terms.MaybeEHintNothing()),
)
@_(
"[ NEWLINE ] [ do_exprs ]",
)
def block_statement(self, p: sly.yacc.YaccProduction):
return terms.EBlock(p.do_exprs or [])
@_(
"DEF identifier '<' type_identifier { ',' type_identifier } '>' '(' [ NEWLINE ] [ params ] ')' [ ':' type ] do"
)
def def_expr(self, p: sly.yacc.YaccProduction):
return terms.EDef(
p.identifier.name,
p.params or [],
body=p.do,
hint=terms.MaybeEHint(p.type or terms.MaybeEHintNothing()),
generics=concat(p.type_identifier0, p.type_identifier1),
)
@_("DEF identifier '(' [ NEWLINE ] [ params ] ')' [ ':' type ] do") # type: ignore[no-redef]
def def_expr(self, p: sly.yacc.YaccProduction):
return terms.EDef(
p.identifier.name,
p.params or [],
body=p.do,
hint=terms.MaybeEHint(p.type or terms.MaybeEHintNothing()),
)
@_("params ',' [ NEWLINE ] param [ NEWLINE ]")
def params(self, p: sly.yacc.YaccProduction):
return p.params + [p.param]
@_("param [ NEWLINE ]") # type: ignore[no-redef]
def params(self, p: sly.yacc.YaccProduction):
return [p.param]
@_("type_identifier")
def type(self, p: sly.yacc.YaccProduction):
return terms.EHint(p.type_identifier.name, [])
@_("type_identifier '<' type { ',' type } '>'") # type: ignore[no-redef]
def type(self, p: sly.yacc.YaccProduction):
return terms.EHint(p.type_identifier.name, concat(p.type0, p.type1))
@_(
"ENUM type_identifier '<' type_identifier { ',' type_identifier } '>' '{' [ NEWLINE ] [ variants ] '}'"
)
def enum(self, p: sly.yacc.YaccProduction):
return terms.EEnumDeclaration(
p.type_identifier0.name,
variants=p.variants or [],
generics=concat(p.type_identifier1, p.type_identifier2),
)
@_("variants variant [ NEWLINE ]")
def variants(self, p: sly.yacc.YaccProduction):
return p.variants + [p.variant]
@_("variant [ NEWLINE ]") # type: ignore[no-redef]
def variants(self, p: sly.yacc.YaccProduction):
return [p.variant]
@_("ENUM type_identifier '{' [ NEWLINE ] [ variants ] '}'") # type: ignore[no-redef]
def enum(self, p: sly.yacc.YaccProduction):
return terms.EEnumDeclaration(p.type_identifier.name, variants=p.variants or [])
@_("TYPE_IDENTIFIER '(' type { ',' type } ')'")
def variant(self, p: sly.yacc.YaccProduction):
return terms.EVariant(p.TYPE_IDENTIFIER, concat(p.type0, p.type1))
@_("TYPE_IDENTIFIER") # type: ignore[no-redef]
def variant(self, p: sly.yacc.YaccProduction):
return terms.EVariant(p.TYPE_IDENTIFIER)
# @_("identifier { ',' identifier }")
# def fields_unnamed(self, p:sly.yacc.YaccProduction):
# return concat(p.identifier0, p.identifier1)
@_("identifier [ ':' type ]")
def param(self, p: sly.yacc.YaccProduction):
return terms.EParam(
p.identifier.name, terms.MaybeEHint(p.type or terms.MaybeEHintNothing())
)
@_("IF expr THEN [ ':' type ] block_statement [ or_else ] END")
def if_expr(self, p: sly.yacc.YaccProduction):
return terms.EIf(
p.expr,
then=p.block_statement,
or_else=terms.MaybeOrElse(p.or_else),
hint=terms.MaybeEHint(p.type or terms.MaybeEHintNothing()),
)
@_("ELSE block_statement")
def or_else(self, p: sly.yacc.YaccProduction): # type: ignore[no-redef]
return p.block_statement
@_("ELIF expr THEN block_statement [ or_else ]") # type: ignore[no-redef]
def or_else(self, p: sly.yacc.YaccProduction):
return terms.EIf(
p.expr, then=p.block_statement, or_else=terms.MaybeOrElse(p.or_else)
)
@_("CASE expr OF [ NEWLINE ] [ cases ] END")
def case_of(self, p: sly.yacc.YaccProduction):
return terms.ECaseOf(p.expr, p.cases or [])
@_("cases pattern do [ NEWLINE ]")
def cases(self, p: sly.yacc.YaccProduction):
return p.cases + [terms.ECase(p.pattern, p.do)]
@_("pattern do [ NEWLINE ]") # type: ignore[no-redef]
def cases(self, p: sly.yacc.YaccProduction):
return [terms.ECase(p.pattern, p.do)]
# @_("clause { ',' clause } do")
# def case(self, p:sly.yacc.YaccProduction):
# import operator
# return terms.ECase(
# functools.reduce(operator.__or__, concat(p.clause0, p.clause1)), p.do
# )
# @_("identifier '=' pattern")
# def clause(self, p:sly.yacc.YaccProduction):
# return {p.identifier.name: p.pattern}
@_("match_as", "match_variant") # , "tuple_pattern", "array_pattern"
def pattern(self, p: sly.yacc.YaccProduction):
return terms.EPattern(p[0])
@_("identifier")
def match_as(self, p: sly.yacc.YaccProduction):
return terms.EMatchAs(p.identifier.name)
# @_("'[' [ pattern ] { ',' pattern } ']'")
# def array_pattern(self, p:sly.yacc.YaccProduction):
# return terms.EMatchArray(concat(p.pattern0, p.pattern1))
# @_("'{' [ pattern ] { ',' pattern } '}'")
# def tuple_pattern(self, p:sly.yacc.YaccProduction):
# return terms.EMatchTuple(concat(p.pattern0, p.pattern1))
@_("TYPE_IDENTIFIER '(' [ NEWLINE ] [ patterns ] ')'")
def match_variant(self, p: sly.yacc.YaccProduction):
return terms.EMatchVariant(p.TYPE_IDENTIFIER, p.patterns or [])
@_("TYPE_IDENTIFIER") # type: ignore[no-redef]
def match_variant(self, p: sly.yacc.YaccProduction):
return terms.EMatchVariant(p.TYPE_IDENTIFIER, [])
@_("patterns ',' [ NEWLINE ] pattern [ NEWLINE ]")
def patterns(self, p: sly.yacc.YaccProduction):
return p.patterns + [p.pattern]
@_("pattern [ NEWLINE ]") # type: ignore[no-redef]
def patterns(self, p: sly.yacc.YaccProduction):
return [p.pattern]
@_("'[' [ NEWLINE ] [ exprs ] ']'")
def array(self, p: sly.yacc.YaccProduction):
return terms.EArray(p.exprs or [])
# @_("'{' [ expr ] { ',' expr } '}'")
# def tuple(self, p:sly.yacc.YaccProduction):
# return terms.ETuple(concat(p.expr0, p.expr1))
@_("expr '(' [ NEWLINE ] [ exprs ] ')'")
def call(self, p: sly.yacc.YaccProduction):
return terms.ECall(p.expr, p.exprs or [])
@_("TYPE_IDENTIFIER '(' [ NEWLINE ] [ exprs ] ')'")
def variant_call(self, p: sly.yacc.YaccProduction):
return terms.EVariantCall(p.TYPE_IDENTIFIER, p.exprs or [])
@_("exprs ',' [ NEWLINE ] expr [ NEWLINE ]")
def exprs(self, p: sly.yacc.YaccProduction):
return p.exprs + [p.expr]
@_("expr [ NEWLINE ]") # type: ignore[no-redef]
def exprs(self, p: sly.yacc.YaccProduction):
return [p.expr]
@_("IDENTIFIER")
def identifier(self, p: sly.yacc.YaccProduction):
return terms.EIdentifier(p.IDENTIFIER)
@_("TYPE_IDENTIFIER")
def type_identifier(self, p: sly.yacc.YaccProduction):
return terms.ETypeIdentifier(p.TYPE_IDENTIFIER)
@_("IDENTIFIER")
def type_identifier(self, p: sly.yacc.YaccProduction):
return terms.ETypeIdentifier(p.IDENTIFIER)
@_("identifier [ ':' type ] '=' expr")
def let(self, p):
return terms.ELet(
id=p.identifier.name,
init=p.expr,
hint=terms.MaybeEHint(p.type or terms.MaybeEHintNothing()),
)
@_("identifier ':' type_identifier '<' type { ',' type } MORE_OR_EQ expr") # type: ignore[no-redef]
def let(self, p):
return terms.ELet(
id=p.identifier.name,
init=p.expr,
hint=terms.MaybeEHint(
terms.EHint(p.type_identifier.name, concat(p.type0, p.type1))
),
)
@_("INT")
def int_literal(self, p: sly.yacc.YaccProduction) -> terms.ENumLiteral:
return terms.ENumLiteral(float(p.INT.replace("_", "")))
@_("FLOAT")
def float_literal(self, p: sly.yacc.YaccProduction) -> terms.EFloatLiteral:
return terms.EFloatLiteral(float(p.FLOAT.replace("_", "")))
@_("STRING")
def str_literal(self, p: sly.yacc.YaccProduction) -> terms.EStrLiteral:
return terms.EStrLiteral(p.STRING[1:-1])