@@ -41,7 +41,8 @@ trait ControlFlowContext[Result] {
41
41
42
42
def block (e : Expr [Unit ]) : Label = block(_ => e)
43
43
def block (fn : Label => Expr [Unit ]) : Label
44
- def reg [T : Type ] : Expr [T ]
44
+ def callReg [T : Type ] : Expr [T ]
45
+ def retReg [T : Type ] : Expr [T ]
45
46
46
47
def end (v : Expr [Result ]) : Expr [Unit ]
47
48
@@ -61,9 +62,9 @@ case class BlockCompiler[ET:Type,Result](seqCtx : SequenceContext[ET], flowCtx :
61
62
def handleRec [AT : Type ,T : Type ](g : Grammar [AT ,T ,ET ], arg : Expr [AT ], yes : Expr [T ]=> Expr [Unit ], no : Expr [Unit ]) : Expr [Unit ] = {
62
63
if recursions.contains(g) then {
63
64
val recLabel = flowCtx.block(recLabel => {
64
- this .copy(recMap= recMap.+ ((g, recLabel))).computeValue(g, flowCtx.reg [AT ], flowCtx.ret(_), flowCtx.end(' {None }))
65
+ this .copy(recMap= recMap.+ ((g, recLabel))).computeValue(g, flowCtx.callReg [AT ], flowCtx.ret(_), flowCtx.end(' {None }))
65
66
})
66
- val retLabel = flowCtx.block(yes(flowCtx.reg [T ]))
67
+ val retLabel = flowCtx.block(yes(flowCtx.retReg [T ]))
67
68
flowCtx.call(recLabel, retLabel, arg)
68
69
} else {
69
70
computeValue(g, arg, yes, no)
@@ -84,15 +85,17 @@ case class BlockCompiler[ET:Type,Result](seqCtx : SequenceContext[ET], flowCtx :
84
85
}
85
86
}
86
87
case Disjunction (l,r) => {
88
+ val lv = computeValue(l, arg, yes, no)
89
+ val rv = computeValue(r, arg, yes, no)
87
90
' { // this is eager disjunction, TODO "correct" backtracking disjunction option
88
91
if $ {speculateReachable(l, arg)}
89
- then $ {computeValue(l, arg, yes, no) }
90
- else $ {computeValue(r, arg, yes, no) }
92
+ then $ {lv }
93
+ else $ {rv }
91
94
}
92
95
}
93
96
case r : Recursion [AT ,T ,ET ] => {
94
- val retLabel = flowCtx.block(yes(flowCtx.reg [T ]))
95
- flowCtx.call(recMap(r.g), retLabel, arg)
97
+ val retLabel = flowCtx.block(yes(flowCtx.retReg [T ]))
98
+ flowCtx.call(recMap.get (r.g).get , retLabel, arg)
96
99
}
97
100
case _ => ???
98
101
}
@@ -143,7 +146,8 @@ object PolyParse {
143
146
val stackPC : ArrayStack [Int ] = new ArrayStack
144
147
val stack : ArrayStack [Any ] = new ArrayStack
145
148
var pc : Int = 0
146
- var arg : Any = null
149
+ var callReg_ : Any = null
150
+ var retReg_ : Any = null
147
151
var loop = true
148
152
while (loop) {
149
153
$ {
@@ -153,22 +157,24 @@ object PolyParse {
153
157
type Label = Int
154
158
def block (f : Int => Expr [Unit ]) : Int = {
155
159
val label = blocks.length
156
- blocks.+= ((label, f(label)))
160
+ blocks += null
161
+ blocks(label) = (label, f(label))
157
162
label
158
163
}
159
- def reg [T : Type ] : Expr [T ] = ' {arg.asInstanceOf [T ]}
164
+ def callReg [T : Type ] : Expr [T ] = ' {callReg_.asInstanceOf [T ]}
165
+ def retReg [T : Type ] : Expr [T ] = ' {retReg_.asInstanceOf [T ]}
160
166
def end (v : Expr [Result ]) : Expr [Unit ] = ' {
161
- arg = $v
167
+ retReg_ = $v
162
168
loop = false
163
169
}
164
170
def call (label : Int , ret : Int , a : Expr [Any ]) : Expr [Unit ] = ' {
165
171
pc = $ {label.toExpr}
166
- arg = $a
172
+ callReg_ = $a
167
173
stackPC.push($ {ret.toExpr})
168
174
}
169
175
def ret (v : Expr [Any ]) : Expr [Unit ] = ' {
170
176
pc = stackPC.pop
171
- arg = $v
177
+ retReg_ = $v
172
178
}
173
179
def push (v : Expr [Any ]) : Expr [Unit ] = ' {
174
180
stack.push($v)
@@ -180,7 +186,7 @@ object PolyParse {
180
186
})
181
187
}
182
188
}
183
- arg .asInstanceOf [Result ]
189
+ retReg_ .asInstanceOf [Result ]
184
190
}
185
191
186
192
def compile [T : Type ,ET : Type ,Seq [_]: MakeSequenceContext ](g : Grammar [Unit ,T ,ET ])(implicit seqT : Type [Seq [T ]]) : Expr [Seq [ET ] => Option [T ]] = {
@@ -204,13 +210,13 @@ object PolyParse {
204
210
})
205
211
}
206
212
207
- def runTest = {
213
+ def main ( argv : Array [ String ]) : Unit = {
208
214
import scala .quoted .Toolbox .Default ._
209
215
object vv {
210
- val v : Grammar [Unit ,Int ,Int ] = term(1 ) <|> v
216
+ val v : Grammar [Unit ,Int ,Int ] = term(1 ) <|> term( 2 ) <|> v
211
217
val v2 : Grammar [(Unit ,Unit ),(Int ,Int ),Int ] = (term(1 ) ++ term(2 )) <|> v2
212
218
}
213
- compile[Int ,Int ,List ](vv.v).show
219
+ println( compile[Int ,Int ,List ](vv.v).show)
214
220
}
215
221
}
216
222
0 commit comments