blob: 992701393ba2e78905c83ce67e379d944fd060ae (
plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
(.module:
[lux #*
[abstract
[monad (#+ do)]]
[control
["." function]
["p" parser ("#;." monad)
["s" code (#+ Parser)]]]
[data
[number (#+ hex)]
[text
["%" format (#+ format)]]
[collection
["." list ("#;." monad)]]]
[macro
["." code]
[syntax (#+ syntax:)]]
[target
["_" scheme (#+ Expression Computation Var)]]]
["." ///
["#/" //
["#/" // #_
[analysis (#+ Variant)]
["#." name]
["#." synthesis]]]])
(template [<name> <base>]
[(type: #export <name>
(<base> Var Expression Expression))]
[Operation ///.Operation]
[Phase ///.Phase]
[Handler ///.Handler]
[Bundle ///.Bundle]
)
(def: prefix Text "LuxRuntime")
(def: unit (_.string /////synthesis.unit))
(def: (flag value)
(-> Bit Computation)
(if value
(_.string "")
_.nil))
(def: (variant' tag last? value)
(-> Expression Expression Expression Computation)
(<| (_.cons/2 tag)
(_.cons/2 last?)
value))
(def: #export (variant [lefts right? value])
(-> (Variant Expression) Computation)
(variant' (_.int (.int lefts)) (flag right?) value))
(def: #export none
Computation
(variant [0 #0 ..unit]))
(def: #export some
(-> Expression Computation)
(|>> [0 #1] ..variant))
(def: #export left
(-> Expression Computation)
(|>> [0 #0] ..variant))
(def: #export right
(-> Expression Computation)
(|>> [0 #1] ..variant))
(def: declaration
(Parser [Text (List Text)])
(p.either (p.and s.local-identifier (p;wrap (list)))
(s.form (p.and s.local-identifier (p.some s.local-identifier)))))
(syntax: (runtime: {[name args] declaration}
definition)
(let [implementation (code.local-identifier (format "@@" name))
runtime (format prefix "__" (/////name.normalize name))
@runtime (` (_.var (~ (code.text runtime))))
argsC+ (list;map code.local-identifier args)
argsLC+ (list;map (|>> /////name.normalize (format "LRV__") code.text (~) (_.var) (`))
args)
declaration (` ((~ (code.local-identifier name))
(~+ argsC+)))
type (` (-> (~+ (list.repeat (list.size argsC+) (` _.Expression)))
_.Computation))]
(wrap (list (` (def: (~' #export) (~ declaration)
(~ type)
(~ (case argsC+
#.Nil
@runtime
_
(` (_.apply/* (~ @runtime) (list (~+ argsC+))))))))
(` (def: (~ implementation)
_.Computation
(~ (case argsC+
#.Nil
(` (_.define-constant (~ @runtime) [(list) #.None] (~ definition)))
_
(` (let [(~+ (|> (list.zip2 argsC+ argsLC+)
(list;map (function (_ [left right])
(list left right)))
list;join))]
(_.define-function (~ @runtime) [(list (~+ argsLC+)) #.None]
(~ definition))))))))))))
(runtime: (slice offset length list)
(<| (_.if (_.null?/1 list)
list)
(_.if (|> offset (_.>/2 (_.int +0)))
(slice (|> offset (_.-/2 (_.int +1)))
length
(_.cdr/1 list)))
(_.if (|> length (_.>/2 (_.int +0)))
(_.cons/2 (_.car/1 list)
(slice offset
(|> length (_.-/2 (_.int +1)))
(_.cdr/1 list))))
_.nil))
(syntax: #export (with-vars {vars (s.tuple (p.many s.local-identifier))}
body)
(wrap (list (` (let [(~+ (|> vars
(list;map (function (_ var)
(list (code.local-identifier var)
(` (_.var (~ (code.text (format "LRV__" (/////name.normalize var)))))))))
list;join))]
(~ body))))))
(runtime: (lux//try op)
(with-vars [error]
(_.with-exception-handler
(_.lambda [(list error) #.None]
(..left error))
(_.lambda [(list) #.None]
(..right (_.apply/* op (list ..unit)))))))
(runtime: (lux//program-args program-args)
(with-vars [@loop @input @output]
(_.letrec (list [@loop (_.lambda [(list @input @output) #.None]
(_.if (_.eqv?/2 _.nil @input)
@output
(_.apply/2 @loop (_.cdr/1 @input) (..some (_.vector/* (list (_.car/1 @input) @output))))))])
(_.apply/2 @loop (_.reverse/1 program-args) ..none))))
(def: runtime//lux
Computation
(_.begin (list @@lux//try
@@lux//program-args)))
(def: last-index
(-> Expression Computation)
(|>> _.length/1 (_.-/2 (_.int +1))))
(runtime: (tuple//left lefts tuple)
(with-vars [last-index-right]
(_.begin
(list (_.define-constant last-index-right (..last-index tuple))
(_.if (_.>/2 lefts last-index-right)
## No need for recursion
(_.vector-ref/2 tuple lefts)
## Needs recursion
(tuple//left (_.-/2 last-index-right lefts)
(_.vector-ref/2 tuple last-index-right)))))))
(runtime: (tuple//right lefts tuple)
(with-vars [last-index-right right-index @slice]
(_.begin
(list (_.define-constant last-index-right (..last-index tuple))
(_.define-constant right-index (_.+/2 (_.int +1) lefts))
(_.cond (list [(_.=/2 last-index-right right-index)
(_.vector-ref/2 tuple right-index)]
[(_.>/2 last-index-right right-index)
## Needs recursion.
(tuple//right (_.-/2 last-index-right lefts)
(_.vector-ref/2 tuple last-index-right))])
(_.begin
(list (_.define-constant @slice (_.make-vector/1 (_.-/2 right-index (_.length/1 tuple))))
(_.vector-copy!/5 @slice (_.int +0) tuple right-index (_.length/1 tuple))
@slice))))
)))
(runtime: (sum//get sum last? wanted-tag)
(with-vars [sum-tag sum-flag sum-value]
(let [no-match _.nil
is-last? (|> sum-flag (_.eqv?/2 (_.string "")))
test-recursion (_.if is-last?
## Must recurse.
(sum//get sum-value
last?
(|> wanted-tag (_.-/2 sum-tag)))
no-match)]
(<| (_.let (list [sum-tag (_.car/1 sum)]
[sum-value (_.cdr/1 sum)]))
(_.let (list [sum-flag (_.car/1 sum-value)]
[sum-value (_.cdr/1 sum-value)]))
(_.if (|> wanted-tag (_.=/2 sum-tag))
(_.if (|> sum-flag (_.eqv?/2 last?))
sum-value
test-recursion))
(_.if (|> wanted-tag (_.>/2 sum-tag))
test-recursion)
(_.if (_.and (list (|> last? (_.eqv?/2 (_.string "")))
(|> wanted-tag (_.</2 sum-tag))))
(variant' (|> sum-tag (_.-/2 wanted-tag)) sum-flag sum-value))
no-match))))
(def: runtime//adt
Computation
(_.begin (list @@tuple//left
@@tuple//right
@@sum//get)))
(runtime: (i64//logical-right-shift shift input)
(_.if (_.=/2 (_.int +0) shift)
input
(|> input
(_.arithmetic-shift/2 (_.*/2 (_.int -1) shift))
(_.bit-and/2 (_.int (hex "+7FFFFFFFFFFFFFFF"))))))
(def: runtime//bit
Computation
(_.begin (list @@i64//logical-right-shift)))
(runtime: (frac//decode input)
(with-vars [@output]
(_.let (list [@output ((_.apply/1 (_.global "string->number")) input)])
(_.if (_.and (list (_.not/1 (_.=/2 @output @output))
(_.not/1 (_.eqv?/2 (_.string "+nan.0") input))))
..none
(..some @output)))))
(def: runtime//frac
Computation
(_.begin
(list @@frac//decode)))
(runtime: (io//current-time _)
(|> (_.apply/* (_.global "current-second") (list))
(_.*/2 (_.int +1,000))
_.exact/1))
(def: runtime//io
(_.begin (list @@io//current-time)))
(def: runtime
Computation
(_.begin (list @@slice
runtime//lux
runtime//bit
runtime//adt
runtime//frac
runtime//io
)))
(def: #export generate
(Operation Any)
(///.with-buffer
(do ////.monad
[_ (///.save! true ["" ..prefix]
..runtime)]
(///.save-buffer! ""))))
|