}
}
-fold :: (arr: ^[..] $T, init: $R, f: (T, R) -> R) -> R {
- val := init;
- for it: *arr do val = f(it, val);
- return val;
-}
-
-// CLEANUP: :slice Move this elsewhere when more slice functionality is added
-fold_slice :: (arr: [] $T, init: $R, f: (T, R) -> R) -> R {
- val := init;
- for it: arr do val = f(it, val);
- return val;
+fold :: proc {
+ (arr: ^[..] $T, init: $R, f: (T, R) -> R) -> R {
+ val := init;
+ for it: *arr do val = f(it, val);
+ return val;
+ },
+
+ (arr: [] $T, init: $R, f: (T, R) -> R) -> R {
+ val := init;
+ for it: arr do val = f(it, val);
+ return val;
+ }
}
map :: (arr: ^[..] $T, data: $R, f: (T, R) -> T) {
}
#private_file
-greatest_impl :: (arr: ^$T, count: i32) -> (i32, T) {
- greatest_idx := 0;
- greatest := arr[0];
+fold_idx_elem :: (arr: ^$T, count: i32, cmp: (T, T) -> bool) -> (i32, T) {
+ idx := 0;
+ elem := arr[0];
for i: 1 .. count {
- if arr[i] > greatest {
- greatest_idx = i;
- greatest = arr[i];
+ if cmp(arr[i], elem) {
+ idx = i;
+ elem = arr[i];
}
}
- return greatest_idx, greatest;
+ return idx, elem;
}
-// This pattern should be expanded on to make all kinds of folding functions.
+#private_file cmp_greater :: (x: $T, y: T) -> bool do return x > y;
+#private_file cmp_less :: (x: $T, y: T) -> bool do return x < y;
+
greatest :: proc {
- (arr: [..] $T) -> (i32, T) { return greatest_impl(arr.data, arr.count); },
- (arr: [] $T) -> (i32, T) { return greatest_impl(arr.data, arr.count); },
- (arr: [$N] $T) -> (i32, T) { return greatest_impl(cast(^T) arr, N); },
+ (arr: [..] $T) -> (i32, T) { return fold_idx_elem(arr.data, arr.count, cmp_greater); },
+ (arr: [] $T) -> (i32, T) { return fold_idx_elem(arr.data, arr.count, cmp_greater); },
+ (arr: [$N] $T) -> (i32, T) { return fold_idx_elem(cast(^T) arr, N, cmp_greater); },
+}
+
+least :: proc {
+ (arr: [..] $T) -> (i32, T) { return fold_idx_elem(arr.data, arr.count, cmp_less); },
+ (arr: [] $T) -> (i32, T) { return fold_idx_elem(arr.data, arr.count, cmp_less); },
+ (arr: [$N] $T) -> (i32, T) { return fold_idx_elem(cast(^T) arr, N, cmp_less); },
}
cresize :: (ptr: rawptr, size: u32) -> rawptr do return raw_resize(context.allocator, ptr, size);
cfree :: (ptr: rawptr) do raw_free(context.allocator, ptr);
+// CLEANUP: Does this really need to be limited to a non-custom runtime?
#if runtime.Runtime != runtime.Runtime_Custom {
new :: ($T: type_expr, allocator := context.allocator, initialize := true) -> ^T {
res := cast(^T) raw_alloc(allocator, sizeof T);
get_contents :: proc {
get_contents_from_file,
- proc (path: str) -> str {
+ (path: str) -> str {
tmp_file, success := file_open(path, OpenMode.Read);
if !success do return .{ null, 0 };
defer file_close(tmp_file);
}
sqrt :: proc { wasm.sqrt_f32, wasm.sqrt_f64, sqrt_poly }
-sqrt_poly :: proc (x: $T) -> T {
+sqrt_poly :: (x: $T) -> T {
return ~~ sqrt_f64(~~ x);
}
// The builtin _start proc.
// Sets up everything needed for execution.
-proc () #export "_start" {
+_start :: () -> void #export "_start" {
__runtime_initialize();
args: [] cstr = .{ null, 0 };
// The builtin _start proc.
// Sets up everything needed for execution.
-proc () #export "_start" {
+_start :: () -> void #export "_start" {
__runtime_initialize();
args : [] cstr;
#error "'stdio' can only be included in the 'wasi' or 'js' runtime."
}
+auto_flush_stdio := true
+
print :: proc {
(x: str) {
io.write(^print_writer, x);
- if x[x.count - 1] == #char "\n" do __flush_stdio();
+ if x[x.count - 1] == #char "\n" && auto_flush_stdio do __flush_stdio();
},
(x: $T) { io.write(^print_writer, x); },
// pointer conversion between two structs, A and B, if A is the first member of B, and is used.
// For example, we can pass `thing` to this procedure, even though it is not a ComponentOne.
// The first member is a ComponentOne and is used, so a implicit pointer conversion is always safe.
- do_something_with_component_one :: proc (c: ^ComponentOne) {
+ do_something_with_component_one :: (c: ^ComponentOne) {
printf("ComponentOne's name is %s.\n", c.name);
}
- print_spacer :: proc () do println("\n===========================================");
+ print_spacer :: () do println("\n===========================================");
}
i32 state_count[Entity_State_Count];
i32 type_count[Entity_Type_Count];
+
+ i32 all_count[Entity_State_Count][Entity_Type_Count];
} EntityHeap;
void entity_heap_init(EntityHeap* entities);
TestFoo :: struct { a: i32; }
-print_foo :: proc () do println("Foo!");
+print_foo :: () do println("Foo!");
SomeEnum :: enum #flags { Val1; Val2; Val3; }
// asdf :: proc (f: Foo) -> ...
// asdf(bar.foo);
-print_foo :: proc (f: ^Foo) {
+print_foo :: (f: ^Foo) {
print("Foo: \n");
print(f.data1);
print("\n");
print("\n");
}
-print_bar :: proc (bar: ^Bar) {
+print_bar :: (bar: ^Bar) {
print(bar.data1);
print("\n");
print(cast(i32) bar.data2);
print("\n");
}
-make_bar :: proc () -> Bar {
+make_bar :: () -> Bar {
bar : Bar;
bar.data1 = 1234;
bar.data2 = 12.34f;
return bar;
}
-f :: proc () -> [5] [2] u32 #export "IAMTHEFUNCTION" {
+f :: () -> [5] [2] u32 #export "IAMTHEFUNCTION" {
// 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;
}
-compress :: proc (arr: [$N] $T, f : proc (T, T) -> T) -> T {
+compress :: (arr: [$N] $T, f : proc (T, T) -> T) -> T {
val := arr[0];
for i: 1..N do val = f(val, arr[i]);
return val;
Vec2 :: struct { x: i32; y: i32; }
Entity :: struct { use pos: Vec2; }
-array_literal_optim :: proc () #export {
+array_literal_optim :: () -> void #export {
bar : [5] u32;
bar = u32.[ 1, 2, 3, 4, 5 ];
bar[2] = 1234;
for b: bar do printf("b: %i\n", b);
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
a : BadUnion;
a.int = 1234;
a.float = 0.5;
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 ], proc (a: $T, b: T) -> T { return a + b; }));
+ println(compress(f32.[ 1, 2, 3, 4, 5 ], (a: $T, b: T) -> T { return a + b; }));
}
use package test { foo as foo_pkg }
use package core
use package core.intrinsics.simd
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
init_positions();
init_velocities();
positions : [OBJECT_COUNT] f32x4
velocities : [OBJECT_COUNT] f32x4
-init_positions :: proc () {
+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));
}
}
-init_velocities :: proc () {
+init_velocities :: () {
for ^v: velocities do *v = f32x4_splat(0.0f);
}
-horizontal_add :: proc (f: f32x4) -> f32 {
+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 :: proc (x: f32x4, y: f32x4) -> f32 {
+distance :: (x: f32x4, y: f32x4) -> f32 {
d := f32x4_sub(x, y);
return horizontal_add(f32x4_mul(d, d));
}
-update :: proc () {
+update :: () {
for i: 0 .. OBJECT_COUNT {
for j: 0 .. OBJECT_COUNT {
dist := distance(positions[i], positions[j]);
}
}
-avg_motion :: proc () -> f32 {
+avg_motion :: () -> f32 {
avg_motion := f32x4_splat(0.0f);
obj_vec := f32x4_splat(cast(f32) OBJECT_COUNT);
for v: velocities {
max_f32 :: #solidify math.max_poly { T = f32 };
-compose :: proc (a: $A, f: proc (A) -> $B, g: proc (B) -> $C) -> C do return g(f(a));
+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 :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
use package runtime
println(Runtime);
println("==================================================");
println(specific_compose_2(
2,
- proc (a: f32) -> f32 { return ~~(a * 2); },
- proc (b: f32) -> f64 { return ~~(b + 6); }));
+ (a: f32) -> f32 { return ~~(a * 2); },
+ (b: f32) -> f64 { return ~~(b + 6); }));
arr1 := array.make(f32);
print_arrays(arr1, arr2);
- print_arrays :: proc (arr1: [..] $T, arr2: [..] $R) {
+ print_arrays :: (arr1: [..] $T, arr2: [..] $R) {
println("==================================================");
print_array(arr1);
print_array(arr2);
}
}
-array_map :: proc (arr: [..] $T, f: proc (T) -> T) {
+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);
{
test2();
- test2 :: proc () {
+ test2 :: () {
test();
is : InternalStruct([] u32);
println("WORLD!!!!");
}
- test :: proc () {
+ test :: () {
println("HELLO!!!");
}
}
}
}
-double :: proc (v: $V) -> V do return v * 2;
+double :: (v: $V) -> V do return v * 2;
use package core
use package core.intrinsics.simd
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
x := f32x4_const(10.0f, 20.0f, 30.0f, 40.0f);
y := f32x4_splat(100.0f);
use package core;
-old_va_test :: proc (fix: Fixes, va: ..i32) {
+old_va_test :: (fix: Fixes, va: ..i32) {
println(fix.prefix);
for v: va do println(v);
println(fix.suffix);
Fixes :: struct { prefix: str; suffix: str; }
-new_va_test :: proc (fix: Fixes, va: ...) {
+new_va_test :: (fix: Fixes, va: ...) {
println(fix.prefix);
for i: 0 .. va.count {
println(fix.suffix);
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
new_va_test(Fixes.{ "foo", "foozle" }, 1, 2, 3.0f, 5.0f);
old_va_test(Fixes.{ "bar", "barzle" }, 1);
use package core_file
use package wasi
-// print_rights :: proc (rights: Rights) {
+// print_rights :: (rights: Rights) {
// print(cast(u32) rights, 2);
// print("\n");
//
// if rights & Rights.SockShutdown != cast(Rights) 0 do print("SockShutdown\n");
// }
-readline :: proc (buf: ^u8, bufsize: u32) -> u32 {
+readline :: (buf: ^u8, bufsize: u32) -> u32 {
iov := IOVec.{ buf, bufsize };
nread : Size;
fd_pread(0, IOVecArray.{ ^iov, 1 }, 0, ^nread);
return nread;
}
-readdir :: proc (fd: FileDescriptor) {
+readdir :: (fd: FileDescriptor) {
buf : [1024] u8;
bufused : Size;
}
}
-timer_start :: proc () -> Timestamp {
+timer_start :: () -> Timestamp {
curr_time: Timestamp;
clock_time_get(ClockID.Realtime, cast(Timestamp) 1, ^curr_time);
return curr_time;
}
-timer_end :: proc (start_time: Timestamp) -> Timestamp {
+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 :: proc (n := 0) -> bool {
+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;
pos: Vec3;
}
-output_s :: proc (sb: ^StringBuilder, s: ^S) -> ^StringBuilder {
+output_s :: (sb: ^StringBuilder, s: ^S) -> ^StringBuilder {
sb |> sba("Hello, I'm ")
|> sba(s.name)
|> sba(". I am ")
return sb;
}
-print_arr :: proc (sb: ^StringBuilder, arr: [] $T) {
+print_arr :: (sb: ^StringBuilder, arr: [] $T) {
sb |> string_builder_clear();
for i: 0 .. arr.count {
sb |> sba("\n") |> string_builder_to_string() |> print();
}
-make_i32_arr :: proc (a := context.allocator, len := 10) -> [] i32 {
+make_i32_arr :: (a := context.allocator, len := 10) -> [] i32 {
arr := cast(^i32) alloc(a, sizeof i32 * len);
return arr[0 .. len];
}
-main :: proc (args: []cstring) {
+main :: (args: []cstring) {
now : Timestamp;
clock_time_get(ClockID.Realtime, cast(Timestamp) 1, ^now);
print(dynarr.count, 10);
}
-foobar :: proc (a: i32, b := 1, c: i64 = 5) {
+foobar :: (a: i32, b := 1, c: i64 = 5) {
print_i64(cast(u64) a);
print("\n");
print_i64(cast(u64) b, 16);
print("\n");
}
-fib :: proc (n: i32) -> i32 {
+fib :: (n: i32) -> i32 {
switch n {
case 0 do return 1;
case 1 do return 1;
return 0;
}
-add :: proc (a: $T, b: T) -> T {
+add :: (a: $T, b: T) -> T {
return a + b;
}
-multi_poly :: proc (a: $T, b: $R) -> R {
+multi_poly :: (a: $T, b: $R) -> R {
return cast(R) (a + cast(T) b);
}
-make_slice :: proc (ptr: ^$T, count: u32) -> [] T {
+make_slice :: (ptr: ^$T, count: u32) -> [] T {
return ptr[4 .. count];
}
-get_slice_length :: proc (s: ^[] $T) -> u32 do return s.count;
+get_slice_length :: (s: ^[] $T) -> u32 do return s.count;
EntityHeap* eh = &context.entities;
if (bh_arr_length(eh->entities) == 0) return;
+ static const char* state_colors[] = {
+ "\e[91m",
+ "\e[93m",
+ "\e[97m",
+ "\e[93m",
+ "\e[94m",
+ "\e[95m",
+ "\e[94m",
+ "\e[95m",
+ "\e[96m",
+ "\e[92m",
+ };
+
printf("\e[2;1H");
+
for (i32 i = 0; i < Entity_State_Count - 1; i++) {
- printf("%25s (%4d) | ", entity_state_strings[i], eh->state_count[i]);
-
+ if (i % 4 == 0) printf("\n");
+ printf("%s \xe2\x96\x88 %s", state_colors[i], entity_state_strings[i]);
+ }
+
+ printf("\n\n");
+
+ for (i32 i = 0; i < Entity_Type_Count; i++) {
+ if (eh->type_count[i] == 0) {
+ printf("\e[90m");
+ } else {
+ printf("\e[97m");
+ }
+
+ printf("%25s (%4d) | ", entity_type_strings[i], eh->type_count[i]);
+
printf("\e[0K");
- for (i32 c = 0; c < eh->state_count[i] * 50 / bh_arr_length(eh->entities); c++) printf("\xe2\x96\x88");
+ for (i32 j = 0; j < Entity_State_Count; j++) {
+ if (eh->all_count[j][i] == 0) continue;
+
+ printf(state_colors[j]);
+
+ i32 count = (eh->all_count[j][i] >> 5) + 1;
+ for (i32 c = 0; c < count * 2; c++) {
+ printf("\xe2\x96\x88");
+ }
+
+ printf("\e[0m");
+ }
printf("\n");
}
}
switch (node->kind) {
case Ast_Kind_Function: {
- strncat(buf, "proc (", 1023);
+ strncat(buf, "(", 1023);
AstFunction *func = (AstFunction *) node;
bh_arr_each(AstParam, param, func->params) {
eh_shift_up(entities, bh_arr_length(entities->entities) - 1);
e->entered_in_queue = 1;
- entities->state_count[e->state]++;
+ entities->state_count[e->state]++;
entities->type_count[e->type]++;
+ entities->all_count[e->state][e->type]++;
}
// nocheckin
}
void entity_heap_change_top(EntityHeap* entities, Entity* new_top) {
- entities->state_count[entities->entities[0]->state]--;
- entities->state_count[new_top->state]--;
+ entities->state_count[entities->entities[0]->state]--;
+ entities->state_count[new_top->state]++;
- // CLEANUP: I don't think both of these should be --?
entities->type_count[entities->entities[0]->type]--;
- entities->type_count[new_top->type]--;
+ entities->type_count[new_top->type]++;
+
+ entities->all_count[entities->entities[0]->state][entities->entities[0]->type]--;
+ entities->all_count[new_top->state][new_top->type]++;
entities->entities[0] = new_top;
eh_shift_down(entities, 0);
void entity_heap_remove_top(EntityHeap* entities) {
entities->state_count[entities->entities[0]->state]--;
entities->type_count[entities->entities[0]->type]--;
+ entities->all_count[entities->entities[0]->state][entities->entities[0]->type]--;
entities->entities[0]->entered_in_queue = 0;
entities->entities[0] = entities->entities[bh_arr_length(entities->entities) - 1];
switch (t1->kind) {
case Type_Kind_Basic:
if (t2->kind == Type_Kind_Basic) {
- // HACK: Not sure if this is right way to check this?
- if (t1 == t2) return 1;
-
+ // Signedness of an integer doesn't matter.
if ((t1->Basic.flags & Basic_Flag_Integer) && (t2->Basic.flags & Basic_Flag_Integer)) {
return t1->Basic.size == t2->Basic.size;
}
char buf[512];
fori (i, 0, 512) buf[i] = 0;
- strncat(buf, "proc (", 511);
+ strncat(buf, "(", 511);
fori (i, 0, type->Function.param_count) {
strncat(buf, type_get_unique_name(type->Function.params[i]), 511);
if (i != type->Function.param_count - 1)
char buf[512];
fori (i, 0, 512) buf[i] = 0;
- strncat(buf, "proc (", 511);
+ strncat(buf, "(", 511);
fori (i, 0, type->Function.param_count) {
strncat(buf, type_get_name(type->Function.params[i]), 511);
if (i != type->Function.param_count - 1)
}
}
- onyx_report_error(call->token->pos, "unable to match overloaded function with provided argument types: (%s)", arg_str);
+ onyx_report_error(call->token->pos, "Unable to match overloaded function with provided argument types: (%s)", arg_str);
bh_free(global_scratch_allocator, arg_str);
}
//
// I think a more general case of accessing fields on r-values is better. For example,
//
- // returns_foo :: proc () -> Foo { ... }
+ // returns_foo :: () -> Foo { ... }
//
// value := returns_foo().foo_member;
//
// Slight hack, but having 0 in the array makes both parts easier
array.push(^nums, 0);
- cmp_asc :: proc (a: $T, b: T) -> i32 do return ~~(a - b);
+ cmp_asc :: (a: $T, b: T) -> i32 do return ~~(a - b);
array.sort(^nums, cmp_asc);
diffs: [3] u32;
return changed;
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day11.txt";
file := reader.make(contents);
return count;
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
contents := #file_contents "./tests/aoc-2020/input/day17.txt";
file := reader.make(contents);
printf("Your seat: %i\n", missing);
}
-cmp_asc :: proc (a: $T, b: T) -> i32 do return ~~(a - b);
+cmp_asc :: (a: $T, b: T) -> i32 do return ~~(a - b);
start, end := find_contiguous_subarray_with_sum(nums, invalid);
- max := array.fold_slice(nums.data[start .. end + 1], cast(u64) 0, math.max_poly);
- min := array.fold_slice(nums.data[start .. end + 1], cast(u64) max, math.min_poly);
+ max := array.fold(nums.data[start .. end + 1], cast(u64) 0, math.max_poly);
+ min := array.fold(nums.data[start .. end + 1], cast(u64) max, math.min_poly);
printf("Extrema sum: %l\n", min + max);
}
velocities : [4] Vec2;
}
-calc_vecs :: proc (n := 0) -> [4] Vec2 {
+calc_vecs :: (n := 0) -> [4] Vec2 {
vecs : [4] Vec2;
i := n;
return vecs;
}
-main :: proc (args: [] cstr) #export "MAIN" {
+main :: (args: [] cstr) {
// Array of structs on stack
{
println("Array of structs on the stack.");
println(nums[50]);
}
-}
\ No newline at end of file
+}
use package core
-count_to :: proc ($N: i32) {
+count_to :: ($N: i32) {
for i: 0 .. N do printf("%i ", i);
print("\n");
}
-alloc_slice :: proc ($T: type_expr, N: i32) -> [] T {
+alloc_slice :: ($T: type_expr, N: i32) -> [] T {
data := cast(^T) calloc(sizeof T * N);
return <[] T>.{ data, N };
}
count_to_30 :: #solidify count_to { N = #value 30 };
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
count_to(5);
count_to(10);
count_to(10);
map.put(^ages, "Jim", 25);
map.put(^ages, "Pam", 24);
- print_age :: proc (ages: ^map.Map(str, u32), name: str) {
+ print_age :: (ages: ^map.Map(str, u32), name: str) {
age := map.get(ages, name);
printf("%s's age is %i.\n", name, age);
}
use package core
Test_VTable :: struct {
- first : proc (i32) -> i32;
- second : proc (i32) -> i32;
+ first : (i32) -> i32;
+ second : (i32) -> i32;
- third : proc (i32, i32) -> i32 = null_proc;
+ third : (i32, i32) -> i32 = null_proc;
}
test_vtable1 := Test_VTable.{
- first = proc (a: i32) -> i32 { return a * 2; },
- second = proc (a: i32) -> i32 { return a + 2; },
+ first = (a: i32) -> i32 { return a * 2; },
+ second = (a: i32) -> i32 { return a + 2; },
}
test_vtable2 := Test_VTable.{
- first = proc (a: i32) -> i32 { return a / 2; },
- second = proc (a: i32) -> i32 { return a - 2; },
+ first = (a: i32) -> i32 { return a / 2; },
+ second = (a: i32) -> i32 { return a - 2; },
}
}
// because of the work i did above, this also works now.
-dummy := proc () { println("hello world!"); }
+dummy := () { println("hello world!"); }
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
dummy();
t := Test_Thing.{ ^test_vtable1, 10 };
println(t.vt.first == test_vtable1.second);
- do_a_thing :: proc (use t: ^Test_Thing) -> i32 {
+ do_a_thing :: (use t: ^Test_Thing) -> i32 {
if vt.second == null_proc || vt.first == null_proc do return data;
res := data |> vt.second() |> vt.first();
return res;
}
-}
\ No newline at end of file
+}
use package core
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
defer println("At the end!");
i := 0;
while i < 10 {
use package core
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
println("Hello, World!");
}
use package core
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
imap : map.Map(i32, str);
map.init(^imap, "");
defer {
use package core
-foo :: proc () -> (i32, i32) {
+foo :: () -> (i32, i32) {
return 50, 60;
}
-something :: proc (f: proc () -> ($T, T)) -> T {
+something :: (f: () -> ($T, T)) -> T {
a, b := f();
return a + b;
}
-get_extrema :: proc (arr: [$N] $T) -> (T, T) {
+get_extrema :: (arr: [$N] $T) -> (T, T) {
min := arr[0];
max := arr[0];
return min, max;
}
-return_multiple_arrays :: proc () -> ([3] i32, [3] f32) {
+return_multiple_arrays :: () -> ([3] i32, [3] f32) {
iarr := i32.[ 0, 1, 2 ];
farr := f32.[ 3, 4, 5 ];
return iarr, farr;
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
a, b := foo();
a, b = b, a;
array_print(iarr);
array_print(farr);
- array_print :: proc (arr: [$N] $T) {
+ array_print :: (arr: [$N] $T) {
for ^e: arr {
print(*e);
print(" ");
use package core
-main :: proc (args: [] cstr) {
- foo :: proc (x: i32, y: f32) {
+main :: (args: [] cstr) {
+ foo :: (x: i32, y: f32) {
printf("x is %i\n", x);
printf("y is %f\n", y);
}
println("\n\n=========================");
- poly_named :: proc (x: $T, y: [$N] T) {
+ poly_named :: (x: $T, y: [$N] T) {
println(x);
print_array(y);
}
println("\n\n=========================");
poly_overloaded :: proc {
- proc (y: [$N] $T) { print("MATCHED Y: "); print_array(y); },
- proc (x: $T) { print("MATCHED X: "); println(x); },
- proc (z: $T) { print("MATCHED Z: "); println(z); },
+ (y: [$N] $T) { print("MATCHED Y: "); print_array(y); },
+ (x: $T) { print("MATCHED X: "); println(x); },
+ (z: $T) { print("MATCHED Z: "); println(z); },
}
poly_overloaded(u32.[ 10, 20 ]);
println("\n\n=========================");
- overload_with_varargs :: proc (x: i32, y: i32, z: ..f32) {
+ overload_with_varargs :: (x: i32, y: i32, z: ..f32) {
println(x);
println(y);
println("\n\n=========================");
- lotza_options :: proc (
+ lotza_options :: (
options_a : bool,
options_b := false,
options_c := true,
// This currently does not work and would require a lot of rewriting of compiler internals to make work.
// named_baked_overloaded_parameters :: proc {
- // proc ($T: type_expr, x: T) do { println("MATCHED X"); },
- // proc ($T: type_expr, y: [$N] T) do { println("MATCHED X"); },
- // proc ($T: type_expr, z: T) do { println("MATCHED X"); },
+ // ($T: type_expr, x: T) do { println("MATCHED X"); },
+ // ($T: type_expr, y: [$N] T) do { println("MATCHED X"); },
+ // ($T: type_expr, z: T) do { println("MATCHED X"); },
// }
// named_baked_overloaded_parameters(i32, 10);
// named_baked_overloaded_parameters(i32, u32.[ 10, 20, 30 ]);
// named_baked_overloaded_parameters(i32, z = u32.[ 10, 20, 30 ]);
-}
\ No newline at end of file
+}
return Complex.{ a.re * b.re - a.im * b.im, a.re * b.im + a.im * b.re };
}
-C :: proc (re: f32, im: f32) -> Complex do return Complex.{ re, im };
+C :: (re: f32, im: f32) -> Complex do return Complex.{ re, im };
return res;
}
-join :: proc (a: Vec($T, $N), b: Vec(T, $M)) -> Vec(T, #value N + M) {
+join :: (a: Vec($T, $N), b: Vec(T, $M)) -> Vec(T, #value N + M) {
out : Vec(T, #value N + M);
for i: 0 .. N do out.data[i] = a.data[i];
for i: 0 .. M do out.data[i + N] = b.data[i];
return out;
}
-make_vec :: proc (data: [$N] $T) -> Vec(T, N) {
+make_vec :: (data: [$N] $T) -> Vec(T, N) {
return <Vec(T, N)>.{ data };
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
{
a := C(2, 3);
b := C(0, 1);
}
test_overload :: proc {
- proc (x: $T, y: T) -> T { return x; },
- proc (x: $T, y: $R) -> R { return y; },
+ (x: $T, y: T) -> T { return x; },
+ (x: $T, y: $R) -> R { return y; },
}
use package core
overloaded :: proc {
- proc (x: str, y: i32) do println("Called str, i32"); ,
- proc (x: f32, y: str) do println("Called f32, str"); ,
- proc (x: i32, y: i32) do println("Called i32, i32"); ,
+ (x: str, y: i32) do println("Called str, i32"); ,
+ (x: f32, y: str) do println("Called f32, str"); ,
+ (x: i32, y: i32) do println("Called i32, i32"); ,
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
x: i32 = 1234;
overloaded(~~x, 4);
use package core
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
NewPolyStruct :: struct (T: type_expr, N: i32) {
x : [N] T;
y : [N] f32;
println(swd.str_member);
poly_match(swd);
- poly_match :: proc (swd: SimpleWithDefault($T, $D)) do println(D);
-}
\ No newline at end of file
+ poly_match :: (swd: SimpleWithDefault($T, $D)) do println(D);
+}
use package core
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
arr := u32.[ 1, 2, 3, 4, 5 ];
for elem: array_to_slice(arr) do printf("%i ", elem);
for root: roots do println(root);
- array_to_slice :: proc (arr: [$N] $T) -> [] T {
+ array_to_slice :: (arr: [$N] $T) -> [] T {
return (#type [] T).{ ~~arr, N };
}
- compute_roots :: proc (arr: [$N] f32) {
+ compute_roots :: (arr: [$N] f32) {
for i: 0 .. N {
arr[i] = math.sqrt(cast(f32) i);
}
}
-}
\ No newline at end of file
+}
use package core
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
some_string := "This is a test string that can be read.\n\n\n\n\n\n1234 4567";
sstream := io.string_stream_make(some_string);
use package core
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
test_simple_struct();
test_simple_union();
test_polymorphic_union();
test_polymorphic_union_with_use();
- test_simple_struct :: proc () {
+ test_simple_struct :: () {
println("Testing a simple structure.");
SimpleStruct :: struct {
cast(u32) age, height, name);
}
- test_simple_union :: proc () {
+ test_simple_union :: () {
println("\n\nTesting a simple union.");
SimpleUnion :: struct #union {
printf("%p == 0x3F000000\n", u.int_val);
}
- test_default_values :: proc () {
+ test_default_values :: () {
println("\n\nTesting a struct with default values.");
DefaultedStruct :: struct {
ds2 := DefaultedStruct.{ i = 3, d = 0 };
print_defaulted(ds2);
- print_defaulted :: proc (use ds: DefaultedStruct) {
+ print_defaulted :: (use ds: DefaultedStruct) {
printf("DefaultedStruct(%i, %f, %l, %d)\n", i, f, l, d);
}
}
- test_simple_use :: proc () {
+ test_simple_use :: () {
println("\n\nTesting a struct with `use`.");
StructWithUse :: struct {
// This will not work. `use`d members cannot be set directly.
// swu3 := StructWithUse.{ 1234, 1, 2, 5678 };
- print_swu :: proc (use swu: StructWithUse) {
+ print_swu :: (use swu: StructWithUse) {
printf("StructWithUse(%i, (%f, %f), %i)\n",
first_member, x, y, last_member);
}
print_swu(swu2);
}
- test_polymorphic :: proc () {
+ test_polymorphic :: () {
println("\n\nTesting a polymorphic struct.");
PolyStruct :: struct (T: type_expr, R: type_expr) {
printf("PolyStruct<f32, i32>(%f, %i)\n", ps2.t_data, ps2.r_data);
}
- test_polymorphic_with_defaults :: proc () {
+ test_polymorphic_with_defaults :: () {
println("\n\nTesting a polymorphic struct with default values.");
PolyStruct :: struct (T: type_expr, R: type_expr) {
printf("PolyStruct<i32, f32>(%i, %f)\n", ps.t_data, ps.r_data);
}
- test_polymorphic_union_with_use :: proc () {
+ test_polymorphic_union_with_use :: () {
println("\n\nTesting a polymorphic union with use.");
PolyStruct :: struct (T: type_expr, R: type_expr) {
printf("%i, %d\n", ps.t_data, ps.r_data);
}
- test_union_with_use :: proc () {
+ test_union_with_use :: () {
println("\n\nTesting a union with use.");
Vec2 :: struct {
printf("Union(%s)\n", u.name);
}
- test_polymorphic_union :: proc () {
+ test_polymorphic_union :: () {
println("\n\nTesting a polymorphic union.");
PolyUnion :: struct (T: type_expr, R: type_expr) #union {
use package core;
-old_va_test :: proc (prefix: str, va: ..i32) {
+old_va_test :: (prefix: str, va: ..i32) {
println(prefix);
for v: va do println(v);
}
-new_va_test :: proc (prefix: str, va: ...) {
+new_va_test :: (prefix: str, va: ...) {
println(prefix);
for i: 0 .. va.count {
}
}
-main :: proc (args: [] cstr) {
+main :: (args: [] cstr) {
new_va_test("foo", 1, 2, 3.0f, 5.0f);
old_va_test("bar", 1);