95 lines
1.7 KiB
PSL
95 lines
1.7 KiB
PSL
import math
|
|
import events
|
|
import components
|
|
import ecs
|
|
|
|
const x = 10s // deduced type i16
|
|
mut y = 14u // deduced type u32
|
|
mut z: f64 = 3.14l
|
|
|
|
func fma(x: f32, y: f32, z: f32) -> f32 {
|
|
return x * y + z
|
|
}
|
|
|
|
struct vec2 {
|
|
x: f32
|
|
y: f32
|
|
}
|
|
|
|
// pass by value
|
|
func length(v: vec2) -> f32 {
|
|
return math.sqrt(v.x * v.x + v.y * v.y)
|
|
}
|
|
|
|
func merge(x: u32, y: u32) -> u64 {
|
|
return (x as u64) or ((y as u64) << 32)
|
|
}
|
|
|
|
mut v = vec2(10, 20)
|
|
length(v)
|
|
|
|
// can be called using method syntax
|
|
v.length()
|
|
|
|
// function pointers
|
|
mut my_func = fma // deduced type (f32, f32, f32) -> f32
|
|
|
|
// pass by reference/pointer with *
|
|
// TODO: const pointer?
|
|
func my_system(event: events.update, position: components.position mut*, velocity: components.velocity*) {
|
|
position += event.dt * velocity
|
|
}
|
|
|
|
func attach(dispatcher: ecs.dispatcher*) {
|
|
// TODO: how does it work? C++-style variadic templates? Oh no...
|
|
dispatcher.system(my_system)
|
|
}
|
|
|
|
// objects with methods
|
|
struct rectangle {
|
|
width: i32
|
|
height: i32
|
|
}
|
|
|
|
func extend(r: rectangle mut*, size: i32) {
|
|
r.width += size
|
|
r.height += size
|
|
}
|
|
|
|
mut r = rectangle(10, 12)
|
|
r.extend(5)
|
|
|
|
// named initializers
|
|
mut r2 = rectangle(width = 20, height = 30)
|
|
|
|
// regular pointers
|
|
mut ptr: i32* = null
|
|
mut x = 15
|
|
ptr = &x
|
|
|
|
// field/method access using pointers is the same as with values
|
|
mut sptr: rectangle mut* = &r
|
|
r.width *= 2
|
|
|
|
// simple generics
|
|
struct array(T) {
|
|
data: *T
|
|
size: u64
|
|
}
|
|
|
|
// TODO: constructors? destructors?
|
|
func new(self: array(T), size: u64) {
|
|
return array(T)(data = mem.alloc(size * sizeof(T)), size = size)
|
|
}
|
|
|
|
// TODO: static arrays?
|
|
// TODO: move-only types? alloc returns smth like unique ptr?
|
|
|
|
struct kvpair(K, V) {
|
|
key: K
|
|
value: V
|
|
}
|
|
|
|
struct arraymap(K, V) {
|
|
values: array(kvpair(K, V))
|
|
}
|