From: Brendan Hansen Date: Tue, 28 Nov 2023 21:38:47 +0000 (-0600) Subject: updated examples X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=30e018fa00db2247b52067977a90d9c2175249ef;p=onyx.git updated examples --- diff --git a/examples/01_hello_world.onyx b/examples/01_hello_world.onyx index f1a5bf58..1ef644c5 100644 --- a/examples/01_hello_world.onyx +++ b/examples/01_hello_world.onyx @@ -16,6 +16,12 @@ package main // this is not necessary. #load "core/module" + +// To use packages, use the 'use' keyword. The 'core' package houses all of the +// standard library functions. In the case of this program, we will just be using +// the 'println' function to print something to the screen. +use core + // Below is the function declaration for `main`. While it might look weird at first // sight, Onyx uses a consistent syntax for declaring static "things" (functions, // structures, enums, etc.). It is always, diff --git a/examples/02_variables.onyx b/examples/02_variables.onyx index 8b749b2f..52d46111 100644 --- a/examples/02_variables.onyx +++ b/examples/02_variables.onyx @@ -3,6 +3,8 @@ // main package unless specified otherwise, and every compilations includes // the standard library. +use core + main :: () { // To declare a local variable, the variable name is simply followed by diff --git a/examples/03_basics.onyx b/examples/03_basics.onyx index f53c1d6b..1e34a831 100644 --- a/examples/03_basics.onyx +++ b/examples/03_basics.onyx @@ -1,6 +1,8 @@ // Now, lets go over the basic types and control flow mechanisms in Onyx. -main :: (args: [] cstr) { +use core + +main :: () { // Here is a list of all the builtin types: // void // bool diff --git a/examples/11_map.onyx b/examples/11_map.onyx index a8944247..19620095 100644 --- a/examples/11_map.onyx +++ b/examples/11_map.onyx @@ -9,8 +9,8 @@ main :: (args: [] cstr) { // To use it, simply create one like so: ages : Map(str, u32); - map.init(^ages, default=0); - defer map.free(^ages); + map.init(&ages); + defer map.free(&ages); // Alternatively you can use the map.make function to achieve the // same thing: @@ -22,26 +22,26 @@ main :: (args: [] cstr) { // returned when the map does not contain the requested key. // To put an entry into the map, use the map.put procedure. - map.put(^ages, "Dwight", 32); - map.put(^ages, "Jim", 25); - map.put(^ages, "Pam", 24); + map.put(&ages, "Dwight", 32); + map.put(&ages, "Jim", 25); + map.put(&ages, "Pam", 24); // To retrieve an entry's value, use the map.get procedure. - print_age :: (ages: ^Map(str, u32), name: str) { + print_age :: (ages: &Map(str, u32), name: str) { age := map.get(ages, name); printf("{}'s age is {}.\n", name, age); } - print_age(^ages, "Dwight"); - print_age(^ages, "Jim"); - print_age(^ages, "Pam"); - print_age(^ages, "Michael"); + print_age(&ages, "Dwight"); + print_age(&ages, "Jim"); + print_age(&ages, "Pam"); + print_age(&ages, "Michael"); // You may noticed if you ran this program that it prints Michael's // age is 0. This is because there was entry for the key 'Michael', // and we provided the default of '0'. To ensure that a key is in the // map, use the map.has procedure - println(map.has(^ages, "Dwight")); - println(map.has(^ages, "Michael")); + println(map.has(&ages, "Dwight")); + println(map.has(&ages, "Michael")); } diff --git a/examples/13_use_keyword.onyx b/examples/13_use_keyword.onyx index 2b8a13bd..7301a58b 100644 --- a/examples/13_use_keyword.onyx +++ b/examples/13_use_keyword.onyx @@ -23,8 +23,8 @@ main :: (args: [] cstr) { { // You can 'use' a package. You hopefully already knew this. - // This brings in all the symbols from the core.alloc package into - // this scope. + // This allows you to use the 'core.alloc' package in this scope + // under the name 'alloc'. use core.alloc // You can also restrict and partially rename the symbols that collide @@ -32,11 +32,8 @@ main :: (args: [] cstr) { } { - // You can also 'use' a package that is a subpackage of another package. Here, 'string' - // is a subpackage of 'core' and the above 'use core' makes this package - // available for you to write code like 'string.equal(...)'. However, because it is - // a namespace, you can 'use' it. - use core.string + // You can use everything inside of a package using a '*' between the include list. + use core.string {*} s := " Some string ..."; t := strip_leading_whitespace(s); // This would have to be 'string.strip_leading_whitespace' otherwise. @@ -68,30 +65,6 @@ main :: (args: [] cstr) { } } - { - // You can 'use' an enum. This is great if you have a burried enum in some other namespace - // and want to do a switch over that enum. - - Player :: struct { - Color :: enum { - Undefined; - Red; Green; Blue; - } - - color := Color.Red; - } - - value := Player.{}; - - use Player.Color; - switch value.color { - case Red do println("The color is red!"); - case Green do println("The color is green!"); - case Blue do println("The color is blue!"); - case #default do println("The color is unknown..."); - } - } - { // You can 'use' struct members that are structures. // This is already explained in the struct example. diff --git a/examples/18_macros.onyx b/examples/18_macros.onyx index 76d394d6..266ee73a 100644 --- a/examples/18_macros.onyx +++ b/examples/18_macros.onyx @@ -73,10 +73,10 @@ main :: (args: [] cstr) { // A powerful feature of Onyx that enables macros to be very useful is code blocks. // Code blocks allow you to capture code and treat it as a compile-time object that can - // be passed around. To create a code blocks, simply put '#quote' in front of the block + // be passed around. To create a code blocks, simply put '[]' in front of the block // of code you want to capture. Notice that this uses '::' because this is a compile-time // value. - simple_code :: #quote { + simple_code :: [] { println("This is a code block!"); }