-
Notifications
You must be signed in to change notification settings - Fork 2
/
prelude.rl.h
192 lines (158 loc) · 4.07 KB
/
prelude.rl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
; @file prelude
(def fov 0.75)
; (def fov 0.2)
(def PI 3.1415926538)
(def true (= 1 1))
(def false (= 1 2))
(def qmap
(lambda (list)
(if (= (size list) 0)
list
(cons (quasicode (first list)) (qmap (rest list))))))
(def quasicode
(lambda (arg)
(if (not (list? arg)) (quote arg)
(if (same (first arg) 'unquote)
(first1 (rest arg))
(cons 'list (qmap arg))))))
(def quasiquote
(macro (arg)
(quasicode arg)))
(def andfn
(lambda (arg)
(if (= (size arg) 0)
true
(if (= (size arg) 1)
(first arg)
(list 'if (first arg) (andfn (rest arg)) false)))))
(def and (macro (...) (andfn ...)))
(def orfn
(lambda (arg)
(if (= (size arg) 0)
false
(if (= (size arg) 1)
(first arg)
(list 'if (first arg) true (orfn (rest arg)))))))
(def or (macro (...) (orfn ...)))
(def quotelist
(lambda (list)
(if (= (size list) 0)
list
(cons (quote (first list)) (quotelist (rest list))))))
(def let_fun
(lambda (binds stmts)
(if (= (size binds) 0)
(cons 'seq stmts)
(list
'let1
(first binds)
(let_fun (rest binds) stmts)))))
(def let
(macro (binds ...)
(let_fun binds ...)))
(def alias_fun
(lambda (binds stmts)
(if (= (size binds) 0)
(list 'seq stmts) ; seq body
(list
'alias1
(first binds)
(alias_fun (rest binds) stmts)))))
(def alias
(macro (binds ...)
(alias_fun binds ...)))
(def require (macro (...) (cons '_require (quotelist ...))))
(def : (macro (base ...) (cons '_element (cons base (quotelist ...)))))
; non-recursive to avoid browser erroring out due to stack overflow
(def slice
(lambda (from to args)
(let
((from' from) (to' to) (args' args)
(res '()))
(while (or (> from 0) (> to 0))
(if (> from 0)
(seq
(set from (- from 1))
(set to (- to 1))
(set args (rest args)))
(seq
(set res (cons (first args) res))
(set to (- to 1))
(set args (rest args)))))
res)))
(def for
(macro (var from to body)
`(seq
(let ((,var ,from))
(while (< ,var ,to)
(let
((_for_res ,body))
(set ,var (+ ,var 1))
_for_res))))))
(def splitfun
(lambda (base args)
(if (= (size args) 1)
(first args)
(let
((pivot (/ (size args) 2))
(left (splitfun base (slice 0 pivot args)))
(right (splitfun base (slice pivot (size args) args))))
(list base left right)))))
; TODO vectors?
(def min2
(macro (a b)
`(let
((%a ,a)
(%b ,b))
(if (< %a %b) %a %b))))
(def min
(macro (...)
(splitfun 'min2 ...)))
(def max2
(macro (a b)
`(let
((%a ,a)
(%b ,b))
(if (> %a %b) %a %b))))
(def max
(macro (...)
(splitfun 'max2 ...)))
(def Vec3f (vector-type 'float 3))
(def Vec4f (vector-type 'float 4))
(def SumType (function-type (list Vec3f) 'float))
(def sum (type SumType (lambda (vec) (+ (: vec x) (: vec y) (: vec z)))))
(def DotType (function-type (list Vec3f Vec3f) 'float))
(def dot (type DotType (lambda (v1 v2) (sum (* v1 v2)))))
(def length (lambda (vec) (sqrt (dot vec vec))))
(def length-squared (lambda (vec) (sum (* vec vec))))
(def NormType (function-type (list Vec3f) Vec3f))
(def normalized (type NormType (lambda (vec) (* vec (/ 1 (length vec))))))
(def angle
(lambda (a b)
(acos (dot (normalized a) (normalized b)))))
(def cross
(lambda (a b)
(vec3f
(- (* a:y b:z) (* a:z b:y))
(- (* a:z b:x) (* a:x b:z))
(- (* a:x b:y) (* a:y b:x)))))
(def X (vec3f 1 0 0))
(def Y (vec3f 0 1 0))
(def Z (vec3f 0 0 1))
(def +X (vec3f 1 0 0))
(def +Y (vec3f 0 1 0))
(def +Z (vec3f 0 0 1))
(def -X (vec3f -1 0 0))
(def -Y (vec3f 0 -1 0))
(def -Z (vec3f 0 0 -1))
(def blend
(lambda (a b f)
(+ a (* f (- b a)))))
(def black (vec3f 0 0 0))
(def blue (vec3f 0 0 1))
(def green (vec3f 0 1 0))
(def cyan (vec3f 0 1 1))
(def red (vec3f 1 0 0))
(def magenta (vec3f 1 0 1))
(def yellow (vec3f 1 1 0))
(def white (vec3f 1 1 1))