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,13 @@
bump: minor
type: feat
**2D value types — `IVec2` and `Rect` (#1, phase 1).** Two by-value spatial
types that lower to packed integers, so they copy like scalars and never
allocate. `IVec2` is an integer 2D vector (a pair of `int` packed into one
`i64`) for tile and grid coordinates — `IVec2.make/zero/x/y/add/sub/scale/dot`,
the grid distance `IVec2.manhattan`, `IVec2.equal`, and `IVec2.to_vector` to
widen into the fixed-point `Vector`. `Rect` is an axis-aligned rectangle (four
Q16.16 `fixed` components packed into one `i128`) for HUD boxes and hitboxes —
`Rect.make/x/y/w/h`, the derived `Rect.right/bottom/center`, and the
`Rect.contains` (point) and `Rect.intersects` (overlap) tests. Both are exact
and deterministic, bit-identical on every platform, and complement the existing
`Vector` and `Color` types that already cover phase 1's other 2D primitives.

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() }
}
}
```

View file

@ -114,6 +114,14 @@ function emit_ns_call(ns: pointer, meth: pointer, e: Node) -> Val {
if is_vector_ns(meth) { return emit_vector_ns(meth, e) } if is_vector_ns(meth) { return emit_vector_ns(meth, e) }
perr(`unknown builtin Vector.{meth}`) perr(`unknown builtin Vector.{meth}`)
} }
if (ns == "IVec2") {
if is_ivec_ns(meth) { return emit_ivec_ns(meth, e) }
perr(`unknown builtin IVec2.{meth}`)
}
if (ns == "Rect") {
if is_rect_ns(meth) { return emit_rect_ns(meth, e) }
perr(`unknown builtin Rect.{meth}`)
}
if (ns == "Duration") { if (ns == "Duration") {
if is_duration_ns(meth) { return emit_duration_ns(meth, e) } if is_duration_ns(meth) { return emit_duration_ns(meth, e) }
perr(`unknown builtin Duration.{meth}`) perr(`unknown builtin Duration.{meth}`)

View file

@ -120,6 +120,8 @@ 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 == "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 == "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 == "Vector") { return "i64" } # a 2D vector: (x, y) fixeds packed into one i64
if (t == "IVec2") { return "i64" } # an integer 2D vector: (x, y) ints packed into one i64
if (t == "Rect") { return "i128" } # a rectangle: (x, y, w, h) fixeds packed into one i128
if (t == "byte") { return "i8" } # a single byte (p[i] on a raw ptr) 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 == "words") or (t == "fixeds") or (t == "pointers") { return "ptr" } # typed buffers
if (t == "void") { return "void" } if (t == "void") { return "void" }

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")
}

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,45 @@
program T {
entry {
# --- IVec2: integer 2D vectors (tile / grid coordinates) ---
let a = IVec2.make(3, 4)
print(IVec2.x(a)) # 3
print(IVec2.y(a)) # 4
let b = IVec2.make(1, 2)
let c = IVec2.add(a, b)
print(IVec2.x(c)) # 4
print(IVec2.y(c)) # 6
let d = IVec2.sub(a, b)
print(IVec2.x(d)) # 2
print(IVec2.y(d)) # 2
let s = IVec2.scale(a, 3)
print(IVec2.x(s)) # 9
print(IVec2.y(s)) # 12
print(IVec2.dot(a, b)) # 3*1 + 4*2 = 11
print(IVec2.manhattan(a, b)) # |2| + |2| = 4
let z = IVec2.zero()
print(IVec2.x(z)) # 0
let eq = IVec2.equal(a, IVec2.make(3, 4))
if eq { print(1) } else { print(0) } # 1
let ne = IVec2.equal(a, b)
if ne { print(1) } else { print(0) } # 0
print(floor(Vector.y(IVec2.to_vector(a)))) # 4
# --- Rect: axis-aligned rectangles (HUD boxes, hitboxes) ---
let r = Rect.make(10.0, 20.0, 30.0, 40.0)
print(floor(Rect.x(r))) # 10
print(floor(Rect.y(r))) # 20
print(floor(Rect.w(r))) # 30
print(floor(Rect.h(r))) # 40
print(floor(Rect.right(r))) # 40
print(floor(Rect.bottom(r))) # 60
print(floor(Vector.x(Rect.center(r)))) # 25
print(floor(Vector.y(Rect.center(r)))) # 40
if Rect.contains(r, 15.0, 25.0) { print(1) } else { print(0) } # 1
if Rect.contains(r, 5.0, 25.0) { print(1) } else { print(0) } # 0
if Rect.contains(r, 40.0, 25.0) { print(1) } else { print(0) } # 0 (right edge exclusive)
let r2 = Rect.make(35.0, 25.0, 20.0, 20.0)
if Rect.intersects(r, r2) { print(1) } else { print(0) } # 1
let r3 = Rect.make(50.0, 0.0, 5.0, 5.0)
if Rect.intersects(r, r3) { print(1) } else { print(0) } # 0
}
}

View file

@ -319,7 +319,9 @@
"type-string", "type-string",
"type-void", "type-void",
"type-words", "type-words",
"type-vector" "type-vector",
"type-ivec2",
"type-rect"
], ],
"world": [ "world": [
"world-get", "world-get",
@ -356,6 +358,31 @@
"vector-angle", "vector-angle",
"vector-from_angle" "vector-from_angle"
], ],
"ivec2": [
"ivec2-make",
"ivec2-zero",
"ivec2-x",
"ivec2-y",
"ivec2-add",
"ivec2-sub",
"ivec2-scale",
"ivec2-dot",
"ivec2-equal",
"ivec2-manhattan",
"ivec2-to_vector"
],
"rect": [
"rect-make",
"rect-x",
"rect-y",
"rect-w",
"rect-h",
"rect-right",
"rect-bottom",
"rect-center",
"rect-contains",
"rect-intersects"
],
"duration": [ "duration": [
"duration-seconds", "duration-seconds",
"duration-minutes", "duration-minutes",

View file

@ -52,7 +52,7 @@ object LudicVocabulary {
"enable", "disable", "match", "machine", "state", "become", "where", "enable", "disable", "match", "machine", "state", "become", "where",
"and", "or", "not", "break", "continue", "new", "emit", "cancel", "try" "and", "or", "not", "break", "continue", "new", "emit", "cancel", "try"
) )
val PRIMITIVES = setOf("int", "long", "fixed", "bool", "entity", "string", "pointer", "byte", "words", "fixeds", "pointers", "Vector", "void") val PRIMITIVES = setOf("int", "long", "fixed", "bool", "entity", "string", "pointer", "byte", "words", "fixeds", "pointers", "Vector", "IVec2", "Rect", "void")
val PHASES = setOf("Start", "Input", "FixedUpdate", "Update", "LateUpdate", "Render") val PHASES = setOf("Start", "Input", "FixedUpdate", "Update", "LateUpdate", "Render")
val WIDGETS = setOf("panel", "col", "row", "label", "button", "image", "spacer") val WIDGETS = setOf("panel", "col", "row", "label", "button", "image", "spacer")

View file

@ -177,7 +177,7 @@
{ "name": "keyword.other.clause.ludic", "match": "\\b(phase|query|on)\\b" }, { "name": "keyword.other.clause.ludic", "match": "\\b(phase|query|on)\\b" },
{ "name": "keyword.other.ludic", "match": "\\b(import|extern)\\b" }, { "name": "keyword.other.ludic", "match": "\\b(import|extern)\\b" },
{ "name": "storage.type.ludic", "match": "\\b(program|property|model|enum|ui|const|var|let|function|handler|entry|state|test)\\b" }, { "name": "storage.type.ludic", "match": "\\b(program|property|model|enum|ui|const|var|let|function|handler|entry|state|test)\\b" },
{ "name": "support.type.primitive.ludic", "match": "\\b(int|long|fixed|bool|entity|string|pointer|byte|words|fixeds|pointers|Vector|void)\\b" }, { "name": "support.type.primitive.ludic", "match": "\\b(int|long|fixed|bool|entity|string|pointer|byte|words|fixeds|pointers|Vector|IVec2|Rect|void)\\b" },
{ "name": "constant.language.boolean.ludic", "match": "\\b(true|false|null)\\b" }, { "name": "constant.language.boolean.ludic", "match": "\\b(true|false|null)\\b" },
{ "name": "constant.language.phase.ludic", "match": "\\b(Start|Input|FixedUpdate|Update|LateUpdate|Render)\\b" } { "name": "constant.language.phase.ludic", "match": "\\b(Start|Input|FixedUpdate|Update|LateUpdate|Render)\\b" }
] ]

View file

@ -177,7 +177,7 @@
{ "name": "keyword.other.clause.ludic", "match": "\\b(phase|query|on)\\b" }, { "name": "keyword.other.clause.ludic", "match": "\\b(phase|query|on)\\b" },
{ "name": "keyword.other.ludic", "match": "\\b(import|extern)\\b" }, { "name": "keyword.other.ludic", "match": "\\b(import|extern)\\b" },
{ "name": "storage.type.ludic", "match": "\\b(program|property|model|enum|ui|const|var|let|function|handler|entry|state|test)\\b" }, { "name": "storage.type.ludic", "match": "\\b(program|property|model|enum|ui|const|var|let|function|handler|entry|state|test)\\b" },
{ "name": "support.type.primitive.ludic", "match": "\\b(int|long|fixed|bool|entity|string|pointer|byte|words|fixeds|pointers|Vector|void)\\b" }, { "name": "support.type.primitive.ludic", "match": "\\b(int|long|fixed|bool|entity|string|pointer|byte|words|fixeds|pointers|Vector|IVec2|Rect|void)\\b" },
{ "name": "constant.language.boolean.ludic", "match": "\\b(true|false|null)\\b" }, { "name": "constant.language.boolean.ludic", "match": "\\b(true|false|null)\\b" },
{ "name": "constant.language.phase.ludic", "match": "\\b(Start|Input|FixedUpdate|Update|LateUpdate|Render)\\b" } { "name": "constant.language.phase.ludic", "match": "\\b(Start|Input|FixedUpdate|Update|LateUpdate|Render)\\b" }
] ]

View file

@ -204,7 +204,7 @@ program LudicLsp {
return c == 43 or c == 45 or c == 42 or c == 47 or c == 37 or c == 60 or c == 62 or c == 61 or c == 40 or c == 41 or c == 123 or c == 125 or c == 91 or c == 93 or c == 44 or c == 58 or c == 46 or c == 33 or c == 64 or c == 59 return c == 43 or c == 45 or c == 42 or c == 47 or c == 37 or c == 60 or c == 62 or c == 61 or c == 40 or c == 41 or c == 123 or c == 125 or c == 91 or c == 93 or c == 44 or c == 58 or c == 46 or c == 33 or c == 64 or c == 59
} }
function is_type_word(w: pointer) -> bool { function is_type_word(w: pointer) -> bool {
return (w == "int") or (w == "long") or (w == "fixed") or (w == "bool") or (w == "entity") or (w == "string") or (w == "pointer") or (w == "byte") or (w == "words") or (w == "fixeds") or (w == "pointers") or (w == "Vector") or (w == "void") return (w == "int") or (w == "long") or (w == "fixed") or (w == "bool") or (w == "entity") or (w == "string") or (w == "pointer") or (w == "byte") or (w == "words") or (w == "fixeds") or (w == "pointers") or (w == "Vector") or (w == "IVec2") or (w == "Rect") or (w == "void")
} }
function is_phase_word(w: pointer) -> bool { function is_phase_word(w: pointer) -> bool {
return (w == "Start") or (w == "Input") or (w == "FixedUpdate") or (w == "Update") or (w == "LateUpdate") or (w == "Render") return (w == "Start") or (w == "Input") or (w == "FixedUpdate") or (w == "Update") or (w == "LateUpdate") or (w == "Render")
@ -1318,6 +1318,31 @@ program LudicLsp {
if (meth == "angle") { return "Vector.angle(v) -> fixed" } if (meth == "angle") { return "Vector.angle(v) -> fixed" }
if (meth == "from_angle") { return "Vector.from_angle(a) -> Vector" } if (meth == "from_angle") { return "Vector.from_angle(a) -> Vector" }
} }
if (ns == "IVec2") {
if (meth == "make") { return "IVec2.make(x, y) -> IVec2" }
if (meth == "zero") { return "IVec2.zero() -> IVec2" }
if (meth == "x") { return "IVec2.x(v) -> int" }
if (meth == "y") { return "IVec2.y(v) -> int" }
if (meth == "add") { return "IVec2.add(a, b) -> IVec2" }
if (meth == "sub") { return "IVec2.sub(a, b) -> IVec2" }
if (meth == "scale") { return "IVec2.scale(v, s) -> IVec2" }
if (meth == "dot") { return "IVec2.dot(a, b) -> int" }
if (meth == "equal") { return "IVec2.equal(a, b) -> bool" }
if (meth == "manhattan") { return "IVec2.manhattan(a, b) -> int" }
if (meth == "to_vector") { return "IVec2.to_vector(v) -> Vector" }
}
if (ns == "Rect") {
if (meth == "make") { return "Rect.make(x, y, w, h) -> Rect" }
if (meth == "x") { return "Rect.x(r) -> fixed" }
if (meth == "y") { return "Rect.y(r) -> fixed" }
if (meth == "w") { return "Rect.w(r) -> fixed" }
if (meth == "h") { return "Rect.h(r) -> fixed" }
if (meth == "right") { return "Rect.right(r) -> fixed" }
if (meth == "bottom") { return "Rect.bottom(r) -> fixed" }
if (meth == "center") { return "Rect.center(r) -> Vector" }
if (meth == "contains") { return "Rect.contains(r, px, py) -> bool" }
if (meth == "intersects") { return "Rect.intersects(a, b) -> bool" }
}
if (ns == "List") { if (ns == "List") {
if (meth == "len") { return "List.len(s) -> int" } if (meth == "len") { return "List.len(s) -> int" }
if (meth == "push") { return "List.push(s, v)" } if (meth == "push") { return "List.push(s, v)" }
@ -2341,7 +2366,7 @@ program LudicLsp {
} }
function comp_types(o: Buf) -> void { function comp_types(o: Buf) -> void {
comp_item(o, "int", 14, "built-in type"); comp_item(o, "long", 14, "built-in type"); comp_item(o, "fixed", 14, "built-in type"); comp_item(o, "bool", 14, "built-in type") comp_item(o, "int", 14, "built-in type"); comp_item(o, "long", 14, "built-in type"); comp_item(o, "fixed", 14, "built-in type"); comp_item(o, "bool", 14, "built-in type")
comp_item(o, "entity", 14, "built-in type"); comp_item(o, "string", 14, "built-in type"); comp_item(o, "pointer", 14, "built-in type"); comp_item(o, "Vector", 14, "built-in type"); comp_item(o, "void", 14, "built-in type") comp_item(o, "entity", 14, "built-in type"); comp_item(o, "string", 14, "built-in type"); comp_item(o, "pointer", 14, "built-in type"); comp_item(o, "Vector", 14, "built-in type"); comp_item(o, "IVec2", 14, "built-in type"); comp_item(o, "Rect", 14, "built-in type"); comp_item(o, "void", 14, "built-in type")
} }
function comp_builtins(o: Buf) -> void { var i = 0; while i < len(g_builtins) { comp_item(o, g_builtins[i], 3, g_bsigs[i]); i = i + 1 } } function comp_builtins(o: Buf) -> void { var i = 0; while i < len(g_builtins) { comp_item(o, g_builtins[i], 3, g_bsigs[i]); i = i + 1 } }
function on_completion(msg: JVal, id: JVal) -> void { function on_completion(msg: JVal, id: JVal) -> void {

View file

@ -71,7 +71,7 @@ static const char* LUDIC_KW_STMT[] = {
"and","or","not","break","continue","new","emit","cancel","try", 0 "and","or","not","break","continue","new","emit","cancel","try", 0
}; };
static const char* LUDIC_TYPES[] = { static const char* LUDIC_TYPES[] = {
"int","long","fixed","bool","entity","string","pointer","byte","words","fixeds","pointers","Vector","void", 0 "int","long","fixed","bool","entity","string","pointer","byte","words","fixeds","pointers","Vector","IVec2","Rect","void", 0
}; };
static const char* LUDIC_PHASES[] = { static const char* LUDIC_PHASES[] = {
"Start","Input","FixedUpdate","Update","LateUpdate","Render", 0 "Start","Input","FixedUpdate","Update","LateUpdate","Render", 0

View file

@ -26,6 +26,8 @@ function selfhost_frags() -> []pointer {
push(f, "selfhost/backend/emit_intrin2.ludic") push(f, "selfhost/backend/emit_intrin2.ludic")
push(f, "selfhost/backend/stdlib/emit_math.ludic") push(f, "selfhost/backend/stdlib/emit_math.ludic")
push(f, "selfhost/backend/stdlib/emit_vector.ludic") push(f, "selfhost/backend/stdlib/emit_vector.ludic")
push(f, "selfhost/backend/stdlib/emit_ivec.ludic")
push(f, "selfhost/backend/stdlib/emit_rect.ludic")
push(f, "selfhost/backend/stdlib/emit_text.ludic") push(f, "selfhost/backend/stdlib/emit_text.ludic")
push(f, "selfhost/backend/stdlib/emit_text_prelude.ludic") push(f, "selfhost/backend/stdlib/emit_text_prelude.ludic")
push(f, "selfhost/backend/stdlib/emit_hash.ludic") push(f, "selfhost/backend/stdlib/emit_hash.ludic")

View file

@ -96,6 +96,7 @@ function cmd_selfhost_test() -> int {
sh_case("math3", "45 90 180 -135 30 60 90") sh_case("math3", "45 90 180 -135 30 60 90")
sh_case("transcend", "2718 1 148 1000 0 2303 1024 1414 3 250 0 1000 1016") sh_case("transcend", "2718 1 148 1000 0 2303 1024 1414 3 250 0 1000 1016")
sh_case("vector", "3 4 5 4 6 2 2 11 2 6 999 999 999 1570 5 10") sh_case("vector", "3 4 5 4 6 2 2 11 2 6 999 999 999 1570 5 10")
sh_case("types2d", "3 4 4 6 2 2 9 12 11 4 0 1 0 4 10 20 30 40 40 60 25 40 1 0 0 1 0")
sh_case("datetime", "10800 330 172800 3 2 0 10957 2026 8 30 4 6 1 0 1 29 28 31 2 1 365 946729815 2000 1 1 12 30 15 6 10957 1") sh_case("datetime", "10800 330 172800 3 2 0 10957 2026 8 30 4 6 1 0 1 29 28 31 2 1 365 946729815 2000 1 1 12 30 15 6 10957 1")
sh_case("datetime2", "2026-08-30 07:05:09 30/08/26 0 -1 3600 5400 30 0") sh_case("datetime2", "2026-08-30 07:05:09 30/08/26 0 -1 3600 5400 30 0")
sh_case("textsplit", "1 1 1 3 1 1 1 1 1 1 1") sh_case("textsplit", "1 1 1 3 1 1 1 1 1 1 1")