Skip to content

Commit

Permalink
Implement inlining
Browse files Browse the repository at this point in the history
  • Loading branch information
InversionSpaces committed Jul 26, 2023
1 parent e199c56 commit 33cdcca
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import aqua.model.inline.Inline
import aqua.model.inline.RawValueInliner.{unfold, valueToModel}
import aqua.types.{ArrowType, ScalarType}
import aqua.raw.value.ApplyBoolOpRaw
import aqua.raw.value.ApplyBoolOpRaw.BoolOpRaw.*

import cats.data.Chain
import cats.syntax.traverse.*
Expand All @@ -23,8 +24,57 @@ object ApplyBoolOpRawInliner extends RawInliner[ApplyBoolOpRaw] {
override def apply[S: Mangler: Exports: Arrows](
raw: ApplyBoolOpRaw,
propertiesAllowed: Boolean
): State[S, (ValueModel, Inline)] =
(unfold(raw.left), unfold(raw.right)).flatMapN { case ((lm, linline), (rm, rinline)) =>
???
): State[S, (ValueModel, Inline)] = for {
left <- unfold(raw.left)
(lmodel, linline) = left
right <- unfold(raw.right)
(rmodel, rinline) = right

(name, compareWith) = raw.op match {
case And => ("and", true)
case Or => ("or", false)
}
resName <- Mangler[S].findAndForbidName(name)

/*
* (seq
* <left-inline>
* (xor
* (match <left-res> <compare-with>
* (seq
* <right-inline>
* (ap <right-res> <res-name>)
* )
* )
* (ap <left-res> <res-name>)
* )
* )
*
* TODO: Handle errors in <right-inline>
*/
predo = SeqModel.wrap(
linline.predo :+ XorModel.wrap(
MatchMismatchModel(
lmodel,
LiteralModel.bool(compareWith),
shouldMatch = true
).wrap(
SeqModel.wrap(
rinline.predo :+ FlattenModel(
rmodel,
resName
).leaf
)
),
FlattenModel(
lmodel,
resName
).leaf
)
)

} yield (
VarModel(resName, ScalarType.bool),
Inline(Chain.one(predo))
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import cats.syntax.monoid.*
import cats.syntax.functor.*
import cats.syntax.flatMap.*
import cats.syntax.apply.*
import cats.syntax.foldable.*

object ApplyIntoCopyRawInliner extends Logging {

Expand Down Expand Up @@ -53,14 +54,14 @@ object ApplyIntoCopyRawInliner extends Logging {
name <- Mangler[S].findAndForbidName(value.name + "_obj_copy")
foldedFields <- intoCopy.fields.nonEmptyTraverse(unfold(_))
varModel = VarModel(name, value.baseType)
valsInline = foldedFields.toSortedMap.values.map(_._2).fold(Inline.empty)(_ |+| _).desugar
valsInline = foldedFields.toList.foldMap { case (_, inline) => inline }.desugar
fields = foldedFields.map(_._1)
objCopy <- copyObj(value, fields, varModel)
} yield {
(
varModel,
Inline(
Chain.one(SeqModel.wrap((valsInline.predo :+ objCopy).toList: _*)),
Chain.one(SeqModel.wrap(valsInline.predo :+ objCopy)),
SeqMode
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ object CallArrowRawInliner extends RawInliner[CallArrowRaw] with Logging {
Chain(
SeqModel.wrap(
sd._2.toList ++
cd._2.toList :+ CallServiceModel(sd._1, value.name, cd._1).leaf: _*
cd._2.toList :+ CallServiceModel(sd._1, value.name, cd._1).leaf
)
)
)
Expand Down
2 changes: 2 additions & 0 deletions model/src/main/scala/aqua/model/ValueModel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ object LiteralModel {
def quote(str: String): LiteralModel = LiteralModel(s"\"$str\"", LiteralType.string)

def number(n: Int): LiteralModel = LiteralModel(n.toString, LiteralType.forInt(n))

def bool(b: Boolean): LiteralModel = LiteralModel(b.toString.toLowerCase, LiteralType.bool)
}

sealed trait PropertyModel {
Expand Down

0 comments on commit 33cdcca

Please sign in to comment.