Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
DieMyst committed Jul 14, 2023
1 parent 56427d1 commit 264d580
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 44 deletions.
14 changes: 8 additions & 6 deletions model/inline/src/main/scala/aqua/model/inline/ArrowInliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,17 @@ object ArrowInliner extends Logging {
callableFuncBody
)
(ops, rets) = opsAndRets

exports <- Exports[S].exports
arrows <- Arrows[S].arrows
returnedFromScopes = rets.collect { case VarModel(name, st @ AbilityType(_, _), _) =>
getAllVarsFromScope(name, None, st, exports, arrows)
// gather all arrows and variables from abilities
returnedFromAbilities = rets.collect { case VarModel(name, st @ AbilityType(_, _), _) =>
getVarsAndArrowsFromAbilities(name, None, st, exports, arrows)
}.fold((Map.empty, Map.empty)) { case (acc, (vars, arrs)) =>
(acc._1 ++ vars, acc._2 ++ arrs)
}
} yield {
val (vals, arrows) = returnedFromScopes
val (vals, arrows) = returnedFromAbilities
(SeqModel.wrap(ops.reverse: _*), rets.reverse, vals, arrows)
}
)
Expand Down Expand Up @@ -214,12 +216,12 @@ object ArrowInliner extends Logging {
allNewNames = newFieldsName.add((name, newName)).toSortedMap
} yield {
val (allVars, allArrows) =
getAllVarsFromScope(vm.name, Option(newName), t, oldExports, oldArrows)
getVarsAndArrowsFromAbilities(vm.name, Option(newName), t, oldExports, oldArrows)
(allNewNames, allVars, allArrows)
}
}

private def getAllVarsFromScope(
private def getVarsAndArrowsFromAbilities(
topOldName: String,
topNewName: Option[String],
st: AbilityType,
Expand All @@ -233,7 +235,7 @@ object ArrowInliner extends Logging {
val currentNewName = topNewName.map(_ + s".$fName")
value match {
case st @ AbilityType(_, _) =>
getAllVarsFromScope(
getVarsAndArrowsFromAbilities(
currentOldName,
currentNewName,
st,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
}
}

private def unfoldScopeProperty[S: Mangler: Exports: Arrows](
private def unfoldAbilityProperty[S: Mangler: Exports: Arrows](
varModel: VarModel,
scopeType: AbilityType,
p: PropertyRaw
Expand Down Expand Up @@ -250,7 +250,7 @@ object ApplyPropertiesRawInliner extends RawInliner[ApplyPropertyRaw] with Loggi
) { case (state, property) =>
state.flatMap {
case (vm @ VarModel(name, st @ AbilityType(_, _), _), leftInline) =>
unfoldScopeProperty(vm, st, property.raw).map { case (vm, inl) =>
unfoldAbilityProperty(vm, st, property.raw).map { case (vm, inl) =>
(
vm,
Inline(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,10 @@ object Exports {
override def resolved(
exportName: String,
value: ValueModel
): State[Map[String, ValueModel], Unit] = {
State.modify(_ + (exportName -> value))
}
): State[Map[String, ValueModel], Unit] = State.modify(_ + (exportName -> value))

override def resolved(exports: Map[String, ValueModel]): State[Map[String, ValueModel], Unit] = {
override def resolved(exports: Map[String, ValueModel]): State[Map[String, ValueModel], Unit] =
State.modify(_ ++ exports)
}

override val exports: State[Map[String, ValueModel], Map[String, ValueModel]] =
State.get
Expand Down
1 change: 1 addition & 0 deletions parser/src/main/scala/aqua/parser/lexer/TypeToken.scala
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ object ArrowTypeToken {
typeDef().backtrack
).map(_.toList)

// {SomeAb, SecondAb} for NamedTypeToken
def abilities(): P0[List[(Option[Name[S]], NamedTypeToken[S])]] =
(`{` *> comma(`Class`.surroundedBy(`/s*`).lift.map(s => Option(Name(s)) -> NamedTypeToken(s)))
.map(_.toList) <* `}`).?.map(_.getOrElse(List.empty))
Expand Down
1 change: 1 addition & 0 deletions parser/src/main/scala/aqua/parser/lexer/ValueToken.scala
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ object CallArrowToken {

case class CallBraces(name: Name[S], abilities: List[ValueToken[S]], args: List[ValueToken[S]])

// {SomeAb, SecondAb} for ValueToken
def abilities(): P[NonEmptyList[ValueToken[S]]] =
`{` *> comma(ValueToken.`value`.surroundedBy(`/s*`)) <* `}`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ import cats.data.{NonEmptyList, NonEmptyMap}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class ScopeValueExprSpec extends AnyFlatSpec with Matchers with AquaSpec {
class AbilityValueExprSpec extends AnyFlatSpec with Matchers with AquaSpec {
import AquaSpec.*

private def parseAndCheckScope(str: String) = {
private def parseAndCheckAbility(str: String) = {
val one = LiteralToken[Id]("1", LiteralType.number)

parseData(
str
) should be(
NamedValueToken(
NamedTypeToken[Id]("ScopeA"),
NamedTypeToken[Id]("AbilityA"),
NonEmptyMap.of(
"v1" -> one,
"f1" -> VarToken(Name[Id]("input"), IntoField[Id]("arrow") :: Nil)
Expand All @@ -32,12 +32,12 @@ class ScopeValueExprSpec extends AnyFlatSpec with Matchers with AquaSpec {
}

"one line struct value" should "be parsed" in {
parseAndCheckScope("""ScopeA(v1 = 1, f1 = input.arrow)""")
parseAndCheckAbility("""AbilityA(v1 = 1, f1 = input.arrow)""")
}

"multiline line struct value" should "be parsed" in {
parseAndCheckScope(
"""ScopeA(v1 = 1, f1 = input.arrow)""".stripMargin)
parseAndCheckAbility(
"""AbilityA(v1 = 1, f1 = input.arrow)""".stripMargin)
}

}
28 changes: 4 additions & 24 deletions semantics/src/main/scala/aqua/semantics/rules/ValuesAlgebra.scala
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ class ValuesAlgebra[S[_], Alg[_]: Monad](implicit
case ca: CallArrowToken[S] =>
callArrowToRaw(ca).map(_.widen[ValueRaw])

case it @ InfixToken(l, r, i) =>
case it @ InfixToken(l, r, _) =>
(valueToRaw(l), valueToRaw(r)).mapN((ll, rr) => ll -> rr).flatMap {
case (Some(leftRaw), Some(rightRaw)) =>
// TODO handle literal types
Expand Down Expand Up @@ -227,6 +227,7 @@ class ValuesAlgebra[S[_], Alg[_]: Monad](implicit

}

// Generate CallArrowRaw for arrow in ability
def callAbType(ab: String, abType: AbilityType, ca: CallArrowToken[S]): Alg[Option[CallArrowRaw]] =
abType.arrows.get(ca.funcName.value) match {
case Some(arrowType) => Option(CallArrowRaw(None, s"$ab.${ca.funcName.value}", Nil, arrowType, None)).pure[Alg]
Expand All @@ -250,6 +251,8 @@ class ValuesAlgebra[S[_], Alg[_]: Monad](implicit
)
)
)(ab =>
// TODO: Hack. Check that we have registered ability type.
// If it exists - this is ability type in file, if not - imported ability
T.getType(ab.value).flatMap {
case Some(abType: AbilityType) =>
callAbType(ab.value, abType, ca)
Expand Down Expand Up @@ -303,29 +306,6 @@ class ValuesAlgebra[S[_], Alg[_]: Monad](implicit
} yield result
}

def checkArguments(token: Token[S], arr: ArrowType, args: List[ValueToken[S]]): Alg[Boolean] =
// TODO: do we really need to check this?
T.checkArgumentsNumber(token, arr.domain.length, args.length).flatMap {
case false => false.pure[Alg]
case true =>
args
.map[Alg[Option[(Token[S], Type)]]](tkn => resolveType(tkn).map(_.map(t => tkn -> t)))
.zip(arr.domain.toList)
.foldLeft(
true.pure[Alg]
) { case (f, (ft, t)) =>
(
f,
ft.flatMap {
case None =>
false.pure[Alg]
case Some((tkn, valType)) =>
T.ensureTypeMatches(tkn, t, valType)
}
).mapN(_ && _)
}
}

}

object ValuesAlgebra {
Expand Down
2 changes: 1 addition & 1 deletion types/src/main/scala/aqua/types/Type.scala
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ case class StructType(name: String, fields: NonEmptyMap[String, Type]) extends D
s"$name{${fields.map(_.toString).toNel.toList.map(kv => kv._1 + ": " + kv._2).mkString(", ")}}"
}

// Scope is an unordered collection of labelled types and arrows
// Ability is an unordered collection of labelled types and arrows
case class AbilityType(name: String, fields: NonEmptyMap[String, Type]) extends NamedType {

lazy val arrows: Map[String, ArrowType] = fields.toNel.collect {
Expand Down

0 comments on commit 264d580

Please sign in to comment.