str_format :: format
str_format_va :: format_va
+Format_Flush_Callback :: struct {
+ data: rawptr = null;
+ func: (rawptr, str) -> bool = null_proc;
+}
+
Format_Output :: struct {
data: ^u8;
count: u32;
capacity: u32;
+ // When the data buffer fills, this procedure
+ // is called with the data, allowing for the
+ // buffer to be cleared and more to be written.
+ flush: Format_Flush_Callback;
+
write :: #match {
(use output: ^Format_Output, c: u8) {
- if count >= capacity do return;
+ if count >= capacity {
+ if flush.func == null_proc do return;
+ if !flush.func(flush.data, data[0 .. count]) do return;
+ count = 0;
+ }
data[count] = c;
count += 1;
(use output: ^Format_Output, s: str) {
for c: s {
- if count >= capacity do return;
+ if count >= capacity {
+ if flush.func == null_proc do return;
+ if !flush.func(flush.data, data[0 .. count]) do return;
+ count = 0;
+ }
data[count] = c;
count += 1;
}
format_va :: #match {}
-#match format_va (buffer: [] u8, format: str, va: [] any) -> str {
- output := Format_Output.{ buffer.data, 0, buffer.count };
+#match format_va (buffer: [] u8, format: str, va: [] any, flush := Format_Flush_Callback.{}) -> str {
+ output := Format_Output.{ buffer.data, 0, buffer.count, flush };
return format_va(^output, format, va);
}
-#match format_va (buffer: ^[..] u8, format: str, va: [] any) {
+#match format_va (buffer: ^[..] u8, format: str, va: [] any, flush := Format_Flush_Callback.{}) {
buffer.count = buffer.capacity;
- out := format_va(*buffer, format, va);
+ out := format_va(*buffer, format, va, flush);
buffer.count = out.count;
}
}
write_format :: (use writer: ^Writer, format: str, va: ..any) {
- // POTENTIAL BUG: this buffer will need to be bigger (or dynamic).
+ flush :: (writer, to_output) => {
+ write_str(writer, to_output);
+ return true;
+ }
+
buffer: [2048] u8;
- write_str(writer, conv.str_format_va(buffer, format, va));
+ write_str(writer, conv.format_va(buffer, format, va, .{writer, flush}));
}
write_escaped_str :: (use writer: ^Writer, s: str) {
}
printf :: (format: str, va: ..any) {
- buffer: [8196] u8;
- print(conv.str_format_va(buffer, format, va));
+ flush :: (_, to_output) => {
+ io.write(^stdio.print_writer, to_output);
+ __flush_stdio();
+ return true;
+ }
+
+ buffer: [1024] u8;
+ print(conv.format_va(buffer, format, va, .{null, flush}));
}
aprintf :: (format: str, va: ..any) -> str {
buffer: [8196] u8;
- out := conv.str_format_va(buffer, format, va);
+ out := conv.format_va(buffer, format, va);
return string.alloc_copy(out);
}
-// This works on both slices and arrays
-print_array :: #match {
- (arr: [$N] $T, sep := " ") {
- for i: 0 .. N {
- print(arr[i]);
- if i != N - 1 do print(sep);
- }
-
- print("\n");
- },
-
- (arr: $T, sep := " ") {
- for i: 0 .. arr.count {
- print(arr.data[i]);
- if i != arr.count - 1 do print(sep);
- }
-
- print("\n");
- }
-}
-
byte_dump :: (ptr: rawptr, byte_count: u32, bytes_per_line := 8) {
temp: [3] u8;
for i: 0 .. 10 do array.push(^arr, i);
// Now if we print the array, we will see the numbers from 0 to 9.
- print_array(arr);
+ println(arr);
// We can remove elements from the end using array.pop.
for i: 0 .. 4 do array.pop(^arr);
- print_array(arr);
+ println(arr);
// We can insert elements at the beginning using array.insert.
for i: 6 .. 10 do array.insert(^arr, 0, i);
- print_array(arr);
+ println(arr);
// There are many other useful functions in the core.array package.
// You can look in core/array.onyx for a list of all of them.
}
print("Doubled primes: ");
- print_array(primes);
+ println(primes);
// There is also support for custom iterators using the built-in Iterator type.
// You have to specify a next, close, and data member. The semantics are roughly
+++ /dev/null
-a was 4, 5, or 6
-a was something else.
-This is a test.
-Here are some numbers:
-1 2 3 4 5
-
-Got this many args: 7
-1203
-Got this many args: 7
-1203
-5
-Some information about those numbers:
-Min: 1
-Max: 5
-Sum: 15
-
-20
-Evens from 6 to 34:
-6 8 10 12 14 16 18 20 22 24 26 28 30 32
-
-Array details:
- Size: 0
- Capacity: 4
- Data ptr: 0x101A68
- Size of elements: 4
- Alignment of elements: 4
-
-
-Array details:
- Size: 0
- Capacity: 4
- Data ptr: 0x101A88
- Size of elements: 8
- Alignment of elements: 8
-
-0 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99
-0 5 10 15 20 4 9 14 19 3 8 13 18 2 7 12 17 1 6 11 16 0 5 10 15 20 4 9 14 19 3 8 13 18 2 7 12 17 1 6 11 16 0 5 10 15 20 4 9 14 19 3 8 13 18 2 7 12 17 1 6 11 16 0 5 10 15 20 4 9 14 19 3 8 13 18 2 7 12 17 1 6 11 16 0 5 10 15 20 4 9 14 19 3 8 13 18 2 7 12
-A has 22? false
-Vec3(0, 0, 0) Vec3(1, 1, 1) Vec3(2, 4, 8) Vec3(3, 9, 27) Vec3(4, 16, 64) Vec3(5, 25, 125) Vec3(6, 36, 216) Vec3(7, 49, 343) Vec3(8, 64, 512) Vec3(9, 81, 729) Vec3(10, 100, 1000) Vec3(11, 121, 1331) Vec3(12, 144, 1728) Vec3(13, 169, 2197) Vec3(14, 196, 2744) Vec3(15, 225, 3375) Vec3(16, 256, 4096) Vec3(17, 289, 4913) Vec3(18, 324, 5832) Vec3(19, 361, 6859) Vec3(20, 400, 8000) Vec3(21, 441, 9261) Vec3(22, 484, 10648) Vec3(23, 529, 12167) Vec3(24, 576, 13824) Vec3(25, 625, 15625) Vec3(26, 676, 17576) Vec3(27, 729, 19683) Vec3(28, 784, 21952) Vec3(29, 841, 24389) Vec3(30, 900, 27000) Vec3(31, 961, 29791) Vec3(32, 1024, 32768) Vec3(33, 1089, 35937) Vec3(34, 1156, 39304) Vec3(35, 1225, 42875) Vec3(36, 1296, 46656) Vec3(37, 1369, 50653) Vec3(38, 1444, 54872) Vec3(39, 1521, 59319) Vec3(40, 1600, 64000) Vec3(41, 1681, 68921) Vec3(42, 1764, 74088) Vec3(43, 1849, 79507) Vec3(44, 1936, 85184) Vec3(45, 2025, 91125) Vec3(46, 2116, 97336) Vec3(47, 2209, 103823) Vec3(48, 2304, 110592) Vec3(49, 2401, 117649) Vec3(50, 2500, 125000) Vec3(51, 2601, 132651) Vec3(52, 2704, 140608) Vec3(53, 2809, 148877) Vec3(54, 2916, 157464) Vec3(55, 3025, 166375) Vec3(56, 3136, 175616) Vec3(57, 3249, 185193) Vec3(58, 3364, 195112) Vec3(59, 3481, 205379) Vec3(60, 3600, 216000) Vec3(61, 3721, 226981) Vec3(62, 3844, 238328) Vec3(63, 3969, 250047) Vec3(64, 4096, 262144) Vec3(65, 4225, 274625) Vec3(66, 4356, 287496) Vec3(67, 4489, 300763) Vec3(68, 4624, 314432) Vec3(69, 4761, 328509) Vec3(70, 4900, 343000) Vec3(71, 5041, 357911) Vec3(72, 5184, 373248) Vec3(73, 5329, 389017) Vec3(74, 5476, 405224) Vec3(75, 5625, 421875) Vec3(76, 5776, 438976) Vec3(77, 5929, 456533) Vec3(78, 6084, 474552) Vec3(79, 6241, 493039) Vec3(80, 6400, 512000) Vec3(81, 6561, 531441) Vec3(82, 6724, 551368) Vec3(83, 6889, 571787) Vec3(84, 7056, 592704) Vec3(85, 7225, 614125) Vec3(86, 7396, 636056) Vec3(87, 7569, 658503) Vec3(88, 7744, 681472) Vec3(89, 7921, 704969) Vec3(90, 8100, 729000) Vec3(91, 8281, 753571) Vec3(92, 8464, 778688) Vec3(93, 8649, 804357) Vec3(94, 8836, 830584) Vec3(95, 9025, 857375) Vec3(96, 9216, 884736) Vec3(97, 9409, 912673) Vec3(98, 9604, 941192) Vec3(99, 9801, 970299)
-0x102D18 0x102D24 0x102D30 0x102D3C 0x102D48 0x102D54 0x102D60 0x102D6C 0x102D78 0x102D84 0x102D90 0x102D9C 0x102DA8 0x102DB4 0x102DC0 0x102DCC 0x102DD8 0x102DE4 0x102DF0 0x102DFC 0x102E08 0x102E14 0x102E20 0x102E2C 0x102E38 0x102E44 0x102E50 0x102E5C 0x102E68 0x102E74 0x102E80 0x102E8C 0x102E98 0x102EA4 0x102EB0 0x102EBC 0x102EC8 0x102ED4 0x102EE0 0x102EEC 0x102EF8 0x102F04 0x102F10 0x102F1C 0x102F28 0x102F34 0x102F40 0x102F4C 0x102F58 0x102F64 0x102F70 0x102F7C 0x102F88 0x102F94 0x102FA0 0x102FAC 0x102FB8 0x102FC4 0x102FD0 0x102FDC 0x102FE8 0x102FF4 0x103000 0x10300C 0x103018 0x103024 0x103030 0x10303C 0x103048 0x103054 0x103060 0x10306C 0x103078 0x103084 0x103090 0x10309C 0x1030A8 0x1030B4 0x1030C0 0x1030CC 0x1030D8 0x1030E4 0x1030F0 0x1030FC 0x103108 0x103114 0x103120 0x10312C 0x103138 0x103144 0x103150 0x10315C 0x103168 0x103174 0x103180 0x10318C 0x103198 0x1031A4 0x1031B0 0x1031BC
-1886 1890 1894 1898 1902 1906 1910 1914 1918 1922 1926 1930
-20 20 20 20 20 19 19 19 19 19 18 18 18 18 18 17 17 17 17 16 16 16 16 15 15 15 15 15 14 14 14 14 14 13 13 13 13 13 12 12 12 12 12 11 11 11 11 10 10 10 10 10 9 9 9 9 9 8 8 8 8 8 7 7 7 7 7 6 6 6 6 5 5 5 5 5 4 4 4 4 4 3 3 3 3 3 2 2 2 2 2 1 1 1 1 0 0 0 0 0
-297 294 291 288 285 282 279 276 273 270 267 264 261 258 255 252 249 246 243 240 237 234 231 228 225 222 219 216 213 210 207 204 201 198 195 192 189 186 183 180 177 174 171 168 165 162 159 156 153 150 147 144 141 138 135 132 129 126 123 120 117 114 111 108 105 102 99 96 93 90 87 84 81 78 75 72 69 66 63 60 57 54 51 48 45 42 39 36 33 30 27 24 21 18 15 12 9 6 3 0
-After adding...
-
-Array details:
- Size: 100
- Capacity: 128
- Data ptr: 0x1026F8
- Size of elements: 4
- Alignment of elements: 4
-
-
-Array details:
- Size: 100
- Capacity: 128
- Data ptr: 0x102908
- Size of elements: 8
- Alignment of elements: 8
-
-Array A sum: 999
-
-Has ^a[20]? true
-Has null? false
-Value at ^a[50]: 0x102A98 == 0x102A98
-Deleteing ^a[20]
-Has ^a[20]? false
-Clearing SOA...
+++ /dev/null
-package main
-
-#load "core/std"
-
-use package core
-
-print_arr_details :: proc (arr: ^[..] $T) {
- print("\nArray details:\n\tSize: ");
- println(arr.count);
- print("\tCapacity: ");
- println(arr.capacity);
- print("\tData ptr: ");
- println(cast(^void) arr.data);
- print("\tSize of elements: ");
- println(sizeof T);
- print("\tAlignment of elements: ");
- println(alignof T);
- print("\n");
-}
-
-print_vec :: proc (v: Vec3) #add_overload print {
- print("Vec3(");
- print(v.x);
- print(", ");
- print(v.y);
- print(", ");
- print(v.z);
- print(")");
-}
-
-// This demonstrates that we have something similar to static 'duck' typing.
-get_count :: proc (x: $T) -> u32 do return x.count;
-
-
-// Because of the static duck typing, this will pass as an
-// array/slice in most places.
-Dummy :: struct {
- count : u32 = 5;
- data : [5] u32;
-}
-
-
-/* This demos some of the power you have with the polymorphic types */
-compose :: proc (a: A, f: proc ($A) -> $B, g: proc (B) -> $C) -> C {
- return a |> f() |> g();
-}
-
-
-SOA :: struct {
- a : [..] i32;
- b : [..] i64;
- c : [..] Vec3;
-}
-
-soa_init :: proc (s: ^SOA) {
- array.init(^s.a);
- array.init(^s.b);
- array.init(^s.c);
-}
-
-soa_deinit :: proc (s: ^SOA) {
- array.free(^s.a);
- array.free(^s.b);
- array.free(^s.c);
-}
-
-get_range :: proc (arr: ^[..] $T) -> range {
- return 0 .. arr.count;
-}
-
-// NOTE: This function will be very useful for for loops. i.e.
-// for i: 0 .. 100 |> by(2) {
-// ...
-// }
-by :: proc (r: range, s: u32) -> range {
- return range.{ low = r.low, high = r.high, step = s };
-}
-
-switch_demo :: proc () {
- switch a := 4; a {
- case 4, 5, 6 {
- println("a was 4, 5, or 6");
- fallthrough fallthrough;
- }
-
- case 10 do println("a was 10");
-
- case #default {
- println("a was something else.");
- }
- }
-}
-
-vararg_test :: proc (prefix: str, nums: ..i32) {
- print(prefix);
- for num: nums {
- print(num);
- print(" ");
- }
-}
-
-NumInfo :: struct {
- min : i32;
- max : i32;
- sum : i32;
-}
-
-get_num_info :: proc (nums: ..i32) -> NumInfo {
- ni : NumInfo;
-
- ni.min = nums[0];
- for num: nums do if num < ni.min do ni.min = num;
-
- ni.max = nums[0];
- for num: nums do if num > ni.max do ni.max = num;
-
- ni.sum = 0;
- for num: nums do ni.sum += num;
-
- return ni;
-}
-
-print_strings :: proc (ss: ..str) {
- for s: ss do print_str(s);
-}
-
-multi_max :: proc (nums: ..$T) -> T {
- print("Got this many args: ");
- println(nums.count);
-
- max := nums[0];
- for num: nums do if num > max do max = num;
- return max;
-}
-
-weird_sum :: proc (n1: $T, others: ..T) -> T {
- s := n1;
- for n: others do s += n;
- return s;
-}
-
-main :: proc (args: [] cstr) {
- switch_demo();
-
- print_strings("This ", "is ", "a ", "test.\n");
-
- vararg_test("Here are some numbers:\n", 1, 2, 3, 4, 5);
- print("\n\n");
-
- multi_max(4, 2, 76, 3, 1203, 2, 4) |> println();
- multi_max(4, 2, 76, 3, 1203, 2, 4) |> println();
-
- weird_sum(4, 1) |> println();
-
- ni := get_num_info(1, 2, 3, 4, 5);
- println("Some information about those numbers:");
- print("Min: ");
- println(ni.min);
- print("Max: ");
- println(ni.max);
- print("Sum: ");
- println(ni.sum);
- print("\n");
-
- res := compose(5, proc (x: i32) -> i32 do return x * 3;,
- proc (x: i32) -> i32 do return x + 5;);
- println(res);
-
- s : SOA;
- soa_init(^s);
- defer {
- println("Clearing SOA...");
- soa_deinit(^s);
- }
-
- println("Evens from 6 to 34:");
- for i: 6 .. 34 |> by(2) {
- print(i);
- print(" ");
- }
- print("\n");
-
- print_arr_details(^s.a);
- print_arr_details(^s.b);
-
- for i: 0 .. 100 {
- array.push(^s.a, (5 * i) % 21);
- array.push(^s.b, 3 * cast(i64) i);
- array.push(^s.c, Vec3.{ i, i * i, i * i * i });
- }
-
- r := ^s.a |> get_range() |> by(3);
- print(r);
- print_array(^s.a);
- print("A has 22? ");
- println(array.contains(^s.a, 22));
-
- // NOTE: Iterating by value
- for vec: s.c {
- print(vec);
- print(" ");
- }
- print("\n");
-
- // NOTE: Iterating by pointer
- for ^vec: s.c {
- print(cast(^void) vec);
- print(" ");
- }
- print("\n");
-
- small : [12] i32;
- for ^it: small do *it = 1234 + cast(u32) it;
-
- for it: small {
- print(it);
- print(" ");
- }
- print("\n");
-
-
- array.sort(^s.a, cmp_dec);
- array.sort(^s.b, cmp_dec);
-
- print_array(^s.a);
- print_array(^s.b);
-
- println("After adding...");
- print_arr_details(^s.a);
- print_arr_details(^s.b);
-
- print("Array A sum: ");
- println(array.fold(^s.a, 0, proc (x: i32, acc: i32) -> i32 do return x + acc;));
- print("\n");
-
- pmap : map.Map(rawptr, rawptr);
- map.init(^pmap, null, 50);
- defer map.free(^pmap);
-
- for i: 0 .. 100 do map.put(^pmap, ^s.a[i], ^s.b[i]);
-
- print("Has ^a[20]? ");
- println(map.has(^pmap, ^s.a[20]));
-
- print("Has null? ");
- println(map.has(^pmap, null));
-
- print("Value at ^a[50]: ");
- print(cast(^void) map.get(^pmap, ^s.a[50]));
- print(" == ");
- println(cast(^void) (^s.b[50]));
-
- println("Deleteing ^a[20]");
- map.delete(^pmap, ^s.a[20]);
-
- print("Has ^a[20]? ");
- println(map.has(^pmap, ^s.a[20]));
-}
-
-
-Vec3 :: struct {
- x: i32;
- y: i32;
- z: i32;
-}
-
-cmp_vec3 :: proc (v1: Vec3, v2: Vec3) -> i32 {
- if v1.x != v2.x do return v1.x - v2.x;
- if v1.y != v2.y do return v1.y - v2.y;
- return v1.z - v2.z;
-}
=========================
10
-10 20 30
+[ 10, 20, 30 ]
10.0000
-10.0000 20.0000 30.0000
+[ 10.0000, 20.0000, 30.0000 ]
=========================
-MATCHED Y: 10 20
+MATCHED Y: [ 10, 20 ]
MATCHED X: 10
MATCHED Z: 10
poly_named :: (x: $T, y: [$N] T) {
println(x);
- print_array(y);
+ println(y);
}
poly_named(10, u32.[ 10, 20, 30 ]);
println("\n\n=========================");
poly_overloaded :: #match {
- (y: [$N] $T) { print("MATCHED Y: "); print_array(y); },
+ (y: [$N] $T) { print("MATCHED Y: "); println(y); },
(x: $T) { print("MATCHED X: "); println(x); },
(z: $T) { print("MATCHED Z: "); println(z); },
}