blob: 6e4988b9bdfc769015da2eaa2abace8d32d83ccf (
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
|
(.module:
[lux (#- case let if)
[abstract
[monad (#+ do)]]
[control
["ex" exception (#+ exception:)]]
[data
["." text
format]
[collection
["." list ("#@." functor fold)]
["." set]]]
[host
["_" python (#+ Expression SVar Statement)]]]
["." // #_
["#." runtime (#+ Operation Phase)]
["#." reference]
["#." primitive]
["#/" //
["#." reference]
["#/" // ("#@." monad)
[synthesis
["." case]]
["#/" // #_
["." reference (#+ Register)]
["#." synthesis (#+ Synthesis Path)]]]]])
(def: #export register
(///reference.local _.var))
(def: #export capture
(///reference.foreign _.var))
(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 (_.apply/* (_.lambda (list (..register register))
bodyO)
(list valueO)))))
(def: #export (record-get generate valueS pathP)
(-> Phase Synthesis (List [Nat Bit])
(Operation (Expression Any)))
(do ////.monad
[valueO (generate valueS)]
(wrap (list@fold (function (_ [idx tail?] source)
(.let [method (.if tail?
//runtime.product//right
//runtime.tuple//left)]
(method source (_.int (.int idx)))))
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 (_.var "lux_pm_cursor_savepoint"))
(def: @cursor (_.var "lux_pm_cursor"))
(def: @temp (_.var "lux_pm_temp"))
(def: (push-cursor! value)
(-> (Expression Any) (Statement Any))
(_.statement (|> @cursor (_.do "append" (list value)))))
(def: peek-and-pop-cursor
(Expression Any)
(|> @cursor (_.do "pop" (list))))
(def: pop-cursor!
(Statement Any)
(_.statement ..peek-and-pop-cursor))
(def: peek-cursor
(Expression Any)
(_.nth (_.int -1) @cursor))
(def: save-cursor!
(Statement Any)
(.let [cursor (_.slice-from (_.int +0) @cursor)]
(_.statement (|> @savepoint (_.do "append" (list cursor))))))
(def: restore-cursor!
(Statement Any)
(_.set (list @cursor) (|> @savepoint (_.do "pop" (list)))))
(def: fail-pm! _.break)
(exception: #export unrecognized-path)
(def: (multi-pop-cursor! pops)
(-> Nat (Statement Any))
(_.delete (_.slice-from (_.int (i/* -1 (.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-cursor <flag>)))
(_.if (_.= _.none @temp)
fail-pm!
(.if simple?
_.pass
(push-cursor! @temp)))))]
[left-choice _.none (<|)]
[right-choice (_.string "") inc]
)
(def: (alternation pre! post!)
(-> (Statement Any) (Statement Any) (Statement Any))
($_ _.then
(_.while (_.bool true)
($_ _.then
..save-cursor!
pre!))
($_ _.then
..restore-cursor!
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-cursor!)
(#/////synthesis.Bind register)
(////@wrap (_.set (list (..register register)) ..peek-cursor))
(^template [<tag> <format>]
(^ (<tag> value))
(////@wrap (_.when (|> value <format> (_.= ..peek-cursor) _.not)
fail-pm!)))
([/////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])
(^template [<pm> <getter> <prep>]
(^ (<pm> idx))
(////@wrap (|> idx <prep> .int _.int (<getter> ..peek-cursor) push-cursor!)))
([/////synthesis.member/left //runtime.tuple//left (<|)]
[/////synthesis.member/right //runtime.product//right inc])
(^ (/////synthesis.!bind-top register thenP))
(do ////.monad
[then! (pattern-matching' generate thenP)]
(////@wrap ($_ _.then
(_.set (list (..register register)) ..peek-and-pop-cursor)
then!)))
(^ (/////synthesis.!multi-pop nextP))
(.let [[extra-pops nextP'] (case.count-pops nextP)]
(do ////.monad
[next! (pattern-matching' generate nextP')]
(////@wrap ($_ _.then
(multi-pop-cursor! (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!)
(_.raise (_.Exception/1 (_.string case.pattern-matching-error)))))))
(def: (gensym prefix)
(-> Text (Operation SVar))
(:: ////.monad map (|>> %n (format prefix) _.var) ///.next))
(def: #export (case generate [valueS pathP])
(-> Phase [Synthesis Path] (Operation (Expression Any)))
(do ////.monad
[initG (generate valueS)
pattern-matching! (pattern-matching generate pathP)
@case (..gensym "case")
@init (..gensym "init")
#let [@dependencies+ (|> (case.storage pathP)
(get@ #case.dependencies)
set.to-list
(list@map (function (_ variable)
(.case variable
(#reference.Local register)
(..register register)
(#reference.Foreign register)
(..capture register)))))]
_ (///.save! ["" (_.code @case)]
(_.def @case (list& @init @dependencies+)
($_ _.then
(_.set (list @cursor) (_.list (list @init)))
(_.set (list @savepoint) (_.list (list)))
pattern-matching!)))]
(wrap (_.apply/* @case (list& initG @dependencies+)))))
|