This commit is contained in:
agra
2026-02-15 23:46:43 +02:00
parent 7da3ecfa7c
commit 3e1e764753
5 changed files with 961 additions and 91 deletions

View File

@@ -38,6 +38,8 @@ Perms :: enum flags { read; write; execute; }
Status :: enum u8 { ok; err; timeout; }
WindowFlags :: enum flags u32 { vsync :: 64; resizable :: 4; hidden :: 128; }
// --- Top-level functions ---
add :: (a: s32, b: s32) -> s32 { a + b; }
@@ -72,13 +74,22 @@ early_return :: (x: s32) -> s32 {
x;
}
// #run compile-time constant
CT_VAL :: #run add(10, 15);
vec3 :: (x: f32, y: f32, z: f32) -> Vector(3, f32) {
.[x, y, z];
}
// #insert helper
// #run compile-time constants
CT_VAL :: #run add(10, 15);
CT_MUL :: #run mul(6, 7);
CT_CHAIN :: #run add(CT_VAL, 5);
// #insert helpers
gen_code :: () -> string {
return "print(\"insert-ok\\n\");";
}
gen_val :: () -> string {
return "print(\"insert-gen: {}\\n\", 42);";
}
// ============================================================
main :: {
@@ -128,6 +139,18 @@ END;
c : Color = .green;
print("enum-lit: {}\n", c);
// Null pointer
np : *s32 = null;
print("null-ptr: {}\n", np);
// String .len
slen := "hello";
print("string-len: {}\n", slen.len);
// Empty string .len
es := "";
print("empty-string: {}\n", es.len);
// ========================================================
// 2. OPERATORS & PRECEDENCE
// ========================================================
@@ -155,16 +178,37 @@ END;
print("chain-gt: {}\n", 100 > v > 0);
print("chain-mixed: {}\n", 100 > v >= 0);
// Equality chains
print("eq-chain: {}\n", 5 == 5 == 5);
print("eq-chain-f: {}\n", 5 == 5 == 6);
// Bitwise
print("band: {}\n", 0xFF & 0x0F);
print("bor: {}\n", 1 | 2 | 4);
// Bitwise on variables
bv1 := 0xFF;
bv2 := 0x0F;
print("band-var: {}\n", bv1 & bv2);
bv3 := 1;
bv4 := 6;
print("bor-var: {}\n", bv3 | bv4);
// Modulo on variables
mv1 := 17;
mv2 := 5;
print("mod-var: {}\n", mv1 % mv2);
// Logical (short-circuit)
print("and: {}\n", true and true);
print("and-false: {}\n", true and false);
print("or: {}\n", false or true);
print("or-false: {}\n", false or false);
// Short-circuit verification
print("short-and: {}\n", false and true);
print("short-or: {}\n", true or false);
// Compound assignment
ca := 10;
ca += 5;
@@ -185,6 +229,36 @@ END;
small : u8 = xx big2;
print("xx-cast: {}\n", small);
// Implicit widening conversions
wu : u8 = 200;
ws : s64 = wu;
print("widen-u8-s64: {}\n", ws);
wi3 : s32 = 42;
wf : f64 = wi3;
print("widen-s32-f64: {}\n", wf);
wf32 : f32 = 1.5;
wf64 : f64 = wf32;
print("widen-f32-f64: {}\n", wf64);
wu2 : u8 = 100;
ws2 : s16 = wu2;
print("widen-u8-s16: {}\n", ws2);
// More xx narrowing
xl : s64 = 12345;
xs : s32 = xx xl;
print("xx-s64-s32: {}\n", xs);
xd : f64 = 1.5;
xf : f32 = xx xd;
print("xx-f64-f32: {}\n", xf);
xdf : f64 = 7.9;
xdi : s32 = xx xdf;
print("xx-f64-s32: {}\n", xdi);
// ========================================================
// 3. TYPE SYSTEM
// ========================================================
@@ -241,6 +315,13 @@ END;
ec : Color = .red;
print("enum: {}\n", ec);
// Enum comparison
ce1 : Color = .red;
ce2 : Color = .red;
ce3 : Color = .blue;
print("enum-eq: {}\n", ce1 == ce2);
print("enum-neq: {}\n", ce1 != ce3);
// Backing type
st : Status = .err;
print("backing: {}\n", st);
@@ -257,6 +338,16 @@ END;
sh = .none;
print("void-variant: {}\n", sh);
// Variant reassignment
sh = .circle(1.0);
print("reassign: {}\n", sh);
sh = .rect(.{ 5, 3 });
print("reassign2: {}\n", sh);
// Type-prefix construction
tp := Shape.circle(2.5);
print("enum-prefix: {}\n", tp);
// Pattern matching
sh2 : Shape = .rect(.{ 5, 3 });
if sh2 == {
@@ -274,6 +365,15 @@ END;
}
print("match-expr: {}\n", ms);
// Match expression with else
me_val := 42;
me_res := if me_val == {
case 1: 10;
case 2: 20;
else: 99;
}
print("match-expr-else: {}\n", me_res);
// Payload capture (block form)
sh4 : Shape = .circle(9.5);
if sh4 == {
@@ -282,6 +382,14 @@ END;
case .none: print("capture: none\n");
}
// Payload capture (arrow form)
sh_ca : Shape = .circle(7.5);
if sh_ca == {
case .circle: (r) => print("capture-arrow: {}\n", r);
case .rect: (sz) => print("capture-arrow: rect\n");
case .none: print("capture-arrow: none\n");
}
// else arm in match
num := 42;
if num == {
@@ -298,6 +406,26 @@ END;
case 3: print("int-match: three\n");
}
// Integer match with else
im_code := 99;
if im_code == {
case 1: print("int-match-else: one\n");
case 2: print("int-match-else: two\n");
else: print("int-match-else: unknown\n");
}
// Bool pattern matching
bm := true;
if bm == {
case true: print("bool-match-t: yes\n");
case false: print("bool-match-t: no\n");
}
bm2 := false;
if bm2 == {
case true: print("bool-match-f: yes\n");
case false: print("bool-match-f: no\n");
}
// Bool conditional
flag := true;
if flag { print("bool: true\n"); }
@@ -321,11 +449,21 @@ END;
print("arr[2]: {}\n", arr[2]);
print("arr.len: {}\n", arr.len);
// Array element assignment
aa : [3]s32 = .[1, 2, 3];
aa[1] = 99;
print("arr-assign: {}\n", aa);
// --- Slices ---
sl : []s32 = .[1, 2, 3, 4, 5];
print("sl[0]: {}\n", sl[0]);
print("sl.len: {}\n", sl.len);
// Slice element write
sla : []s32 = .[10, 20, 30];
sla[1] = 55;
print("sl-assign: {}\n", sla);
// Subslicing
sub := arr[1..4];
print("sub: {}\n", sub);
@@ -334,9 +472,17 @@ END;
tail := arr[2..];
print("tail: {}\n", tail);
// Slice of slice
sos : []s32 = .[10, 20, 30, 40, 50];
mid := sos[1..4];
inner := mid[0..2];
print("slice-of-slice: {}\n", inner);
// String subslicing
msg := "hello world";
print("strsub: {}\n", msg[6..11]);
print("str-prefix: {}\n", msg[..5]);
print("str-suffix: {}\n", msg[6..]);
// --- Pointers ---
pv := Point.{ 10, 20 };
@@ -351,6 +497,32 @@ END;
print("mp[0]: {}\n", mp[0]);
print("mp[3]: {}\n", mp[3]);
// Many-pointer write
mpw : [5]s32 = .[10, 20, 30, 40, 50];
mpw_ptr : [*]s32 = @mpw[0];
mpw_ptr[2] = 99;
print("mp-write: {}\n", mpw[2]);
// --- Vectors ---
vc := vec3(1, 3, 2);
print("vec-construct: {}\n", vc);
va := vec3(1, 2, 3);
vb := vec3(4, 5, 6);
print("vec-add: {}\n", va + vb);
print("vec-sub: {}\n", vec3(5, 5, 5) - vec3(1, 2, 3));
print("vec-mul: {}\n", vec3(2, 3, 4) * vec3(1, 2, 3));
print("vec-div: {}\n", vec3(10, 9, 8) / vec3(2, 3, 4));
print("vec-scalar: {}\n", vec3(1, 3, 2) * 2.0);
print("vec-neg: {}\n", -vec3(1, 3, 2));
ve := vec3(10, 20, 30);
print("vec-x: {}\n", ve.x);
print("vec-y: {}\n", ve.y);
print("vec-z: {}\n", ve.z);
print("vec-idx: {}\n", ve[1]);
// ========================================================
// 4. CONTROL FLOW
// ========================================================
@@ -360,11 +532,38 @@ END;
ite := if true then 1 else 2;
print("ite: {}\n", ite);
// If-then-else both branches
ie_a := if true then 10 else 20;
ie_b := if false then 10 else 20;
print("ite-both: {} {}\n", ie_a, ie_b);
// If block
if 1 < 2 {
print("if-block: yes\n");
}
// If without else (statement)
if false { print("should-not-print\n"); }
print("if-no-else: after\n");
// Nested if
nx := 10;
if nx > 5 {
if nx > 8 {
print("nested-if: deep\n");
}
}
// If-else-if chain
eiv := 2;
if eiv == 1 {
print("if-else-if: first\n");
} else if eiv == 2 {
print("if-else-if: second\n");
} else {
print("if-else-if: other\n");
}
// If block as expression
ibe := 10 + if true { 5; } else { 0; };
print("if-block-expr: {}\n", ibe);
@@ -374,6 +573,10 @@ END;
while wi < 5 { wi += 1; }
print("while: {}\n", wi);
// While with false condition (never executes)
while false { print("should-not-print\n"); }
print("while-false: skipped\n");
// While with break
wb := 0;
while wb < 100 {
@@ -392,7 +595,44 @@ END;
}
print("while-continue: {}\n", wsum);
// For loop basic (using write like example 19)
// While sum 1..10
wsum2 := 0;
wi2 := 1;
while wi2 <= 10 {
wsum2 += wi2;
wi2 += 1;
}
print("while-sum: {}\n", wsum2);
// Nested while
nw_outer := 0;
nw_count := 0;
while nw_outer < 3 {
nw_inner := 0;
while nw_inner < 3 {
nw_count += 1;
nw_inner += 1;
}
nw_outer += 1;
}
print("nested-while: {}\n", nw_count);
// Nested while with break in inner
nb_outer := 0;
nb_icount := 0;
while nb_outer < 5 {
nb_i := 0;
while nb_i < 5 {
if nb_i == 1 { break; }
nb_i += 1;
}
nb_icount += nb_i;
nb_outer += 1;
if nb_outer == 2 { break; }
}
print("nested-break: {} {}\n", nb_outer, nb_icount);
// For loop basic
farr : [4]s32 = .[10, 20, 30, 40];
write("for:");
for farr {
@@ -439,6 +679,44 @@ END;
}
write("\n");
// For on slice
fsl : []s32 = .[10, 20, 30];
write("for-slice:");
for fsl {
print(" {}", it);
}
write("\n");
// For on slice with it_index
write("for-slice-idx:");
for fsl {
print(" {}:{}", it_index, it);
}
write("\n");
// Nested for
nf_a : [2]s32 = .[0, 1];
nf_b : [2]s32 = .[0, 1];
write("for-nested:");
for nf_a {
oa := it;
for nf_b {
print(" ({},{})", oa, it);
}
}
write("\n");
// For with break preserving it_index
fbi : [5]s32 = .[10, 20, 30, 40, 50];
fbi_idx := 0;
for fbi {
if it == 30 { fbi_idx = it_index; break; }
}
print("for-break-idx: {}\n", fbi_idx);
// Multiple print placeholders
print("multi: {} {} {}\n", 1, 2, 3);
// ========================================================
// 5. FUNCTIONS & DECLARATIONS
// ========================================================
@@ -470,6 +748,7 @@ END;
// Generic — single param
print("generic-s32: {}\n", identity(42));
print("generic-f32: {}\n", identity(1.5));
print("generic-bool: {}\n", identity(true));
// Generic — multiple params
print("generic-multi: {}\n", pair_add(10, 20));
@@ -482,6 +761,13 @@ END;
halve :: (x: f32) -> f32 => x / 2.0;
print("lambda-ret: {}\n", halve(10.0));
// Local function (non-lambda)
local_add :: (a: s32, b: s32) -> s32 { a + b; }
print("local-fn: {}\n", local_add(3, 4));
// Nested function calls
print("fn-nested: {}\n", add(mul(2, 3), mul(4, 5)));
// Variadic (typed)
print("varargs: {}\n", typed_sum(1, 2, 3, 4, 5));
@@ -509,6 +795,14 @@ END;
}
print("outer: {}\n", sv);
// Shadow with different type
st_v := 42;
print("shadow-type: {}\n", st_v);
{
st_v := 3.14;
print("shadow-type: {}\n", st_v);
}
// Nested scopes (3 levels)
nv := 1;
{
@@ -521,6 +815,15 @@ END;
}
print("nest1: {}\n", nv);
// Scope isolation
{ iso := 100; print("scope-isolate: {}\n", iso); }
// Reuse name after scope exit
sr := 1;
print("scope-reuse: {}\n", sr);
{ sr := 2; print("scope-reuse: {}\n", sr); }
print("scope-reuse: {}\n", sr);
// Multiple defers (LIFO order)
{
defer print("defer-c\n");
@@ -528,6 +831,14 @@ END;
defer print("defer-a\n");
}
// Four defers
{
defer print("d1\n");
defer print("d2\n");
defer print("d3\n");
defer print("d4\n");
}
// Defer in nested scopes
{
defer print("outer-defer\n");
@@ -536,6 +847,12 @@ END;
}
}
// Defer in if block
if true {
defer print("defer-in-if: deferred\n");
print("defer-in-if: body\n");
}
// ========================================================
// 7. BUILT-IN FUNCTIONS
// ========================================================
@@ -546,10 +863,12 @@ END;
// sqrt
print("sqrt: {}\n", sqrt(9.0));
print("sqrt-f64: {}\n", sqrt(16.0));
// size_of
print("sizeof-s32: {}\n", size_of(s32));
print("sizeof-f64: {}\n", size_of(f64));
print("sizeof-struct: {}\n", size_of(Point));
// type_of + category matching
tv := 42;
@@ -560,16 +879,58 @@ END;
else: print("typeof: other\n");
}
// type_of — float
tf := 3.14;
if type_of(tf) == {
case float: print("typeof-float: float\n");
else: print("typeof-float: other\n");
}
// type_of — string
ts := "hello";
if type_of(ts) == {
case string: print("typeof-string: string\n");
else: print("typeof-string: other\n");
}
// type_of — bool
tb := true;
if type_of(tb) == {
case bool: print("typeof-bool: bool\n");
else: print("typeof-bool: other\n");
}
// type_of — struct
tst := Point.{ 1, 2 };
if type_of(tst) == {
case struct: print("typeof-struct: struct\n");
else: print("typeof-struct: other\n");
}
// type_of — enum
ten : Color = .red;
if type_of(ten) == {
case enum: print("typeof-enum: enum\n");
else: print("typeof-enum: other\n");
}
// type_name
print("typename: {}\n", type_name(Point));
// field_count
// field_count on struct
print("fieldcount: {}\n", field_count(Point));
// field_name
// field_count on enum
print("fieldcount-enum: {}\n", field_count(Color));
// field_name on struct
print("fieldname0: {}\n", field_name(Point, 0));
print("fieldname1: {}\n", field_name(Point, 1));
// field_name on enum
print("fieldname-enum0: {}\n", field_name(Color, 0));
print("fieldname-enum2: {}\n", field_name(Color, 2));
// field_value (use any_to_string to avoid sext-on-Any bug)
fv_pt := Point.{ 11, 22 };
write("fieldval0: ");
@@ -579,13 +940,21 @@ END;
write(any_to_string(field_value(fv_pt, 1)));
write("\n");
// field_index on enum
// field_index on plain enum
fi_c : Color = .green;
print("fieldidx: {}\n", field_index(Color, fi_c));
// field_index on tagged enum
fi_sh : Shape = .circle(1.0);
print("fieldidx-tagged: {}\n", field_index(Shape, fi_sh));
fi_sh2 : Shape = .none;
print("fieldidx-tagged2: {}\n", field_index(Shape, fi_sh2));
// cast
cval : f64 = 3.7;
print("cast: {}\n", cast(s32) cval);
cv2 : s32 = 42;
print("cast-int-f64: {}\n", cast(f64) cv2);
// ========================================================
// 8. COMPILE-TIME
@@ -595,9 +964,18 @@ END;
// #run constant
print("run-const: {}\n", CT_VAL);
// #run with expression
print("run-expr: {}\n", CT_MUL);
// #run chained dependency
print("run-chain: {}\n", CT_CHAIN);
// #insert with function
#insert gen_code();
// #insert additional
#insert gen_val();
// ========================================================
// 9. FLAGS
// ========================================================
@@ -611,8 +989,29 @@ END;
if perm & .read { print("has-read: yes\n"); }
if perm & .execute { print("has-exec: yes\n"); }
// Test flag negative
pt : Perms = .write;
if pt & .read {
print("flags-neg: has-read\n");
} else {
print("flags-neg: no-read\n");
}
// Single flag
ps : Perms = .execute;
print("flags-single: {}\n", ps);
// All flags
pall : Perms = .read | .write | .execute;
print("flags-all: {}\n", pall);
// Cast to int
print("flags-raw: {}\n", cast(s64) perm);
// Flags with explicit values
wf : WindowFlags = .vsync | .resizable;
print("flags-explicit: {}\n", wf);
print("flags-explicit-raw: {}\n", cast(s64) wf);
print("=== DONE ===\n");
}