+++ /dev/null
-#load "core/std"
-#load "progs/foo_test"
-
-use package core
-
-Foo :: struct {
- data1 : i32;
- data2 : f32;
-}
-
-Bar :: struct {
- use foo : Foo;
- bar_data : str;
- // bar_data2 : cstring;
-}
-
-// BUG: This should cause some kind of error since this should be
-// infinite in size, but it doesn't so... fix that.
-// - brendanfh 2020/09/08
-// Link :: struct {
-// data : i32;
-// next : Link;
-// }
-
-
-// bar : Bar;
-// bar.data1 = 123;
-// bar.data2 = 524782.1f;
-
-// asdf :: proc (f: Foo) -> ...
-// asdf(bar.foo);
-
-print_foo :: (f: ^Foo) {
- print("Foo: \n");
- print(f.data1);
- print("\n");
- print(cast(i32) f.data2);
- print("\n");
-}
-
-print_bar :: (bar: ^Bar) {
- print(bar.data1);
- print("\n");
- print(cast(i32) bar.data2);
- print("\n");
- print(bar.bar_data);
- print("\n");
-}
-
-make_bar :: () -> Bar {
- bar : Bar;
- bar.data1 = 1234;
- bar.data2 = 12.34f;
- bar.bar_data = "This is a test";
-
- return bar;
-}
-
-f :: () -> [5] [2] u32 {
- // This method of returning an array will leak memory, since this is never freed.
- // mem := cast(^u32) calloc(sizeof [5] [2] u32);
- // for i: 0 .. 10 do mem[i] = 1234 + i;
- // return ~~mem;
-
-
- // This is safe ONLY when the value is used without calling another function.
- mem : [5] [2] u32;
- for ^m2: mem do for ^m: *m2 do *m = 4567;
- return mem;
-}
-
-compress :: (arr: [$N] $T, f: (T, T) -> T) -> T {
- val := arr[0];
- for i: 1..N do val = f(val, arr[i]);
- return val;
-}
-
-BadUnion :: struct {
- use container : struct #union {
- int: i32;
- float: f32;
- };
-}
-
-Vec2 :: struct { x: i32; y: i32; }
-Entity :: struct { use pos: Vec2; }
-
-array_literal_optim :: () -> void {
- bar : [5] u32;
- bar = u32.[ 1, 2, 3, 4, 5 ];
- bar[2] = 1234;
- for b: bar do printf("b: %i\n", b);
-}
-
-main :: (args: [] cstr) {
- a : BadUnion;
- a.int = 1234;
- a.float = 0.5;
- printf("%i == 4\n", sizeof BadUnion);
- printf("%p\n", a.int);
- printf("%f\n", a.float);
-
- e := Entity.{ Vec2.{ 1, 2 } };
- printf("Entity(%i, %i)\n", e.x, e.y);
-
- {
- foo : [5] [2] u32;
- foo = f();
-
- array_literal_optim();
-
- printf("%p == %p\n", cast(^u32) foo, ^foo);
- for ^thing: foo do for t: *thing do printf("%p %i\n", thing, t);
-
- println(compress(f32.[ 1, 2, 3, 4, 5 ], (a: $T, b: T) -> T { return a + b; }));
- }
-
- use package test { foo as foo_pkg }
- test :: package test
- use test.foo.SomeEnum
-
- printf("Val2: %i\n", cast(i32) Val2);
- printf("Val3: %i\n", cast(i32) Val3);
-
- foo_pkg.print_foo();
- foo := test.foo.TestFoo.{ a = 1234 };
- printf("foo.a: %i\n", foo.a);
-
- bar := make_bar();
-
- print(sizeof Bar);
- print("\n");
- print_bar(^bar);
- print_foo(^bar.foo);
- print(bar.foo.data1);
- print("\n");
-
- program := "+ + * s - /";
- accumulator := 0;
-
- for token: program {
- switch token {
- case #char "+" do accumulator += 1;
- case #char "-" do accumulator -= 1;
- case #char "*" do accumulator *= 2;
- case #char "/" do accumulator /= 2;
- case #char "s" do accumulator *= accumulator;
- case #default ---
- }
- }
-
- printf("The program \"%s\" calculates the value %i\n", program, accumulator);
-}
+++ /dev/null
-package main
-
-#load "core/std"
-#load "core/intrinsics/simd"
-
-use package core
-use package core.intrinsics.simd
-
-main :: (args: [] cstr) {
- init_positions();
- init_velocities();
-
- print("Beginning simulation.\n");
-
- for i: 0 .. 20 {
- update();
- print(cast(i64) (avg_motion() * 100000000000000000.0f));
- print("\n");
- }
-}
-
-OBJECT_COUNT :: 10000
-
-positions : [OBJECT_COUNT] f32x4
-velocities : [OBJECT_COUNT] f32x4
-
-init_positions :: () {
- for ^p: positions {
- *p = f32x4_replace_lane(*p, 0, random.float(-127.0f, 127.0f));
- *p = f32x4_replace_lane(*p, 1, random.float(-127.0f, 127.0f));
- *p = f32x4_replace_lane(*p, 2, random.float(-127.0f, 127.0f));
- *p = f32x4_replace_lane(*p, 3, random.float(-127.0f, 127.0f));
- }
-}
-
-init_velocities :: () {
- for ^v: velocities do *v = f32x4_splat(0.0f);
-}
-
-horizontal_add :: (f: f32x4) -> f32 {
- return f32x4_extract_lane(f, 0)
- + f32x4_extract_lane(f, 1)
- + f32x4_extract_lane(f, 2)
- + f32x4_extract_lane(f, 3);
-}
-
-distance :: (x: f32x4, y: f32x4) -> f32 {
- d := f32x4_sub(x, y);
- return horizontal_add(f32x4_mul(d, d));
-}
-
-update :: () {
- for i: 0 .. OBJECT_COUNT {
- for j: 0 .. OBJECT_COUNT {
- dist := distance(positions[i], positions[j]);
- if dist == 0.0f do continue;
-
- velocities[i] = f32x4_add(velocities[i], f32x4_div(f32x4_sub(positions[j], positions[i]), f32x4_splat(dist)));
- }
- }
-
- for i: 0 .. OBJECT_COUNT {
- positions[i] = f32x4_add(positions[i], velocities[i]);
- }
-}
-
-avg_motion :: () -> f32 {
- avg_motion := f32x4_splat(0.0f);
- obj_vec := f32x4_splat(cast(f32) OBJECT_COUNT);
- for v: velocities {
- avg_motion = f32x4_add(avg_motion, f32x4_div(v, obj_vec));
- }
-
- return distance(f32x4_splat(0.0f), avg_motion);
-}
+++ /dev/null
-#load "core/std"
-
-use package core
-
-max_f32 :: #solidify math.max_poly { T = f32 };
-
-compose :: (a: $A, f: (A) -> $B, g: (B) -> $C) -> C do return g(f(a));
-
-specific_compose_0 :: #solidify compose { B = f32 };
-specific_compose_1 :: #solidify specific_compose_0 { A = f32 };
-specific_compose_2 :: #solidify specific_compose_1 { C = f64 };
-
-main :: (args: [] cstr) {
- use package runtime
- println(Runtime);
- println("==================================================");
-
- printf("max(1, 2) = %i\n", math.max(1, 2));
- printf("max_f32(1.0, 2.0) = %f\n", max_f32(1, 2));
-
- // printf("max_f32(1, 2) = %i\n", max_f32(cast(u32) 1, cast(u32) 2));
-
- println(specific_compose_2(
- 2,
- (a: f32) -> f32 { return ~~(a * 2); },
- (b: f32) -> f64 { return ~~(b + 6); }));
-
-
- arr1 := array.make(f32);
- arr2 := array.make(i32);
- defer array.free(^arr1);
- defer array.free(^arr2);
-
- for i: 0 .. 10 {
- array.push(^arr1, ~~i);
- array.push(^arr2, ~~i);
- }
- print_arrays(arr1, arr2);
-
- array_map(arr1, double);
- array_map(arr2, double);
-
- print_arrays(arr1, arr2);
-
- print_arrays :: (arr1: [..] $T, arr2: [..] $R) {
- println("==================================================");
- print_array(arr1);
- print_array(arr2);
- println("==================================================");
- }
-}
-
-array_map :: (arr: [..] $T, f: (T) -> T) {
- foo := #solidify math.max_poly { T = T };
- is := (#type InternalStruct(T)).{ foo = foo(6, 2) };
- printf("%i\n", is.foo);
-
- for ^v: arr do *v = f(*v);
-
- {
- test2();
-
- test2 :: () {
- test();
-
- is : InternalStruct([] u32);
-
- println("WORLD!!!!");
- }
-
- test :: () {
- println("HELLO!!!");
- }
- }
-
- InternalStruct :: struct (SOMETHING: type_expr) {
- foo : SOMETHING;
- }
-}
-
-double :: (v: $V) -> V do return v * 2;
+++ /dev/null
-package main
-
-#load "core/std"
-
-// NOTE: Didn't realize this would work so easily
-use package core { string_builder_append as sba }
-use package core
-use package core_file
-use package wasi
-
-// print_rights :: (rights: Rights) {
-// print(cast(u32) rights, 2);
-// print("\n");
-//
-// if rights & Rights.DataSync != cast(Rights) 0 do print("DataSync\n");
-// if rights & Rights.Read != cast(Rights) 0 do print("Read\n");
-// if rights & Rights.Seek != cast(Rights) 0 do print("Seek\n");
-// if rights & Rights.FdStatSetFlags != cast(Rights) 0 do print("FdStatSetFlags\n");
-// if rights & Rights.Sync != cast(Rights) 0 do print("Sync\n");
-// if rights & Rights.Tell != cast(Rights) 0 do print("Tell\n");
-// if rights & Rights.Write != cast(Rights) 0 do print("Write\n");
-// if rights & Rights.Advise != cast(Rights) 0 do print("Advise\n");
-// if rights & Rights.Allocate != cast(Rights) 0 do print("Allocate\n");
-// if rights & Rights.PathCreateDirectory != cast(Rights) 0 do print("PathCreateDirectory\n");
-// if rights & Rights.PathCreateFile != cast(Rights) 0 do print("PathCreateFile\n");
-// if rights & Rights.PathLinkSource != cast(Rights) 0 do print("PathLinkSource\n");
-// if rights & Rights.PathLinkTarget != cast(Rights) 0 do print("PathLinkTarget\n");
-// if rights & Rights.PathOpen != cast(Rights) 0 do print("PathOpen\n");
-// if rights & Rights.ReadDir != cast(Rights) 0 do print("ReadDir\n");
-// if rights & Rights.PathReadlink != cast(Rights) 0 do print("PathReadlink\n");
-// if rights & Rights.PathRenameSource != cast(Rights) 0 do print("PathRenameSource\n");
-// if rights & Rights.PathRenameTarget != cast(Rights) 0 do print("PathRenameTarget\n");
-// if rights & Rights.PathFilestatGet != cast(Rights) 0 do print("PathFilestatGet\n");
-// if rights & Rights.PathFilestateSetSize != cast(Rights) 0 do print("PathFilestateSetSize\n");
-// if rights & Rights.PathFilestateSetTimes != cast(Rights) 0 do print("PathFilestateSetTimes\n");
-// if rights & Rights.FilestatGet != cast(Rights) 0 do print("FilestatGet\n");
-// if rights & Rights.FilestatSetSize != cast(Rights) 0 do print("FilestatSetSize\n");
-// if rights & Rights.FilestatSetTimes != cast(Rights) 0 do print("FilestatSetTimes\n");
-// if rights & Rights.PathSymlink != cast(Rights) 0 do print("PathSymlink\n");
-// if rights & Rights.PathRemoveDirectory != cast(Rights) 0 do print("PathRemoveDirectory\n");
-// if rights & Rights.PathUnlinkFile != cast(Rights) 0 do print("PathUnlinkFile\n");
-// if rights & Rights.PollFDReadWrite != cast(Rights) 0 do print("PollFDReadWrite\n");
-// if rights & Rights.SockShutdown != cast(Rights) 0 do print("SockShutdown\n");
-// }
-
-readline :: (buf: ^u8, bufsize: u32) -> u32 {
- iov := IOVec.{ buf, bufsize };
- nread : Size;
- fd_pread(0, IOVecArray.{ ^iov, 1 }, 0, ^nread);
-
- return nread;
-}
-
-readdir :: (fd: FileDescriptor) {
- buf : [1024] u8;
- bufused : Size;
-
- if fd_readdir(fd, cast(^u8) buf, 1024, cast(DirCookie) 0, ^bufused) != Errno.Success {
- print("Failed to readdir\n");
- return;
- }
-
- dirent := cast(^DirEnt) buf;
- while true {
- print(str.{ cast(^u8) (cast(u32) dirent + sizeof DirEnt), dirent.d_namlen });
- print("\n");
-
- print("\td_namlen: ");
- print_i64(cast(u64) dirent.d_namlen, 16);
- print("\n");
- print("\td_type: ");
- print_i64(cast(u64) dirent.d_type, 16);
- print("\n");
-
- bufused -= sizeof DirEnt + dirent.d_namlen;
- dirent = cast(^DirEnt) (cast(u32) dirent + sizeof DirEnt + dirent.d_namlen);
-
- if bufused <= 0 do break;
- }
-}
-
-timer_start :: () -> Timestamp {
- curr_time: Timestamp;
- clock_time_get(ClockID.Realtime, cast(Timestamp) 1, ^curr_time);
- return curr_time;
-}
-
-timer_end :: (start_time: Timestamp) -> Timestamp {
- curr_time: Timestamp;
- clock_time_get(ClockID.Realtime, cast(Timestamp) 1, ^curr_time);
- return (curr_time - start_time) / 1000000;
-}
-
-is_prime :: (n := 0) -> bool {
- sqrt :: cast(i32) (sqrt_f32(cast(f32) n));
- for i: 2 .. sqrt + 1 do if n % i == 0 do return false;
- return true;
-}
-
-Vec3 :: struct {
- x: f32;
- y: f32;
- z: f32;
-}
-
-S :: struct {
- name: string;
- age: u32;
- pos: Vec3;
-}
-
-output_s :: (sb: ^StringBuilder, s: ^S) -> ^StringBuilder {
- sb |> sba("Hello, I'm ")
- |> sba(s.name)
- |> sba(". I am ")
- |> sba(cast(u64) s.age)
- |> sba(" years old. I am at (")
- |> sba(cast(u64) s.pos.x) |> sba(", ")
- |> sba(cast(u64) s.pos.y) |> sba(", ")
- |> sba(cast(u64) s.pos.z) |> sba(").\n");
-
- return sb;
-}
-
-print_arr :: (sb: ^StringBuilder, arr: [] $T) {
- sb |> string_builder_clear();
-
- for i: 0 .. arr.count {
- sb |> sba(cast(u64) arr[i]) |> sba(" ");
- }
-
- sb |> sba("\n") |> string_builder_to_string() |> print();
-}
-
-make_i32_arr :: (a := context.allocator, len := 10) -> [] i32 {
- arr := cast(^i32) alloc(a, sizeof i32 * len);
- return arr[0 .. len];
-}
-
-main :: (args: []cstring) {
- now : Timestamp;
- clock_time_get(ClockID.Realtime, cast(Timestamp) 1, ^now);
-
- random_seed(cast(u32) now);
-
- sb := string_builder_make(256);
-
- timer := timer_start();
- defer {
- ^sb |> string_builder_clear()
- |> sba("\nTime taken: ")
- |> sba(cast(u64) timer_end(timer), 10)
- |> sba("ms\n")
- |> string_builder_to_string()
- |> print();
- }
-
- ^sb |> sba("There are ")
- |> sba(cast(u64) args.count)
- |> sba(" arguments.\n");
-
- for i: 0 .. args.count do ^sb |> sba(args[i]) |> sba(" ");
- ^sb |> sba("\n")
- |> string_builder_to_string()
- |> print();
-
- cont := file_get_contents(string_make(args[1]));
- defer cfree(cont.data);
- print(cont);
-
- sum: u64 = 0;
- for i: 0 .. 20000 do if is_prime(i) do sum += cast(u64) i;
- print("Sum of primes less than 20000 is: ");
- print_i64(sum);
- print("\n");
-
- matches := string_split("This is a test string to test splitting. It surprisingly works very well.", #char " ");
- defer cfree(matches.data);
-
- string_builder_clear(^sb);
- for i: 0 .. matches.count {
- ^sb |> sba(matches[i])
- |> sba("\n");
- }
-
- ^sb |> string_builder_to_string() |> print();
-
- program := "+ + * s - /";
- tokens := string_split(program, #char " ");
- defer cfree(tokens.data);
-
- acc := 0;
- for i: 0 .. tokens.count {
- switch tokens[i][0] {
- case #char "+" do acc += 1;
- case #char "-" do acc -= 1;
- case #char "*" do acc *= 2;
- case #char "/" do acc /= 2;
- case #char "s" do acc *= acc;
-
- case #default {
- print("Unexpected token: ");
- print_i64(cast(u64) tokens[i][0], 16);
- print("\n");
-
- // This breaks out of the for loop
- break break;
- }
- }
- }
-
- string_builder_clear(^sb);
- ^sb |> sba("The program evaluated to '") |> sba(cast(u64) acc) |> sba("'\n");
- ^sb |> string_builder_to_string() |> print();
- ^sb |> string_builder_clear();
-
- person := S.{ name = "Tester", age = 45, pos = Vec3.{ 1.0f, 2.0f, 3.0f } };
- ^sb |> output_s(^person)
- |> sba("Here is another message in the same string!\n")
- |> string_builder_to_string()
- |> print();
-
- if res := 5 + 4; res == 9 {
- ^sb |> string_builder_clear()
- |> sba("This worked! ")
- |> sba(cast(u64) ^res)
- |> sba("\n")
- |> string_builder_to_string()
- |> print();
- } else {
- ^sb |> string_builder_clear()
- |> sba("This did not work! ")
- |> sba(cast(u64) ^res)
- |> sba("\n")
- |> string_builder_to_string()
- |> print();
- }
-
- ^sb |> string_builder_clear();
- while i := 0; i < 10 {
- ^sb |> sba("Loop: ") |> sba(cast(u64) i) |> sba("\n");
-
- i += 1;
- } else {
- print("Never ran the loop\n");
- }
-
- ^sb |> string_builder_to_string() |> print();
-
-
- arr : [128] i32;
- for i: 0 .. 128 do arr[i] = i * i;
-
- print_arr(^sb, arr[5 .. 10]);
-
- ss := string_substr("Hello, World!", "World");
- if ss.count > 0 do print(ss);
- print("\n");
-
- foobar(10, 1230);
-
- sl := make_i32_arr();
- print_i64(cast(u64) sl.count);
-
- print_i64(cast(u64) fib(20));
- print("\n");
-
- print(add(cast(u64) 20, cast(u64) 5));
- print(add(cast(u64) 20, cast(u64) 5));
- print(add(cast(u64) 20, cast(u64) 5));
- print(add(cast(u64) 20, cast(u64) 5));
- print("\n");
- print(cast(u64) add(20.0f, 5.0f));
- print("\n");
-
- slice := make_slice(cast(^u32) 1234, 5678);
- print(cast(u64) slice.data);
- print("\n");
- print(cast(u64) get_slice_length(^slice));
- print("\n");
-
- print(multi_poly(5.4f, 10));
- print("\n");
-
- dynarr : [..] Vec3;
- array_init(^dynarr);
- for i: 0 .. 16 do array_push(^dynarr, Vec3.{ cast(f32) i, cast(f32) (i * i), cast(f32) (i * i * i) });
- for i: 0 .. 16 {
- print(cast(u32) dynarr.data[i].x);
- print(" ");
- print(cast(u32) dynarr.data[i].y);
- print(" ");
- print(cast(u32) dynarr.data[i].z);
- print("\n");
- }
-
- print(dynarr.count, 10);
-}
-
-foobar :: (a: i32, b := 1, c: i64 = 5) {
- print_i64(cast(u64) a);
- print("\n");
- print_i64(cast(u64) b, 16);
- print("\n");
- print_i64(c);
- print("\n");
-}
-
-fib :: (n: i32) -> i32 {
- switch n {
- case 0 do return 1;
- case 1 do return 1;
- case #default {
- return fib(n - 1) + fib(n - 2);
- }
- }
-
- return 0;
-}
-
-add :: (a: $T, b: T) -> T {
- return a + b;
-}
-
-multi_poly :: (a: $T, b: $R) -> R {
- return cast(R) (a + cast(T) b);
-}
-
-make_slice :: (ptr: ^$T, count: u32) -> [] T {
- return ptr[4 .. count];
-}
-
-get_slice_length :: (s: ^[] $T) -> u32 do return s.count;