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 KeywordRestParameterNode
72+ # (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 splat
96+ # (def f(*r, x, y) → posts = [x, y]). Currently unused by codegen
97+ # (post-rest parameters aren't observed in test/), but the parser
98+ # emits the field so future tests get a proper AST.
99+ @nd_posts = "".split(",")
92100
93101 @nd_count = 0
94102 @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 + 1
444454 nid
445455 end
@@ -663,6 +673,9 @@
663673 if field == "rest"
664674 @nd_rest[nid] = ref_id
665675 end
676+ if field == "keyword_rest"
677+ @nd_keyword_rest[nid] = ref_id
678+ end
666679 if field == "rescue_clause"
667680 @nd_rescue_clause[nid] = ref_id
668681 end
@@ -744,6 +757,9 @@
744757 if field == "rights"
745758 @nd_rights[nid] = ids_str
746759 end
760+ if field == "posts"
761+ @nd_posts[nid] = ids_str
762+ end
747763 end
748764
749765 # ---- Convenience: get stmts of a body node ----
@@ -6146,6 +6162,36 @@
61466162 result = result + @nd_name[rest]
61476163 end
61486164 end
6165+ # Post-rest required params: `def f(*r, x, y)` -> `x, y` come AFTER
6166+ # rest in the AST's `posts` slot. Same shape as `requireds` (each
6167+ # entry is a RequiredParameterNode); flow them straight through.
6168+ posts = parse_id_list(@nd_posts[params])
6169+ k = 0
6170+ while k < posts.length
6171+ if @nd_type[posts[k]] == "RequiredParameterNode"
6172+ if result != ""
6173+ result = result + ","
6174+ end
6175+ result = result + @nd_name[posts[k]]
6176+ end
6177+ k = k + 1
6178+ end
6179+ # Keyword rest (`**kw`). Anonymous `**` synthesizes `__anon_kwrest`.
6180+ # NoKeywordsParameterNode (`**nil`) is skipped here -- it doesn't
6181+ # carry a slot.
6182+ kwrest = @nd_keyword_rest[params]
6183+ if kwrest >= 0
6184+ if @nd_type[kwrest] == "KeywordRestParameterNode"
6185+ if result != ""
6186+ result = result + ","
6187+ end
6188+ kn = @nd_name[kwrest]
6189+ if kn == ""
6190+ kn = "__anon_kwrest"
6191+ end
6192+ result = result + kn
6193+ end
6194+ end
61496195 # Block parameter (&block)
61506196 blk = @nd_block[params]
61516197 if blk >= 0
@@ -6239,6 +6285,29 @@
62396285 result = result + "int_array"
62406286 end
62416287 end
6288+ # Post-rest required params (`def f(*r, x, y)`).
6289+ posts = parse_id_list(@nd_posts[params])
6290+ k = 0
6291+ while k < posts.length
6292+ if @nd_type[posts[k]] == "RequiredParameterNode"
6293+ if result != ""
6294+ result = result + ","
6295+ end
6296+ result = result + "int"
6297+ end
6298+ k = k + 1
6299+ end
6300+ # Keyword rest (**kw). Spinel kwargs use symbol keys (matches
6301+ # `f(a: 1)` keyword hash construction), so the slot is sym_poly_hash.
6302+ kwrest = @nd_keyword_rest[params]
6303+ if kwrest >= 0
6304+ if @nd_type[kwrest] == "KeywordRestParameterNode"
6305+ if result != ""
6306+ result = result + ","
6307+ end
6308+ result = result + "sym_poly_hash"
6309+ end
6310+ end
62426311 # Block parameter (&block)
62436312 blk = @nd_block[params]
62446313 if blk >= 0
@@ -6305,6 +6374,29 @@
63056374 result = result + "-1"
63066375 end
63076376 end
6377+ # Post-rest required params (`def f(*r, x, y)`) — no defaults.
6378+ posts = parse_id_list(@nd_posts[params])
6379+ k = 0
6380+ while k < posts.length
6381+ if @nd_type[posts[k]] == "RequiredParameterNode"
6382+ if result != ""
6383+ result = result + ","
6384+ end
6385+ result = result + "-1"
6386+ end
6387+ k = k + 1
6388+ end
6389+ # Keyword rest (**kw): no compile-time default, slot stays NULL
6390+ # until the caller provides a hash.
6391+ kwrest = @nd_keyword_rest[params]
6392+ if kwrest >= 0
6393+ if @nd_type[kwrest] == "KeywordRestParameterNode"
6394+ if result != ""
6395+ result = result + ","
6396+ end
6397+ result = result + "-1"
6398+ end
6399+ end
63086400 # Block param
63096401 blk = @nd_block[params]
63106402 if blk >= 0