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,7 @@
---
id: ivec2
title: IVec2
order: 8
---
Integer 2D vector math for tile and grid coordinates, cell offsets, and integer sizes. An <code>IVec2</code> is a pair of whole-number <code>int</code> components (x, y) packed into one value, so it is copied by value and never allocates. Every operation is exact integer arithmetic — no rounding, and bit-identical on every platform. Use it wherever a fractional part would be meaningless; reach for <code>Vector</code> when you need sub-pixel precision. Arguments are positional.

View file

@ -0,0 +1,22 @@
---
id: ivec2-add
name: IVec2.add
category: ivec2
kind: namespace-method
tokens: IVec2.add
sig: IVec2.add(a, b) -> IVec2
tip: Component-wise sum of two integer vectors.
order: 4
ns: IVec2
member: add
---
Adds two integer vectors component-wise. Adding a direction step to a position is how a token moves one cell on a grid.
```ludic
program Demo {
handler Step phase Update {
let next = IVec2.add(pos, IVec2.make(1, 0))
}
}
```

View file

@ -0,0 +1,22 @@
---
id: ivec2-dot
name: IVec2.dot
category: ivec2
kind: namespace-method
tokens: IVec2.dot
sig: IVec2.dot(a, b) -> int
tip: The dot product ax*bx + ay*by.
order: 7
ns: IVec2
member: dot
---
Returns the integer dot product <code>ax*bx + ay*by</code>. Its sign tells you whether two directions point roughly the same way.
```ludic
program Demo {
handler Step phase Update {
let facing = IVec2.dot(heading, toTarget)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: ivec2-equal
name: IVec2.equal
category: ivec2
kind: namespace-method
tokens: IVec2.equal
sig: IVec2.equal(a, b) -> bool
tip: True when both components match.
order: 8
ns: IVec2
member: equal
---
Compares two integer vectors for exact equality — true only when both the <code>x</code> and <code>y</code> components match.
```ludic
program Demo {
handler Step phase Update {
if IVec2.equal(pos, goal) { win() }
}
}
```

View file

@ -0,0 +1,22 @@
---
id: ivec2-make
name: IVec2.make
category: ivec2
kind: namespace-method
tokens: IVec2.make
sig: IVec2.make(x, y) -> IVec2
tip: Build an integer vector from x and y components.
order: 0
ns: IVec2
member: make
---
Builds an <code>IVec2</code> from its integer <code>x</code> and <code>y</code> components. This is the usual way to name a tile or grid cell; read the parts back with <code>IVec2.x</code> and <code>IVec2.y</code>.
```ludic
program Demo {
handler Step phase Update {
let cell = IVec2.make(4, 7)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: ivec2-manhattan
name: IVec2.manhattan
category: ivec2
kind: namespace-method
tokens: IVec2.manhattan
sig: IVec2.manhattan(a, b) -> int
tip: Grid distance |dx| + |dy|.
order: 9
ns: IVec2
member: manhattan
---
Returns the Manhattan (taxicab) distance <code>|dx| + |dy|</code> between two cells — the number of orthogonal steps between them, the natural distance metric on a 4-connected grid.
```ludic
program Demo {
handler Step phase Update {
let steps = IVec2.manhattan(pos, goal)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: ivec2-scale
name: IVec2.scale
category: ivec2
kind: namespace-method
tokens: IVec2.scale
sig: IVec2.scale(v, s) -> IVec2
tip: Multiply both components by an integer.
order: 6
ns: IVec2
member: scale
---
Multiplies both components by an integer scalar — useful to convert a cell coordinate into a pixel offset by the tile size.
```ludic
program Demo {
handler Step phase Update {
let px = IVec2.scale(cell, 16)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: ivec2-sub
name: IVec2.sub
category: ivec2
kind: namespace-method
tokens: IVec2.sub
sig: IVec2.sub(a, b) -> IVec2
tip: Component-wise difference of two integer vectors.
order: 5
ns: IVec2
member: sub
---
Subtracts <code>b</code> from <code>a</code> component-wise, giving the integer offset from one cell to another.
```ludic
program Demo {
handler Step phase Update {
let delta = IVec2.sub(target, pos)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: ivec2-to_vector
name: IVec2.to_vector
category: ivec2
kind: namespace-method
tokens: IVec2.to_vector
sig: IVec2.to_vector(v) -> Vector
tip: Widen to a fixed-point Vector.
order: 10
ns: IVec2
member: to_vector
---
Widens an integer vector into a fixed-point <code>Vector</code>, so a grid coordinate can flow into the sub-pixel <code>Vector.*</code> math (interpolation, rotation, length).
```ludic
program Demo {
handler Step phase Update {
let world = IVec2.to_vector(cell)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: ivec2-x
name: IVec2.x
category: ivec2
kind: namespace-method
tokens: IVec2.x
sig: IVec2.x(v) -> int
tip: The x component of an integer vector.
order: 2
ns: IVec2
member: x
---
Reads the <code>x</code> (column) component of an <code>IVec2</code> as a plain <code>int</code>.
```ludic
program Demo {
handler Step phase Update {
let col = IVec2.x(IVec2.make(4, 7))
}
}
```

View file

@ -0,0 +1,22 @@
---
id: ivec2-y
name: IVec2.y
category: ivec2
kind: namespace-method
tokens: IVec2.y
sig: IVec2.y(v) -> int
tip: The y component of an integer vector.
order: 3
ns: IVec2
member: y
---
Reads the <code>y</code> (row) component of an <code>IVec2</code> as a plain <code>int</code>.
```ludic
program Demo {
handler Step phase Update {
let row = IVec2.y(IVec2.make(4, 7))
}
}
```

View file

@ -0,0 +1,22 @@
---
id: ivec2-zero
name: IVec2.zero
category: ivec2
kind: namespace-method
tokens: IVec2.zero
sig: IVec2.zero() -> IVec2
tip: The origin cell, (0, 0).
order: 1
ns: IVec2
member: zero
---
Returns the origin <code>(0, 0)</code> — a handy neutral value to start an accumulation or mark an unset cell.
```ludic
program Demo {
handler Step phase Update {
let origin = IVec2.zero()
}
}
```

View file

@ -0,0 +1,7 @@
---
id: rect
title: Rect
order: 9
---
Axis-aligned rectangles for HUD layout boxes, hitboxes, and camera regions. A <code>Rect</code> is four Q16.16 <code>fixed</code> components — position <code>(x, y)</code> (its top-left corner) and size <code>(w, h)</code> — packed into a single value that is copied by value and never allocates. It offers fast point-in-rect and rectangle-overlap tests. Every operation is deterministic fixed-point, bit-identical on every platform. Arguments are positional.

View file

@ -0,0 +1,22 @@
---
id: rect-bottom
name: Rect.bottom
category: rect
kind: namespace-method
tokens: Rect.bottom
sig: Rect.bottom(r) -> fixed
tip: The bottom edge, y + h.
order: 6
ns: Rect
member: bottom
---
Returns the bottom edge, <code>y + h</code> — the y coordinate just below the rectangle.
```ludic
program Demo {
handler Step phase Update {
let base = Rect.bottom(hud)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: rect-center
name: Rect.center
category: rect
kind: namespace-method
tokens: Rect.center
sig: Rect.center(r) -> Vector
tip: The center point as a Vector.
order: 7
ns: Rect
member: center
---
Returns the center point <code>(x + w/2, y + h/2)</code> as a <code>Vector</code> — the anchor you want when placing a label or spawning at the middle of a box.
```ludic
program Demo {
handler Step phase Update {
let mid = Rect.center(hud)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: rect-contains
name: Rect.contains
category: rect
kind: namespace-method
tokens: Rect.contains
sig: Rect.contains(r, px, py) -> bool
tip: True when the point is inside.
order: 8
ns: Rect
member: contains
---
Tests whether the point <code>(px, py)</code> falls inside the rectangle. The left and top edges are inclusive; the right and bottom edges are exclusive, so adjacent rectangles tile without overlap. The classic use is a mouse-in-button hit test.
```ludic
program Demo {
handler Step phase Update {
if Rect.contains(button, mx, my) { press() }
}
}
```

View file

@ -0,0 +1,22 @@
---
id: rect-h
name: Rect.h
category: rect
kind: namespace-method
tokens: Rect.h
sig: Rect.h(r) -> fixed
tip: The height.
order: 4
ns: Rect
member: h
---
Reads the height of the rectangle.
```ludic
program Demo {
handler Step phase Update {
let height = Rect.h(hud)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: rect-intersects
name: Rect.intersects
category: rect
kind: namespace-method
tokens: Rect.intersects
sig: Rect.intersects(a, b) -> bool
tip: True when two rectangles overlap.
order: 9
ns: Rect
member: intersects
---
Tests whether two rectangles overlap (axis-aligned bounding-box test). Touching edges do not count as overlapping. This is the cheap broad-phase check before any finer collision work.
```ludic
program Demo {
handler Step phase Update {
if Rect.intersects(player, hazard) { hurt() }
}
}
```

View file

@ -0,0 +1,22 @@
---
id: rect-make
name: Rect.make
category: rect
kind: namespace-method
tokens: Rect.make
sig: Rect.make(x, y, w, h) -> Rect
tip: Build a rectangle from a corner and a size.
order: 0
ns: Rect
member: make
---
Builds a <code>Rect</code> from its top-left corner <code>(x, y)</code> and size <code>(w, h)</code>, all <code>fixed</code>. Read the parts back with <code>Rect.x</code> / <code>Rect.y</code> / <code>Rect.w</code> / <code>Rect.h</code>.
```ludic
program Demo {
handler Step phase Update {
let hud = Rect.make(8.0, 8.0, 96.0, 16.0)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: rect-right
name: Rect.right
category: rect
kind: namespace-method
tokens: Rect.right
sig: Rect.right(r) -> fixed
tip: The right edge, x + w.
order: 5
ns: Rect
member: right
---
Returns the right edge, <code>x + w</code> — the x coordinate just past the rectangle. Handy for anchoring something to a box's right side.
```ludic
program Demo {
handler Step phase Update {
let edge = Rect.right(hud)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: rect-w
name: Rect.w
category: rect
kind: namespace-method
tokens: Rect.w
sig: Rect.w(r) -> fixed
tip: The width.
order: 3
ns: Rect
member: w
---
Reads the width of the rectangle.
```ludic
program Demo {
handler Step phase Update {
let width = Rect.w(hud)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: rect-x
name: Rect.x
category: rect
kind: namespace-method
tokens: Rect.x
sig: Rect.x(r) -> fixed
tip: The left edge (x position).
order: 1
ns: Rect
member: x
---
Reads the left edge — the <code>x</code> position of the rectangle's top-left corner.
```ludic
program Demo {
handler Step phase Update {
let left = Rect.x(hud)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: rect-y
name: Rect.y
category: rect
kind: namespace-method
tokens: Rect.y
sig: Rect.y(r) -> fixed
tip: The top edge (y position).
order: 2
ns: Rect
member: y
---
Reads the top edge — the <code>y</code> position of the rectangle's top-left corner.
```ludic
program Demo {
handler Step phase Update {
let top = Rect.y(hud)
}
}
```

View file

@ -0,0 +1,21 @@
---
id: type-ivec2
name: IVec2
category: types
kind: type
tokens: IVec2
sig: IVec2
tip: An integer 2D vector — two int components (x, y), copied by value.
order: 14
---
<code>IVec2</code> is an integer 2D vector: two whole-number <code>int</code> components, <code>x</code> and <code>y</code>, packed into a single value that is copied by value and never heap-allocates. It is the natural type for tile and grid coordinates, cell offsets, and integer sizes — anywhere a fractional part would be meaningless. Build one with <code>IVec2.make(x, y)</code> (or <code>IVec2.zero()</code>), read the parts with <code>IVec2.x</code> / <code>IVec2.y</code>, and combine them with the <code>IVec2.*</code> math — add, sub, scale, dot, and the grid-distance <code>IVec2.manhattan</code>. Every operation is exact integer arithmetic, so results are bit-identical on every platform. Widen to a sub-pixel <code>Vector</code> with <code>IVec2.to_vector</code> when you need fractional math.
```ludic
program Demo {
handler Step phase Update {
var cell: IVec2 = IVec2.make(4, 7)
cell = IVec2.add(cell, IVec2.make(1, 0))
}
}
```

View file

@ -0,0 +1,21 @@
---
id: type-rect
name: Rect
category: types
kind: type
tokens: Rect
sig: Rect
tip: A rectangle — position (x, y) and size (w, h), copied by value.
order: 15
---
<code>Rect</code> is an axis-aligned rectangle: four Q16.16 <code>fixed</code> components — the top-left corner <code>(x, y)</code> and the size <code>(w, h)</code> — packed into a single value that is copied by value and never heap-allocates. It is the natural type for HUD layout boxes, hitboxes, and camera or viewport regions. Build one with <code>Rect.make(x, y, w, h)</code>, read the parts with <code>Rect.x</code> / <code>Rect.y</code> / <code>Rect.w</code> / <code>Rect.h</code> (or the derived <code>Rect.right</code> / <code>Rect.bottom</code> / <code>Rect.center</code>), and test against it with <code>Rect.contains</code> for a point and <code>Rect.intersects</code> for overlap. Every operation is deterministic fixed-point, so results are bit-identical on every platform.
```ludic
program Demo {
handler Step phase Update {
let button: Rect = Rect.make(8.0, 8.0, 96.0, 16.0)
if Rect.contains(button, mx, my) { press() }
}
}
```