ludic/selfhost/tests/vector.ludic
Orkuncakilkaya 121053e179
Some checks failed
docs / build-and-deploy (push) Has been cancelled
feat(stdlib): 2D Vector type + Vector.* namespace (issue #25)
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>
2026-08-30 02:09:38 +03:00

27 lines
1.2 KiB
Text

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