Skip to content

Commit

Permalink
refactor: cleaned up method naming
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mie6 committed Jan 5, 2025
1 parent f81087b commit 33d1e44
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ trait PlatformSpecific {
} yield {
src.close()
val internal = p.internal
new Context(internal.instrs, input, internal.numRegs, Some(file.getName)).run()
new Context(internal.instrs, input, internal.numRefs, Some(file.getName)).run()
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion parsley/shared/src/main/scala/parsley/Parsley.scala
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ final class Parsley[+A] private [parsley] (private [parsley] val internal: front
* @group run
*/
def parse[Err: ErrorBuilder](input: String): Result[Err, A] = {
try new Context(internal.instrs, input, internal.numRegs, None).run()
try new Context(internal.instrs, input, internal.numRefs, None).run()
catch {
// $COVERAGE-OFF$
case UserException(err) => throw err // scalastyle:ignore throw
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ private [deepembedding] trait StrictParsley[+A] {
* 1. any sharable, fail-only, handler instructions are then generated at the end of the instruction stream
* 1. jump-labels in the code are removed, and tail-call optimisation is applied
*
* @param numRegsUsedByParent the number of registers in use by the parent (should one exist), required by callee-save
* @param usedRefs the registers used by this parser (these may require allocation)
* @param minRef the number of references determined to currently exist according to the context (or -1 if this is root)
* @param usedRefs the references used by this parser (these may require allocation)
* @param recs a stream of pairs of rec nodes and the generators for their strict parsers
* (this is just because more `Cont` operations are performed later)
* @param state the code generator state
Expand All @@ -48,7 +48,8 @@ private [deepembedding] trait StrictParsley[+A] {
(implicit state: CodeGenState): Array[Instr] = {
implicit val instrs: InstrBuffer = newInstrBuffer
perform {
generateCalleeSave[M, Array[Instr]](minRef, this.codeGen(producesResults = true), usedRefs) |> {
allocateAndExpandRefs(minRef, usedRefs)
this.codeGen[M, Array[Instr]](producesResults = true) |> {
// When `minRef` is -1 this is top level, otherwise it is a flatMap
instrs += (if (minRef >= 0) instructions.Return else instructions.Halt)
val letRets = finaliseLets(bodyMap)
Expand Down Expand Up @@ -97,24 +98,16 @@ private [deepembedding] object StrictParsley {
/** Make a fresh instruction buffer */
private def newInstrBuffer: InstrBuffer = new ResizableArray()

/** If required, generates callee-save around a main body of instructions.
/** Allocates references, and, if required, generates an instruction to expand array size.
*
* This is needed when using `flatMap`, as it is unaware of the register
* context of its parent, other than the number used. The expectation is
* that such a parser will save all the registers used by its parents that
* it itself does not explicitly use and restore them when it is completed
* (if the parent has not allocated a register it may be assumed that
* it is local to the `flatMap`: this is a documented limitation of the
* system).
* context of its parents.
*
* @param numRegsUsedByParent the size of the current register array as dictated by the parent
* @param bodyGen a computation that generates instructions into the instruction buffer
* @param reqRegs the number of registered used by the parser in question
* @param allocatedRegs the slots used by the registered allocated local to the parser
* @param minRef the number of references in existance, according to the context
* @param usedRefs the referenced used in this parser that may need allocation
* @param instrs the instruction buffer
* @param state the code generation state, for label generation
*/
private def generateCalleeSave[M[_, +_], R](minRef: Int, bodyGen: =>M[R, Unit], usedRefs: Set[Ref[_]])(implicit instrs: InstrBuffer): M[R, Unit] = {
private def allocateAndExpandRefs(minRef: Int, usedRefs: Set[Ref[_]])(implicit instrs: InstrBuffer): Unit = {
var nextSlot = math.max(minRef, 0)
for (r <- usedRefs if !r.allocated) {
r.allocate(nextSlot)
Expand All @@ -129,7 +122,6 @@ private [deepembedding] object StrictParsley {
if (minRef >= 0 && (minRef < totalSlotsRequired)) {
instrs += new instructions.ExpandRefs(totalSlotsRequired)
}
bodyGen
}

/** Generates each of the shared, non-recursive, parsers that have been ''used'' by
Expand Down Expand Up @@ -243,9 +235,9 @@ private [deepembedding] trait MZero extends StrictParsley[Nothing]
/** This is the escapulated state required for code generation,
* which is threaded through the entire backend.
*
* @param numRegs the number of registers required by the parser being generated
* @param numRefs the number of references required by the parser being generated
*/
private [deepembedding] class CodeGenState(val numRegs: Int) {
private [deepembedding] class CodeGenState(val numRefs: Int) {
/** The next jump-label identifier. */
private var current = 0
/** The shared-parsers that have been referenced at some point in the generation so far. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private [parsley] abstract class LazyParsley[+A] private [deepembedding] {
// $COVERAGE-ON$

// The instructions used to execute this parser along with the number of registers it uses
final private [parsley] lazy val (instrs: Array[Instr], numRegs: Int) = computeInstrs
final private [parsley] lazy val (instrs: Array[Instr], numRefs: Int) = computeInstrs

/** This parser is the result of a `flatMap` operation, and as such may need to expand
* the refs set. If so, it needs to know what the minimum free slot is according to
Expand Down Expand Up @@ -115,11 +115,11 @@ private [parsley] abstract class LazyParsley[+A] private [deepembedding] {
val usedRefs: Set[Ref[_]] = letFinderState.usedRefs
implicit val letMap: LetMap = LetMap(letFinderState.lets, letFinderState.recs)
for { sp <- this.optimised } yield {
implicit val state: backend.CodeGenState = new backend.CodeGenState(letFinderState.numRegs)
implicit val state: backend.CodeGenState = new backend.CodeGenState(letFinderState.numRefs)
sp.generateInstructions(minRef, usedRefs, letMap.bodies)
}
}
}, letFinderState.numRegs)
}, letFinderState.numRefs)
}

// Pass 1
Expand Down Expand Up @@ -243,7 +243,7 @@ private [deepembedding] class LetFinderState {
/** Returns all the registers used by the parser */
private [frontend] def usedRefs: Set[Ref[_]] = _usedRefs.toSet
/** Returns the number of registers used by the parser */
private [frontend] def numRegs: Int = _usedRefs.size
private [frontend] def numRefs: Int = _usedRefs.size
}

/** Represents a map of let-bound lazy parsers to their strict equivalents. */
Expand Down

0 comments on commit 33d1e44

Please sign in to comment.