From c892bede6525be16336dcd99a27eb286787a74df Mon Sep 17 00:00:00 2001 From: Brendan Hansen Date: Thu, 5 Aug 2021 07:18:43 -0500 Subject: [PATCH] cleaned up more examples --- examples/02_variables.onyx | 6 ++--- examples/03_basics.onyx | 19 +++++++------- examples/04_fixed_arrays.onyx | 16 ++++++------ examples/10_switch_statements.onyx | 2 +- examples/12_varargs.onyx | 40 +++++++++++++++++++----------- examples/13_use_keyword.onyx | 2 +- 6 files changed, 47 insertions(+), 38 deletions(-) diff --git a/examples/02_variables.onyx b/examples/02_variables.onyx index 98d2606a..2160c4b3 100644 --- a/examples/02_variables.onyx +++ b/examples/02_variables.onyx @@ -18,7 +18,7 @@ main :: (args: [] cstr) { // inferred from the right hand side of the equals. bar := 15; - print("foo is %i\n", foo); - print("footwo is %i\n", footwo); - print("bar is %i\n", bar); + printf("foo is {}\n", foo); + printf("footwo is {}\n", footwo); + printf("bar is {}\n", bar); } diff --git a/examples/03_basics.onyx b/examples/03_basics.onyx index c3ca6b57..58e5275f 100644 --- a/examples/03_basics.onyx +++ b/examples/03_basics.onyx @@ -36,8 +36,8 @@ main :: (args: [] cstr) { foo := 10; foo_ptr := ^foo; - printf("foo is %i\n", foo); - printf("foo_ptr is %p\n", foo_ptr); + printf("foo is {}\n", foo); + printf("foo_ptr is {}\n", foo_ptr); // An important type to know before proceeding is the range type. When designing @@ -52,7 +52,7 @@ main :: (args: [] cstr) { // to specify the step in a range literal. rng := 5 .. 25; print("rng is "); - print(rng); + println(rng); // Onyx has 3 different array-like types: // * Fixed-size arrays, written [N] , where N is a compile time know integer. @@ -67,7 +67,7 @@ main :: (args: [] cstr) { for i: 0 .. 10 { fixed_arr[i] = i * i; } - print("fixed_arr[3] is %i\n", fixed_arr[3]); + printf("fixed_arr[3] is {}\n", fixed_arr[3]); // A quick thing to note about fixed size arrays is that they copy their contents // on assignment. In this example, another_arr declares space on the stack for 10 @@ -79,7 +79,7 @@ main :: (args: [] cstr) { fixed_arr[3] = 1234; - printf("another_arr[3] is %i\n", another_arr[3]); + printf("another_arr[3] is {}\n", another_arr[3]); // Slices are a concept not unique to Onyx. They represent a pointer and a count, @@ -92,7 +92,7 @@ main :: (args: [] cstr) { // of fixed_arr. It is important to know that this does NOT make a copy of the // data. It simply points into the same memory as fixed_arr. slice_arr := fixed_arr[3 .. 9]; - print("slice_arr[3] is %i\n", slice_arr[3]); + printf("slice_arr[3] is {}\n", slice_arr[3]); // Dynamic arrays are the most common arrays used in practice. They represent a @@ -108,8 +108,7 @@ main :: (args: [] cstr) { array.push(^dyn_arr, i * i * i); } - print("dyn_arr is "); - print_array(dyn_arr); + printf("dyn_arr is {}\n", dyn_arr); @@ -155,7 +154,7 @@ main :: (args: [] cstr) { // for i to be 11. while i := 0; i < 10 { - printf("%i ", i); + printf("{} ", i); i += 1; } else { print("The while loop never ran..."); @@ -172,7 +171,7 @@ main :: (args: [] cstr) { // break or continue the loop that you want to. Take this example. while i := 0; i < 10 { while j := 0; j < 10 { - printf("%i %j\n", i, j); + printf("{} {}\n", i, j); if i == 1 && j == 2 do break break; j += 1; diff --git a/examples/04_fixed_arrays.onyx b/examples/04_fixed_arrays.onyx index 6f005bf1..b68f91dc 100644 --- a/examples/04_fixed_arrays.onyx +++ b/examples/04_fixed_arrays.onyx @@ -30,13 +30,13 @@ main :: (args: [] cstr) { // To access certain elements of the array, simply use the array access operator, // [...], as so. This sets the fourth element of the array to be 1234. arr[3] = 1234; - printf("arr[3] is %i\n", arr[3]); + printf("arr[3] is {}\n", arr[3]); // Fixed-size arrays can be iterated over using a `for` loop as so. for element: arr { - printf("%i ", element); + printf("{} ", element); } - printf("\n"); + print("\n"); // Fixed-size arrays are passed to procedures by reference. In the following example, @@ -50,7 +50,7 @@ main :: (args: [] cstr) { array_proc(arr); for element: arr { - printf("%i ", element); + printf("{} ", element); } @@ -59,8 +59,8 @@ main :: (args: [] cstr) { // array. Slices and dynamic arrays do not operate this way. other_arr : [10] i32; other_arr = arr; - for other_element: other_arr do printf("%i ", other_element); - printf("\n"); + for other_element: other_arr do printf("{} ", other_element); + print("\n"); // This does mean that the following pattern of double buffering arrays (which // is used widely in many C programs), will work in Onyx, but it will be much @@ -84,8 +84,8 @@ main :: (args: [] cstr) { // OR arr_lit := i32.[ 2, 3, 5, 7, 11 ]; - for elem: arr_lit do printf("%i ", elem); - printf("\n"); + for elem: arr_lit do printf("{} ", elem); + print("\n"); // The type of the elements of the array is given right before the `.`. The // expression `u32.[ 1, 2, 3, 4, 5 ]` has the type `[5] u32`. The elements diff --git a/examples/10_switch_statements.onyx b/examples/10_switch_statements.onyx index 0f8bc03e..9c9bec6c 100644 --- a/examples/10_switch_statements.onyx +++ b/examples/10_switch_statements.onyx @@ -50,7 +50,7 @@ main :: (args: [] cstr) { case 5 { x := math.max(20, 50); - defer printf("x in case '5' is %i.\n", x); + defer printf("x in case '5' is {}.\n", x); if x >= 40 do fallthrough; diff --git a/examples/12_varargs.onyx b/examples/12_varargs.onyx index 268d1013..34d9948e 100644 --- a/examples/12_varargs.onyx +++ b/examples/12_varargs.onyx @@ -29,7 +29,7 @@ main :: (args: [] cstr) { typed_varargs :: (args: ..i32) { print("Typed variadic arguments: "); for x: args { - printf("%i ", x); + printf("{} ", x); } print("\n"); } @@ -47,12 +47,19 @@ main :: (args: [] cstr) { // can be achieved because in Onyx, untyped varargs know how many varargs are passed // through a runtime count. This means if you call vararg_get more times than there // are varargs, you will get empty or default results back. + // + // Most of the time with untyped varargs, you will have some kind of format + // specifier to indicate how many and of what type the varargs are. For example + // 'printf' used to use this: + // + // printf :: (format: str, va: ...) -> void + // printf("%i %f %l %d", int, float, long, double); untyped_varargs :: (args: ...) { print("Untyped variadic arguments: "); x: i32; while vararg_get(args, ^x) { - printf("%p ", x); + printf("{} ", cast(rawptr) x); } print("\n"); @@ -62,18 +69,21 @@ main :: (args: [] cstr) { + // Untyped varargs will remain in the language for now, but a much better approach + // has been taken to have mixed type variadic arguments, and it is called 'any'. + // This is a procedure that takes a variadic number of 'any's. The powerful fact + // about 'any' is that is contains a member called 'type', which can be used to + // lookup type information at runtime. 'printf' already uses this for its '{}' format + // specifiers, and it knows how to print type names. + any_varargs :: (args: ..any) { + print("Any variadic arguments: "); - // Most of the time with untyped varargs, you will have some kind of format - // specifier to indicate how many and of what type the varargs are. For example - // 'printf' uses this: - // - // printf :: (format: str, va: ...) -> void - // printf("%i %f %l %d", int, float, long, double); - // - // There are plans for the future to have better runtime type information available, - // which would allow you to introspect the types of each vararg at runtime. This - // would mean that instead of using '%i' style specifiers in printf, you could use - // '{}' to indicate the next variadic argument, whose type is automatically determined. - // If I'm not explaining this very well I apologize and hope that when the feature - // is implemented, it will make sense why it is a good idea. + for arg: args { + printf("{}, ", arg.type); + } + + print("\n"); + } + + any_varargs(1, 2.0f, "test", any_varargs); } diff --git a/examples/13_use_keyword.onyx b/examples/13_use_keyword.onyx index 7c6e5380..b3576401 100644 --- a/examples/13_use_keyword.onyx +++ b/examples/13_use_keyword.onyx @@ -56,7 +56,7 @@ main :: (args: [] cstr) { // procedure between two areas of concern. basically_a_method :: (use dummy: ^Dummy) { - printf("Dummy with name '%s', and age '%i'.\n", name, age); + printf("Dummy with name '{}', and age '{}'.\n", name, age); } dummy := Dummy.{ "Phil", 15 }; -- 2.25.1