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,
properties: Chain[PropertyRaw],
propertiesAllowed: Boolean
): State[S, (ValueModel, Inline)] = {
((raw, properties.uncons) match {
case (
vr @ VarRaw(_, st @ StreamType(_)),
Some(IntoIndexRaw(idx, _), otherProperties)
) =>
unfold(vr).flatMap {
case (VarModel(nameVM, _, _), inl) =>
for {
/**
* Unfold `stream[idx]`
*/
private def unfoldStreamGate[S: Mangler: Exports: Arrows](
streamName: String,
streamType: StreamType,
idx: ValueRaw
): State[S, (VarModel, Inline)] = for {
/**
* Inline idx
*/
idxInlined <- unfold(idx)
(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`)
sizeInline = CallServiceModel(
"math",
@ -271,29 +273,59 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
args = List(idxVM, LiteralModel.number(1)),
result = sizeVar
).leaf
gateInlined <- StreamGateInliner(nameVM, st, sizeVar)
gateInlined <- StreamGateInliner(streamName, streamType, sizeVar)
(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 {
case vr: VarModel => removeProperties(vr)
case _ => (idxVM, Inline.empty).pure[State[S, *]]
}
(idxFlat, idxFlatInline) = idxFlattened
/**
* Construct stream[idx]
*/
gate = gateVM.withProperty(
IntoIndexModel
.fromValueModel(idxFlat, st.element)
.fromValueModel(idxFlat, streamType.element)
.getOrElse(
internalError(s"Unexpected: could not convert ($idxFlat) to IntoIndexModel")
)
)
propsInlined <- unfoldProperties(
Inline(
} yield gate -> Inline(
idxInline.predo
.append(sizeInline) ++
gateInline.predo ++
idxFlatInline.predo,
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,
propertiesAllowed
)