-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4670575
commit 1c1d87b
Showing
7 changed files
with
82 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
parser/src/main/scala/aqua/parser/expr/func/FailExpr.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package aqua.parser.expr.func | ||
|
||
import aqua.parser.Expr | ||
import aqua.parser.lexer.ValueToken | ||
import aqua.parser.lift.Span.S | ||
import aqua.parser.lexer.Token.* | ||
|
||
import cats.Comonad | ||
import cats.arrow.FunctionK | ||
import cats.parse.Parser | ||
|
||
final case class FailExpr[F[_]]( | ||
value: ValueToken[F] | ||
) extends Expr[F](FailExpr, value) { | ||
|
||
override def mapK[K[_]: Comonad](fk: FunctionK[F, K]): Expr[K] = | ||
FailExpr[K](value.mapK(fk)) | ||
|
||
} | ||
|
||
object FailExpr extends Expr.Leaf { | ||
|
||
override def p: Parser[Expr[S]] = | ||
(`fail` *> ` ` *> ValueToken.`value`).map(FailExpr.apply) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
semantics/src/main/scala/aqua/semantics/expr/func/FailSem.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package aqua.semantics.expr.func | ||
|
||
import aqua.parser.expr.func.FailExpr | ||
import aqua.semantics.rules.ValuesAlgebra | ||
import aqua.semantics.rules.types.TypesAlgebra | ||
import aqua.semantics.Prog | ||
import aqua.raw.Raw | ||
import aqua.types.ScalarType | ||
import aqua.raw.ops.FailTag | ||
|
||
import cats.Monad | ||
import cats.syntax.flatMap.* | ||
import cats.syntax.functor.* | ||
import cats.syntax.traverse.* | ||
|
||
class FailSem[S[_]](val expr: FailExpr[S]) extends AnyVal { | ||
|
||
def program[Alg[_]: Monad](implicit | ||
V: ValuesAlgebra[S, Alg], | ||
T: TypesAlgebra[S, Alg] | ||
): Prog[Alg, Raw] = for { | ||
maybeValue <- V.valueToRaw(expr.value) | ||
result <- maybeValue.traverse(vr => | ||
T.ensureTypeMatches( | ||
token = expr.value, | ||
expected = ScalarType.string, | ||
givenType = vr.`type` | ||
).map(isStr => | ||
if (isStr) FailTag(vr).funcOpLeaf | ||
else Raw.error("Argument of `fail` is not a string") | ||
) | ||
) | ||
} yield result.getOrElse( | ||
Raw.error("Resolution for `fail` argument failed") | ||
) | ||
} |