forked from regb/scala-smtlib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TailPrinter.scala
47 lines (36 loc) · 1.07 KB
/
TailPrinter.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package smtlib
package printer
import common.LinkedList
import trees.Commands._
import trees.CommandsResponses._
import trees.Terms._
import trees.Tree
import java.io.Writer
import scala.collection.mutable.Stack
object TailPrinter extends Printer {
override val name: String = "tail-printer"
override protected def newContext(writer: Writer) = new TailContext(writer)
}
class TailContext(writer: Writer) extends PrintingContext(writer) {
var actions = new LinkedList[() => Unit]
var actionStack = List[LinkedList[() => Unit]]()
override def print(tree: Tree): Unit = {
actions.append(() => super.print(tree))
}
override def print(str: String): Unit = {
actions.append(() => super.print(str))
}
override protected def finish(): Unit = {
while (!actions.isEmpty || !actionStack.isEmpty) {
if (actions.isEmpty) {
actions = actionStack.head
actionStack = actionStack.tail
} else {
val action = actions.pop()
actionStack ::= actions
actions = new LinkedList[() => Unit]
action()
}
}
}
}