84 lines
1.7 KiB
PSL
84 lines
1.7 KiB
PSL
import math
|
|
import events
|
|
import components
|
|
import ecs
|
|
|
|
const x = 10s // deduced type i16
|
|
var y = 14u // deduced type u32
|
|
var 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)
|
|
|
|
// return type deduced as u64
|
|
func merge(x: u32, y: u32):
|
|
return (x as u64) or ((y as u64) << 32)
|
|
|
|
var v = vec2(10, 20)
|
|
length(v)
|
|
|
|
// can be called using method syntax
|
|
v.length()
|
|
|
|
// function pointers
|
|
var 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, 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, size: i32):
|
|
r.width += size
|
|
r.height += size
|
|
|
|
var r = rectangle(10, 12)
|
|
r.extend(5)
|
|
|
|
// named initializers
|
|
var r2 = rectangle(width = 20, height = 30)
|
|
|
|
// regular pointers
|
|
var ptr: *i32 = null
|
|
var x = 15
|
|
ptr = &x
|
|
|
|
// field/method access using pointers is the same as with values
|
|
var sptr: *rectangle = &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))
|