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

@ -0,0 +1,21 @@
---
id: type-vector
name: Vector
category: types
kind: type
tokens: Vector
sig: Vector
tip: A 2D vector — two fixed components (x, y), copied by value.
order: 13
---
<code>Vector</code> is a 2D vector: two Q16.16 <code>fixed</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 positions, velocities, directions, and offsets. Build one with <code>Vector.make(x, y)</code> (or <code>Vector.zero()</code>), read the parts with <code>Vector.x</code> / <code>Vector.y</code>, and combine them with the <code>Vector.*</code> math — add, scale, dot, length, normalize, rotate, and the rest. Every operation is deterministic fixed-point, so results are bit-identical on every platform.
```ludic
program Demo {
handler Step phase Update {
let velocity: Vector = Vector.scale(Vector.from_angle(heading), speed)
position = Vector.add(position, velocity)
}
}
```

View file

@ -0,0 +1,7 @@
---
id: vector
title: Vector
order: 7
---
2D vector math for positions, velocities, and directions. A <code>Vector</code> is a pair of Q16.16 fixed components packed into one value, so it is copied by value and never allocates. Every operation is deterministic integer fixed-point — bit-identical on every platform, the same guarantee the rest of the runtime gives. Arguments are positional; angles are in radians.

View file

@ -0,0 +1,22 @@
---
id: vector-add
name: Vector.add
category: vector
kind: namespace-method
tokens: Vector.add
sig: Vector.add(a, b) -> Vector
tip: Component-wise sum a + b.
order: 4
ns: Vector
member: add
---
Returns the component-wise sum <code>a + b</code> — the workhorse for stepping a position by a velocity each frame.
```ludic
program Demo {
handler Step phase Update {
pos = Vector.add(pos, velocity)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: vector-angle
name: Vector.angle
category: vector
kind: namespace-method
tokens: Vector.angle
sig: Vector.angle(v) -> fixed
tip: The angle of a vector in radians.
order: 13
ns: Vector
member: angle
---
Returns the angle of <code>v</code> in radians, measured from +x, in the range -pi..pi (via <code>atan2</code>). Feed it back into <code>Vector.from_angle</code> or <code>Vector.rotate</code>.
```ludic
program Demo {
handler Step phase Update {
let heading = Vector.angle(velocity)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: vector-distance
name: Vector.distance
category: vector
kind: namespace-method
tokens: Vector.distance
sig: Vector.distance(a, b) -> fixed
tip: The distance between two points.
order: 9
ns: Vector
member: distance
---
Returns the distance between points <code>a</code> and <code>b</code> — <code>Vector.length(Vector.sub(a, b))</code>. Handy for range checks and proximity triggers.
```ludic
program Demo {
handler Step phase Update {
if Vector.distance(pos, target) < 8.0 { arrived() }
}
}
```

View file

@ -0,0 +1,22 @@
---
id: vector-dot
name: Vector.dot
category: vector
kind: namespace-method
tokens: Vector.dot
sig: Vector.dot(a, b) -> fixed
tip: The dot product of two vectors.
order: 7
ns: Vector
member: dot
---
Returns the dot product <code>ax*bx + ay*by</code> as a <code>fixed</code>. Zero when the vectors are perpendicular; use it to project one vector onto another or to test facing.
```ludic
program Demo {
handler Step phase Update {
let facing = Vector.dot(forward, toTarget)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: vector-from_angle
name: Vector.from_angle
category: vector
kind: namespace-method
tokens: Vector.from_angle
sig: Vector.from_angle(a) -> Vector
tip: A unit vector pointing at an angle.
order: 14
ns: Vector
member: from_angle
---
Returns the unit vector <code>(cos a, sin a)</code> pointing at <code>a</code> radians. Scale it by a speed to launch a projectile in a chosen direction.
```ludic
program Demo {
handler Step phase Update {
let shot = Vector.scale(Vector.from_angle(aim), 200.0)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: vector-length
name: Vector.length
category: vector
kind: namespace-method
tokens: Vector.length
sig: Vector.length(v) -> fixed
tip: The length (magnitude) of a vector.
order: 8
ns: Vector
member: length
---
Returns the Euclidean length of <code>v</code>, computed with the deterministic fixed-point square root. When you only need to compare magnitudes, comparing <code>Vector.dot(v, v)</code> avoids the square root.
```ludic
program Demo {
handler Step phase Update {
let speed = Vector.length(velocity)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: vector-lerp
name: Vector.lerp
category: vector
kind: namespace-method
tokens: Vector.lerp
sig: Vector.lerp(a, b, t) -> Vector
tip: Linear interpolation between two vectors.
order: 11
ns: Vector
member: lerp
---
Returns the component-wise linear interpolation between <code>a</code> and <code>b</code> by <code>t</code> (a <code>fixed</code> in 0.0..1.0). Great for smoothing a camera toward a target.
```ludic
program Demo {
handler Step phase Update {
camera = Vector.lerp(camera, target, 0.1)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: vector-make
name: Vector.make
category: vector
kind: namespace-method
tokens: Vector.make
sig: Vector.make(x, y) -> Vector
tip: Build a vector from x and y components.
order: 0
ns: Vector
member: make
---
Builds a <code>Vector</code> from its <code>x</code> and <code>y</code> components (both <code>fixed</code>). This is the usual way to make a position or a velocity; read the parts back with <code>Vector.x</code> and <code>Vector.y</code>.
```ludic
program Demo {
handler Step phase Update {
let pos = Vector.make(64.0, 32.0)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: vector-normalize
name: Vector.normalize
category: vector
kind: namespace-method
tokens: Vector.normalize
sig: Vector.normalize(v) -> Vector
tip: A unit vector in the same direction.
order: 10
ns: Vector
member: normalize
---
Returns a unit-length vector pointing the same way as <code>v</code>. The zero vector maps to itself, so this never divides by zero. Scale the result by a speed to build a velocity.
```ludic
program Demo {
handler Step phase Update {
let dir = Vector.normalize(Vector.sub(target, pos))
}
}
```

View file

@ -0,0 +1,22 @@
---
id: vector-rotate
name: Vector.rotate
category: vector
kind: namespace-method
tokens: Vector.rotate
sig: Vector.rotate(v, angle) -> Vector
tip: Rotate a vector by an angle in radians.
order: 12
ns: Vector
member: rotate
---
Returns <code>v</code> rotated counter-clockwise by <code>angle</code> radians, using the deterministic fixed-point sine table. Positive angles turn toward +y.
```ludic
program Demo {
handler Step phase Update {
let aimed = Vector.rotate(forward, spin)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: vector-scale
name: Vector.scale
category: vector
kind: namespace-method
tokens: Vector.scale
sig: Vector.scale(v, s) -> Vector
tip: Scale a vector by a scalar.
order: 6
ns: Vector
member: scale
---
Returns <code>v</code> scaled by the <code>fixed</code> factor <code>s</code>. Multiply a direction by a speed, or a velocity by delta-time.
```ludic
program Demo {
handler Step phase Update {
let step = Vector.scale(direction, speed)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: vector-sub
name: Vector.sub
category: vector
kind: namespace-method
tokens: Vector.sub
sig: Vector.sub(a, b) -> Vector
tip: Component-wise difference a - b.
order: 5
ns: Vector
member: sub
---
Returns the component-wise difference <code>a - b</code> — the vector that points from <code>b</code> to <code>a</code>. Pair it with <code>Vector.length</code> or <code>Vector.normalize</code> to get a distance or a direction.
```ludic
program Demo {
handler Step phase Update {
let toTarget = Vector.sub(target, pos)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: vector-x
name: Vector.x
category: vector
kind: namespace-method
tokens: Vector.x
sig: Vector.x(v) -> fixed
tip: The x component of a vector.
order: 2
ns: Vector
member: x
---
Returns the <code>x</code> component of <code>v</code> as a <code>fixed</code>. The companion of <code>Vector.y</code>.
```ludic
program Demo {
handler Step phase Update {
let px = Vector.x(pos)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: vector-y
name: Vector.y
category: vector
kind: namespace-method
tokens: Vector.y
sig: Vector.y(v) -> fixed
tip: The y component of a vector.
order: 3
ns: Vector
member: y
---
Returns the <code>y</code> component of <code>v</code> as a <code>fixed</code>. The companion of <code>Vector.x</code>.
```ludic
program Demo {
handler Step phase Update {
let py = Vector.y(pos)
}
}
```

View file

@ -0,0 +1,22 @@
---
id: vector-zero
name: Vector.zero
category: vector
kind: namespace-method
tokens: Vector.zero
sig: Vector.zero() -> Vector
tip: The zero vector, (0, 0).
order: 1
ns: Vector
member: zero
---
Returns the zero vector <code>(0, 0)</code> — a convenient identity for accumulating a sum of vectors or clearing a velocity.
```ludic
program Demo {
handler Step phase Update {
var velocity = Vector.zero()
}
}
```

View file

@ -73,6 +73,7 @@ function lbl(pfx: pointer) -> pointer { let r = (pfx + itoa(ll_lbl)); ll_lbl =
function llty(t: pointer) -> pointer { 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 == "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

@ -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) } if is_hash_ns(meth) { return emit_hash_ns(meth, e) }
perr(`unknown builtin Hash.{meth}`) 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 var bare: pointer = null
let labels = new []pointer let labels = new []pointer
if (ns == "Screen") { 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
}
}

View file

@ -329,7 +329,8 @@
"type-slices", "type-slices",
"type-string", "type-string",
"type-void", "type-void",
"type-words" "type-words",
"type-vector"
], ],
"world": [ "world": [
"world-get", "world-get",
@ -348,5 +349,22 @@
"world-attach", "world-attach",
"world-detach", "world-detach",
"world-query_next" "world-query_next"
],
"vector": [
"vector-make",
"vector-zero",
"vector-x",
"vector-y",
"vector-add",
"vector-sub",
"vector-scale",
"vector-dot",
"vector-length",
"vector-distance",
"vector-normalize",
"vector-lerp",
"vector-rotate",
"vector-angle",
"vector-from_angle"
] ]
} }

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" "and", "or", "not", "break", "continue", "new", "emit", "cancel"
) )
val PRIMITIVES = setOf("int", "long", "fixed", "bool", "entity", "string", "pointer", "byte", "words", "fixeds", "pointers", "void") val PRIMITIVES = setOf("int", "long", "fixed", "bool", "entity", "string", "pointer", "byte", "words", "fixeds", "pointers", "Vector", "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)\\b" }, { "name": "storage.type.ludic", "match": "\\b(program|property|model|enum|ui|const|var|let|function|handler|entry|state)\\b" },
{ "name": "support.type.primitive.ludic", "match": "\\b(int|long|fixed|bool|entity|string|pointer|byte|words|fixeds|pointers|void)\\b" }, { "name": "support.type.primitive.ludic", "match": "\\b(int|long|fixed|bool|entity|string|pointer|byte|words|fixeds|pointers|Vector|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)\\b" }, { "name": "storage.type.ludic", "match": "\\b(program|property|model|enum|ui|const|var|let|function|handler|entry|state)\\b" },
{ "name": "support.type.primitive.ludic", "match": "\\b(int|long|fixed|bool|entity|string|pointer|byte|words|fixeds|pointers|void)\\b" }, { "name": "support.type.primitive.ludic", "match": "\\b(int|long|fixed|bool|entity|string|pointer|byte|words|fixeds|pointers|Vector|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 == "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 == "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")
@ -1301,6 +1301,23 @@ program LudicLsp {
if (meth == "fnv1a_64") { return "Hash.fnv1a_64(s) -> long" } if (meth == "fnv1a_64") { return "Hash.fnv1a_64(s) -> long" }
if (meth == "mix64") { return "Hash.mix64(x) -> long" } if (meth == "mix64") { return "Hash.mix64(x) -> long" }
} }
if (ns == "Vector") {
if (meth == "make") { return "Vector.make(x, y) -> Vector" }
if (meth == "zero") { return "Vector.zero() -> Vector" }
if (meth == "x") { return "Vector.x(v) -> fixed" }
if (meth == "y") { return "Vector.y(v) -> fixed" }
if (meth == "add") { return "Vector.add(a, b) -> Vector" }
if (meth == "sub") { return "Vector.sub(a, b) -> Vector" }
if (meth == "scale") { return "Vector.scale(v, s) -> Vector" }
if (meth == "dot") { return "Vector.dot(a, b) -> fixed" }
if (meth == "length") { return "Vector.length(v) -> fixed" }
if (meth == "distance") { return "Vector.distance(a, b) -> fixed" }
if (meth == "normalize") { return "Vector.normalize(v) -> Vector" }
if (meth == "lerp") { return "Vector.lerp(a, b, t) -> Vector" }
if (meth == "rotate") { return "Vector.rotate(v, angle) -> Vector" }
if (meth == "angle") { return "Vector.angle(v) -> fixed" }
if (meth == "from_angle") { return "Vector.from_angle(a) -> Vector" }
}
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)" }
@ -2287,7 +2304,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, "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, "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", 0 "and","or","not","break","continue","new","emit","cancel", 0
}; };
static const char* LUDIC_TYPES[] = { static const char* LUDIC_TYPES[] = {
"int","long","fixed","bool","entity","string","pointer","byte","words","fixeds","pointers","void", 0 "int","long","fixed","bool","entity","string","pointer","byte","words","fixeds","pointers","Vector","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

@ -25,6 +25,7 @@ function selfhost_frags() -> []pointer {
push(f, "selfhost/emit_intrin.ludic") push(f, "selfhost/emit_intrin.ludic")
push(f, "selfhost/emit_intrin2.ludic") push(f, "selfhost/emit_intrin2.ludic")
push(f, "selfhost/emit_math.ludic") push(f, "selfhost/emit_math.ludic")
push(f, "selfhost/emit_vector.ludic")
push(f, "selfhost/emit_text.ludic") push(f, "selfhost/emit_text.ludic")
push(f, "selfhost/emit_hash.ludic") push(f, "selfhost/emit_hash.ludic")
push(f, "selfhost/emit_list.ludic") push(f, "selfhost/emit_list.ludic")

View file

@ -46,6 +46,7 @@ function cmd_selfhost_test() -> int {
sh_case("memsys", "65 66 0 99 12345 1") sh_case("memsys", "65 66 0 99 12345 1")
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("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")
sh_case("hash", "-2128831035 -468965076 114400290 114400290 0 -873187034 1095738169 0 1364076727 -2114883783 -845898438 -845898438 -78065325 -78057399 -3750763034362895579 -5808556873153909620 -4100651535478758590 -4100651535478758590 0 -5451962507482445012 7256831767414464289") sh_case("hash", "-2128831035 -468965076 114400290 114400290 0 -873187034 1095738169 0 1364076727 -2114883783 -845898438 -845898438 -78065325 -78057399 -3750763034362895579 -5808556873153909620 -4100651535478758590 -4100651535478758590 0 -5451962507482445012 7256831767414464289")
sh_case("long", "1000000000000 1000000000001 1000000000005 3000000000000 1 1 1 -1000000000000 1000000 13") sh_case("long", "1000000000000 1000000000001 1000000000005 3000000000000 1 1 1 -1000000000000 1000000 13")