more forward declarations

This commit is contained in:
agra
2026-02-24 17:37:52 +02:00
parent 97475d6cfe
commit 566121c45a
13 changed files with 867 additions and 88 deletions

View File

@@ -1,6 +1,22 @@
#import "modules/std.sx";
math :: #import "modules/math";
dot :: (a: Vector(3,f32), b: Vector(3,f32)) -> f32 {
a.x*b.x + a.y*b.y + a.z*b.z;
}
cross :: (a: Vector(3,f32), b: Vector(3,f32)) -> Vector(3,f32) {
.[a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x];
}
length :: (v: Vector(3,f32)) -> f32 {
math.sqrt(dot(v, v));
}
normalize :: (v: Vector(3,f32)) -> Vector(3,f32) {
v / length(v);
}
vec3 :: (x:f32, y:f32, z:f32) -> Vector(3,f32) {
.[x, y, z];
}
@@ -10,19 +26,19 @@ main :: () {
b := vec3(0, 1, 0);
// dot product
d := math.dot(a, b);
d := dot(a, b);
print("dot: {}\n", d);
// cross product
cr := math.cross(a, b);
cr := cross(a, b);
print("cross: {}\n", cr);
// length
v := vec3(3, 4, 0);
len := math.length(v);
len := length(v);
print("length: {}\n", len);
// normalize
n := math.normalize(v);
n := normalize(v);
print("norm: {}\n", n);
}

View File

@@ -12,7 +12,7 @@ sumOf10 :: () -> s32 {
someSum :: #run sumOf10();
main :: {
main :: () {
// Basic while loop: count to 5
i := 0;
while i < 5 {

View File

@@ -1,6 +1,6 @@
#import "modules/std.sx";
main :: {
main :: () {
i := 0;
while i < 10 {
i+=1;

View File

@@ -31,6 +31,7 @@ main :: () {
// 1. Basic closure with capture
offset := 100;
add_offset := closure((x: s64) -> s64 => x + offset);
print("basic: {}\n", add_offset(42));
// 2. Capture by value (snapshot semantics)

View File

@@ -1,5 +1,5 @@
#import "modules/std.sx";
#import "modules/math";
#import "modules/math/math.sx";
pkg :: #import "modules/testpkg";
// ============================================================
@@ -277,7 +277,7 @@ SumBox :: struct ($T: Type/Summable) {
}
// ============================================================
main :: {
main :: () {
// ========================================================
// 1. LITERALS
@@ -1977,7 +1977,6 @@ END;
if x > 100 { return 100; }
return x + offset;
});
// Workaround: assign result with explicit type so print wraps correctly
r1 : s64 = clamp(10);
r2 : s64 = clamp(0 - 5);
r3 : s64 = clamp(999);

25
examples/issue-0002.sx Normal file
View File

@@ -0,0 +1,25 @@
#import "modules/std.sx";
// Issue: nested field assignment through pointer
// self.inner.field = value should work when self is a pointer
Inner :: struct {
len: s64;
cap: s64;
}
Outer :: struct {
inner: Inner;
count: s64;
reset :: (self: *Outer) {
self.inner.len = 0; // error: field assignment target must be a variable
self.count += 1;
}
}
main :: () {
o := Outer.{ inner = Inner.{ len = 5, cap = 10 }, count = 0 };
o.reset();
print("{}\n", o.inner.len);
}

View File

@@ -1,5 +1,4 @@
// This file lives in modules/testpkg/ but imports modules/std.sx
// via cwd-relative path (not relative to this file's directory).
#import "modules/std.sx";
// This file lives in modules/testpkg/ and imports std relative to its directory.
#import "../std.sx";
cwd_greet :: () -> string { format("cwd-import-ok"); }

View File

@@ -0,0 +1,14 @@
#import "modules/std.sx";
Vec2 :: union {
data: [2]f32;
struct { x, y: f32; };
}
main :: () {
uv : Vec2 = ---;
uv.x = 1.0;
uv.y = 2.0;
print("promoted-x: {}\n", uv.x);
print("promoted-data0: {}\n", uv.data[0]);
}