Skip to content

Commit

Permalink
Pass type in RestrictionTag(Model)
Browse files Browse the repository at this point in the history
  • Loading branch information
InversionSpaces committed Jun 20, 2023
1 parent de22a35 commit 54c4829
Show file tree
Hide file tree
Showing 18 changed files with 105 additions and 77 deletions.
10 changes: 6 additions & 4 deletions backend/air/src/main/scala/aqua/backend/air/Air.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ object DataView {

case class Variable(name: String) extends DataView

case class Stream(name: String) extends DataView

case class VarLens(name: String, lens: String, isField: Boolean = true) extends DataView {
def append(sublens: String): VarLens = copy(lens = lens + sublens)
}
Expand All @@ -57,7 +55,6 @@ object DataView {
case InitPeerId "%init_peer_id%"
case LastError "%last_error%"
case Variable(name) name
case Stream(name) name
case VarLens(name, lens, isField)
if (isField) name + ".$" + lens
else name + lens
Expand Down Expand Up @@ -90,7 +87,12 @@ object Air {

case class Next(label: String) extends Air(Keyword.Next)

case class Fold(iterable: DataView, label: String, instruction: Air, lastNextInstruction: Option[Air]) extends Air(Keyword.Fold)
case class Fold(
iterable: DataView,
label: String,
instruction: Air,
lastNextInstruction: Option[Air]
) extends Air(Keyword.Fold)

case class Match(left: DataView, right: DataView, instruction: Air) extends Air(Keyword.Match)

Expand Down
30 changes: 18 additions & 12 deletions backend/air/src/main/scala/aqua/backend/air/AirGen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package aqua.backend.air
import aqua.model.*
import aqua.raw.ops.Call
import aqua.res.*
import aqua.types.{ArrayType, CanonStreamType, StreamType}
import aqua.types.{ArrayType, CanonStreamType, StreamType, Type}
import cats.Eval
import cats.data.Chain
import cats.free.Cofree
Expand All @@ -26,14 +26,17 @@ object AirGen extends Logging {
s".[$idx]${propertyToString(tail)}"
}

def varNameToString(name: String, `type`: Type): String =
(`type` match {
case _: StreamType => "$" + name
case _: CanonStreamType => "#" + name
case _ => name
}).replace('.', '_')

def valueToData(vm: ValueModel): DataView = vm match {
case LiteralModel(value, _) => DataView.StringScalar(value)
case VarModel(name, t, property) =>
val n = (t match {
case _: StreamType => "$" + name
case _: CanonStreamType => "#" + name
case _ => name
}).replace('.', '_')
val n = varNameToString(name, t)
if (property.isEmpty) DataView.Variable(n)
else {
val functors = property.find {
Expand Down Expand Up @@ -97,8 +100,8 @@ object AirGen extends Logging {
case ForModel.NeverMode => NeverGen
}
Eval later ForGen(valueToData(iterable), item, opsToSingle(ops), m)
case RestrictionRes(item, isStream) =>
Eval later NewGen(item, isStream, opsToSingle(ops))
case RestrictionRes(item, itemType) =>
Eval later NewGen(varNameToString(item, itemType), opsToSingle(ops))
case CallServiceRes(serviceId, funcName, CallRes(args, exportTo), peerId) =>
Eval.later(
ServiceCallGen(
Expand Down Expand Up @@ -179,14 +182,17 @@ case class MatchMismatchGen(
else Air.Mismatch(left, right, body.generate)
}

case class ForGen(iterable: DataView, item: String, body: AirGen, mode: Option[AirGen]) extends AirGen {
case class ForGen(iterable: DataView, item: String, body: AirGen, mode: Option[AirGen])
extends AirGen {
override def generate: Air = Air.Fold(iterable, item, body.generate, mode.map(_.generate))
}

case class NewGen(item: String, isStream: Boolean, body: AirGen) extends AirGen {
case class NewGen(name: String, body: AirGen) extends AirGen {

override def generate: Air =
Air.New(if (isStream) DataView.Stream("$" + item) else DataView.Variable(item), body.generate)
override def generate: Air = Air.New(
DataView.Variable(name),
body.generate
)
}

case class NextGen(item: String) extends AirGen {
Expand Down
11 changes: 6 additions & 5 deletions compiler/src/test/scala/aqua/compiler/AquaCompilerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class AquaCompilerSpec extends AnyFlatSpec with Matchers {
SeqRes.wrap(
getDataSrv("-relay-", ScalarType.string),
getDataSrv(peers.name, peers.`type`),
RestrictionRes("results", true).wrap(
RestrictionRes(results.name, resultsType).wrap(
SeqRes.wrap(
ParRes.wrap(
FoldRes(peer.name, peers, Some(ForModel.NeverMode)).wrap(
Expand Down Expand Up @@ -273,23 +273,24 @@ class AquaCompilerSpec extends AnyFlatSpec with Matchers {
val Some(funcWrap) = aquaRes.funcs.find(_.funcName == "wrap")
val Some(barfoo) = aquaRes.funcs.find(_.funcName == "barfoo")

val resVM = VarModel("res", StreamType(ScalarType.string))
val resStreamType = StreamType(ScalarType.string)
val resVM = VarModel("res", resStreamType)
val resCanonVM = VarModel("-res-fix-0", CanonStreamType(ScalarType.string))
val resFlatVM = VarModel("-res-flat-0", ArrayType(ScalarType.string))

barfoo.body.equalsOrShowDiff(
SeqRes.wrap(
RestrictionRes("res", true).wrap(
RestrictionRes(resVM.name, resStreamType).wrap(
SeqRes.wrap(
// res <- foo()
ApRes(
LiteralModel.fromRaw(LiteralRaw.quote("I am MyFooBar foo")),
CallModel.Export("res", StreamType(ScalarType.string))
CallModel.Export(resVM.name, resVM.`type`)
).leaf,
// res <- bar()
ApRes(
LiteralModel.fromRaw(LiteralRaw.quote(" I am MyFooBar bar")),
CallModel.Export("res", StreamType(ScalarType.string))
CallModel.Export(resVM.name, resVM.`type`)
).leaf,
// canonicalization
CanonRes(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,8 @@ object TagInliner extends Logging {
} -> None
}

case RestrictionTag(name, isStream) =>
pure(RestrictionModel(name, isStream))
case RestrictionTag(name, typ) =>
pure(RestrictionModel(name, typ))

case _: SeqGroupTag => pure(SeqModel)
case ParTag.Detach => pure(DetachModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ object ApplyGateRawInliner extends RawInliner[ApplyGateRaw] with Logging {

val incrVar = VarModel(idxIncrName, ScalarType.u32)

RestrictionModel(varSTest.name, true).wrap(
RestrictionModel(varSTest.name, streamType).wrap(
increment(idxModel, incrVar),
ForModel(iter.name, VarModel(streamName, streamType), Some(ForModel.NeverMode)).wrap(
PushToStreamModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package aqua.model.inline.raw
import aqua.model.{
CallModel,
CallServiceModel,
CanonicalizeModel,
FlattenModel,
ForModel,
FunctorModel,
Expand All @@ -15,7 +14,6 @@ import aqua.model.{
OpModel,
PropertyModel,
PushToStreamModel,
RestrictionModel,
SeqModel,
ValueModel,
VarModel,
Expand Down Expand Up @@ -153,7 +151,7 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
properties.map {
case iir @ IntoIndexRaw(vr, t) =>
unfold(vr, propertiesAllowed = false).flatMap {
case (vm@VarModel(_, _, _), inline) if vm.properties.nonEmpty =>
case (vm @ VarModel(_, _, _), inline) if vm.properties.nonEmpty =>
removeProperties(vm).map { case (vf, inlf) =>
PropertyRawWithModel(iir, Option(IntoIndexModel(vf.name, t))) -> Inline(
inline.flattenValues ++ inlf.flattenValues,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,24 @@ object CollectionRawInliner extends RawInliner[CollectionRaw] {
raw: CollectionRaw,
propertiesAllowed: Boolean
): State[S, (ValueModel, Inline)] = unfoldCollection(raw)

def unfoldCollection[S: Mangler: Exports: Arrows](
raw: CollectionRaw,
assignToName: Option[String] = None
): State[S, (ValueModel, Inline)] =
for {
streamName <-
raw.boxType match {
case _: StreamType => assignToName.map(s => State.pure(s)).getOrElse(Mangler[S].findAndForbidName("stream-inline"))
case _: CanonStreamType => Mangler[S].findAndForbidName("canon_stream-inline")
case _: ArrayType => Mangler[S].findAndForbidName("array-inline")
case _: OptionType => Mangler[S].findAndForbidName("option-inline")
}
streamName <- raw.boxType match {
case _: StreamType =>
assignToName
.map(s => State.pure(s))
.getOrElse(Mangler[S].findAndForbidName("stream-inline"))
case _: CanonStreamType => Mangler[S].findAndForbidName("canon_stream-inline")
case _: ArrayType => Mangler[S].findAndForbidName("array-inline")
case _: OptionType => Mangler[S].findAndForbidName("option-inline")
}

stream = VarModel(streamName, StreamType(raw.elementType))
streamType = StreamType(raw.elementType)
stream = VarModel(streamName, streamType)
streamExp = CallModel.Export(stream.name, stream.`type`)

valsWithInlines <- raw.values
Expand All @@ -48,13 +51,13 @@ object CollectionRawInliner extends RawInliner[CollectionRaw] {

// push values to the stream, that is gathering the collection
vals = valsWithInlines.map { case (v, _) =>
PushToStreamModel(v, streamExp).leaf
}
PushToStreamModel(v, streamExp).leaf
}

// all inlines will be added before pushing values to the stream
inlines = valsWithInlines.flatMap { case (_, t) =>
Chain.fromOption(t)
}
Chain.fromOption(t)
}

canonName <-
if (raw.boxType.isStream) State.pure(streamName)
Expand All @@ -67,19 +70,19 @@ object CollectionRawInliner extends RawInliner[CollectionRaw] {
} yield VarModel(canonName, canon.`type`) -> Inline.tree(
raw.boxType match {
case ArrayType(_) =>
RestrictionModel(streamName, isStream = true).wrap(
SeqModel.wrap((inlines ++ vals :+ CanonicalizeModel(stream, canon).leaf).toList: _*)
RestrictionModel(streamName, streamType).wrap(
SeqModel.wrap(inlines ++ vals :+ CanonicalizeModel(stream, canon).leaf)
)
case OptionType(_) =>
RestrictionModel(streamName, isStream = true).wrap(
RestrictionModel(streamName, streamType).wrap(
SeqModel.wrap(
SeqModel.wrap(inlines.toList:_*),
XorModel.wrap((vals :+ NullModel.leaf).toList: _*),
CanonicalizeModel(stream, canon).leaf
SeqModel.wrap(inlines),
XorModel.wrap(vals :+ NullModel.leaf),
CanonicalizeModel(stream, canon).leaf
)
)
case _ =>
SeqModel.wrap((inlines ++ vals).toList: _*)
case _ =>
SeqModel.wrap(inlines ++ vals)
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class ArrowInlinerSpec extends AnyFlatSpec with Matchers {
.callArrow[InliningState](
FuncArrow(
"stream-callback",
RestrictionTag(streamVar.name, true).wrap(
RestrictionTag(streamVar.name, streamType).wrap(
SeqTag.wrap(
DeclareStreamTag(streamVar).leaf,
CallArrowRawTag.func("cb", Call(streamVar :: Nil, Nil)).leaf
Expand All @@ -115,7 +115,7 @@ class ArrowInlinerSpec extends AnyFlatSpec with Matchers {
._2

model.equalsOrShowDiff(
RestrictionModel(streamVar.name, true).wrap(
RestrictionModel(streamVar.name, streamType).wrap(
MetaModel
.CallArrowModel("cb")
.wrap(
Expand Down Expand Up @@ -191,7 +191,7 @@ class ArrowInlinerSpec extends AnyFlatSpec with Matchers {
.callArrow[InliningState](
FuncArrow(
"stream-callback",
RestrictionTag(streamVar.name, true).wrap(
RestrictionTag(streamVar.name, streamType).wrap(
SeqTag.wrap(
DeclareStreamTag(streamVar).leaf,
CallArrowRawTag.func("cb", Call(streamVarLambda :: Nil, Nil)).leaf
Expand All @@ -218,7 +218,7 @@ class ArrowInlinerSpec extends AnyFlatSpec with Matchers {
._2

model.equalsOrShowDiff(
RestrictionModel(streamVar.name, true).wrap(
RestrictionModel(streamVar.name, streamType).wrap(
CallServiceModel(
LiteralModel.quote("test-service"),
"some-call",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CollectionRawInlinerSpec extends AnyFlatSpec with Matchers {

tree.get.equalsOrShowDiff(
// create a stream
RestrictionModel("option-inline", true).wrap(
RestrictionModel("option-inline", StreamType(nestedType)).wrap(
SeqModel.wrap(
// create an object
CallServiceModel(
Expand Down
4 changes: 2 additions & 2 deletions model/raw/src/main/scala/aqua/raw/ops/RawTag.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import aqua.raw.arrow.FuncRaw
import aqua.raw.ops.RawTag.Tree
import aqua.raw.value.{CallArrowRaw, ValueRaw, VarRaw}
import aqua.tree.{TreeNode, TreeNodeCompanion}
import aqua.types.{ArrowType, ProductType}
import aqua.types.{ArrowType, DataType, ProductType}
import cats.{Eval, Show}
import cats.data.{Chain, NonEmptyList}
import cats.free.Cofree
Expand Down Expand Up @@ -89,7 +89,7 @@ case class NextTag(item: String) extends RawTag {
copy(item = map.getOrElse(item, item))
}

case class RestrictionTag(name: String, isStream: Boolean) extends SeqGroupTag {
case class RestrictionTag(name: String, `type`: DataType) extends SeqGroupTag {

override def restrictsVarNames: Set[String] = Set(name)

Expand Down
11 changes: 6 additions & 5 deletions model/res/src/main/scala/aqua/res/MakeRes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ object MakeRes {

def hop(onPeer: ValueModel): ResolvedOp.Tree = {
val streamName = "hop-stream-drop"
val canonName = "hop-canon-drop"
val elementType = ScalarType.u8
val streamType = StreamType(elementType)
val canonName = "hop-canon-drop"

RestrictionRes(streamName, isStream = true).wrap(
RestrictionRes(canonName, isStream = false).wrap(
RestrictionRes(streamName, streamType).wrap(
RestrictionRes(canonName, streamType).wrap(
CanonRes(
operand = VarModel(streamName, StreamType(elementType)),
operand = VarModel(streamName, streamType),
peerId = onPeer,
exportTo = CallModel.Export(canonName, CanonStreamType(elementType))
).leaf
Expand All @@ -36,7 +37,7 @@ object MakeRes {
case MatchMismatchModel(a, b, s) =>
MatchMismatchRes(a, b, s).leaf
case ForModel(item, iter, mode) if !isNillLiteral(iter) => FoldRes(item, iter, mode).leaf
case RestrictionModel(item, isStream) => RestrictionRes(item, isStream).leaf
case RestrictionModel(item, itemType) => RestrictionRes(item, itemType).leaf
case DetachModel => ParRes.leaf
case ParModel => ParRes.leaf
case XorModel => XorRes.leaf
Expand Down
11 changes: 7 additions & 4 deletions model/res/src/main/scala/aqua/res/ResolvedOp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package aqua.res
import aqua.model.{CallModel, ForModel, ValueModel, VarModel}
import aqua.raw.ops.Call
import aqua.tree.{TreeNode, TreeNodeCompanion}
import aqua.types.DataType
import cats.data.Chain
import cats.free.Cofree
import cats.Show
Expand Down Expand Up @@ -31,12 +32,13 @@ case class MatchMismatchRes(left: ValueModel, right: ValueModel, shouldMatch: Bo
override def toString: String = s"(${if (shouldMatch) "match" else "mismatch"} $left $right)"
}

case class FoldRes(item: String, iterable: ValueModel, mode: Option[ForModel.Mode] = None) extends ResolvedOp {
case class FoldRes(item: String, iterable: ValueModel, mode: Option[ForModel.Mode] = None)
extends ResolvedOp {
override def toString: String = s"(fold $iterable $item ${mode.map(_.toString).getOrElse("")}"
}

case class RestrictionRes(item: String, isStream: Boolean) extends ResolvedOp {
override def toString: String = s"(new ${if (isStream) "$" else ""}$item "
case class RestrictionRes(item: String, `type`: DataType) extends ResolvedOp {
override def toString: String = s"(new ${`type`.airPrefix}$item "
}

case class CallServiceRes(
Expand All @@ -52,7 +54,8 @@ case class ApRes(operand: ValueModel, exportTo: CallModel.Export) extends Resolv
override def toString: String = s"(ap $operand $exportTo)"
}

case class CanonRes(operand: ValueModel, peerId: ValueModel, exportTo: CallModel.Export) extends ResolvedOp {
case class CanonRes(operand: ValueModel, peerId: ValueModel, exportTo: CallModel.Export)
extends ResolvedOp {
override def toString: String = s"(canon $peerId $operand $exportTo)"
}

Expand Down
Loading

0 comments on commit 54c4829

Please sign in to comment.