Skip to content

Commit

Permalink
Initial version of singleton types for single chars - unfortunatelly …
Browse files Browse the repository at this point in the history
…with asInstanceOf.

Co-authored-by: kasiaMarek <kasia@marek.net>
  • Loading branch information
susuro and kasiaMarek committed Dec 20, 2023
1 parent 21c5821 commit 76d5dc7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/scala/net/marek/tyre/Tyre.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ sealed trait Tyre[+R]:
def <|>[S](re: Tyre[S]): Tyre[Either[R, S]] = Or(this, re)
def map[S](f: R => S): Tyre[S] = Conv(this, f)

private case class Single[T <: Char & Singleton](s: T) extends Tyre[T]

private case class Pred(f: Char => Boolean) extends Tyre[Char]

private case class Or[+R1, +R2](left: Tyre[R1], right: Tyre[R2]) extends Tyre[Either[R1, R2]]:
Expand All @@ -30,6 +32,7 @@ object Tyre:
def epsilon: Tyre[Unit] = Epsilon

object Pred:
def single(s: Char & Singleton): Tyre[s.type] = Single(s)
def pred(f: Char => Boolean): Tyre[Char] = Pred(f)
def any: Tyre[Char] = Pred(_ => true)
def in(cs: List[Range]): Tyre[Char] = Pred(c => cs.exists(r => r.from <= c && r.to >= c))
Expand Down
20 changes: 20 additions & 0 deletions src/main/scala/net/marek/tyre/automaton/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ private[tyre] class TyreCompiler[IN <: Tuple, R](val context: Context[R *: IN]):

private def compile[IS <: Tuple, T](tyre: Tyre[T], continuation: Continuation[T, IS]): Automaton[IS] = tyre match

case Single(s) =>
val initState =
new InitNonAcceptingState[IS]:
type OS = IS
lazy val state = new NonAcceptingState[IS]:
val next: List[Transition[IS]] =
continuation.initStates.map:
case is: InitAcceptingState[?] =>
new AcceptingTransition[IS]:
lazy val routine = Compose(PushChar(), Transform(x => is.op(x.asInstanceOf[s.type *: IS])))
case is: InitNonAcceptingState[?] =>
new NonAcceptingTransition[IS]:
type OS = is.OS
lazy val nextState: NonAcceptingState[OS] = is.state
lazy val routine = Compose(PushChar(), Transform(x => is.op(x.asInstanceOf[s.type *: IS])))
def test(c: Char) = c == s
def op(x: IS) = x
new Automaton[IS]:
val initStates = List(initState)

case Pred(f) =>
val initState =
new InitNonAcceptingState[IS]:
Expand Down
5 changes: 4 additions & 1 deletion src/main/scala/net/marek/tyre/pattern/Re.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ sealed private trait Re
private case object ReAny extends Re:
override def toString(): String = "."

private case class ReSingle(s: Char & Singleton) extends Re:
override def toString(): String = s"$s"

private case class ReIn(cs: List[Range]) extends Re:
override def toString(): String = cs
.map:
Expand Down Expand Up @@ -50,7 +53,7 @@ private case class ReHole(index: Int) extends Re:
override def toString(): String = s"@$index"

private object Re:
def char(c: Char): ReIn = ReIn(List(Range(c, c)))
def char(c: Char & Singleton): ReSingle = ReSingle(c)

private enum CastOp(val symbol: Char):
case Stringify extends CastOp('s')
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private def tyreImpl(sc: Expr[StringContext], args: Expr[Seq[Any]])(using Quotes
def toTyre(re: Re)(using Quotes): Expr[Tyre[?]] = re match
case ReEpsilon => '{ Epsilon }
case ReAny => '{ Pred.any }
case ReSingle(s) => '{ Pred.single(${ Expr(s) }) }
case ReIn(cs) => '{ Pred.in(${ Expr(cs) }) }
case ReNotIn(cs) => '{ Pred.notIn(${ Expr(cs) }) }
case ReAnd(re1, re2) =>
Expand Down

0 comments on commit 76d5dc7

Please sign in to comment.