extend default to s64
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#import "modules/std.sx";
|
||||
math :: #import "modules/std/math.sx";
|
||||
math :: #import "modules/math.sx";
|
||||
|
||||
vec3 :: (x:f32, y:f32, z:f32) -> Vector(3,f32) {
|
||||
.[x, y, z];
|
||||
|
||||
@@ -1,12 +1,27 @@
|
||||
#import "modules/std.sx";
|
||||
#import "modules/math.sx";
|
||||
|
||||
test :: () -> s32 {
|
||||
0;
|
||||
}
|
||||
|
||||
Vec4 :: Vector(4,f32);
|
||||
|
||||
main :: () {
|
||||
x:Type = f64;
|
||||
x : Type = f64;
|
||||
v:f64 = 3.2;
|
||||
print("{}\n", x);
|
||||
print("{}\n", v);
|
||||
|
||||
x= Vec4;
|
||||
x = Vec4;
|
||||
print("{}\n", x);
|
||||
|
||||
x = test;
|
||||
print("{}\n", x);
|
||||
}
|
||||
|
||||
// ** stdout **
|
||||
// f64
|
||||
// 3.200000
|
||||
// Vec4
|
||||
// () -> s32
|
||||
@@ -11,9 +11,12 @@ Color :: struct {
|
||||
main :: () {
|
||||
p := Point.{10, 20};
|
||||
c := Color.{255, 128, 0};
|
||||
pc := &p;
|
||||
print("p: {}\n", p);
|
||||
print("c: {}\n", c);
|
||||
print("n: {}\n", 42);
|
||||
print("s: {}\n", "hello");
|
||||
print("b: {}\n", true);
|
||||
print("&p: {}\n", pc);
|
||||
print("&p: {}\n", &p);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#import "modules/std.sx";
|
||||
|
||||
quickSort :: (items: []$T) {
|
||||
partition :: (items: []T, lo: s32, hi: s32) -> s32 {
|
||||
quick_sort :: (items: []$T) {
|
||||
partition :: (items: []T, lo: s64, hi: s64) -> s64 {
|
||||
pivot := items[hi];
|
||||
i := lo - 1;
|
||||
j := lo;
|
||||
@@ -21,7 +21,7 @@ quickSort :: (items: []$T) {
|
||||
i;
|
||||
}
|
||||
|
||||
sort :: (items: []T, lo: s32, hi: s32) {
|
||||
sort :: (items: []T, lo: s64, hi: s64) {
|
||||
if lo < hi {
|
||||
pi := partition(items, lo, hi);
|
||||
sort(items, lo, pi - 1);
|
||||
@@ -33,7 +33,7 @@ quickSort :: (items: []$T) {
|
||||
}
|
||||
|
||||
main :: () {
|
||||
arr : []s32 = .[333, 2, 3, 5, 2, 2, 3, 4, 5, 6, 6, 1];
|
||||
quickSort(arr);
|
||||
arr : []s64 = .[333, 2, 3, 5, 2, 2, 3, 4, 5, 6, 6, 1];
|
||||
quick_sort(arr);
|
||||
print("{}\n", arr);
|
||||
}
|
||||
|
||||
12
examples/24-list.sx
Normal file
12
examples/24-list.sx
Normal file
@@ -0,0 +1,12 @@
|
||||
#import "modules/std.sx";
|
||||
|
||||
List :: struct ($T: Type) {
|
||||
items: [*]T = .[];
|
||||
len: s32 = 0;
|
||||
cap: s32 = 0;
|
||||
}
|
||||
|
||||
main :: () {
|
||||
list := List(s32).{};
|
||||
print("{}\n", list);
|
||||
}
|
||||
31
examples/25-slices.sx
Normal file
31
examples/25-slices.sx
Normal file
@@ -0,0 +1,31 @@
|
||||
#import "modules/std.sx";
|
||||
|
||||
main :: () {
|
||||
arr : [5]s32 = .[3, 1, 4, 1, 5];
|
||||
print("arr.len = {}\n", arr.len);
|
||||
|
||||
// subslice array
|
||||
sub := arr[1..4];
|
||||
print("arr[1..4] = {}\n", sub);
|
||||
print("sub.len = {}\n", sub.len);
|
||||
|
||||
// open-ended
|
||||
head := arr[..3];
|
||||
tail := arr[2..];
|
||||
print("arr[..3] = {}\n", head);
|
||||
print("arr[2..] = {}\n", tail);
|
||||
|
||||
// slice of slice
|
||||
sl : []s32 = .[10, 20, 30, 40, 50];
|
||||
mid := sl[1..4];
|
||||
print("sl[1..4] = {}\n", mid);
|
||||
rest := mid[1..];
|
||||
print("mid[1..] = {}\n", rest);
|
||||
|
||||
// string subslicing
|
||||
msg := "hello world";
|
||||
word := msg[6..11];
|
||||
print("msg[6..11] = {}\n", word);
|
||||
prefix := msg[..5];
|
||||
print("msg[..5] = {}\n", prefix);
|
||||
}
|
||||
@@ -1,15 +1,15 @@
|
||||
Vector :: ($N: int, $T: Type) -> Type #builtin;
|
||||
write :: (str: string) -> void #builtin;
|
||||
sqrt :: (x: $T) -> T #builtin;
|
||||
size_of :: ($T: Type) -> s32 #builtin;
|
||||
alloc :: (size: s32) -> string #builtin;
|
||||
size_of :: ($T: Type) -> s64 #builtin;
|
||||
alloc :: (size: s64) -> string #builtin;
|
||||
type_of :: (val: $T) -> Type #builtin;
|
||||
type_name :: ($T: Type) -> string #builtin;
|
||||
field_count :: ($T: Type) -> s32 #builtin;
|
||||
field_name :: ($T: Type, idx: s32) -> string #builtin;
|
||||
field_value :: (s: $T, idx: s32) -> Any #builtin;
|
||||
field_count :: ($T: Type) -> s64 #builtin;
|
||||
field_name :: ($T: Type, idx: s64) -> string #builtin;
|
||||
field_value :: (s: $T, idx: s64) -> Any #builtin;
|
||||
|
||||
int_to_string :: (n: s32) -> string {
|
||||
int_to_string :: (n: s64) -> string {
|
||||
if n == 0 { return "0"; }
|
||||
neg := n < 0;
|
||||
v := if neg then 0 - n else n;
|
||||
@@ -35,8 +35,8 @@ bool_to_string :: (b: bool) -> string {
|
||||
float_to_string :: (f: f64) -> string {
|
||||
neg := f < 0.0;
|
||||
v := if neg then 0.0 - f else f;
|
||||
int_part := cast(s32) v;
|
||||
frac := cast(s32) ((v - cast(f64) int_part) * 1000000.0);
|
||||
int_part := cast(s64) v;
|
||||
frac := cast(s64) ((v - cast(f64) int_part) * 1000000.0);
|
||||
if frac < 0 { frac = 0 - frac; }
|
||||
istr := int_to_string(int_part);
|
||||
fstr := int_to_string(frac);
|
||||
@@ -59,6 +59,68 @@ float_to_string :: (f: f64) -> string {
|
||||
buf;
|
||||
}
|
||||
|
||||
int_to_hex_string :: (n: s64) -> string {
|
||||
if n == 0 { return "0"; }
|
||||
|
||||
// Split into four 16-bit groups for correct unsigned treatment
|
||||
g0 := n % 65536;
|
||||
if g0 < 0 { g0 = g0 + 65536; }
|
||||
r1 := (n - g0) / 65536;
|
||||
g1 := r1 % 65536;
|
||||
if g1 < 0 { g1 = g1 + 65536; }
|
||||
r2 := (r1 - g1) / 65536;
|
||||
g2 := r2 % 65536;
|
||||
if g2 < 0 { g2 = g2 + 65536; }
|
||||
r3 := (r2 - g2) / 65536;
|
||||
g3 := r3 % 65536;
|
||||
if g3 < 0 { g3 = g3 + 65536; }
|
||||
|
||||
buf := alloc(16);
|
||||
// Group 3: digits 0-3 (bits 48-63)
|
||||
i := 3;
|
||||
v := g3;
|
||||
while i >= 0 {
|
||||
d := v % 16;
|
||||
buf[i] = if d < 10 then d + 48 else d - 10 + 97;
|
||||
v = v / 16;
|
||||
i -= 1;
|
||||
}
|
||||
// Group 2: digits 4-7 (bits 32-47)
|
||||
i = 7;
|
||||
v = g2;
|
||||
while i >= 4 {
|
||||
d := v % 16;
|
||||
buf[i] = if d < 10 then d + 48 else d - 10 + 97;
|
||||
v = v / 16;
|
||||
i -= 1;
|
||||
}
|
||||
// Group 1: digits 8-11 (bits 16-31)
|
||||
i = 11;
|
||||
v = g1;
|
||||
while i >= 8 {
|
||||
d := v % 16;
|
||||
buf[i] = if d < 10 then d + 48 else d - 10 + 97;
|
||||
v = v / 16;
|
||||
i -= 1;
|
||||
}
|
||||
// Group 0: digits 12-15 (bits 0-15)
|
||||
i = 15;
|
||||
v = g0;
|
||||
while i >= 12 {
|
||||
d := v % 16;
|
||||
buf[i] = if d < 10 then d + 48 else d - 10 + 97;
|
||||
v = v / 16;
|
||||
i -= 1;
|
||||
}
|
||||
// Skip leading zeros (keep at least 1 digit)
|
||||
start := 0;
|
||||
while start < 15 {
|
||||
if buf[start] != 48 { break; }
|
||||
start += 1;
|
||||
}
|
||||
substr(buf, start, 16 - start);
|
||||
}
|
||||
|
||||
concat :: (a: string, b: string) -> string {
|
||||
al := a.len;
|
||||
bl := b.len;
|
||||
@@ -70,7 +132,7 @@ concat :: (a: string, b: string) -> string {
|
||||
buf;
|
||||
}
|
||||
|
||||
substr :: (s: string, start: s32, len: s32) -> string {
|
||||
substr :: (s: string, start: s64, len: s64) -> string {
|
||||
buf := alloc(len);
|
||||
i := 0;
|
||||
while i < len {
|
||||
@@ -94,7 +156,7 @@ struct_to_string :: (s: $T) -> string {
|
||||
}
|
||||
|
||||
enum_to_string :: (e: $T) -> string {
|
||||
concat(".", field_name(T, cast(s32) e));
|
||||
concat(".", field_name(T, cast(s64) e));
|
||||
}
|
||||
|
||||
vector_to_string :: (v: $T) -> string {
|
||||
@@ -130,8 +192,13 @@ slice_to_string :: (items: []$T) -> string {
|
||||
concat(result, "]");
|
||||
}
|
||||
|
||||
pointer_to_string :: (p: $T) -> string {
|
||||
addr : s64 = xx p;
|
||||
concat(type_name(T), concat("@", int_to_hex_string(addr)));
|
||||
}
|
||||
|
||||
union_to_string :: (u: $T) -> string {
|
||||
tag := cast(s32) u;
|
||||
tag := cast(s64) u;
|
||||
result := concat(".", field_name(T, tag));
|
||||
payload := field_value(u, tag);
|
||||
pstr := any_to_string(payload);
|
||||
@@ -156,6 +223,8 @@ any_to_string :: (val: Any) -> string {
|
||||
case array: result = array_to_string(cast(type) val);
|
||||
case slice: result = slice_to_string(cast(type) val);
|
||||
case union: result = union_to_string(cast(type) val);
|
||||
case pointer: result = pointer_to_string(cast(type) val);
|
||||
case type: { s : string = xx val; result = s; }
|
||||
}
|
||||
result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user