diff --git a/examples/clojures.jt b/examples/closures.jt similarity index 100% rename from examples/clojures.jt rename to examples/closures.jt diff --git a/examples/custom-types.jt b/examples/custom-types.jt new file mode 100644 index 0000000..534e670 --- /dev/null +++ b/examples/custom-types.jt @@ -0,0 +1,11 @@ +import io + +// you can create your own types with 'type' +type T = int + +fn main () { + T a = 2 + + // since T is int, io.puts with an int works + io.puts(a) +} diff --git a/examples/function-overload.jt b/examples/function-overload.jt new file mode 100644 index 0000000..4912ef9 --- /dev/null +++ b/examples/function-overload.jt @@ -0,0 +1,16 @@ +import io +import integer + +fn my_puts(string str) { + io.puts(str) +} + +fn my_puts(int my_int) { + io.puts(integer.to_str(my_int)) +} + +fn main () { + my_puts(2) + my_puts("aaa") +} + diff --git a/examples/hello.jt b/examples/hello.jt index 11df9ab..798ae28 100644 --- a/examples/hello.jt +++ b/examples/hello.jt @@ -1,5 +1,6 @@ import io -fn main () { - io.shit("pant") +fn main () -> int { + io.puts("pant") + ret 0 } diff --git a/examples/higher-order-functions.jt b/examples/higher-order-functions.jt new file mode 100644 index 0000000..6cf325a --- /dev/null +++ b/examples/higher-order-functions.jt @@ -0,0 +1,26 @@ +import io + +// takes a function that receives two ints, returns an int +// Func is the function type keyword, to not switch it with fn (which declares +// a function) +fn function_tester (Func function ([int, int] -> int)) -> int { + func(2, 2) +} + +fn add(int a, int b) -> int { + a + b +} + +fn main () { + // passes the function add to function_tester + res := function_tester(add) + + // you can also create functions and put them in variables. not putting a + // function name on the fn block makes it return a Func instance to be put + // in a variable + anonymous := (fn () {}) + + // anonymous has type Func ([] -> void) + + io.puts("res = {res}") +} diff --git a/examples/strings.jt b/examples/strings.jt new file mode 100644 index 0000000..d7554f4 --- /dev/null +++ b/examples/strings.jt @@ -0,0 +1,15 @@ +import io + +fn main () { + s := "this is a string" + io.puts(s) + + s := "this is {s}" + io.puts(s) + + s + 2 // invalid + + // this however, is valid, there is an io.puts that handles int, + // more on function overload in a bit + io.puts(2) +} diff --git a/examples/structs.jt b/examples/structs.jt new file mode 100644 index 0000000..229058d --- /dev/null +++ b/examples/structs.jt @@ -0,0 +1,14 @@ +import io + +struct MyStruct { + int var1, + int var2, + int var3 +} + +fn main () { + st = MyStruct{1, 2, 3} + + // TODO: define a way for printable things + io.puts(st) +}