feat(lang): L5 generic records and functions
property Pool<T> { ... }, function first<T>(xs: []T) -> T, map<T, U> over fn
types; a type writes an instance as Pool<Thing>, nested as deep as needed. The
parser names an instance Pool$Thing and remembers its generic and arguments; the
checker takes the generic declarations out, infers a call's type arguments from
its arguments or its result's declared slot, and makes each instance once as an
ordinary record or function, checked like any other. Errors print Pool<Thing>.
An instance keeps its generic's module and export (L3). ludic-fmt keeps type
arguments together while spacing comparisons and shifts.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
57b66bdf47
commit
6a24b14f0e
26 changed files with 61796 additions and 51949 deletions
|
|
@ -48,6 +48,7 @@ function ck_args(e: Node, name: pointer, labels: []pointer, tys: []pointer) -> v
|
|||
if n != len(tys) { ck_err("arity", e, `{name} takes {itoa(len(tys))} argument(s) and this call gives {itoa(n)}`) }
|
||||
var i = 0
|
||||
while i < n {
|
||||
if i < len(tys) { ck_expect = tys[i] }
|
||||
let at = ck_expr(e.kids[i])
|
||||
if i < len(tys) { ck_give(tys[i], at, e.kids[i], `argument {itoa(i + 1)} of {name}`) }
|
||||
i += 1
|
||||
|
|
@ -126,6 +127,8 @@ function ck_call_named(e: Node, name: pointer) -> pointer {
|
|||
if bt != null { return bt }
|
||||
let f = ck_fn(name)
|
||||
if f != null { return ck_call_fn(e, name, f) }
|
||||
let gt = gen_template(g_gen_fns, name)
|
||||
if gt != null { return gen_call(e, name, gt) }
|
||||
let x = ck_extern(name)
|
||||
if x != null { return ck_call_fn(e, name, x) }
|
||||
let g = ck_global(name)
|
||||
|
|
|
|||
|
|
@ -29,6 +29,8 @@ function ck_any(n: Node) -> void {
|
|||
}
|
||||
function ck_expr(e: Node) -> pointer {
|
||||
if e == null { return "?" }
|
||||
let ex = ck_expect
|
||||
ck_expect = null
|
||||
let k = e.kind
|
||||
if k == E_INT { return "int" }
|
||||
if k == E_FLOAT {
|
||||
|
|
@ -51,7 +53,10 @@ function ck_expr(e: Node) -> pointer {
|
|||
if k == E_UN { return ck_un(e) }
|
||||
if k == E_MEMBER { return ck_member(e) }
|
||||
if k == E_INDEX { return ck_index_of(e) }
|
||||
if k == E_CALL { return ck_call(e) }
|
||||
if k == E_CALL {
|
||||
ck_call_expect = ex
|
||||
return ck_call(e)
|
||||
}
|
||||
if k == S_EMIT { ck_emit(e); return "?" }
|
||||
ck_walk(e)
|
||||
return "?"
|
||||
|
|
|
|||
173
selfhost/check/check_gen.ludic
Normal file
173
selfhost/check/check_gen.ludic
Normal file
|
|
@ -0,0 +1,173 @@
|
|||
# check_gen.ludic — L5: generic records and functions, made real. A generic declaration is taken
|
||||
# out of the program before anything is checked; each instance the program names is a copy of it
|
||||
# with its type parameters replaced, checked like any other declaration and put back before the
|
||||
# emitter runs - so the emitter only ever sees `Pool$Thing` and `first$int`, never a `T`.
|
||||
#
|
||||
# A record's instances are every `Pool<...>` the program writes. A function's are found at its
|
||||
# calls: the types of the arguments decide its parameters, and where they cannot (a function
|
||||
# that takes no T), the slot the call is written into does - `let p: Pool<Thing> = pool_new()`.
|
||||
var g_gen_recs: []Node = new []Node # the generic records, as written
|
||||
var g_gen_fns: []Node = new []Node # the generic functions, as written
|
||||
var g_gen_out: []Node = new []Node # the instances made, in order: spliced into prog
|
||||
var g_gen_work: []Node = new []Node # function instances whose bodies still need checking
|
||||
var ck_expect: pointer = null # the type the expression being checked is written into
|
||||
var ck_call_expect: pointer = null # ... as it stood when the call now being checked began
|
||||
|
||||
function gen_template(list: []Node, name: pointer) -> Node {
|
||||
var i = 0
|
||||
while i < len(list) {
|
||||
if (list[i].s == name) { return list[i] }
|
||||
i += 1
|
||||
}
|
||||
return null
|
||||
}
|
||||
# the generic declarations leave the program; what the emitter meets is only ever an instance
|
||||
function gen_collect() -> void {
|
||||
let keep = new []Node
|
||||
var gone_before = 0
|
||||
var i = 0
|
||||
while i < len(prog) {
|
||||
let d = prog[i]
|
||||
if (d.kind == N_COMP or d.kind == N_FN) and d.tps != null {
|
||||
if d.kind == N_COMP { push(g_gen_recs, d) } else { push(g_gen_fns, d) }
|
||||
if i < g_prog_user_end { gone_before += 1 }
|
||||
} else {
|
||||
push(keep, d)
|
||||
}
|
||||
i += 1
|
||||
}
|
||||
prog = keep
|
||||
g_prog_user_end = g_prog_user_end - gone_before
|
||||
}
|
||||
function gen_index(xs: []pointer, s: pointer) -> int {
|
||||
var i = 0
|
||||
while i < len(xs) {
|
||||
if (xs[i] == s) { return i }
|
||||
i += 1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
# a type with the parameters replaced by the arguments: T, []T, fn(T)->T, Pool<T>
|
||||
function gen_subst(t: pointer, tps: []pointer, args: []pointer) -> pointer {
|
||||
if t == null { return null }
|
||||
let at = gen_index(tps, t)
|
||||
if at >= 0 { return args[at] }
|
||||
if is_slice_ty(t) { return "[]" + gen_subst(slice_elem(t), tps, args) }
|
||||
if is_fn_type(t) {
|
||||
let ps = fn_ty_params(t)
|
||||
var out: pointer = "fn("
|
||||
var i = 0
|
||||
while i < len(ps) {
|
||||
if i > 0 { out = out + "," }
|
||||
out = out + gen_subst(ps[i], tps, args)
|
||||
i += 1
|
||||
}
|
||||
return out + ")->" + gen_subst(fn_ty_ret(t), tps, args)
|
||||
}
|
||||
let gi = gen_find(t)
|
||||
if gi >= 0 and g_gi_open[gi] == 1 {
|
||||
let inner = gen_split(g_gi_args[gi])
|
||||
let now = new []pointer
|
||||
var j = 0
|
||||
while j < len(inner) {
|
||||
push(now, gen_subst(inner[j], tps, args))
|
||||
j += 1
|
||||
}
|
||||
return gen_instance(g_gi_gen[gi], now)
|
||||
}
|
||||
return t
|
||||
}
|
||||
function gen_clone(n: Node, tps: []pointer, args: []pointer) -> Node {
|
||||
if n == null { return null }
|
||||
let c = new Node
|
||||
c.kind = n.kind
|
||||
c.s = n.s
|
||||
if n.kind == E_NEW { c.s = gen_subst(n.s, tps, args) }
|
||||
c.ival = n.ival
|
||||
c.ty = gen_subst(n.ty, tps, args)
|
||||
c.line = n.line
|
||||
c.file = n.file
|
||||
c.vis = n.vis
|
||||
c.a = gen_clone(n.a, tps, args)
|
||||
c.b = gen_clone(n.b, tps, args)
|
||||
c.c = gen_clone(n.c, tps, args)
|
||||
c.kids = new []Node
|
||||
var i = 0
|
||||
while i < len(n.kids) {
|
||||
push(c.kids, gen_clone(n.kids[i], tps, args))
|
||||
i += 1
|
||||
}
|
||||
return c
|
||||
}
|
||||
# the instance record a name stands for, made the first time it is asked for
|
||||
function gen_record(name: pointer) -> Node {
|
||||
let gi = gen_find(name)
|
||||
if gi < 0 or g_gi_open[gi] == 1 { return null }
|
||||
let t = gen_template(g_gen_recs, g_gi_gen[gi])
|
||||
if t == null { return null }
|
||||
let args = gen_split(g_gi_args[gi])
|
||||
let tps = gen_split(t.tps)
|
||||
if len(args) != len(tps) {
|
||||
let at = new Node
|
||||
at.file = g_gi_file[gi]
|
||||
at.line = g_gi_line[gi]
|
||||
ck_err("generic", at, `{t.s} takes {itoa(len(tps))} type argument(s) and {gen_show(name)} gives {itoa(len(args))}`)
|
||||
return null
|
||||
}
|
||||
let r = gen_clone(t, tps, args)
|
||||
r.s = name
|
||||
push(g_gen_out, r)
|
||||
ck_tab_put(ck_rec_k, ck_rec_v, name, r)
|
||||
return r
|
||||
}
|
||||
# what a template's parameter types learn from the argument types
|
||||
function gen_unify(p: pointer, a: pointer, tps: []pointer, binds: []pointer) -> void {
|
||||
if p == null or ck_unknown(a) or (a == "null") { return }
|
||||
let at = gen_index(tps, p)
|
||||
if at >= 0 {
|
||||
if binds[at] == null { binds[at] = a }
|
||||
return
|
||||
}
|
||||
if is_slice_ty(p) and is_slice_ty(a) {
|
||||
gen_unify(slice_elem(p), slice_elem(a), tps, binds)
|
||||
return
|
||||
}
|
||||
if is_fn_type(p) and is_fn_type(a) {
|
||||
let pp = fn_ty_params(p)
|
||||
let ap = fn_ty_params(a)
|
||||
var i = 0
|
||||
while i < len(pp) and i < len(ap) {
|
||||
gen_unify(pp[i], ap[i], tps, binds)
|
||||
i += 1
|
||||
}
|
||||
gen_unify(fn_ty_ret(p), fn_ty_ret(a), tps, binds)
|
||||
return
|
||||
}
|
||||
let pi2 = gen_find(p)
|
||||
let ai = gen_find(a)
|
||||
if pi2 >= 0 and ai >= 0 and (g_gi_gen[pi2] == g_gi_gen[ai]) {
|
||||
let pa = gen_split(g_gi_args[pi2])
|
||||
let aa = gen_split(g_gi_args[ai])
|
||||
var j = 0
|
||||
while j < len(pa) and j < len(aa) {
|
||||
gen_unify(pa[j], aa[j], tps, binds)
|
||||
j += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
# Pool$Thing as the program wrote it: Pool<Thing>
|
||||
function gen_show(t: pointer) -> pointer {
|
||||
if t == null { return "?" }
|
||||
if is_slice_ty(t) { return "[]" + gen_show(slice_elem(t)) }
|
||||
let gi = gen_find(t)
|
||||
if gi < 0 { return t }
|
||||
let args = gen_split(g_gi_args[gi])
|
||||
var out: pointer = g_gi_gen[gi] + "<"
|
||||
var i = 0
|
||||
while i < len(args) {
|
||||
if i > 0 { out = out + ", " }
|
||||
out = out + gen_show(args[i])
|
||||
i += 1
|
||||
}
|
||||
return out + ">"
|
||||
}
|
||||
132
selfhost/check/check_gen_call.ludic
Normal file
132
selfhost/check/check_gen_call.ludic
Normal file
|
|
@ -0,0 +1,132 @@
|
|||
# check_gen_call.ludic — L5: a call to a generic function. Its arguments' types bind the type
|
||||
# parameters (a literal only binds what nothing else did, so first(xs, 0) is decided by xs), the
|
||||
# slot the call is written into binds what they cannot, and the call is rewritten to name the
|
||||
# instance: `first(xs)` on a []int calls first$int, which is made and checked once however many
|
||||
# calls reach it.
|
||||
function gen_instance_name(name: pointer, binds: []pointer) -> pointer {
|
||||
var out: pointer = name
|
||||
var i = 0
|
||||
while i < len(binds) {
|
||||
out = out + "$" + gen_mangle(binds[i])
|
||||
i += 1
|
||||
}
|
||||
return out
|
||||
}
|
||||
function gen_fn_instance(t: Node, iname: pointer, binds: []pointer) -> Node {
|
||||
let have = ck_fn(iname)
|
||||
if have != null { return have }
|
||||
let f = gen_clone(t, gen_split(t.tps), binds)
|
||||
f.s = iname
|
||||
push(g_gen_out, f)
|
||||
push(g_gen_work, f)
|
||||
ck_tab_put(ck_fn_k, ck_fn_v, iname, f)
|
||||
return f
|
||||
}
|
||||
# each parameter's argument node, positional or named
|
||||
function gen_arg_nodes(e: Node, labels: []pointer) -> []Node {
|
||||
let out = new []Node
|
||||
var i = 0
|
||||
while i < len(labels) {
|
||||
push(out, null)
|
||||
i += 1
|
||||
}
|
||||
var j = 0
|
||||
while j < len(e.kids) {
|
||||
let k = e.kids[j]
|
||||
if k.kind == E_FINIT {
|
||||
let at = gen_index(labels, k.s)
|
||||
if at >= 0 { out[at] = k.a }
|
||||
} else {
|
||||
if j < len(out) { out[j] = k }
|
||||
}
|
||||
j += 1
|
||||
}
|
||||
return out
|
||||
}
|
||||
function gen_call(e: Node, name: pointer, t: Node) -> pointer {
|
||||
let expect = ck_call_expect
|
||||
let tps = gen_split(t.tps)
|
||||
let labels = new []pointer
|
||||
let ptys = new []pointer
|
||||
ck_params(t, labels, ptys)
|
||||
if len(e.kids) != len(ptys) { ck_err("arity", e, `{name} takes {itoa(len(ptys))} argument(s) and this call gives {itoa(len(e.kids))}`) }
|
||||
let nodes = gen_arg_nodes(e, labels)
|
||||
let atys = new []pointer
|
||||
let binds = new []pointer
|
||||
var i = 0
|
||||
while i < len(tps) {
|
||||
push(binds, null)
|
||||
i += 1
|
||||
}
|
||||
i = 0
|
||||
while i < len(nodes) {
|
||||
var at: pointer = "?"
|
||||
if nodes[i] != null { at = ck_expr(nodes[i]) }
|
||||
push(atys, at)
|
||||
if nodes[i] != null and not ck_is_lit(nodes[i]) { gen_unify(ptys[i], at, tps, binds) }
|
||||
i += 1
|
||||
}
|
||||
gen_unify(t.ty, expect, tps, binds)
|
||||
i = 0
|
||||
while i < len(nodes) {
|
||||
if nodes[i] != null and ck_is_lit(nodes[i]) { gen_unify(ptys[i], atys[i], tps, binds) }
|
||||
i += 1
|
||||
}
|
||||
var k = 0
|
||||
while k < len(tps) {
|
||||
if binds[k] == null {
|
||||
ck_err("generic", e, `cannot tell what {tps[k]} is in this call to {name}: pass it an argument that says, or write the call where its result has a declared type`)
|
||||
return "?"
|
||||
}
|
||||
k += 1
|
||||
}
|
||||
let f = gen_fn_instance(t, gen_instance_name(name, binds), binds)
|
||||
e.a.s = f.s
|
||||
let fl = new []pointer
|
||||
let fp = new []pointer
|
||||
ck_params(f, fl, fp)
|
||||
var j = 0
|
||||
while j < len(nodes) {
|
||||
if nodes[j] != null { ck_give(fp[j], atys[j], nodes[j], `argument {itoa(j + 1)} of {name}`) }
|
||||
j += 1
|
||||
}
|
||||
return f.ty
|
||||
}
|
||||
# every instance a generic body asks for is made and checked in turn; then the records the
|
||||
# program only ever names in a type, and the instances go back in where the program's own
|
||||
# declarations end
|
||||
function gen_finish() -> void {
|
||||
var w = 0
|
||||
while w < len(g_gen_work) {
|
||||
ck_fn_body(g_gen_work[w])
|
||||
w += 1
|
||||
}
|
||||
var r = 0
|
||||
while r < len(g_gi_name) {
|
||||
if g_gi_open[r] == 0 and gen_template(g_gen_recs, g_gi_gen[r]) != null { ck_record(g_gi_name[r]) }
|
||||
r += 1
|
||||
}
|
||||
if len(g_gen_out) == 0 { return }
|
||||
let np = new []Node
|
||||
var i = 0
|
||||
while i < len(prog) {
|
||||
if i == g_prog_user_end {
|
||||
var o = 0
|
||||
while o < len(g_gen_out) {
|
||||
push(np, g_gen_out[o])
|
||||
o += 1
|
||||
}
|
||||
}
|
||||
push(np, prog[i])
|
||||
i += 1
|
||||
}
|
||||
if g_prog_user_end >= len(prog) {
|
||||
var o2 = 0
|
||||
while o2 < len(g_gen_out) {
|
||||
push(np, g_gen_out[o2])
|
||||
o2 += 1
|
||||
}
|
||||
}
|
||||
prog = np
|
||||
g_prog_user_end += len(g_gen_out)
|
||||
}
|
||||
|
|
@ -69,6 +69,10 @@ function ck_index() -> void {
|
|||
}
|
||||
function ck_fn(name: pointer) -> Node { return ck_tab_get(ck_fn_k, ck_fn_v, name) }
|
||||
function ck_global(name: pointer) -> Node { return ck_tab_get(ck_gl_k, ck_gl_v, name) }
|
||||
function ck_record(name: pointer) -> Node { return ck_tab_get(ck_rec_k, ck_rec_v, name) }
|
||||
function ck_record(name: pointer) -> Node {
|
||||
let r = ck_tab_get(ck_rec_k, ck_rec_v, name)
|
||||
if r != null { return r }
|
||||
return gen_record(name)
|
||||
}
|
||||
function ck_enum(name: pointer) -> Node { return ck_tab_get(ck_en_k, ck_en_v, name) }
|
||||
function ck_extern(name: pointer) -> Node { return ck_tab_get(ck_ex_k, ck_ex_v, name) }
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ function ck_target(t: Node) -> pointer {
|
|||
}
|
||||
function ck_let(s: Node) -> void {
|
||||
var t: pointer = "?"
|
||||
ck_expect = s.ty
|
||||
if s.a != null { t = ck_expr(s.a) }
|
||||
if s.ty != null {
|
||||
if s.a != null { ck_give(s.ty, t, s.a, s.s) }
|
||||
|
|
@ -33,6 +34,7 @@ function ck_let(s: Node) -> void {
|
|||
}
|
||||
function ck_assign(s: Node) -> void {
|
||||
let lt = ck_target(s.a)
|
||||
if (s.s == "=") { ck_expect = lt }
|
||||
let rt = ck_expr(s.b)
|
||||
var what: pointer = "this assignment"
|
||||
if s.a.kind == E_ID { what = s.a.s }
|
||||
|
|
@ -97,6 +99,7 @@ function ck_stmt(s: Node) -> void {
|
|||
}
|
||||
if k == S_RETURN {
|
||||
if s.a == null { return }
|
||||
ck_expect = ck_ret
|
||||
let t = ck_expr(s.a)
|
||||
if (ck_ret != "void") { ck_give(ck_ret, t, s.a, "the result") }
|
||||
return
|
||||
|
|
@ -132,10 +135,9 @@ function ck_listener(l: Node) -> void {
|
|||
ck_block(l.a)
|
||||
ck_pop(m)
|
||||
}
|
||||
# LUDIC_CHECK=0 turns the pass off, for bisecting a checker fault; nothing else should need it
|
||||
# the pass also makes the generics real (check_gen.ludic), so it always runs
|
||||
function check_program() -> void {
|
||||
let off = getenv("LUDIC_CHECK")
|
||||
if off != null and (off == "0") { return }
|
||||
gen_collect()
|
||||
ck_index()
|
||||
var i = 0
|
||||
while i < g_prog_user_end {
|
||||
|
|
@ -150,6 +152,7 @@ function check_program() -> void {
|
|||
ck_listener(g_onlisten[j])
|
||||
j += 1
|
||||
}
|
||||
gen_finish()
|
||||
if ck_errors > 0 and not ck_reporting() {
|
||||
let m = `{itoa(ck_errors)} type error(s)\n`
|
||||
file_write(file_stderr(), m, len(m))
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@ function ck_is_int(t: pointer) -> bool {
|
|||
return false
|
||||
}
|
||||
# "an int", "a float": the article a message puts before a type
|
||||
function ck_a(t: pointer) -> pointer {
|
||||
function ck_a(t0: pointer) -> pointer {
|
||||
let t = gen_show(t0)
|
||||
let c = t[0]
|
||||
if c == 'a' or c == 'e' or c == 'i' or c == 'o' or c == 'u' or c == 'A' or c == 'E' or c == 'I' or c == 'O' or c == 'U' { return `an {t}` }
|
||||
return `a {t}`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue