blob: edfa2d038d0967830dd054baae4a3294416715bf (
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
|
(.module:
[lux (#- case let if)
[abstract
[monad (#+ do)]]
[control
["ex" exception (#+ exception:)]]
[data
["." text
format]
[collection
["." list ("#@." functor fold)]]]
[target
["_" js (#+ Expression Computation Var Statement)]]]
["." // #_
["#." runtime (#+ Operation Phase)]
["#." reference]
["#." primitive]
["#/" // #_
["#." reference]
["#/" // ("#@." monad)
[synthesis
["." case]]
["#/" // #_
[reference (#+ Register)]
["#." synthesis (#+ Synthesis Path)]]]]])
(def: #export register
(///reference.local _.var))
(def: #export (let generate [valueS register bodyS])
(-> Phase [Synthesis Register Synthesis]
(Operation Computation))
(do ////.monad
[valueO (generate valueS)
bodyO (generate bodyS)]
## TODO: Find some way to do 'let' without paying the price of the closure.
(wrap (_.apply/* (_.closure (list (..register register))
(_.return bodyO))
(list valueO)))))
(def: #export (record-get generate valueS pathP)
(-> Phase Synthesis (List (Either Nat Nat))
(Operation Expression))
(do ////.monad
[valueO (generate valueS)]
(wrap (list@fold (function (_ side source)
(.let [method (.case side
(^template [<side> <accessor>]
(<side> lefts)
(<accessor> (_.i32 (.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 Computation))
(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 Statement)
(_.statement (|> @cursor (_.do "push" (list value)))))
(def: peek-and-pop-cursor
Expression
(|> @cursor (_.do "pop" (list))))
(def: pop-cursor!
Statement
(_.statement ..peek-and-pop-cursor))
(def: length
(|>> (_.the "length")))
(def: last-index
(|>> ..length (_.- (_.i32 +1))))
(def: peek-cursor
Expression
(|> @cursor (_.at (last-index @cursor))))
(def: save-cursor!
Statement
(.let [cursor (|> @cursor (_.do "slice" (list)))]
(_.statement (|> @savepoint (_.do "push" (list cursor))))))
(def: restore-cursor!
Statement
(_.set @cursor (|> @savepoint (_.do "pop" (list)))))
(def: fail-pm! _.break)
(exception: #export unrecognized-path)
(def: (multi-pop-cursor! pops)
(-> Nat Statement)
(.let [popsJS (_.i32 (.int pops))]
(_.statement (|> @cursor (_.do "splice" (list (|> @cursor ..length (_.- popsJS))
popsJS))))))
(template [<name> <flag> <prep>]
[(def: (<name> simple? idx)
(-> Bit Nat Statement)
($_ _.then
(_.set @temp (|> idx <prep> .int _.i32 (//runtime.sum//get ..peek-cursor <flag>)))
(.if simple?
(_.when (_.= _.null @temp)
fail-pm!)
(_.if (_.= _.null @temp)
fail-pm!
(push-cursor! @temp)))))]
[left-choice _.null (<|)]
[right-choice (_.string "") inc]
)
(def: (alternation pre! post!)
(-> Statement Statement Statement)
($_ _.then
(_.do-while _.false
($_ _.then
..save-cursor!
pre!))
($_ _.then
..restore-cursor!
post!)))
(def: (pattern-matching' generate pathP)
(-> Phase Path (Operation Statement))
(.case pathP
(^ (/////synthesis.path/then bodyS))
(do ////.monad
[body! (generate bodyS)]
(wrap (_.return body!)))
#/////synthesis.Pop
(////@wrap pop-cursor!)
(#/////synthesis.Bind register)
(////@wrap (_.define (..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 .int) //runtime.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 (push-cursor! (_.at (_.i32 +0) ..peek-cursor)))
(^template [<pm> <getter>]
(^ (<pm> lefts))
(////@wrap (push-cursor! (<getter> (_.i32 (.int lefts)) ..peek-cursor))))
([/////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
(_.define (..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> leftP rightP))
(do ////.monad
[left! (pattern-matching' generate leftP)
right! (pattern-matching' generate rightP)]
(wrap (<combinator> left! right!))))
([/////synthesis.path/seq _.then]
[/////synthesis.path/alt alternation])
_
(////.throw unrecognized-path [])))
(def: (pattern-matching generate pathP)
(-> Phase Path (Operation Statement))
(do ////.monad
[pattern-matching! (pattern-matching' generate pathP)]
(wrap ($_ _.then
(_.do-while _.false
pattern-matching!)
(_.throw (_.string case.pattern-matching-error))))))
(def: #export (case generate [valueS pathP])
(-> Phase [Synthesis Path] (Operation Computation))
(do ////.monad
[stack-init (generate valueS)
path! (pattern-matching generate pathP)
#let [closure (<| (_.closure (list))
($_ _.then
(_.declare @temp)
(_.define @cursor (_.array (list stack-init)))
(_.define @savepoint (_.array (list)))
path!))]]
(wrap (_.apply/* closure (list)))))
|