Skip to content

Commit

Permalink
add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
SenkJu committed Sep 11, 2020
1 parent 24c4ae4 commit e8f50e0
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
31 changes: 31 additions & 0 deletions examples/data_structures.snek
Original file line number Diff line number Diff line change
@@ -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"]));
39 changes: 39 additions & 0 deletions examples/functions.snek
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit e8f50e0

Please sign in to comment.