feat(stdlib): 2D Vector type + Vector.* namespace (issue #25)
Some checks failed
docs / build-and-deploy (push) Has been cancelled

Implement the Vec.* half of #25 under the proper (de-abbreviated) name
Vector, unblocking it with a self-contained value type instead of waiting
on the full #1 type system.

A Vector is two Q16.16 fixed components (x, y) packed into one i64 — a true
by-value type that lives in a register and never allocates (reuses the new
`long`/i64 support; llty maps `Vector` to i64). Fifteen operations, all
deterministic fixed-point reusing fx_mul/fx_div/fx_lerp and the @fn_fx_*
prelude: make/zero/x/y, add/sub/scale/dot, length/distance/normalize/lerp,
rotate/angle/from_angle.

New selfhost/emit_vector.ludic (wired into emit_ns_call + the frag list),
the `Vector` primitive type in llty and the grammars/LSP/JetBrains tokens,
docs (type-vector + 15 Vector.* pages + section), and a registered test.
Reseeded; C-free fixpoint holds; all suites green (45/25/29); site + check.py OK.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-08-30 02:09:38 +03:00
parent 2e0047514b
commit 121053e179
30 changed files with 13740 additions and 11881 deletions

View file

@ -73,6 +73,7 @@ function lbl(pfx: pointer) -> pointer { let r = (pfx + itoa(ll_lbl)); ll_lbl =
function llty(t: pointer) -> pointer {
if (t == "int") or (t == "bool") or (t == "fixed") or (t == "entity") { return "i32" } # entity = an i32 handle (self())
if (t == "long") { return "i64" } # a 64-bit signed integer
if (t == "Vector") { return "i64" } # a 2D vector: (x, y) fixeds packed into one i64
if (t == "byte") { return "i8" } # a single byte (p[i] on a raw ptr)
if (t == "words") or (t == "fixeds") or (t == "pointers") { return "ptr" } # typed buffers
if (t == "void") { return "void" }

View file

@ -231,6 +231,10 @@ function emit_ns_call(ns: pointer, meth: pointer, e: Node) -> Val {
if is_hash_ns(meth) { return emit_hash_ns(meth, e) }
perr(`unknown builtin Hash.{meth}`)
}
if (ns == "Vector") {
if is_vector_ns(meth) { return emit_vector_ns(meth, e) }
perr(`unknown builtin Vector.{meth}`)
}
var bare: pointer = null
let labels = new []pointer
if (ns == "Screen") {

146
selfhost/emit_vector.ludic Normal file
View file

@ -0,0 +1,146 @@
# emit_vector.ludic — the Vector.* namespace: a 2D vector value type. A Vector is
# a pair of Q16.16 fixed components (x, y) packed into a single i64 — x in the
# high 32 bits, y in the low 32 — so it is a true by-value type (assignment
# copies, no heap allocation) that lives in one register. All arithmetic is the
# same deterministic integer fixed-point the rest of the runtime uses, reusing
# fx_mul_code / fx_div_code / fx_lerp_code and the @fn_fx_* prelude. llty maps the
# `Vector` type to i64 (see emit_core.ludic).
#
# NOTE: helper results are bound to a `let` before interpolation — a function
# call inside a backtick `{...}` hole would nest backticks and break.
# pack two fixed i32 codes (x, y) into the i64 Vector representation -> i64 code
function vec_pack(x: pointer, y: pointer) -> pointer {
let xe = emit_bind(`sext i32 {x} to i64`)
let xs = emit_bind(`shl i64 {xe}, 32`)
let ye = emit_bind(`zext i32 {y} to i64`)
return emit_bind(`or i64 {xs}, {ye}`)
}
# the x component (high 32 bits) of an i64 Vector code -> i32 fixed code
function vec_x(v: pointer) -> pointer {
let s = emit_bind(`lshr i64 {v}, 32`)
return emit_bind(`trunc i64 {s} to i32`)
}
# the y component (low 32 bits) of an i64 Vector code -> i32 fixed code
function vec_y(v: pointer) -> pointer {
return emit_bind(`trunc i64 {v} to i32`)
}
function is_vector_ns(meth: pointer) -> bool {
if (meth == "make") or (meth == "zero") or (meth == "x") or (meth == "y") { return true }
if (meth == "add") or (meth == "sub") or (meth == "scale") or (meth == "dot") { return true }
if (meth == "length") or (meth == "distance") or (meth == "normalize") or (meth == "lerp") { return true }
if (meth == "rotate") or (meth == "angle") or (meth == "from_angle") { return true }
return false
}
function emit_vector_ns(meth: pointer, e: Node) -> Val {
if (meth == "zero") { # the origin, (0, 0)
return val("0", "Vector")
}
if (meth == "make") { # make(x, y: fixed) -> Vector
let x = emit_expr(e.kids[0]); let y = emit_expr(e.kids[1])
return val(vec_pack(x.code, y.code), "Vector")
}
if (meth == "x") { # the x component -> fixed
let v = emit_expr(e.kids[0])
return val(vec_x(v.code), "fixed")
}
if (meth == "y") { # the y component -> fixed
let v = emit_expr(e.kids[0])
return val(vec_y(v.code), "fixed")
}
if (meth == "add") { # component-wise a + b
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1])
let ax = vec_x(a.code); let ay = vec_y(a.code); let bx = vec_x(b.code); let by = vec_y(b.code)
let sx = emit_bind(`add i32 {ax}, {bx}`)
let sy = emit_bind(`add i32 {ay}, {by}`)
return val(vec_pack(sx, sy), "Vector")
}
if (meth == "sub") { # component-wise a - b
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1])
let ax = vec_x(a.code); let ay = vec_y(a.code); let bx = vec_x(b.code); let by = vec_y(b.code)
let sx = emit_bind(`sub i32 {ax}, {bx}`)
let sy = emit_bind(`sub i32 {ay}, {by}`)
return val(vec_pack(sx, sy), "Vector")
}
if (meth == "scale") { # v * s (s: fixed)
let v = emit_expr(e.kids[0]); let s = emit_expr(e.kids[1])
let vx = vec_x(v.code); let vy = vec_y(v.code)
let sx = fx_mul_code(vx, s.code)
let sy = fx_mul_code(vy, s.code)
return val(vec_pack(sx, sy), "Vector")
}
if (meth == "dot") { # ax*bx + ay*by -> fixed
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1])
let ax = vec_x(a.code); let ay = vec_y(a.code); let bx = vec_x(b.code); let by = vec_y(b.code)
let px = fx_mul_code(ax, bx); let py = fx_mul_code(ay, by)
return val(emit_bind(`add i32 {px}, {py}`), "fixed")
}
if (meth == "length") { # sqrt(x*x + y*y) -> fixed
g_uses_mathrt = true
let v = emit_expr(e.kids[0])
let vx = vec_x(v.code); let vy = vec_y(v.code)
let xx = fx_mul_code(vx, vx); let yy = fx_mul_code(vy, vy)
let s = emit_bind(`add i32 {xx}, {yy}`)
return val(emit_bind(`call i32 @fn_fx_sqrt(i32 {s})`), "fixed")
}
if (meth == "distance") { # length(a - b) -> fixed
g_uses_mathrt = true
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1])
let ax = vec_x(a.code); let ay = vec_y(a.code); let bx = vec_x(b.code); let by = vec_y(b.code)
let dx = emit_bind(`sub i32 {ax}, {bx}`)
let dy = emit_bind(`sub i32 {ay}, {by}`)
let xx = fx_mul_code(dx, dx); let yy = fx_mul_code(dy, dy)
let s = emit_bind(`add i32 {xx}, {yy}`)
return val(emit_bind(`call i32 @fn_fx_sqrt(i32 {s})`), "fixed")
}
if (meth == "normalize") { # v / length(v); the zero vector maps to itself
g_uses_mathrt = true
let v = emit_expr(e.kids[0])
let vx = vec_x(v.code); let vy = vec_y(v.code)
let xx = fx_mul_code(vx, vx); let yy = fx_mul_code(vy, vy)
let s = emit_bind(`add i32 {xx}, {yy}`)
let len = emit_bind(`call i32 @fn_fx_sqrt(i32 {s})`)
let zero = emit_bind(`icmp eq i32 {len}, 0`)
let denom = emit_bind(`select i1 {zero}, i32 65536, i32 {len}`) # avoid divide-by-zero
let inv = fx_div_code("65536", denom)
let nx = fx_mul_code(vx, inv); let ny = fx_mul_code(vy, inv)
return val(vec_pack(nx, ny), "Vector")
}
if (meth == "rotate") { # rotate by angle (radians, fixed)
g_uses_mathrt = true
let v = emit_expr(e.kids[0]); let ang = emit_expr(e.kids[1])
let sn = emit_bind(`call i32 @fn_fx_sin(i32 {ang.code})`)
let ca = emit_bind(`add i32 {ang.code}, 102944`) # cos(a) = sin(a + pi/2)
let cs = emit_bind(`call i32 @fn_fx_sin(i32 {ca})`)
let vx = vec_x(v.code); let vy = vec_y(v.code)
let xc = fx_mul_code(vx, cs); let ys = fx_mul_code(vy, sn)
let xs = fx_mul_code(vx, sn); let yc = fx_mul_code(vy, cs)
let rx = emit_bind(`sub i32 {xc}, {ys}`) # x*cos - y*sin
let ry = emit_bind(`add i32 {xs}, {yc}`) # x*sin + y*cos
return val(vec_pack(rx, ry), "Vector")
}
if (meth == "angle") { # atan2(y, x) -> fixed radians
g_uses_mathrt = true
let v = emit_expr(e.kids[0])
let vx = vec_x(v.code); let vy = vec_y(v.code)
return val(emit_bind(`call i32 @fn_fx_atan2(i32 {vy}, i32 {vx})`), "fixed")
}
if (meth == "from_angle") { # unit vector at angle a: (cos a, sin a)
g_uses_mathrt = true
let ang = emit_expr(e.kids[0])
let sn = emit_bind(`call i32 @fn_fx_sin(i32 {ang.code})`)
let ca = emit_bind(`add i32 {ang.code}, 102944`)
let cs = emit_bind(`call i32 @fn_fx_sin(i32 {ca})`)
return val(vec_pack(cs, sn), "Vector")
}
# lerp(a, b, t: fixed) -> Vector — component-wise linear interpolation
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1]); let t = emit_expr(e.kids[2])
let ax = vec_x(a.code); let ay = vec_y(a.code); let bx = vec_x(b.code); let by = vec_y(b.code)
let lx = fx_lerp_code(ax, bx, t.code)
let ly = fx_lerp_code(ay, by, t.code)
return val(vec_pack(lx, ly), "Vector")
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,27 @@
program T {
entry {
let a = Vector.make(3.0, 4.0)
print(floor(Vector.x(a))) # 3
print(floor(Vector.y(a))) # 4
print(floor(Vector.length(a))) # 5 (3-4-5)
let b = Vector.make(1.0, 2.0)
let c = Vector.add(a, b)
print(floor(Vector.x(c))) # 4
print(floor(Vector.y(c))) # 6
let d = Vector.sub(a, b)
print(floor(Vector.x(d))) # 2
print(floor(Vector.y(d))) # 2
print(floor(Vector.dot(a, b))) # 11
print(floor(Vector.distance(a, b))) # 2 (sqrt 8)
print(floor(Vector.x(Vector.scale(a, 2.0)))) # 6
let n = Vector.normalize(a)
print(floor(Vector.length(n) * 1000)) # 999 (unit)
print(floor(Vector.x(Vector.from_angle(0.0)) * 1000)) # 999 (cos 0)
let r = Vector.rotate(Vector.make(1.0, 0.0), 1.5707963) # rotate 90 deg
print(floor(Vector.y(r) * 1000)) # 999 (-> (0,1))
print(floor(Vector.angle(Vector.make(0.0, 1.0)) * 1000)) # pi/2 -> 1570
let m = Vector.lerp(Vector.zero(), Vector.make(10.0, 20.0), 0.5)
print(floor(Vector.x(m))) # 5
print(floor(Vector.y(m))) # 10
}
}