added some examples; small bug fix
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 7 Jan 2021 18:41:01 +0000 (12:41 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Thu, 7 Jan 2021 18:41:01 +0000 (12:41 -0600)
bin/onyx
build.bat
examples/05_slices.onyx
examples/06_dynamic_arrays.onyx
onyx.exe
src/onyxchecker.c

index f3b6fd232377e35567e574df17181135bbb2c49d..b2d10f0dfedb6584c5886b9d6f72090f8e300798 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index afb37535e118ea11be2dcb274ff98554a42d1c85..959743559a45c2fc53c1561c7513250689879051 100644 (file)
--- a/build.bat
+++ b/build.bat
@@ -1,8 +1,16 @@
 @echo off
 
+set SOURCE_FILES=src/onyx.c src/onyxbuiltins.c src/onyxchecker.c src/onyxclone.c src/onyxdoc.c src/onyxentities.c src/onyxerrors.c src/onyxlex.c src/onyxparser.c src/onyxsempass.c src/onyxsymres.c src/onyxtypes.c src/onyxutils.c src/onyxwasm.c
+
+if "%1" == "1" (
+    set FLAGS=/O2 /MT /Z7
+) else (
+    set FLAGS=/Od /MDd /Z7
+)
+
 del *.pdb > NUL 2> NUL
 del *.ilk > NUL 2> NUL
 
-cl.exe /Od /MDd /Z7 /I include /std:c17 /Tc src/onyx.c src/onyxbuiltins.c src/onyxchecker.c src/onyxclone.c src/onyxdoc.c src/onyxentities.c src/onyxerrors.c src/onyxlex.c src/onyxparser.c src/onyxsempass.c src/onyxsymres.c src/onyxtypes.c src/onyxutils.c src/onyxwasm.c /link /DEBUG /OUT:onyx.exe /incremental:no /opt:ref /subsystem:console
+cl.exe %FLAGS% /I include /std:c17 /Tc %SOURCE_FILES% /link /DEBUG /OUT:onyx.exe /incremental:no /opt:ref /subsystem:console
 
 del *.obj > NUL 2> NUL
\ No newline at end of file
index b8d94c49f13a1d6e23a8d7b55df96f6faa18fbf1..182052dbd2e29b2d4fb77dbd7e521c9359297b5d 100644 (file)
@@ -1,6 +1,44 @@
+// Slices are a useful and simple data structure that are commonly
+// used throughout almost all Onyx programs. A slice in Onyx simply
+// represents a two member structure, consisting of a pointer, and
+// an unsigned count. Although that sounds deceptively simple, it
+// is a powerful construct to have. In fact, strings in Onyx, i.e.
+// the 'str' type, is actually just a slice of u8.
+
 #load "core/std/wasi"
 
 use package core
 
 main :: proc (args: [] cstr) {
+    // A dummy array to demonstrate.
+    arr := i32.[ 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 ];
+
+    // In Onyx a slice type is simply written as an empty pair of
+    // square brackets, with the type of elements of the slice
+    // immediately following.
+    slice : [] i32;
+
+    // Slices have two members, data and count. You can set them directly.
+    slice.data  = ^arr[3];
+    slice.count = 4;
+
+    // You can iterate over slices directly using a for-loop.
+    for elem: slice do printf("%i ", elem);
+    print("\n");
+
+    // Another equivalent way of writing lines 21 and 22 is to
+    // use the array subscript notation, but provide a range for
+    // the index as so. This is generally called the slice literal
+    // notation.
+    slice = arr[3 .. 7];
+
+    // Printing it out to verify it is the same.
+    for elem: slice do printf("%i ", elem);
+    print("\n");
+
+    // Since strings are represented as slices in Onyx, substrings
+    // are easily accessed using the same slice literal syntax.
+    some_string := "This is a test string for demoing purposes.";
+    substr      := some_string.data[10 .. 14];
+    println(substr);
 }
index b8d94c49f13a1d6e23a8d7b55df96f6faa18fbf1..5738d5ff72c8dab5c3c4c2b5a30be77f05b1a798 100644 (file)
@@ -1,6 +1,50 @@
+// Dynamic arrays are arguably the most useful data structure of
+// all time. They allow for the array to be dynamically resized,
+// which means you don't need to know upfront the maximum number
+// of elements that will be in your array. It can always grow to
+// the size it needs to.
+
+// Dynamic arrays in Onyx are easy to use on purpose, because I
+// know how useful they are in almost every program I write.
+
 #load "core/std/wasi"
 
 use package core
 
 main :: proc (args: [] cstr) {
+    // Declaring dynamic arrays in Onyx looks like this. The
+    // '..' makes it a dynamic array, as opposed to a slice
+    // or fixed size array.
+    arr : [..] i32;    
+
+    // Currently, there are no implicit procedure calls anywhere
+    // in the language because I find explicitly specifying
+    // everything to be less bug prone in the long run, at the
+    // trade off of being mildly tedious. Since there are implicit
+    // procedure calls, we have to initialize and free the dynamic
+    // array explicitly. The 'defer' simply runs array.free at the
+    // end of this procedure scope. We will talk more about how to
+    // use the defer keyword later.
+    array.init(^arr);
+    defer array.free(^arr);
+
+    // Once the dynamic array has been initialized, we can begin
+    // to use it. The most common function you will probably use
+    // is array.push. It simply appends a new element to end of
+    // the array.
+    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);
+
+    // We can remove elements from the end using array.pop.
+    for i: 0 .. 4 do array.pop(^arr);
+    print_array(arr);
+
+    // We can insert elements at the beginning using array.insert.
+    for i: 6 .. 10 do array.insert(^arr, 0, i);
+    print_array(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.
 }
index f7f0822f76199eafd6b5d5f4a8098bef1de8ae94..7258d4213114a631c7461b356aef196b1028d84d 100644 (file)
Binary files a/onyx.exe and b/onyx.exe differ
index f324e1c8ec792bd28705599253a94fff57b225e4..ba3596afb884246b30ebb17c85617c8c6d284a79 100644 (file)
@@ -305,11 +305,12 @@ no_match:
     }
 
     char* arg_str = bh_alloc(global_scratch_allocator, 1024);
+    arg_str[0] = '\0';
 
     bh_arr_each(AstArgument *, arg, call->arg_arr) {
         strncat(arg_str, type_get_name((*arg)->value->type), 1023);
 
-        if ((*arg)->next != NULL)
+        if (arg != &bh_arr_last(call->arg_arr))
             strncat(arg_str, ", ", 1023);
     }