blob: 18979b0fae6ce1a4349a96a08240f47b54076843 (
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
|
(.module:
[lux (#- case let if)
[abstract
[monad (#+ do)]]
[control
["ex" exception (#+ exception:)]]
[data
["." text
format]
[collection
["." list ("#@." functor fold)]
["." set]]]
[host
["_" ruby (#+ Expression Statement)]]]
["." // #_
["#." runtime (#+ Operation Phase)]
["#." reference]
["#." primitive]
["#/" //
["#." reference]
["#/" // ("#@." monad)
[synthesis
["." case]]
["#/" // #_
["." reference (#+ Register)]
["#." synthesis (#+ Synthesis Path)]]]]])
(def: #export register
(///reference.local _.local))
(def: #export capture
(///reference.foreign _.local))
(def: #export (let generate [valueS register bodyS])
(-> Phase [Synthesis Register Synthesis]
(Operation (Expression Any)))
(do ////.monad
[valueO (generate valueS)
bodyO (generate bodyS)]
## TODO: Find some way to do 'let' without paying the price of the closure.
(wrap (|> bodyO
_.return
(_.lambda #.None (list (..register register)))
(_.do "call" (list valueO))))))
(def: #export (record-get generate valueS pathP)
(-> Phase Synthesis (List (Either Nat Nat))
(Operation (Expression Any)))
(do ////.monad
[valueO (generate valueS)]
(wrap (list@fold (function (_ side source)
(.let [method (.case side
(^template [<side> <accessor>]
(<side> lefts)
(<accessor> (_.int (.int lefts))))
([#.Left //runtime.tuple//left]
[#.Right //runtime.tuple//right]))]
(method source)))
valueO
pathP))))
(def: #export (if generate [testS thenS elseS])
(-> Phase [Synthesis Synthesis Synthesis]
(Operation (Expression Any)))
(do ////.monad
[testO (generate testS)
thenO (generate thenS)
elseO (generate elseS)]
(wrap (_.? testO thenO elseO))))
(def: @savepoint (_.local "lux_pm_savepoint"))
(def: @cursor (_.local "lux_pm_cursor"))
(def: @temp (_.local "lux_pm_temp"))
(def: (push! value)
(-> (Expression Any) (Statement Any))
(_.statement (|> @cursor (_.do "push" (list value)))))
(def: peek-and-pop
(Expression Any)
(|> @cursor (_.do "pop" (list))))
(def: pop!
(Statement Any)
(_.statement ..peek-and-pop))
(def: peek
(Expression Any)
(_.nth (_.int -1) @cursor))
(def: save!
(Statement Any)
(.let [cursor (_.array-range (_.int +0) (_.int -1) @cursor)]
(_.statement (|> @savepoint (_.do "push" (list cursor))))))
(def: restore!
(Statement Any)
(_.set (list @cursor) (|> @savepoint (_.do "pop" (list)))))
(def: fail! _.break)
(exception: #export unrecognized-path)
(def: (multi-pop! pops)
(-> Nat (Statement Any))
(_.statement (_.do "slice!" (list (_.int (i/* -1 (.int pops)))
(_.int (.int pops)))
@cursor)))
(template [<name> <flag> <prep>]
[(def: (<name> simple? idx)
(-> Bit Nat (Statement Any))
($_ _.then
(_.set (list @temp) (|> idx <prep> .int _.int (//runtime.sum//get ..peek <flag>)))
(.if simple?
(_.when (_.= _.nil @temp)
fail!)
(_.if (_.= _.nil @temp)
fail!
(..push! @temp)))))]
[left-choice _.nil (<|)]
[right-choice (_.string "") inc]
)
(def: (alternation pre! post!)
(-> (Statement Any) (Statement Any) (Statement Any))
($_ _.then
(_.while (_.bool true)
($_ _.then
..save!
pre!))
($_ _.then
..restore!
post!)))
(def: (pattern-matching' generate pathP)
(-> Phase Path (Operation (Statement Any)))
(.case pathP
(^ (/////synthesis.path/then bodyS))
(:: ////.monad map _.return (generate bodyS))
#/////synthesis.Pop
(////@wrap ..pop!)
(#/////synthesis.Bind register)
(////@wrap (_.set (list (..register register)) ..peek))
(^template [<tag> <format>]
(^ (<tag> value))
(////@wrap (_.when (|> value <format> (_.= ..peek) _.not)
fail!)))
([/////synthesis.path/bit //primitive.bit]
[/////synthesis.path/i64 //primitive.i64]
[/////synthesis.path/f64 //primitive.f64]
[/////synthesis.path/text //primitive.text])
(^template [<complex> <simple> <choice>]
(^ (<complex> idx))
(////@wrap (<choice> false idx))
(^ (<simple> idx nextP))
(|> nextP
(pattern-matching' generate)
(:: ////.monad map (_.then (<choice> true idx)))))
([/////synthesis.side/left /////synthesis.simple-left-side ..left-choice]
[/////synthesis.side/right /////synthesis.simple-right-side ..right-choice])
(^ (/////synthesis.member/left 0))
(////@wrap (|> ..peek (_.nth (_.int +0)) ..push!))
(^template [<pm> <getter>]
(^ (<pm> lefts))
(////@wrap (|> ..peek (<getter> (_.int (.int lefts))) ..push!)))
([/////synthesis.member/left //runtime.tuple//left]
[/////synthesis.member/right //runtime.tuple//right])
(^ (/////synthesis.!bind-top register thenP))
(do ////.monad
[then! (pattern-matching' generate thenP)]
(////@wrap ($_ _.then
(_.set (list (..register register)) ..peek-and-pop)
then!)))
(^ (/////synthesis.!multi-pop nextP))
(.let [[extra-pops nextP'] (case.count-pops nextP)]
(do ////.monad
[next! (pattern-matching' generate nextP')]
(////@wrap ($_ _.then
(..multi-pop! (n/+ 2 extra-pops))
next!))))
(^template [<tag> <combinator>]
(^ (<tag> preP postP))
(do ////.monad
[pre! (pattern-matching' generate preP)
post! (pattern-matching' generate postP)]
(wrap (<combinator> pre! post!))))
([/////synthesis.path/seq _.then]
[/////synthesis.path/alt ..alternation])
_
(////.throw unrecognized-path [])))
(def: (pattern-matching generate pathP)
(-> Phase Path (Operation (Statement Any)))
(do ////.monad
[pattern-matching! (pattern-matching' generate pathP)]
(wrap ($_ _.then
(_.while (_.bool true)
pattern-matching!)
(_.statement (_.raise (_.string case.pattern-matching-error)))))))
(def: #export (case generate [valueS pathP])
(-> Phase [Synthesis Path] (Operation (Expression Any)))
(do ////.monad
[initG (generate valueS)
pattern-matching! (pattern-matching generate pathP)]
(wrap (|> ($_ _.then
(_.set (list @cursor) (_.array (list initG)))
(_.set (list @savepoint) (_.array (list)))
pattern-matching!)
(_.lambda #.None (list))
(_.do "call" (list))))))
|