Here you will find concise examples of most things in the Sylt language.
Variables
constant :: 3.14
variable := []
two: int : 2
text: str = "abc"
num := .5 // => 0.5
num = 2.0 // => 2.0, note the lack of ":"
num += 1.0 // => 3.0
num -= 2.0 // => 1.0
num *= 10. // => 10.0
num /= 2. // => 5.0
If and else
if 1 < 0 do
// ...
else if "hello" == "" do
// ...
else do
// ...
end
one :: 1 if true else 2
Operators
1 + 1
2 - 2
3 * 3
10 / 2
-4
1 < 2
2 > 1
1.0 <= 1.0
1.0 >= 1.0
"a" == "a"
"a" != "b"
(1 + 1) * 2
true or false
true and true
not true
[1, 2] <=> [1, 2] // Assert equal
<!> // Assert unreachable, i.e. crash the program
Functions
hello :: fn do
print("Hello world")
end
add :: fn x: int, y: int -> int do
ret x + y
end
two_squared :: fn n -> n * n end(2) (1)
// Different ways of calling functions
hello()
print("Hello, world!")
add(1, 2)
hello'
print' "Hello, world!"
add' 1, 2
"Hello, world!" -> print()
1 -> add' 2
[1, 2, 3] -> for_each' print
1 | This creates a function with an implicit return. The function is then called immediately. |
Loops
i := 0
loop i < 10 do
print(i)
i += 1
end
j := 0
loop do
print(j)
if j == 0 do
j += 2
continue
end
if j == 2 do
break
end
end
Types
// Basic types
integer: int = 100
decimal: float = 1.5
string: str = "hello"
boolean: bool = true
nothing: void = nil
// Composite types
list: [int] = [1, 2, 3]
tuple: (str, float) = ("cookies", 1.5)
dict: {str: int} = {"a": 1, "b": 2}
set: {int} = {1, 2, 3}
function: fn -> void = fn do end
// Disambiguation
empty_dict := {:}
empty_set := {}
empty_tuple: () = ()
one_element_tuple: (int,) = (1,)
Blobs
Spider :: blob {
hp: int,
position: (float, float),
eat_bug: fn -> void,
}
new_spider :: fn x, y ->
Spider {
hp: 5,
position: (x, y),
eat_bug: fn do
self.hp += 1
end,
}
end
spider := new_spider(0.0, 0.0)
Imports
use file // file.thing
use folder/file // file.thing
use folder/ // folder.thing (1)
use file as name // name.thing
use /res/ // res.thing (2)
1 |
Same as
use folder/exports.sy as folder
.
|
2 |
Leading
/
signifies the project root.
|
Standard library
// Math
sin :: fn t: float -> float end
cos :: fn t: float -> float end
atan2 :: fn x: float, y: float -> float end
sqrt :: fn x: float -> float end
abs :: fn x: float -> float end
sign :: fn x: *X -> *X end
clamp :: fn a: *X, low: *X, high: *X -> *X end
min :: fn a: float, b: float -> float end
max :: fn a: float, b: float -> float end
rem :: fn x: *X, y: *X -> *X end
pow :: fn x: float, y: float -> float end
angle :: fn coord: (float, float) -> float end
magnitude :: fn coord: (float, float) -> float end
magnitude_squared :: fn coord: (float, float) -> float end
normalize :: fn coord: (float, float) -> (float, float) end
reflect :: fn v: (float, float), n: (float, float) -> (float, float) end
dot :: fn coord: (float, float) -> float end
// Conversions
as_float :: fn n: int -> float end
as_int :: fn n: float -> int end
as_str :: fn x: *X -> str end
as_chars :: fn s: str -> [int] end
as_char :: fn s: str -> int end
// Iterators
filter :: fn list: [*Item], f: fn *Item -> bool -> [*Item] end
fold :: fn list: [*Item], init: *Item, f: fn *Item, *Item -> *Out -> *Out end
for_each :: fn list: [*Item], f: fn *Item -> void -> void end
map :: fn list: [*Item], f: fn *Item -> *Out -> [*Out] end
reduce :: fn list: [*Item], f: fn *Item, *Item -> *Out -> *Out end
// System
print :: fn x: *X -> void end
dbg :: fn x: *X -> *X end
args :: fn -> {str: str} end
debug_assertions :: fn -> bool end
thread_sleep :: fn seconds: float -> void end
// Lists, sets and dicts
push :: fn list: [*Item], item: *Item -> void end
add :: fn set: {*Item}, item: *Item -> void end
clear :: fn list: [*Item] -> void end
len :: fn x: *Container -> int end
pop :: fn list: [*Item] -> *Item end
last :: fn list: [*Item] -> *Item end
random_choice :: fn list: [*Item] -> *Item end