From: Brendan Hansen Date: Thu, 7 Jan 2021 18:41:01 +0000 (-0600) Subject: added some examples; small bug fix X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=34dce43bcb32ab38a6bd913e9c662469c8cd0a04;p=onyx.git added some examples; small bug fix --- diff --git a/bin/onyx b/bin/onyx index f3b6fd23..b2d10f0d 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/build.bat b/build.bat index afb37535..95974355 100644 --- 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 diff --git a/examples/05_slices.onyx b/examples/05_slices.onyx index b8d94c49..182052db 100644 --- a/examples/05_slices.onyx +++ b/examples/05_slices.onyx @@ -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); } diff --git a/examples/06_dynamic_arrays.onyx b/examples/06_dynamic_arrays.onyx index b8d94c49..5738d5ff 100644 --- a/examples/06_dynamic_arrays.onyx +++ b/examples/06_dynamic_arrays.onyx @@ -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. } diff --git a/onyx.exe b/onyx.exe index f7f0822f..7258d421 100644 Binary files a/onyx.exe and b/onyx.exe differ diff --git a/src/onyxchecker.c b/src/onyxchecker.c index f324e1c8..ba3596af 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -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); }