Commit e571fbdb
e571fbdbbf44Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 4 files · ~4 modified
Description
Add M14: optional/default parameters
- compiler/analyze: Scope tracks per-param default node (pdefault) and nrequired; the DefNode walk collects ParametersNode "optionals" (OptionalParameterNode name+value) after the requireds. Optional param types are inferred from both the default value (infer_default_param_types, nil defaults excluded) and call-site args.
- codegen: emit_arg_or_default / emit_args_filled emit a call's args, filling omitted optional params with their defaults (a nil default -> the param type's default value). Wired through free-function calls, virtual dispatch (defaults evaluated into the per-call temps), and Class.new (initialize defaults).
Free functions and initialize with one or more optional params, called
with varying arity, match CRuby. make test: 16 (no regressions; broad
foundational capability).
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- e571fbdbbf44
- Parent
- 4ab5f574821d
src/analyze.cmodifiedView file
@@ -331,10 +331,14 @@
331331
332332
/* ---- scope assignment ---- */333333
334-
static void scope_add_param(Scope *s, const char *name) {335-
if (s->nparams % 8 == 0)334+
static void scope_add_param(Scope *s, const char *name, int defnode) {335+
if (s->nparams % 8 == 0) {336336
s->pnames = realloc(s->pnames, sizeof(char *) * (size_t)(s->nparams + 8));337+
s->pdefault = realloc(s->pdefault, sizeof(int) * (size_t)(s->nparams + 8));338+
}339+
s->pdefault[s->nparams] = defnode;337340
s->pnames[s->nparams++] = strdup(name);341+
if (defnode < 0) s->nrequired = s->nparams;338342
LocalVar *lv = scope_local_intern(s, name);339343
lv->is_param = 1;340344
}@@ -367,8 +371,15 @@
367371
const int *reqs = nt_arr(c->nt, pn, "requireds", &rn);368372
for (int i = 0; i < rn; i++) {369373
const char *pname = nt_str(c->nt, reqs[i], "name");370-
if (pname) scope_add_param(s, pname);374+
if (pname) scope_add_param(s, pname, -1);371375
}376+
int on = 0;377+
const int *opts = nt_arr(c->nt, pn, "optionals", &on);378+
for (int i = 0; i < on; i++) {379+
const char *pname = nt_str(c->nt, opts[i], "name");380+
int dv = nt_ref(c->nt, opts[i], "value");381+
if (pname) scope_add_param(s, pname, dv);382+
}372383
}373384
child = new_idx;374385
}@@ -629,6 +640,24 @@
629640
return changed;630641
}631642
643+
/* Optional parameters get a type from their default value too. */644+
static int infer_default_param_types(Compiler *c) {645+
int changed = 0;646+
for (int s = 0; s < c->nscopes; s++) {647+
Scope *sc = &c->scopes[s];648+
for (int i = 0; i < sc->nparams; i++) {649+
if (sc->pdefault[i] < 0) continue;650+
TyKind dt = infer_type(c, sc->pdefault[i]);651+
if (dt == TY_NIL) continue; /* nil default doesn't pin the type */652+
LocalVar *p = scope_local(sc, sc->pnames[i]);653+
if (!p) continue;654+
TyKind merged = ty_unify(p->type, dt);655+
if (merged != p->type) { p->type = merged; changed = 1; }656+
}657+
}658+
return changed;659+
}660+
632661
static int infer_param_types(Compiler *c) {633662
const NodeTable *nt = c->nt;634663
int changed = 0;@@ -788,6 +817,7 @@
788817
int ch = 0;789818
ch |= infer_write_types(c);790819
ch |= infer_param_types(c);820+
ch |= infer_default_param_types(c);791821
ch |= infer_block_params(c);792822
ch |= infer_ivar_types(c);793823
ch |= infer_inherited_ivars(c);src/codegen.cmodifiedView file
Loading diff…
src/compiler.cmodifiedView file
Loading diff…
src/compiler.hmodifiedView file
Loading diff…