From e4205dfbbf6838f0fd0aae3cb6190b8433c7745a Mon Sep 17 00:00:00 2001 From: Dima Date: Wed, 17 May 2023 23:10:18 +0900 Subject: [PATCH] feat!: add name scopes to if/else/try blocks (#715) --- semantics/src/main/scala/aqua/semantics/Prog.scala | 9 +++++++++ .../aqua/semantics/expr/func/ElseOtherwiseSem.scala | 10 +++++++++- .../main/scala/aqua/semantics/expr/func/IfSem.scala | 8 +++++++- .../main/scala/aqua/semantics/expr/func/TrySem.scala | 10 +++++++--- 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/semantics/src/main/scala/aqua/semantics/Prog.scala b/semantics/src/main/scala/aqua/semantics/Prog.scala index 2de2a305..d6340449 100644 --- a/semantics/src/main/scala/aqua/semantics/Prog.scala +++ b/semantics/src/main/scala/aqua/semantics/Prog.scala @@ -2,6 +2,7 @@ package aqua.semantics import aqua.parser.lexer.Token import aqua.semantics.rules.abilities.AbilitiesAlgebra +import aqua.semantics.rules.locations.LocationsAlgebra import aqua.semantics.rules.names.NamesAlgebra import cats.Monad import cats.syntax.flatMap.* @@ -37,6 +38,14 @@ sealed abstract class Prog[Alg[_]: Monad, A] extends (Alg[A] => Alg[A]) { (_: Unit, m: A) => N.endScope() as m ) ) + + def locationsScope[S[_]]()(implicit L: LocationsAlgebra[S, Alg]): Prog[Alg, A] = + wrap( + RunAround( + L.beginScope(), + (_: Unit, m: A) => L.endScope() as m + ) + ) } case class RunAfter[Alg[_]: Monad, A](prog: Alg[A]) extends Prog[Alg, A] { diff --git a/semantics/src/main/scala/aqua/semantics/expr/func/ElseOtherwiseSem.scala b/semantics/src/main/scala/aqua/semantics/expr/func/ElseOtherwiseSem.scala index 35ee5bce..bde069bd 100644 --- a/semantics/src/main/scala/aqua/semantics/expr/func/ElseOtherwiseSem.scala +++ b/semantics/src/main/scala/aqua/semantics/expr/func/ElseOtherwiseSem.scala @@ -5,16 +5,24 @@ import aqua.parser.expr.func.ElseOtherwiseExpr import aqua.raw.Raw import aqua.semantics.Prog import aqua.semantics.rules.abilities.AbilitiesAlgebra +import aqua.semantics.rules.locations.LocationsAlgebra +import aqua.semantics.rules.names.NamesAlgebra import cats.syntax.applicative.* import cats.Monad class ElseOtherwiseSem[S[_]](val expr: ElseOtherwiseExpr[S]) extends AnyVal { - def program[Alg[_]: Monad](implicit A: AbilitiesAlgebra[S, Alg]): Prog[Alg, Raw] = + def program[Alg[_]: Monad](implicit + A: AbilitiesAlgebra[S, Alg], + N: NamesAlgebra[S, Alg], + L: LocationsAlgebra[S, Alg] + ): Prog[Alg, Raw] = Prog .after[Alg, Raw] { case FuncOp(g) => XorTag.wrap(g).toFuncOp.pure[Alg] case g => g.pure[Alg] } .abilitiesScope(expr.token) + .namesScope(expr.token) + .locationsScope() } diff --git a/semantics/src/main/scala/aqua/semantics/expr/func/IfSem.scala b/semantics/src/main/scala/aqua/semantics/expr/func/IfSem.scala index 079a340c..294d9348 100644 --- a/semantics/src/main/scala/aqua/semantics/expr/func/IfSem.scala +++ b/semantics/src/main/scala/aqua/semantics/expr/func/IfSem.scala @@ -7,6 +7,8 @@ import aqua.raw.Raw import aqua.semantics.Prog import aqua.semantics.rules.ValuesAlgebra import aqua.semantics.rules.abilities.AbilitiesAlgebra +import aqua.semantics.rules.locations.LocationsAlgebra +import aqua.semantics.rules.names.NamesAlgebra import aqua.semantics.rules.types.TypesAlgebra import aqua.types.Type import cats.Monad @@ -19,7 +21,9 @@ class IfSem[S[_]](val expr: IfExpr[S]) extends AnyVal { def program[Alg[_]: Monad](implicit V: ValuesAlgebra[S, Alg], T: TypesAlgebra[S, Alg], - A: AbilitiesAlgebra[S, Alg] + A: AbilitiesAlgebra[S, Alg], + N: NamesAlgebra[S, Alg], + L: LocationsAlgebra[S, Alg] ): Prog[Alg, Raw] = Prog .around( @@ -55,4 +59,6 @@ class IfSem[S[_]](val expr: IfExpr[S]) extends AnyVal { } ) .abilitiesScope[S](expr.token) + .namesScope[S](expr.token) + .locationsScope() } diff --git a/semantics/src/main/scala/aqua/semantics/expr/func/TrySem.scala b/semantics/src/main/scala/aqua/semantics/expr/func/TrySem.scala index bab2eecc..e0a9ec6d 100644 --- a/semantics/src/main/scala/aqua/semantics/expr/func/TrySem.scala +++ b/semantics/src/main/scala/aqua/semantics/expr/func/TrySem.scala @@ -6,6 +6,8 @@ import aqua.raw.Raw import aqua.semantics.Prog import aqua.semantics.rules.ValuesAlgebra import aqua.semantics.rules.abilities.AbilitiesAlgebra +import aqua.semantics.rules.locations.LocationsAlgebra +import aqua.semantics.rules.names.NamesAlgebra import aqua.semantics.rules.types.TypesAlgebra import cats.syntax.applicative.* import cats.Monad @@ -13,9 +15,9 @@ import cats.Monad class TrySem[S[_]](val expr: TryExpr[S]) extends AnyVal { def program[Alg[_]: Monad](implicit - V: ValuesAlgebra[S, Alg], - T: TypesAlgebra[S, Alg], - A: AbilitiesAlgebra[S, Alg] + A: AbilitiesAlgebra[S, Alg], + N: NamesAlgebra[S, Alg], + L: LocationsAlgebra[S, Alg] ): Prog[Alg, Raw] = Prog .after[Alg, Raw] { @@ -25,4 +27,6 @@ class TrySem[S[_]](val expr: TryExpr[S]) extends AnyVal { Raw.error("Wrong body of the try expression").pure[Alg] } .abilitiesScope(expr.token) + .namesScope(expr.token) + .locationsScope() }