Skip to content

Commit

Permalink
feat!: add name scopes to if/else/try blocks (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
DieMyst authored May 17, 2023
1 parent 9450321 commit e4205df
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
9 changes: 9 additions & 0 deletions semantics/src/main/scala/aqua/semantics/Prog.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.*
Expand Down Expand Up @@ -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] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -55,4 +59,6 @@ class IfSem[S[_]](val expr: IfExpr[S]) extends AnyVal {
}
)
.abilitiesScope[S](expr.token)
.namesScope[S](expr.token)
.locationsScope()
}
10 changes: 7 additions & 3 deletions semantics/src/main/scala/aqua/semantics/expr/func/TrySem.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ 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

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] {
Expand All @@ -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()
}

0 comments on commit e4205df

Please sign in to comment.