Refactor, add comments

This commit is contained in:
InversionSpaces 2023-10-06 16:02:09 +00:00
parent dcf6f04796
commit a1f61ba299
2 changed files with 77 additions and 45 deletions

View File

@ -248,22 +248,24 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
} }
} }
private def unfoldRawWithProperties[S: Mangler: Exports: Arrows]( /**
raw: ValueRaw, * Unfold `stream[idx]`
properties: Chain[PropertyRaw], */
propertiesAllowed: Boolean private def unfoldStreamGate[S: Mangler: Exports: Arrows](
): State[S, (ValueModel, Inline)] = { streamName: String,
((raw, properties.uncons) match { streamType: StreamType,
case ( idx: ValueRaw
vr @ VarRaw(_, st @ StreamType(_)), ): State[S, (VarModel, Inline)] = for {
Some(IntoIndexRaw(idx, _), otherProperties) /**
) => * Inline idx
unfold(vr).flatMap { */
case (VarModel(nameVM, _, _), inl) =>
for {
idxInlined <- unfold(idx) idxInlined <- unfold(idx)
(idxVM, idxInline) = idxInlined (idxVM, idxInline) = idxInlined
sizeName <- Mangler[S].findAndForbidName(s"${nameVM}_size") /**
* Inline size which is `idx + 1`
* TODO: Refactor to apply optimizations
*/
sizeName <- Mangler[S].findAndForbidName(s"${streamName}_size")
sizeVar = VarModel(sizeName, idxVM.`type`) sizeVar = VarModel(sizeName, idxVM.`type`)
sizeInline = CallServiceModel( sizeInline = CallServiceModel(
"math", "math",
@ -271,29 +273,59 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
args = List(idxVM, LiteralModel.number(1)), args = List(idxVM, LiteralModel.number(1)),
result = sizeVar result = sizeVar
).leaf ).leaf
gateInlined <- StreamGateInliner(nameVM, st, sizeVar) gateInlined <- StreamGateInliner(streamName, streamType, sizeVar)
(gateVM, gateInline) = gateInlined (gateVM, gateInline) = gateInlined
/**
* Remove properties from idx
* as we need to use it in index
* TODO: Do not generate it
* if it is not needed,
* e.g. in `join`
*/
idxFlattened <- idxVM match { idxFlattened <- idxVM match {
case vr: VarModel => removeProperties(vr) case vr: VarModel => removeProperties(vr)
case _ => (idxVM, Inline.empty).pure[State[S, *]] case _ => (idxVM, Inline.empty).pure[State[S, *]]
} }
(idxFlat, idxFlatInline) = idxFlattened (idxFlat, idxFlatInline) = idxFlattened
/**
* Construct stream[idx]
*/
gate = gateVM.withProperty( gate = gateVM.withProperty(
IntoIndexModel IntoIndexModel
.fromValueModel(idxFlat, st.element) .fromValueModel(idxFlat, streamType.element)
.getOrElse( .getOrElse(
internalError(s"Unexpected: could not convert ($idxFlat) to IntoIndexModel") internalError(s"Unexpected: could not convert ($idxFlat) to IntoIndexModel")
) )
) )
propsInlined <- unfoldProperties( } yield gate -> Inline(
Inline(
idxInline.predo idxInline.predo
.append(sizeInline) ++ .append(sizeInline) ++
gateInline.predo ++ gateInline.predo ++
idxFlatInline.predo, idxFlatInline.predo,
mergeMode = SeqMode mergeMode = SeqMode
), )
gate,
private def unfoldRawWithProperties[S: Mangler: Exports: Arrows](
raw: ValueRaw,
properties: Chain[PropertyRaw],
propertiesAllowed: Boolean
): State[S, (ValueModel, Inline)] = {
((raw, properties.uncons) match {
/**
* To inline
*/
case (
vr @ VarRaw(_, st @ StreamType(_)),
Some(IntoIndexRaw(idx, _), otherProperties)
) =>
unfold(vr).flatMap {
case (VarModel(nameVM, _, _), inl) =>
for {
gateInlined <- unfoldStreamGate(nameVM, st, idx)
(gateVM, gateInline) = gateInlined
propsInlined <- unfoldProperties(
gateInline,
gateVM,
otherProperties, otherProperties,
propertiesAllowed propertiesAllowed
) )