Basic PeerIdInterpreter

This commit is contained in:
dmitry 2021-03-15 12:48:41 +03:00
parent b8d72677ee
commit ab4a26ee96
2 changed files with 26 additions and 1 deletions

View File

@ -0,0 +1,25 @@
package aqua.ast.algebra.scope
import aqua.ast.algebra.{ReportError, StackInterpreter}
import aqua.parser.lexer.Value
import cats.data.State
import cats.~>
import monocle.Lens
import monocle.macros.GenLens
class PeerIdInterpreter[F[_], X](implicit lens: Lens[X, PeerIdState[F]], error: ReportError[F, X])
extends StackInterpreter[F, X, PeerIdState[F], Value[F]](GenLens[PeerIdState[F]](_.stack))
with (PeerIdOp[F, *] ~> State[X, *]) {
override def apply[A](fa: PeerIdOp[F, A]): State[X, A] =
(fa match {
case CurrentPeerId() =>
getState.map(_.stack.headOption)
case _: ErasePeerId[F] =>
endScope
case opi: OnPeerId[F] =>
beginScope(opi.id)
}).asInstanceOf[State[X, A]]
}
case class PeerIdState[F[_]](stack: List[Value[F]])

View File

@ -2,7 +2,7 @@ package aqua.ast.algebra.scope
import aqua.parser.lexer.Value
trait PeerIdOp[F[_], T]
sealed trait PeerIdOp[F[_], T]
case class OnPeerId[F[_]](id: Value[F]) extends PeerIdOp[F, Unit]
case class ErasePeerId[F[_]]() extends PeerIdOp[F, Unit]