Commit afb3ee23
afb3ee23dd49Merged
Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(parse,codegen): support KeywordRestParameterNode declaration (def f(**kw))
Adds the kwrest slot to method signatures. The collect_pnames /
collect_ptypes / collect_defaults walkers now recognize **kw between
the rest and block slots and emit a sym_poly_hash typed parameter
(matches f(a: 1) keyword-hash construction at call sites). Anonymous
** synthesizes __anon_kwrest so future forwarding desugar
(def f(...)) can name the slot deterministically.
Also registers the @nd_keyword_rest and @nd_posts AST slots in the codegen's reader so the new fields the parser emits (folded into this commit) land in the right places.
Body-side methods on a kwrest local (kw[:k], kw.length, kw.key?) are deferred -- the existing sp_SymPolyHash_* runtime already supports them, but plumbing the call sites is its own task. This CL is the declaration-side floor; forwarding (next CL) leans on the slot without reading it.
Test exercises (test/kwargs_param.rb):
- def f(a, **kw); a + 1; end -- declaration compiles, body ignores kw
- def f(a, b, **nil) -- explicit kwarg rejection still works post-A1
Tests: 290 pass. Bootstrap: gen2.c == gen3.c.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- afb3ee23dd49
- Parent
- fea5369dddb0
spinel_codegen.rbmodifiedView file
@nd_constant_path = [] @nd_superclass = [] @nd_rest = [] # ParametersNode#keyword_rest -- holds a KeywordRestParameterNode # (def f(**kw)) or NoKeywordsParameterNode (def f(**nil)). @nd_keyword_rest = [] @nd_rescue_clause = [] @nd_ensure_clause = [] @nd_expression = [] @nd_exceptions = "".split(",") @nd_targets = "".split(",") @nd_rights = "".split(",") # ParametersNode#posts -- required params after the splat # (def f(*r, x, y) → posts = [x, y]). Currently unused by codegen # (post-rest parameters aren't observed in test/), but the parser # emits the field so future tests get a proper AST. @nd_posts = "".split(",") @nd_count = 0 @root_id = 0 @nd_constant_path.push(-1) @nd_superclass.push(-1) @nd_rest.push(-1) @nd_keyword_rest.push(-1) @nd_rescue_clause.push(-1) @nd_ensure_clause.push(-1) @nd_expression.push(-1) @nd_exceptions.push("") @nd_targets.push("") @nd_rights.push("") @nd_posts.push("") @nd_count = @nd_count + 1 nid end if field == "rest" @nd_rest[nid] = ref_id end if field == "keyword_rest" @nd_keyword_rest[nid] = ref_id end if field == "rescue_clause" @nd_rescue_clause[nid] = ref_id end if field == "rights" @nd_rights[nid] = ids_str end if field == "posts" @nd_posts[nid] = ids_str end end # ---- Convenience: get stmts of a body node ---- result = result + @nd_name[rest] end end # Post-rest required params: `def f(*r, x, y)` -> `x, y` come AFTER # rest in the AST's `posts` slot. Same shape as `requireds` (each # entry is a RequiredParameterNode); flow them straight through. posts = parse_id_list(@nd_posts[params]) k = 0 while k < posts.length if @nd_type[posts[k]] == "RequiredParameterNode" if result != "" result = result + "," end result = result + @nd_name[posts[k]] end k = k + 1 end # Keyword rest (`**kw`). Anonymous `**` synthesizes `__anon_kwrest`. # NoKeywordsParameterNode (`**nil`) is skipped here -- it doesn't # carry a slot. kwrest = @nd_keyword_rest[params] if kwrest >= 0 if @nd_type[kwrest] == "KeywordRestParameterNode" if result != "" result = result + "," end kn = @nd_name[kwrest] if kn == "" kn = "__anon_kwrest" end result = result + kn end end # Block parameter (&block) blk = @nd_block[params] if blk >= 0 result = result + "int_array" end end # Post-rest required params (`def f(*r, x, y)`). posts = parse_id_list(@nd_posts[params]) k = 0 while k < posts.length if @nd_type[posts[k]] == "RequiredParameterNode" if result != "" result = result + "," end result = result + "int" end k = k + 1 end # Keyword rest (**kw). Spinel kwargs use symbol keys (matches # `f(a: 1)` keyword hash construction), so the slot is sym_poly_hash. kwrest = @nd_keyword_rest[params] if kwrest >= 0 if @nd_type[kwrest] == "KeywordRestParameterNode" if result != "" result = result + "," end result = result + "sym_poly_hash" end end # Block parameter (&block) blk = @nd_block[params] if blk >= 0 result = result + "-1" end end # Post-rest required params (`def f(*r, x, y)`) — no defaults. posts = parse_id_list(@nd_posts[params]) k = 0 while k < posts.length if @nd_type[posts[k]] == "RequiredParameterNode" if result != "" result = result + "," end result = result + "-1" end k = k + 1 end # Keyword rest (**kw): no compile-time default, slot stays NULL # until the caller provides a hash. kwrest = @nd_keyword_rest[params] if kwrest >= 0 if @nd_type[kwrest] == "KeywordRestParameterNode" if result != "" result = result + "," end result = result + "-1" end end # Block param blk = @nd_block[params] if blk >= 0spinel_parse.cmodifiedView file
Loading diff…
test/kwargs_param.rbaddedView file
Loading diff…
test/kwargs_param.rb.expectedaddedView file
Loading diff…