forked from luna/jorts
add more examples
This commit is contained in:
parent
b3ac29fd3b
commit
a4685423a4
7 changed files with 85 additions and 2 deletions
11
examples/custom-types.jt
Normal file
11
examples/custom-types.jt
Normal file
|
@ -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)
|
||||
}
|
16
examples/function-overload.jt
Normal file
16
examples/function-overload.jt
Normal file
|
@ -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")
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
import io
|
||||
|
||||
fn main () {
|
||||
io.shit("pant")
|
||||
fn main () -> int {
|
||||
io.puts("pant")
|
||||
ret 0
|
||||
}
|
||||
|
|
26
examples/higher-order-functions.jt
Normal file
26
examples/higher-order-functions.jt
Normal file
|
@ -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}")
|
||||
}
|
15
examples/strings.jt
Normal file
15
examples/strings.jt
Normal file
|
@ -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)
|
||||
}
|
14
examples/structs.jt
Normal file
14
examples/structs.jt
Normal file
|
@ -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)
|
||||
}
|
Loading…
Reference in a new issue