Skip to content

Commit 957eb8b

Browse files
committed
block compiler partially functional
1 parent 2ba7fab commit 957eb8b

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

src/main/scala/Polyparse.scala

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ trait ControlFlowContext[Result] {
4141

4242
def block(e : Expr[Unit]) : Label = block(_ => e)
4343
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]
4546

4647
def end(v : Expr[Result]) : Expr[Unit]
4748

@@ -61,9 +62,9 @@ case class BlockCompiler[ET:Type,Result](seqCtx : SequenceContext[ET], flowCtx :
6162
def handleRec[AT:Type,T:Type](g : Grammar[AT,T,ET], arg : Expr[AT], yes : Expr[T]=>Expr[Unit], no : Expr[Unit]) : Expr[Unit] = {
6263
if recursions.contains(g) then {
6364
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}))
6566
})
66-
val retLabel = flowCtx.block(yes(flowCtx.reg[T]))
67+
val retLabel = flowCtx.block(yes(flowCtx.retReg[T]))
6768
flowCtx.call(recLabel, retLabel, arg)
6869
} else {
6970
computeValue(g, arg, yes, no)
@@ -84,15 +85,17 @@ case class BlockCompiler[ET:Type,Result](seqCtx : SequenceContext[ET], flowCtx :
8485
}
8586
}
8687
case Disjunction(l,r) => {
88+
val lv = computeValue(l, arg, yes, no)
89+
val rv = computeValue(r, arg, yes, no)
8790
'{ // this is eager disjunction, TODO "correct" backtracking disjunction option
8891
if ${speculateReachable(l, arg)}
89-
then ${computeValue(l, arg, yes, no)}
90-
else ${computeValue(r, arg, yes, no)}
92+
then ${lv}
93+
else ${rv}
9194
}
9295
}
9396
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)
9699
}
97100
case _ => ???
98101
}
@@ -143,7 +146,8 @@ object PolyParse {
143146
val stackPC : ArrayStack[Int] = new ArrayStack
144147
val stack : ArrayStack[Any] = new ArrayStack
145148
var pc : Int = 0
146-
var arg : Any = null
149+
var callReg_ : Any = null
150+
var retReg_ : Any = null
147151
var loop = true
148152
while(loop) {
149153
${
@@ -153,22 +157,24 @@ object PolyParse {
153157
type Label = Int
154158
def block(f : Int=>Expr[Unit]) : Int = {
155159
val label = blocks.length
156-
blocks.+=((label, f(label)))
160+
blocks += null
161+
blocks(label) = (label, f(label))
157162
label
158163
}
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]}
160166
def end(v : Expr[Result]) : Expr[Unit] = '{
161-
arg = $v
167+
retReg_ = $v
162168
loop = false
163169
}
164170
def call(label : Int, ret : Int, a : Expr[Any]) : Expr[Unit] = '{
165171
pc = ${label.toExpr}
166-
arg = $a
172+
callReg_ = $a
167173
stackPC.push(${ret.toExpr})
168174
}
169175
def ret(v : Expr[Any]) : Expr[Unit] = '{
170176
pc = stackPC.pop
171-
arg = $v
177+
retReg_ = $v
172178
}
173179
def push(v : Expr[Any]) : Expr[Unit] = '{
174180
stack.push($v)
@@ -180,7 +186,7 @@ object PolyParse {
180186
})
181187
}
182188
}
183-
arg.asInstanceOf[Result]
189+
retReg_.asInstanceOf[Result]
184190
}
185191

186192
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 {
204210
})
205211
}
206212

207-
def runTest = {
213+
def main(argv : Array[String]) : Unit = {
208214
import scala.quoted.Toolbox.Default._
209215
object vv {
210-
val v : Grammar[Unit,Int,Int] = term(1) <|> v
216+
val v : Grammar[Unit,Int,Int] = term(1) <|> term(2) <|> v
211217
val v2 : Grammar[(Unit,Unit),(Int,Int),Int] = (term(1) ++ term(2)) <|> v2
212218
}
213-
compile[Int,Int,List](vv.v).show
219+
println(compile[Int,Int,List](vv.v).show)
214220
}
215221
}
216222

0 commit comments

Comments
 (0)