From e8f50e0473a04647c9660d45e972d307be7c0d5a Mon Sep 17 00:00:00 2001 From: Senk Ju Date: Sat, 12 Sep 2020 01:14:55 +0200 Subject: [PATCH] add more examples --- README.md | 1 + examples/data_structures.snek | 31 ++++++++++++++++++++++++++++ examples/functions.snek | 39 +++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) create mode 100644 examples/data_structures.snek create mode 100644 examples/functions.snek diff --git a/README.md b/README.md index 20a1ce7..a49269a 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,7 @@ Binaries of the latest unreleased version can be found on the [actions tab](http ## Example ``` +// This is a comment let add = func(a, b) { let result = a + b; diff --git a/examples/data_structures.snek b/examples/data_structures.snek new file mode 100644 index 0000000..dada7bf --- /dev/null +++ b/examples/data_structures.snek @@ -0,0 +1,31 @@ +// Simple array +let simple_array = [1, "Axolotl", 2.3]; +print(simple_array[1]); +simple_array[1] = "Gods"; +array_push(simple_array, 42); +print(array_length(simple_array)); + +// Arrays may contain any expression +let complex_array = [ + func() { + 2 + 3 + }, + 42, + 3.4, + [ + "Another array" + ], + { + "my_key": 5 + } +]; + +// Hashes contain key-value-pairs +let my_hash = { + "my_func": func(a) { + a + 7 + }, + "number": 23 +}; + +print(my_hash["my_func"](my_hash["number"])); \ No newline at end of file diff --git a/examples/functions.snek b/examples/functions.snek new file mode 100644 index 0000000..5129c1d --- /dev/null +++ b/examples/functions.snek @@ -0,0 +1,39 @@ +// Simple function +let add = func(a, b) { + return a + b; +}; + +print(add(1, 2)); + +// Implicit returns are supported +let add_impl = func(a, b) { + a + b +}; + +print(add_impl(1, 2)); + +// Functions are expressions +func(name) { + print("Hello, my name is " . name); +}("Senk Ju"); + +let func_arr = [ + func() { + return "Axolotl"; + } +]; + +print(func_arr[0]()); + +// Closures are supported +let my_closure = func() { + let name = "Senk Ju"; + + let print_name = func() { + print("Hello, my name is " . name); + }; + + print_name(); +}; + +my_closure(); \ No newline at end of file