Commit 887d2bcb
887d2bcb138eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
Merge pull request #281 from ryanseys/rs-node-keyword-rest-param
feat(parse,codegen): support KeywordRestParameterNode declaration (def f(**kw))
Details
- Commit
- 887d2bcb138e
- Parent
- 613f076fbe56
- Merge parent
- afb3ee23dd49
spinel_codegen.rbmodifiedView file
@@ -68,6 +68,9 @@
6868
@nd_constant_path = []6969
@nd_superclass = []7070
@nd_rest = []71+
# ParametersNode#keyword_rest -- holds a KeywordRestParameterNode72+
# (def f(**kw)) or NoKeywordsParameterNode (def f(**nil)).73+
@nd_keyword_rest = []7174
@nd_rescue_clause = []7275
@nd_ensure_clause = []7376
@nd_expression = []@@ -89,6 +92,11 @@
8992
@nd_exceptions = "".split(",")9093
@nd_targets = "".split(",")9194
@nd_rights = "".split(",")95+
# ParametersNode#posts -- required params after the splat96+
# (def f(*r, x, y) → posts = [x, y]). Currently unused by codegen97+
# (post-rest parameters aren't observed in test/), but the parser98+
# emits the field so future tests get a proper AST.99+
@nd_posts = "".split(",")92100
93101
@nd_count = 094102
@root_id = 0@@ -421,6 +429,7 @@
421429
@nd_constant_path.push(-1)422430
@nd_superclass.push(-1)423431
@nd_rest.push(-1)432+
@nd_keyword_rest.push(-1)424433
@nd_rescue_clause.push(-1)425434
@nd_ensure_clause.push(-1)426435
@nd_expression.push(-1)@@ -440,6 +449,7 @@
440449
@nd_exceptions.push("")441450
@nd_targets.push("")442451
@nd_rights.push("")452+
@nd_posts.push("")443453
@nd_count = @nd_count + 1444454
nid445455
end@@ -663,6 +673,9 @@
663673
if field == "rest"664674
@nd_rest[nid] = ref_id665675
end676+
if field == "keyword_rest"677+
@nd_keyword_rest[nid] = ref_id678+
end666679
if field == "rescue_clause"667680
@nd_rescue_clause[nid] = ref_id668681
end@@ -744,6 +757,9 @@
744757
if field == "rights"745758
@nd_rights[nid] = ids_str746759
end760+
if field == "posts"761+
@nd_posts[nid] = ids_str762+
end747763
end748764
749765
# ---- Convenience: get stmts of a body node ----@@ -6146,6 +6162,36 @@
61466162
result = result + @nd_name[rest]61476163
end61486164
end6165+
# Post-rest required params: `def f(*r, x, y)` -> `x, y` come AFTER6166+
# rest in the AST's `posts` slot. Same shape as `requireds` (each6167+
# entry is a RequiredParameterNode); flow them straight through.6168+
posts = parse_id_list(@nd_posts[params])6169+
k = 06170+
while k < posts.length6171+
if @nd_type[posts[k]] == "RequiredParameterNode"6172+
if result != ""6173+
result = result + ","6174+
end6175+
result = result + @nd_name[posts[k]]6176+
end6177+
k = k + 16178+
end6179+
# Keyword rest (`**kw`). Anonymous `**` synthesizes `__anon_kwrest`.6180+
# NoKeywordsParameterNode (`**nil`) is skipped here -- it doesn't6181+
# carry a slot.6182+
kwrest = @nd_keyword_rest[params]6183+
if kwrest >= 06184+
if @nd_type[kwrest] == "KeywordRestParameterNode"6185+
if result != ""6186+
result = result + ","6187+
end6188+
kn = @nd_name[kwrest]6189+
if kn == ""6190+
kn = "__anon_kwrest"6191+
end6192+
result = result + kn6193+
end6194+
end61496195
# Block parameter (&block)61506196
blk = @nd_block[params]61516197
if blk >= 0@@ -6239,6 +6285,29 @@
62396285
result = result + "int_array"62406286
end62416287
end6288+
# Post-rest required params (`def f(*r, x, y)`).6289+
posts = parse_id_list(@nd_posts[params])6290+
k = 06291+
while k < posts.length6292+
if @nd_type[posts[k]] == "RequiredParameterNode"6293+
if result != ""6294+
result = result + ","6295+
end6296+
result = result + "int"6297+
end6298+
k = k + 16299+
end6300+
# Keyword rest (**kw). Spinel kwargs use symbol keys (matches6301+
# `f(a: 1)` keyword hash construction), so the slot is sym_poly_hash.6302+
kwrest = @nd_keyword_rest[params]6303+
if kwrest >= 06304+
if @nd_type[kwrest] == "KeywordRestParameterNode"6305+
if result != ""6306+
result = result + ","6307+
end6308+
result = result + "sym_poly_hash"6309+
end6310+
end62426311
# Block parameter (&block)62436312
blk = @nd_block[params]62446313
if blk >= 0@@ -6305,6 +6374,29 @@
63056374
result = result + "-1"63066375
end63076376
end6377+
# Post-rest required params (`def f(*r, x, y)`) — no defaults.6378+
posts = parse_id_list(@nd_posts[params])6379+
k = 06380+
while k < posts.length6381+
if @nd_type[posts[k]] == "RequiredParameterNode"6382+
if result != ""6383+
result = result + ","6384+
end6385+
result = result + "-1"6386+
end6387+
k = k + 16388+
end6389+
# Keyword rest (**kw): no compile-time default, slot stays NULL6390+
# until the caller provides a hash.6391+
kwrest = @nd_keyword_rest[params]6392+
if kwrest >= 06393+
if @nd_type[kwrest] == "KeywordRestParameterNode"6394+
if result != ""6395+
result = result + ","6396+
end6397+
result = result + "-1"6398+
end6399+
end63086400
# Block param63096401
blk = @nd_block[params]63106402
if blk >= 0spinel_parse.cmodifiedView file
Loading diff…
test/kwargs_param.rbaddedView file
Loading diff…
test/kwargs_param.rb.expectedaddedView file
Loading diff…