/* zweic -- a compiler for Zwei
*
* Stephane Micheloud & LAMP
*
* $Id$
*/
package zweic;
import java.io.PrintWriter;
/**
* This class implements a pretty printer for abstract syntax
* trees in Zwei.
*/
class Printer(out: PrintWriter) {
import Operators._;
/**
* Indentation string.
*/
private val step = " ";
/**
* indentation level.
*/
private var level = 0;
/**
* Prints a tree node.
*/
def print(tree: Tree): Printer = tree match {
case Program(decls, main) =>
for (val d <- decls)
print(d).println;
print(main).println
// ... à compléter ...
case Select(prefix, selector) =>
print(prefix).print(".").print(selector)
}
/**
* Print abstract syntax trees separated by commas.
*/
private def print(trees: List[Tree]): Printer = trees match {
// ... à compléter ...
case _ => this
}
/**
* Print a string.
*/
private def print(value: String): Printer = {
out.print(value);
this
}
private def print(name: Name): Printer = print(name.name);
private def print(formal: Pair[Name, TypeTree]): Printer =
print(formal._2).print(" ").print(formal._1);
/**
* Print abstract syntax trees separated by commas.
*/
private def printFormals(formals: List[Pair[Name, TypeTree]]): Printer =
formals match {
// ... à compléter ...
case _ => this
}
/**
* Print an end-of-line character and indent the following
* line appropriately.
*/
private def println = {
out.println();
for (val i <- List.range(0, level)) out.print(step);
this
}
/**
* Increment the indentation level.
*/
private def indent = {
level = level + 1;
this
}
/**
* Decrement the indentation level.
*/
private def undent = {
level = level - 1;
this
}
}