Created
January 24, 2023 15:08
-
-
Save jelinski/4041bcf4c0068333f8062238462b2757 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Bindable { | |
def label: String | |
} | |
case class Port(label: String) extends Bindable | |
case class Net(label: String) extends Bindable | |
case class Binding(from: Bindable, to: Bindable){ | |
override def toString: String = s"$from -> $to" | |
} | |
case class Instance(module: Module, bindings: Set[Binding]) | |
trait Module { | |
def ports(): Set[Port] | |
def nets(): Set[Net] | |
def instances(): Set[Instance] | |
} | |
object M1 extends Module { | |
val x: Port = Port("x") | |
val y: Port = Port("y") | |
override def ports(): Set[Port] = Set(x, y) | |
override def nets(): Set[Net] = Set.empty | |
override def instances(): Set[Instance] = Set.empty | |
} | |
object M2 extends Module { | |
val u: Port = Port("u") | |
val v: Port = Port("v") | |
val t: Net = Net("t") | |
val i1: Instance = Instance( | |
M1, | |
Set( | |
Binding(M1.x, u), | |
Binding(M1.y, t) | |
) | |
) | |
val i2: Instance = Instance( | |
M1, | |
Set( | |
Binding(M1.x, t), | |
Binding(M1.y, u) | |
) | |
) | |
override def ports(): Set[Port] = Set(u, v) | |
override def nets(): Set[Net] = Set(t) | |
override def instances(): Set[Instance] = Set(i1, i2) | |
} | |
def traverseAndPrint(module: Module, level: Int = 0): Unit = { | |
def printPadded(message: String) = println(s"${" ".repeat(level*2)}$message") | |
printPadded(s"Module of type ${module.getClass.getSimpleName}") | |
printPadded(s"Ports: ${module.ports()}") | |
printPadded(s"Nets: ${module.nets()}") | |
printPadded(s"Instances: ") | |
if (module.instances().isEmpty) printPadded("- No instances") | |
else { | |
module.instances().foreach(i => | |
if(i.bindings.isEmpty) printPadded("- No Bindings") | |
else printPadded(s"- Bindings: ${i.bindings.mkString("; ")}") | |
traverseAndPrint(i.module, level + 1) | |
) | |
} | |
} | |
println("MODULE M1:") | |
traverseAndPrint(M1) | |
println("MODULE M2:") | |
traverseAndPrint(M2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment