feat(types): IVec2 + Rect 2D value types (#1)
All checks were successful
bootstrap / cfree-fixpoint (push) Successful in 20s
ci / build-and-test (push) Successful in 1m26s
commit-lint / conventional-commits (push) Successful in 6s
docs / build-and-deploy (push) Successful in 21s

Phase 1 of the fuller type-system proposal: two by-value spatial types
that lower to packed integers (no heap, copy like scalars).

- IVec2 — integer 2D vector, a pair of int packed into one i64, for tile
  and grid coordinates: make/zero/x/y/add/sub/scale/dot, the grid distance
  manhattan, equal, and to_vector (widen into the fixed-point Vector).
- Rect — axis-aligned rectangle, four Q16.16 fixed components packed into
  one i128, for HUD boxes and hitboxes: make/x/y/w/h, the derived
  right/bottom/center, and the contains (point) / intersects (overlap) tests.

Both are exact and deterministic, bit-identical on every platform. Vector
and Color already cover phase 1's other 2D primitives.

Wired end to end: emit_core llty (IVec2->i64, Rect->i128), emit_call
dispatch, the FRAGS list + reseeded seed, a selfhost test (types2d),
per-symbol docs + type pages + inventory, and the vocabulary/editor sync
(header, JetBrains, TextMate, LSP).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Orkun ÇAKILKAYA 2026-08-31 18:29:41 +03:00
parent 3df6640fa5
commit 2c9f9ac549
40 changed files with 26731 additions and 24058 deletions

View file

@ -0,0 +1,87 @@
# emit_ivec.ludic — the IVec2.* namespace: an integer 2D vector value type. An
# IVec2 is a pair of i32 components (x, y) packed into one i64 — x in the high
# 32 bits, y in the low 32 — exactly like Vector (emit_vector.ludic) but with
# whole-integer components and integer arithmetic. It is the natural type for
# tile / grid coordinates, cell offsets, and integer sizes, where a fractional
# part is meaningless and rounding would be a bug. Being one i64, it is a true
# by-value type (assignment copies, no heap) and lives in a single register.
# Reuses vec_pack / vec_x / vec_y from emit_vector.ludic; llty maps `IVec2` to
# i64 (see emit_core.ludic). Every operation is exact integer arithmetic, so it
# is deterministic on every platform.
# |d| for a signed i32 code -> i32 code (branchless: select on d < 0)
function ivec_abs(d: pointer) -> pointer {
let neg = emit_bind(`sub i32 0, {d}`)
let lt = emit_bind(`icmp slt i32 {d}, 0`)
return emit_bind(`select i1 {lt}, i32 {neg}, i32 {d}`)
}
function is_ivec_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 == "equal") or (meth == "manhattan") or (meth == "to_vector") { return true }
return false
}
function emit_ivec_ns(meth: pointer, e: Node) -> Val {
if (meth == "zero") { # the origin, (0, 0)
return val("0", "IVec2")
}
if (meth == "make") { # make(x, y: int) -> IVec2
let x = emit_expr(e.kids[0]); let y = emit_expr(e.kids[1])
return val(vec_pack(x.code, y.code), "IVec2")
}
if (meth == "x") { # the x component -> int
let v = emit_expr(e.kids[0])
return val(vec_x(v.code), "int")
}
if (meth == "y") { # the y component -> int
let v = emit_expr(e.kids[0])
return val(vec_y(v.code), "int")
}
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), "IVec2")
}
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), "IVec2")
}
if (meth == "scale") { # v * s (s: int)
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 = emit_bind(`mul i32 {vx}, {s.code}`)
let sy = emit_bind(`mul i32 {vy}, {s.code}`)
return val(vec_pack(sx, sy), "IVec2")
}
if (meth == "dot") { # ax*bx + ay*by -> int
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 = emit_bind(`mul i32 {ax}, {bx}`); let py = emit_bind(`mul i32 {ay}, {by}`)
return val(emit_bind(`add i32 {px}, {py}`), "int")
}
if (meth == "equal") { # a == b (both components) -> bool
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1])
let c = emit_bind(`icmp eq i64 {a.code}, {b.code}`)
return val(emit_bind(`zext i1 {c} to i32`), "bool")
}
if (meth == "manhattan") { # |dx| + |dy| -> int (grid distance)
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 adx = ivec_abs(dx); let ady = ivec_abs(dy)
return val(emit_bind(`add i32 {adx}, {ady}`), "int")
}
# to_vector(v) -> Vector — widen each integer component to a Q16.16 fixed
let v = emit_expr(e.kids[0])
let vx = vec_x(v.code); let vy = vec_y(v.code)
let fx = emit_bind(`shl i32 {vx}, 16`)
let fy = emit_bind(`shl i32 {vy}, 16`)
return val(vec_pack(fx, fy), "Vector")
}

View file

@ -0,0 +1,95 @@
# emit_rect.ludic — the Rect.* namespace: an axis-aligned 2D rectangle value
# type. A Rect is four Q16.16 fixed components (x, y, w, h) packed into a single
# i128 — x in bits 96..127, y in 64..95, w in 32..63, h in 0..31 — so, like
# Vector, it is copied by value and never heap-allocates. (x, y) is the top-left
# corner; (w, h) is the size. It is the natural type for HUD layout boxes,
# hitboxes, and camera/viewport regions, with fast point-in-rect and
# rect-overlap tests. Every operation is deterministic integer fixed-point,
# bit-identical on every platform. llty maps `Rect` to i128 (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 four fixed i32 codes (x, y, w, h) into the i128 Rect representation -> i128
function rect_pack(x: pointer, y: pointer, w: pointer, h: pointer) -> pointer {
let xe = emit_bind(`zext i32 {x} to i128`); let xs = emit_bind(`shl i128 {xe}, 96`)
let ye = emit_bind(`zext i32 {y} to i128`); let ys = emit_bind(`shl i128 {ye}, 64`)
let we = emit_bind(`zext i32 {w} to i128`); let ws = emit_bind(`shl i128 {we}, 32`)
let he = emit_bind(`zext i32 {h} to i128`)
let o1 = emit_bind(`or i128 {xs}, {ys}`)
let o2 = emit_bind(`or i128 {o1}, {ws}`)
return emit_bind(`or i128 {o2}, {he}`)
}
# one 32-bit field of an i128 Rect at bit offset `sh` -> i32 fixed code
function rect_field(r: pointer, sh: int) -> pointer {
if (sh == 0) { return emit_bind(`trunc i128 {r} to i32`) }
let s = emit_bind(`lshr i128 {r}, {itoa(sh)}`)
return emit_bind(`trunc i128 {s} to i32`)
}
# a 0/1 bool (i32) from a comparison of two fixed i32 codes with `cmp` (an LLVM
# icmp predicate like sge / slt)
function rect_cmp(cmp: pointer, a: pointer, b: pointer) -> pointer {
let c = emit_bind(`icmp {cmp} i32 {a}, {b}`)
return emit_bind(`zext i1 {c} to i32`)
}
function is_rect_ns(meth: pointer) -> bool {
if (meth == "make") or (meth == "x") or (meth == "y") or (meth == "w") or (meth == "h") { return true }
if (meth == "right") or (meth == "bottom") or (meth == "center") { return true }
if (meth == "contains") or (meth == "intersects") { return true }
return false
}
function emit_rect_ns(meth: pointer, e: Node) -> Val {
if (meth == "make") { # make(x, y, w, h: fixed) -> Rect
let x = emit_expr(e.kids[0]); let y = emit_expr(e.kids[1])
let w = emit_expr(e.kids[2]); let h = emit_expr(e.kids[3])
return val(rect_pack(x.code, y.code, w.code, h.code), "Rect")
}
if (meth == "x") { let r = emit_expr(e.kids[0]); return val(rect_field(r.code, 96), "fixed") }
if (meth == "y") { let r = emit_expr(e.kids[0]); return val(rect_field(r.code, 64), "fixed") }
if (meth == "w") { let r = emit_expr(e.kids[0]); return val(rect_field(r.code, 32), "fixed") }
if (meth == "h") { let r = emit_expr(e.kids[0]); return val(rect_field(r.code, 0), "fixed") }
if (meth == "right") { # x + w -> fixed (the right edge)
let r = emit_expr(e.kids[0])
let rx = rect_field(r.code, 96); let rw = rect_field(r.code, 32)
return val(emit_bind(`add i32 {rx}, {rw}`), "fixed")
}
if (meth == "bottom") { # y + h -> fixed (the bottom edge)
let r = emit_expr(e.kids[0])
let ry = rect_field(r.code, 64); let rh = rect_field(r.code, 0)
return val(emit_bind(`add i32 {ry}, {rh}`), "fixed")
}
if (meth == "center") { # (x + w/2, y + h/2) -> Vector
let r = emit_expr(e.kids[0])
let rx = rect_field(r.code, 96); let ry = rect_field(r.code, 64)
let rw = rect_field(r.code, 32); let rh = rect_field(r.code, 0)
let hw = emit_bind(`ashr i32 {rw}, 1`); let hh = emit_bind(`ashr i32 {rh}, 1`)
let cx = emit_bind(`add i32 {rx}, {hw}`); let cy = emit_bind(`add i32 {ry}, {hh}`)
return val(vec_pack(cx, cy), "Vector")
}
if (meth == "contains") { # contains(r, px, py: fixed) -> bool
let r = emit_expr(e.kids[0]); let px = emit_expr(e.kids[1]); let py = emit_expr(e.kids[2])
let rx = rect_field(r.code, 96); let ry = rect_field(r.code, 64)
let rw = rect_field(r.code, 32); let rh = rect_field(r.code, 0)
let rr = emit_bind(`add i32 {rx}, {rw}`); let rb = emit_bind(`add i32 {ry}, {rh}`)
let c1 = rect_cmp("sge", px.code, rx); let c2 = rect_cmp("slt", px.code, rr)
let c3 = rect_cmp("sge", py.code, ry); let c4 = rect_cmp("slt", py.code, rb)
let a1 = emit_bind(`and i32 {c1}, {c2}`); let a2 = emit_bind(`and i32 {c3}, {c4}`)
return val(emit_bind(`and i32 {a1}, {a2}`), "bool")
}
# intersects(a, b: Rect) -> bool — AABB overlap (touching edges do not overlap)
let a = emit_expr(e.kids[0]); let b = emit_expr(e.kids[1])
let axl = rect_field(a.code, 96); let ayl = rect_field(a.code, 64)
let aw = rect_field(a.code, 32); let ah = rect_field(a.code, 0)
let bxl = rect_field(b.code, 96); let byl = rect_field(b.code, 64)
let bw = rect_field(b.code, 32); let bh = rect_field(b.code, 0)
let axr = emit_bind(`add i32 {axl}, {aw}`); let ayb = emit_bind(`add i32 {ayl}, {ah}`)
let bxr = emit_bind(`add i32 {bxl}, {bw}`); let byb = emit_bind(`add i32 {byl}, {bh}`)
let c1 = rect_cmp("slt", axl, bxr); let c2 = rect_cmp("slt", bxl, axr)
let c3 = rect_cmp("slt", ayl, byb); let c4 = rect_cmp("slt", byl, ayb)
let a1 = emit_bind(`and i32 {c1}, {c2}`); let a2 = emit_bind(`and i32 {c3}, {c4}`)
return val(emit_bind(`and i32 {a1}, {a2}`), "bool")
}