blob: f7b1adb7ae9f600e4257d6e1f01a37eab588810b (
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
|
(.module:
[lux (#- case let if)
[control
[monad (#+ do)]
["ex" exception (#+ exception:)]]
[data
[number]
["." text
format]
[collection
[list ("list/" Functor<List> Fold<List>)]
[set (#+ Set)]]]]
[/////
[reference (#+ Register)]
[host ["_" scheme (#+ Expression Computation Var)]]
[compiler ("operation/" Monad<Operation>)
[synthesis (#+ Synthesis Path)]]]
[//runtime (#+ Operation Compiler)]
[//reference])
(def: #export (let translate [valueS register bodyS])
(-> Compiler [Synthesis Register Synthesis]
(Operation Computation))
(do compiler.Monad<Operation>
[valueO (translate valueS)
bodyO (translate bodyS)]
(wrap (_.let (list [(//reference.local' register) valueO])
bodyO))))
(def: #export (record-get translate valueS pathP)
(-> Compiler Synthesis (List [Nat Bit])
(Operation Expression))
(do compiler.Monad<Operation>
[valueO (translate valueS)]
(wrap (list/fold (function (_ [idx tail?] source)
(.let [method (.if tail?
//runtime.product//right
//runtime.product//left)]
(method source (_.int (:coerce Int idx)))))
valueO
pathP))))
(def: #export (if translate [testS thenS elseS])
(-> Compiler [Synthesis Synthesis Synthesis]
(Operation Computation))
(do compiler.Monad<Operation>
[testO (translate testS)
thenO (translate thenS)
elseO (translate elseS)]
(wrap (_.if testO thenO elseO))))
(def: @savepoint (_.var "lux_pm_cursor_savepoint"))
(def: @cursor (_.var "lux_pm_cursor"))
(def: top _.length/1)
(def: (push! value var)
(-> Expression Var Computation)
(_.set! var (_.cons/2 value var)))
(def: (pop! var)
(-> Var Computation)
(_.set! var var))
(def: (push-cursor! value)
(-> Expression Computation)
(push! value @cursor))
(def: save-cursor!
Computation
(push! @cursor @savepoint))
(def: restore-cursor!
Computation
(_.set! @cursor (_.car/1 @savepoint)))
(def: cursor-top
Computation
(_.car/1 @cursor))
(def: pop-cursor!
Computation
(pop! @cursor))
(def: pm-error (_.string "PM-ERROR"))
(def: fail-pm! (_.raise/1 pm-error))
(def: @temp (_.var "lux_pm_temp"))
(exception: #export (unrecognized-path)
"")
(def: $alt_error (_.var "alt_error"))
(def: (pm-catch handler)
(-> Expression Computation)
(_.lambda [(list $alt_error) #.None]
(_.if (|> $alt_error (_.eqv?/2 pm-error))
handler
(_.raise/1 $alt_error))))
(def: (pattern-matching' translate pathP)
(-> Compiler Path (Operation Expression))
(.case pathP
(^ (synthesis.path/then bodyS))
(translate bodyS)
#synthesis.Pop
(operation/wrap pop-cursor!)
(#synthesis.Bind register)
(operation/wrap (_.define (//reference.local' register) [(list) #.None]
cursor-top))
(^template [<tag> <format> <=>]
(^ (<tag> value))
(operation/wrap (_.when (|> value <format> (<=> cursor-top) _.not/1)
fail-pm!)))
([synthesis.path/bit _.bool _.eqv?/2]
[synthesis.path/i64 _.int _.=/2]
[synthesis.path/f64 _.float _.=/2]
[synthesis.path/text _.string _.eqv?/2])
(^template [<pm> <flag> <prep>]
(^ (<pm> idx))
(operation/wrap (_.let (list [@temp (|> idx <prep> .int _.int (//runtime.sum//get cursor-top <flag>))])
(_.if (_.null?/1 @temp)
fail-pm!
(push-cursor! @temp)))))
([synthesis.side/left _.nil (<|)]
[synthesis.side/right (_.string "") inc])
(^template [<pm> <getter> <prep>]
(^ (<pm> idx))
(operation/wrap (|> idx <prep> .int _.int (<getter> cursor-top) push-cursor!)))
([synthesis.member/left //runtime.product//left (<|)]
[synthesis.member/right //runtime.product//right inc])
(^template [<tag> <computation>]
(^ (<tag> [leftP rightP]))
(do compiler.Monad<Operation>
[leftO (pattern-matching' translate leftP)
rightO (pattern-matching' translate rightP)]
(wrap <computation>)))
([synthesis.path/seq (_.begin (list leftO
rightO))]
[synthesis.path/alt (_.with-exception-handler
(pm-catch (_.begin (list restore-cursor!
rightO)))
(_.lambda [(list) #.None]
(_.begin (list save-cursor!
leftO))))])
_
(compiler.throw unrecognized-path [])))
(def: (pattern-matching translate pathP)
(-> Compiler Path (Operation Computation))
(do compiler.Monad<Operation>
[pattern-matching! (pattern-matching' translate pathP)]
(wrap (_.with-exception-handler
(pm-catch (_.raise/1 (_.string "Invalid expression for pattern-matching.")))
(_.lambda [(list) #.None]
pattern-matching!)))))
(def: #export (case translate [valueS pathP])
(-> Compiler [Synthesis Path] (Operation Computation))
(do compiler.Monad<Operation>
[valueO (translate valueS)]
(<| (:: @ map (_.let (list [@cursor (_.list/* (list valueO))]
[@savepoint (_.list/* (list))])))
(pattern-matching translate pathP))))
|