updated examples
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 28 Nov 2023 21:38:47 +0000 (15:38 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 28 Nov 2023 21:38:47 +0000 (15:38 -0600)
examples/01_hello_world.onyx
examples/02_variables.onyx
examples/03_basics.onyx
examples/11_map.onyx
examples/13_use_keyword.onyx
examples/18_macros.onyx

index f1a5bf5843febe782b079d46b255d9c4bca2c9b0..1ef644c52e85407b24293fe6d9ae9767ba6da64a 100644 (file)
@@ -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,
index 8b749b2faf0271519b3f74f27c2fe6c6c9c15afd..52d461117f736e3273c25f16c7d2d05c72800bec 100644 (file)
@@ -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
index f53c1d6b900555509a81120b71169bb246dc77d9..1e34a831dbcc5a995de2dc4be4f472a700400d9b 100644 (file)
@@ -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
index a8944247900782d480697124a7d98ee28da399e1..19620095db80125d5350c0852e5a5dcf0afacd29 100644 (file)
@@ -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"));
 }
index 2b8a13bdbb0cb55d2eb32c3d31026ab4dc23f3a4..7301a58b95acc4b7d6bd67cae45bac4c1da5de85 100644 (file)
@@ -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.
index 76d394d63a44e8ebbc83fa5e6f661253d459d4d3..266ee73a7e72f301610e945e538c87c28c24e70e 100644 (file)
@@ -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!");
     }