For...try expression (#115)

This commit is contained in:
Dmitry Kurinskiy 2021-05-14 11:36:49 +03:00 committed by GitHub
parent 3066e390b7
commit c4a2752e18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 8 deletions

View File

@ -7,7 +7,7 @@ scalaVersion := dottyVersion
val baseAquaVersion = settingKey[String]("base aqua version")
val catsV = "2.6.0"
val catsParseV = "0.3.2"
val catsParseV = "0.3.3"
val monocleV = "3.0.0-M5"
val scalaTestV = "3.2.7" // TODO update version for scala 3-RC3
val fs2V = "3.0.2"

View File

@ -7,10 +7,18 @@ import aqua.parser.lift.LiftParser
import aqua.parser.lift.LiftParser._
import cats.Comonad
import cats.parse.{Parser => P}
import cats.syntax.comonad._
case class ForExpr[F[_]](item: Name[F], iterable: Value[F], par: Option[F[Unit]]) extends Expr[F]
case class ForExpr[F[_]](
item: Name[F],
iterable: Value[F],
mode: Option[(F[ForExpr.Mode], ForExpr.Mode)]
) extends Expr[F]
object ForExpr extends Expr.AndIndented {
sealed trait Mode
case object TryMode extends Mode
case object ParMode extends Mode
override def validChildren: List[Expr.Companion] = List(
Expr.defer(OnExpr),
@ -23,8 +31,9 @@ object ForExpr extends Expr.AndIndented {
)
override def p[F[_]: LiftParser: Comonad]: P[ForExpr[F]] =
((`for` *> ` ` *> Name.p[F] <* ` <- `) ~ Value.`value`[F] ~ (` ` *> `par`.lift).?).map {
case ((item, iterable), par) =>
ForExpr(item, iterable, par)
((`for` *> ` ` *> Name.p[F] <* ` <- `) ~ Value
.`value`[F] ~ (` ` *> (`par`.as(ParMode: Mode).lift | `try`.as(TryMode: Mode).lift)).?).map {
case ((item, iterable), mode) =>
ForExpr(item, iterable, mode.map(m => m -> m.extract))
}
}

View File

@ -27,7 +27,11 @@ class ForExprSpec extends AnyFlatSpec with Matchers with AquaSpec {
)
parseFor("for some <- false par") should be(
ForExpr[Id]("some", toBool(false), Some(()))
ForExpr[Id]("some", toBool(false), Some(ForExpr.ParMode -> ForExpr.ParMode))
)
parseFor("for some <- false try") should be(
ForExpr[Id]("some", toBool(false), Some(ForExpr.TryMode -> ForExpr.TryMode))
)
}
}

View File

@ -1,7 +1,7 @@
package aqua.semantics.expr
import aqua.model.Model
import aqua.model.func.body.{ForTag, FuncOp, NextTag, OpTag, ParTag, SeqTag}
import aqua.model.func.body.{ForTag, FuncOp, NextTag, OpTag, ParTag, SeqTag, XorTag}
import aqua.parser.expr.ForExpr
import aqua.semantics.Prog
import aqua.semantics.rules.ValuesAlgebra
@ -36,7 +36,10 @@ class ForSem[F[_]](val expr: ForExpr[F]) extends AnyVal {
FuncOp.wrap(
ForTag(expr.item.value, ValuesAlgebra.valueToModel(expr.iterable, t)),
FuncOp.node(
expr.par.fold[OpTag](SeqTag)(_ => ParTag),
expr.mode.map(_._2).fold[OpTag](SeqTag) {
case ForExpr.ParMode => ParTag
case ForExpr.TryMode => XorTag
},
Chain(op, FuncOp.leaf(NextTag(expr.item.value)))
)
)