Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Simplify further

  • Loading branch information...
commit eb255efa668ea7220ca23d69b85e2951e2bf1576 1 parent 88da23e
@colder authored
View
58 src/insane/alias/EffectRepresentations.scala
@@ -16,6 +16,7 @@ trait EffectRepresentations extends PointToGraphsDefs with PointToEnvs {
import global._
import PointToGraphs._
+ import Automatons._
class SimpleEffectRepresentation(env: PTEnv) {
@@ -70,40 +71,18 @@ trait EffectRepresentations extends PointToGraphsDefs with PointToEnvs {
}
}
- object EffectNFA {
- import utils.Automatons._
-
- final case class State(id: Int) extends StateAbs {
- val name = id.toString
- }
-
- type Automaton = Automatons.Automaton[State, Symbol]
- type Transition = Automatons.Transition[State, Symbol]
-
- class DotConverter(atm: Automaton, title: String) extends AutomatonDotConverter(atm, title, "") {
- override def transitionLabel(t: Transition): String = t.label.name.toString.split('$').toList.last.trim
- override def stateLabel(s: State): String = s.id.toString
- }
+ class EffectNFADotConverter(atm: Automaton[Symbol], title: String) extends AutomatonDotConverter(atm, title, "") {
+ override def transitionLabel(t: Transition[Symbol]): String = t.label.name.toString.split('$').toList.last.trim
}
- def dumpNFA(env: EffectNFA.Automaton, dest: String) {
+ def dumpNFA(env: Automaton[Symbol], dest: String) {
reporter.debug("Dumping ENFA to "+dest+"...")
- new EffectNFA.DotConverter(env, "Effect Automaton").writeFile(dest)
- }
-
- object StateIDs {
- private var nextId = 0;
- def nextStateID() = {
- nextId += 1
- nextId
- }
+ new EffectNFADotConverter(env, "Effect Automaton").writeFile(dest)
}
class NFAEffectRepresentation(env: PTEnv) {
- def newState : EffectNFA.State = EffectNFA.State(StateIDs.nextStateID())
-
- def getNFA: EffectNFA.Automaton = {
+ def getNFA: Automaton[Symbol] = {
def nodeToID(n: Node): AnyRef = n match {
// we collapse load nodes and INodes as much as possible
@@ -115,43 +94,43 @@ trait EffectRepresentations extends PointToGraphsDefs with PointToEnvs {
n
}
- val entry = newState
+ val entry = newState()
- var nToS = Map[AnyRef, EffectNFA.State]()
+ var nToS = Map[AnyRef, State]()
- var entryTransitions = Set[EffectNFA.Transition]()
+ var entryTransitions = Set[Transition[Symbol]]()
for (v <- env.ptGraph.V) {
val id = nodeToID(v)
val state = nToS.get(id) match {
case Some(s) => s
case None =>
- val s = newState
+ val s = newState()
nToS += id -> s
s
}
v match {
case LVNode(CFGTrees.SymRef(s, _, _), _) =>
- entryTransitions += new EffectNFA.Transition(entry, s, state)
+ entryTransitions += Transition(entry, s, state)
case OBNode(s) =>
- entryTransitions += new EffectNFA.Transition(entry, s, state)
+ entryTransitions += Transition(entry, s, state)
case _ =>
""
}
}
- var finals = Set[EffectNFA.State]();
+ var finals = Set[State]();
val transitions = env.ptGraph.E.collect {
case IEdge(v1, l, v2) =>
finals += nToS(nodeToID(v2))
- new EffectNFA.Transition(nToS(nodeToID(v1)), l.sym, nToS(nodeToID(v2)))
+ Transition(nToS(nodeToID(v1)), l.sym, nToS(nodeToID(v2)))
case OEdge(v1, l, v2) =>
- new EffectNFA.Transition(nToS(nodeToID(v1)), l.sym, nToS(nodeToID(v2)))
+ Transition(nToS(nodeToID(v1)), l.sym, nToS(nodeToID(v2)))
}
- var res = new EffectNFA.Automaton(
+ var res = new Automaton(
nToS.values,
transitions ++ entryTransitions,
entry,
@@ -171,10 +150,7 @@ trait EffectRepresentations extends PointToGraphsDefs with PointToEnvs {
import utils.RegularExpressions._
def getRegex(): Regex[Symbol] = {
- val nfaBuilder = new NFAEffectRepresentation(env)
- implicit val stateBuilder = nfaBuilder.newState _
-
- val nfa = nfaBuilder.getNFA
+ val nfa = new NFAEffectRepresentation(env).getNFA
RegexHelpers.nfaToRegex(nfa)
}
View
73 src/insane/utils/Automatons.scala
@@ -4,47 +4,53 @@ package utils
object Automatons {
import Graphs._
- abstract class StateAbs extends VertexAbs {
- val name: String
+ final case class State(id: Int) extends VertexAbs{
+ val name = id.toString
}
- case class Transition[S <: StateAbs, L](v1: S, label: L, v2: S) extends LabeledEdgeAbs[L, S]
+ private var nextStateID = 0;
+ def newState() = {
+ nextStateID += 1
+ State(nextStateID)
+ }
+
+ case class Transition[L](v1: State, label: L, v2: State) extends LabeledEdgeAbs[L, State]
- case class Automaton[S <: StateAbs, L](
- val entry: S,
- val finals: Set[S],
- val graph: LabeledImmutableDirectedGraphImp[L, S, Transition[S, L]]
+ case class Automaton[L](
+ val entry: State,
+ val finals: Set[State],
+ val graph: LabeledImmutableDirectedGraphImp[L, State, Transition[L]]
) {
- def this(states: Iterable[S], transitions: Iterable[Transition[S, L]], entry: S, finals: Iterable[S]) =
- this(entry, finals.toSet, new LabeledImmutableDirectedGraphImp[L, S, Transition[S, L]](states.toSet ++ finals + entry, transitions.toSet))
+ def this(states: Iterable[State], transitions: Iterable[Transition[L]], entry: State, finals: Iterable[State]) =
+ this(entry, finals.toSet, new LabeledImmutableDirectedGraphImp[L, State, Transition[L]](states.toSet ++ finals + entry, transitions.toSet))
- def this(entry: S) =
- this(entry, Set(), new LabeledImmutableDirectedGraphImp[L, S, Transition[S, L]]())
+ def this(entry: State) =
+ this(entry, Set(), new LabeledImmutableDirectedGraphImp[L, State, Transition[L]]())
lazy val transitions = graph.E
lazy val states = graph.V
- def removeTransitions(trs: Iterable[Transition[S, L]]): Automaton[S, L] = {
+ def removeTransitions(trs: Iterable[Transition[L]]): Automaton[L] = {
var newGraph = trs.foldLeft(graph)((g, e) => g - e)
copy(graph = newGraph)
}
- def addTransitions(trs: Iterable[Transition[S, L]]): Automaton[S, L] = {
+ def addTransitions(trs: Iterable[Transition[L]]): Automaton[L] = {
var newGraph = trs.foldLeft(graph)((g, e) => g + e)
copy(graph = newGraph)
}
- def removeStates(sts: Iterable[S]): Automaton[S, L] = {
+ def removeStates(sts: Iterable[State]): Automaton[L] = {
assert(!sts.toSet.apply(entry), "Trying to remove entry state!")
copy(finals = finals -- sts, graph = graph -- sts)
}
- def removeDeadPaths: Automaton[S, L] = {
- var markedStates = Set[S](entry) ++ finals
+ def removeDeadPaths: Automaton[L] = {
+ var markedStates = Set[State](entry) ++ finals
- def visit(s: S, from: Set[S]): Unit = {
+ def visit(s: State, from: Set[State]): Unit = {
for (in <- graph.ins(s)) {
if (markedStates(in.v1)) {
markedStates ++= from + s
@@ -59,15 +65,15 @@ object Automatons {
removeStates(states -- markedStates)
}
- case class StateSig(ins: Set[(S, L)], outs: Set[(S, L)])
+ case class StateSig(ins: Set[(State, L)], outs: Set[(State, L)])
object StateSig {
- def fromState(s: S): StateSig = {
+ def fromState(s: State): StateSig = {
StateSig(graph.ins(s).map(t => (t.v1, t.label)).toSet,
graph.outs(s).map(t => (t.v2, t.label)).toSet)
}
}
- def collapseSimilarStates: Automaton[S, L] = {
+ def collapseSimilarStates: Automaton[L] = {
// Keep the head of each kind, remove the rest
val toRemove = states.groupBy(StateSig.fromState _).values.flatMap(_.tail)
@@ -75,14 +81,18 @@ object Automatons {
}
}
- class AutomatonDotConverter[S <: StateAbs, L](atm: Automaton[S, L], _title: String, _prefix: String) extends DotConverter[S, Transition[S, L]](atm.graph, _title, _prefix) {
+ class AutomatonDotConverter[L](atm: Automaton[L], _title: String, _prefix: String) extends DotConverter[State, Transition[L]](atm.graph, _title, _prefix) {
import utils.DotHelpers
- def transitionOptions(t: Transition[S, L], opts: List[String]): List[String] = opts
- def transitionLabel(t: Transition[S, L]): String = t.label.toString
+ def transitionOptions(t: Transition[L], opts: List[String]): List[String] = opts
+ def transitionLabel(t: Transition[L]): String = t.label.toString
+
+ override final def edgeToString(res: StringBuffer, t: Transition[L]) {
+ res append DotHelpers.labeledArrow(vToS(t.v1), transitionLabel(t), vToS(t.v2), transitionOptions(t, Nil))
+ }
- def stateToString(res: StringBuffer, s: S) {
+ override final def vertexToString(res: StringBuffer, s: State) {
var opts = List("fontsize=10")
if (atm.entry == s) {
@@ -94,21 +104,8 @@ object Automatons {
opts = "shape=circle" :: opts
}
}
-
- opts = stateOptions(s, opts)
- res append DotHelpers.node(vToS(s), stateLabel(s), opts)
-
- }
- def stateLabel(s: S): String = s.name
- def stateOptions(s: S, opts: List[String]): List[String] = opts
-
- override final def edgeToString(res: StringBuffer, t: Transition[S, L]) {
- res append DotHelpers.labeledArrow(vToS(t.v1), transitionLabel(t), vToS(t.v2), transitionOptions(t, Nil))
- }
-
- override final def vertexToString(res: StringBuffer, v: S) {
- stateToString(res, v)
+ res append DotHelpers.node(vToS(s), s.name, opts)
}
}
View
37 src/insane/utils/RegularExpressions.scala
@@ -90,12 +90,9 @@ object RegularExpressions {
object RegexHelpers {
import Automatons._
- type RegexTransition[S <: StateAbs, W] = Transition[S, Regex[W]]
- type RegexAutomaton[S <: StateAbs, W] = Automaton[S, Regex[W]]
+ def nfaToRegex[W](atm: Automaton[W]): Regex[W] = {
- def nfaToRegex[S <: StateAbs, W](atm: Automaton[S, W])(implicit stateBuilder: () => S): Regex[W] = {
-
- def ripState(rnfa: RegexAutomaton[S, W], s: S): RegexAutomaton[S, W] = {
+ def ripState(rnfa: Automaton[Regex[W]], s: State): Automaton[Regex[W]] = {
val selfRegs = rnfa.graph.ins(s).filter(_.v1 == s). // Only self loops
map(t => RegAst.around(t.label)) // Construct self loop regexes
@@ -105,7 +102,7 @@ object RegularExpressions {
selfRegs.reduce(_ combOr _)
}
- var newTransitions = Set[RegexTransition[S, W]]()
+ var newTransitions = Set[Transition[Regex[W]]]()
for (in <- rnfa.graph.ins(s); out <- rnfa.graph.outs(s) if in.v1 != s && out.v2 != s) {
val v1 = in.v1
@@ -119,12 +116,12 @@ object RegularExpressions {
rnfa.removeStates(Set(s)).addTransitions(newTransitions)
}
- val finalState = stateBuilder()
+ val finalState = newState()
- val transitions = atm.transitions.map{t => Transition[S, Regex[W]](t.v1, new RegLit[W](t.label), t.v2)} ++
- atm.finals.map(s => Transition[S, Regex[W]](s, RegEps[W](), finalState))
+ val transitions = atm.transitions.map{t => Transition[Regex[W]](t.v1, new RegLit[W](t.label), t.v2)} ++
+ atm.finals.map(s => Transition[Regex[W]](s, RegEps[W](), finalState))
- var rnfa = new RegexAutomaton[S, W](atm.states+finalState, transitions, atm.entry, Set(finalState))
+ var rnfa = new Automaton[Regex[W]](atm.states+finalState, transitions, atm.entry, Set(finalState))
val removableStates = (rnfa.states -- rnfa.finals) - rnfa.entry
@@ -139,14 +136,18 @@ object RegularExpressions {
}
}
- def regexToNFA[S <: StateAbs, W](reg: Regex[W])(implicit stateBuilder: () => S): Automatons.Automaton[S, W] = {
- val entryState = stateBuilder()
- val finalState = stateBuilder()
-
- var states = Set[S]()
- var transitions = Set[Automatons.Transition[S, W]]()
- def convertRegex(from: S, r: Regex[W], to: S) {
-
+ def regexToNFA[W](reg: Regex[W]): Automaton[W] = {
+ val entryState = newState()
+ val finalState = newState()
+
+ var states = Set[State]()
+ var transitions = Set[Transition[W]]()
+ def convertRegex(from: State, r: Regex[W], to: State) = r match {
+ case RegCons(ls) =>
+ case RegOr(ls) =>
+ case RegAst(ls) =>
+ case RegLit(ls) =>
+ case RegLit(ls) =>
}
null
}
Please sign in to comment.
Something went wrong with that request. Please try again.