pslang/examples/ir_test.psl

79 lines
1.5 KiB
PSL

func print(c: u8):
foreign func putchar(c: i32) -> i32
putchar(c as i32)
func print_i32(x: i32):
if x < 0:
print('-')
print_i32(-x)
return
if x >= 10:
print_i32(x / 10)
print('0' + (x % 10 as u8))
func print_f32(x: f32):
if x < 0.0:
print('-')
print_f32(-x)
return
foreign func floorf(x: f32) -> f32
let floor = floorf(x) as i32
print_i32(floor)
print('.')
mut y = x - (floor as f32)
mut i = 0
while i < 5:
y = y * 10.0
let yfloor = floorf(y) as i32
print('0' + (yfloor as u8))
y = y - (yfloor as f32)
i = i + 1
struct vec3:
x: f32
y: f32
z: f32
func print_vec3(v: vec3):
print('(')
print_f32(v.x)
print(',')
print_f32(v.y)
print(',')
print_f32(v.z)
print(')')
func dot(a: vec3, b: vec3) -> f32:
return a.x * b.x + a.y * b.y + a.z * b.z
func add(a: vec3, b: vec3) -> vec3:
return vec3(a.x + b.x, a.y + b.y, a.z + b.z)
func mult(a: vec3, b: f32) -> vec3:
return vec3(a.x * b, a.y * b, a.z * b)
func normalized(v: vec3) -> vec3:
foreign func sqrtf(x: f32) -> f32
return mult(v, 1.0 / sqrtf(dot(v, v)))
struct ray:
origin: vec3
direction: vec3
func intersect_plane(ray: ray, normal: vec3, value: f32) -> f32:
return (value - dot(ray.origin, normal)) / dot(ray.direction, normal)
let t = intersect_plane(ray(vec3(0.0, 0.0, 5.0), vec3(0.0, 1.0, -1.0)), vec3(0.0, 0.0, 1.0), 0.0)
let a = vec3(5.0, 1.0, 4.0)
let n = normalized(vec3(1.0, 2.0, 3.0))
let b = add(a, mult(n, -dot(a, n)))
print_vec3(a)
print('\n')
print_vec3(n)
print('\n')
print_vec3(b)
print('\n')
print_f32(dot(b, n))
print('\n')