Skip to content

Commit f9bf2b2

Browse files
committed
refactor: switched ChainPre to use a custom AndThen, instead of dedicated stack
1 parent 0af9497 commit f9bf2b2

2 files changed

Lines changed: 8 additions & 21 deletions

File tree

parsley/shared/src/main/scala/parsley/internal/deepembedding/backend/IterativeEmbedding.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,14 @@ private [deepembedding] final class ChainPre[A](p: StrictParsley[A], op: StrictP
7373
override def codeGen[M[_, +_]: ContOps, R](producesResults: Boolean)(implicit instrs: InstrBuffer, state: CodeGenState): M[R, Unit] = {
7474
val body = state.freshLabel()
7575
val handler = state.freshLabel()
76-
if (producesResults) instrs += new instructions.Push(instructions.PreOps.empty)
76+
if (producesResults) instrs += new instructions.Push(identity[A] _)
7777
instrs += new instructions.PushHandler(handler)
7878
instrs += new instructions.Label(body)
7979
suspend(op.codeGen[M, R](producesResults)) >> {
8080
instrs += new instructions.Label(handler)
8181
instrs += (if (producesResults) new instructions.ChainPre(body) else new instructions.SkipMany(body))
8282
suspend(p.codeGen[M, R](producesResults)) |> {
83-
if (producesResults) instrs += instructions.ChainPreReduce
83+
if (producesResults) instrs += instructions.Apply
8484
}
8585
}
8686
}

parsley/shared/src/main/scala/parsley/internal/machine/instructions/IterativeInstrs.scala

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,18 @@ private [internal] final class ChainPost(var label: Int) extends InstrWithLabel
7171
// $COVERAGE-ON$
7272
}
7373

74-
private [internal] final class PreOps(val op: Any => Any, val rest: PreOps)
75-
private [internal] object PreOps {
76-
val empty: PreOps = null
77-
private [instructions] def reduce(ops: PreOps, x: Any): Any = {
78-
if (ops eq empty) x
79-
else reduce(ops.rest, ops.op(x))
80-
}
74+
private final class AndThen[-A, B, +C](f: A => B, g: B => C) extends (A => C) {
75+
final def apply(x: A): C = g match {
76+
case g: AndThen[_, _, _] => g(f(x))
77+
case g => g(f(x))
78+
}
8179
}
8280

8381
private [internal] final class ChainPre(var label: Int) extends InstrWithLabel {
8482
override def apply(ctx: Context): Unit = {
8583
if (ctx.good) {
8684
val f = ctx.stack.pop[Any => Any]()
87-
ctx.stack.exchange(new PreOps(f, ctx.stack.peek[PreOps]))
85+
ctx.stack.exchange(new AndThen(f, ctx.stack.peek[Any => Any]))
8886
ctx.updateCheckOffset()
8987
ctx.pc = label
9088
}
@@ -99,17 +97,6 @@ private [internal] final class ChainPre(var label: Int) extends InstrWithLabel {
9997
override def toString: String = s"ChainPre($label)"
10098
// $COVERAGE-ON$
10199
}
102-
private [internal] object ChainPreReduce extends Instr {
103-
override def apply(ctx: Context): Unit = {
104-
ensureRegularInstruction(ctx)
105-
val x = ctx.stack.upop()
106-
val f = ctx.stack.peek[PreOps]
107-
ctx.exchangeAndContinue(PreOps.reduce(f, x))
108-
}
109-
// $COVERAGE-OFF$
110-
override def toString: String = s"ChainPreReduce"
111-
// $COVERAGE-ON$
112-
}
113100

114101
private [internal] final class Chainl(var label: Int) extends InstrWithLabel {
115102
override def apply(ctx: Context): Unit = {

0 commit comments

Comments
 (0)