From 741132ce955e5f1d7e545119ed5d709097e9ad01 Mon Sep 17 00:00:00 2001 From: Jamie Willis Date: Mon, 29 Apr 2024 13:35:11 +0100 Subject: [PATCH] removed redundant handler in chainr1 implementation --- .../backend/IterativeEmbedding.scala | 10 +++------- .../machine/instructions/CoreInstrs.scala | 4 ++-- .../instructions/IterativeInstrs.scala | 20 +++++++------------ 3 files changed, 12 insertions(+), 22 deletions(-) diff --git a/parsley/shared/src/main/scala/parsley/internal/deepembedding/backend/IterativeEmbedding.scala b/parsley/shared/src/main/scala/parsley/internal/deepembedding/backend/IterativeEmbedding.scala index 6103f0c47..db3485621 100644 --- a/parsley/shared/src/main/scala/parsley/internal/deepembedding/backend/IterativeEmbedding.scala +++ b/parsley/shared/src/main/scala/parsley/internal/deepembedding/backend/IterativeEmbedding.scala @@ -115,19 +115,15 @@ private [deepembedding] final class Chainr[A, B](p: StrictParsley[A], op: Strict override def codeGen[M[_, +_]: ContOps, R](producesResults: Boolean)(implicit instrs: InstrBuffer, state: CodeGenState): M[R, Unit]= { if (producesResults) { val body = state.freshLabel() - // handler1 is where the check offset is kept - val handler1 = state.getLabel(instructions.Refail) - val handler2 = state.freshLabel() + val handler = state.freshLabel() instrs += new instructions.Push(identity[Any] _) - instrs += new instructions.PushHandler(handler1) instrs += new instructions.Label(body) suspend(p.codeGen[M, R](producesResults=true)) >> { - instrs += new instructions.PushHandler(handler2) + instrs += new instructions.PushHandler(handler) suspend(op.codeGen[M, R](producesResults=true)) |> { instrs += new instructions.ChainrJump(body) - instrs += new instructions.Label(handler2) + instrs += new instructions.Label(handler) instrs += instructions.ChainrOpHandler(wrap) - if (!producesResults) instrs += instructions.Pop } } } diff --git a/parsley/shared/src/main/scala/parsley/internal/machine/instructions/CoreInstrs.scala b/parsley/shared/src/main/scala/parsley/internal/machine/instructions/CoreInstrs.scala index 028f2f3f8..192c68afa 100644 --- a/parsley/shared/src/main/scala/parsley/internal/machine/instructions/CoreInstrs.scala +++ b/parsley/shared/src/main/scala/parsley/internal/machine/instructions/CoreInstrs.scala @@ -279,7 +279,7 @@ private [internal] final class RestoreAndPushHandler(var label: Int) extends Ins // $COVERAGE-ON$ } -private [internal] object Refail extends Instr { +/*private [internal] object Refail extends Instr { override def apply(ctx: Context): Unit = { ensureHandlerInstruction(ctx) ctx.handlers = ctx.handlers.tail @@ -289,4 +289,4 @@ private [internal] object Refail extends Instr { // $COVERAGE-OFF$ override def toString: String = "Refail" // $COVERAGE-ON$ -} +}*/ diff --git a/parsley/shared/src/main/scala/parsley/internal/machine/instructions/IterativeInstrs.scala b/parsley/shared/src/main/scala/parsley/internal/machine/instructions/IterativeInstrs.scala index 0ff124727..412ef10c2 100644 --- a/parsley/shared/src/main/scala/parsley/internal/machine/instructions/IterativeInstrs.scala +++ b/parsley/shared/src/main/scala/parsley/internal/machine/instructions/IterativeInstrs.scala @@ -111,14 +111,6 @@ private [internal] final class Chainl(var label: Int) extends InstrWithLabel { // $COVERAGE-ON$ } -private [instructions] object DualHandler { - def popSecondHandlerAndJump(ctx: Context, label: Int): Unit = { - ctx.handlers = ctx.handlers.tail - ctx.updateCheckOffset() - ctx.pc = label - } -} - private [internal] final class ChainrJump(var label: Int) extends InstrWithLabel { override def apply(ctx: Context): Unit = { ensureRegularInstruction(ctx) @@ -127,7 +119,8 @@ private [internal] final class ChainrJump(var label: Int) extends InstrWithLabel val acc = ctx.stack.peek[Any => Any] // We perform the acc after the tos function; the tos function is "closer" to the final p ctx.stack.exchange((y: Any) => acc(f(x, y))) - DualHandler.popSecondHandlerAndJump(ctx, label) + ctx.handlers = ctx.handlers.tail + ctx.pc = label } // $COVERAGE-OFF$ @@ -138,9 +131,7 @@ private [internal] final class ChainrJump(var label: Int) extends InstrWithLabel private [internal] final class ChainrOpHandler(wrap: Any => Any) extends Instr { override def apply(ctx: Context): Unit = { ensureHandlerInstruction(ctx) - val check = ctx.handlers.check - ctx.handlers = ctx.handlers.tail - ctx.catchNoConsumed(check) { + ctx.catchNoConsumed(ctx.handlers.check) { ctx.handlers = ctx.handlers.tail ctx.addErrorToHintsAndPop() val y = ctx.stack.upop() @@ -163,7 +154,10 @@ private [internal] final class SepEndBy1Jump(var label: Int) extends InstrWithLa ctx.stack.pop_() // the bool ctx.stack.peek[mutable.Builder[Any, Any]] += x ctx.stack.upush(true) - DualHandler.popSecondHandlerAndJump(ctx, label) + // pop second handler and jump + ctx.handlers = ctx.handlers.tail + ctx.updateCheckOffset() + ctx.pc = label } // $COVERAGE-OFF$