Files
sx/examples/0030-basic-builtins.sx
agra 59f0aa7716 std: restructure — std/ modules, namespace tail, std/xml.sx
allocators/fs/process/socket/log/trace/test move under modules/std/
(allocators.sx becomes std/mem.sx; the Allocator protocol moves into
the std.sx prelude, impls stay in mem.sx). New std/xml.sx holds
xml_escape as xml.escape. std.sx gains the carried namespace tail —
flat-importing std.sx now also provides mem./xml./log. — with the
remaining modules (fs/process/socket/json/cli/hash/test) deferred from
the tail until the global last-wins maps are fully own-wins (pulling
them into every closure collides bare names corpus-wide; they stay
direct imports: modules/std/fs.sx etc.). log.sx's internal emit
renamed log_emit (it clobbered consumer fns named emit program-wide).
bundle.sx uses xml.escape via the carried alias. Consumer import paths
swept mechanically; .ir snapshots recaptured for the larger std
closure. m3te + game build unchanged.
2026-06-11 06:10:59 +03:00

128 lines
3.3 KiB
Plaintext

#import "modules/std.sx";
#import "modules/math/math.sx";
#import "modules/compiler.sx";
#import "modules/std/test.sx";
pkg :: #import "modules/testpkg";
Point :: struct { x, y: s32; }
Color :: enum { red; green; blue; }
Shape :: enum {
circle: f32;
rect: struct { w, h: f32; };
none;
}
main :: () {
// ========================================================
// 7. BUILT-IN FUNCTIONS
// ========================================================
print("=== 7. Builtins ===\n");
// out
out("out-ok\n");
// 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));
// align_of
print("alignof-u8: {}\n", align_of(u8));
print("alignof-s32: {}\n", align_of(s32));
print("alignof-s64: {}\n", align_of(s64));
print("alignof-struct: {}\n", align_of(Point));
// type_of + category matching
tv := 42;
ttype := type_of(tv);
if ttype == {
case int: print("typeof: int\n");
case float: print("typeof: float\n");
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 on struct
print("fieldcount: {}\n", field_count(Point));
// 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 };
out("fieldval0: ");
out(any_to_string(field_value(fv_pt, 0)));
out("\n");
out("fieldval1: ");
out(any_to_string(field_value(fv_pt, 1)));
out("\n");
// 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);
}