Skip to content

Commit 39669f2

Browse files
committed
Implement the discard reference via dedicated AST dispatch
1 parent 5579460 commit 39669f2

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

py/dml/ast.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ def __setstate__(self, data):
5555
'default',
5656
'default_dml12',
5757
'delete',
58+
'discardref',
5859
'dml',
5960
'dml_typedef',
6061
'dowhile',

py/dml/codegen.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1222,16 +1222,13 @@ def expr_variable(tree, location, scope):
12221222
if in_dev_tree:
12231223
e = in_dev_tree
12241224
if e is None:
1225-
# TODO/HACK: The discard ref is exposed like this to allow it to be as
1226-
# keyword-like as possible while still allowing it to be shadowed.
1227-
# Once we remove support for discard_ref_shadowing the discard ref
1228-
# should become a proper keyword and its codegen be done via dedicated
1229-
# dispatch
1230-
if name == '_' and tree.site.dml_version() != (1, 2):
1231-
return mkDiscardRef(tree.site)
12321225
raise EIDENT(tree.site, name)
12331226
return e
12341227

1228+
@expression_dispatcher
1229+
def expr_discard(tree, location, scope):
1230+
return mkDiscardRef(tree.site)
1231+
12351232
@expression_dispatcher
12361233
def expr_objectref(tree, location, scope):
12371234
[name] = tree.args

py/dml/dmllex14.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@
1313

1414
tokens = (common_tokens
1515
+ ('HASHCONDOP', 'HASHCOLON')
16+
+ ('DISCARD',)
1617
+ tuple(hashids.values()))
1718

1819
t_HASHCONDOP = r'\#\?'
1920
t_HASHCOLON = r'\#:'
21+
t_DISCARD = r'_'
2022

2123
keywords_dml14 = dict(keywords_common)
2224
for kw in ['param', 'saved', 'async', 'await', 'with', 'shared', 'stringify',
2325
'export', 'as', 'independent', 'startup', 'memoized', 'hook']:
2426
keywords_dml14[kw] = kw.upper()
2527
tokens += (kw.upper(),)
2628

29+
keywords_dml14['_'] = 'DISCARD'
30+
2731
reserved_idents = reserved_idents_common + (
2832
'PARAM', 'SAVED', 'INDEPENDENT', 'STARTUP', 'MEMOIZED')
2933

py/dml/dmlparse.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1801,6 +1801,11 @@ def expression_ident(t):
18011801
| DEFAULT'''
18021802
t[0] = ast.variable(site(t), t[1])
18031803

1804+
@prod_dml14
1805+
def expression_discardref(t):
1806+
'''expression : discard'''
1807+
t[0] = t[1]
1808+
18041809
@prod_dml14
18051810
def expression_this(t):
18061811
'''expression : THIS'''
@@ -2660,6 +2665,11 @@ def objident(t):
26602665
| REGISTER'''
26612666
t[0] = t[1]
26622667

2668+
@prod_dml14
2669+
def discard(t):
2670+
'discard : DISCARD'
2671+
t[0] = ast.discard(site(t, 1))
2672+
26632673
def ident_rule(idents):
26642674
return 'ident : ' + "\n| ".join(idents)
26652675

0 commit comments

Comments
 (0)