diff --git a/sources/zweic/Printer.scala b/sources/zweic/Printer.scala new file mode 100755 index 0000000..4fad2d0 --- /dev/null +++ b/sources/zweic/Printer.scala @@ -0,0 +1,98 @@ +/* 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 + } +} diff --git a/sources/zweic/PrinterTest.scala b/sources/zweic/PrinterTest.scala new file mode 100755 index 0000000..771f7db --- /dev/null +++ b/sources/zweic/PrinterTest.scala @@ -0,0 +1,45 @@ +/* zweic -- a compiler for Zwei + * + * Stephane Micheloud & LAMP + * + * $Id$ + */ + +package zweic; + + +/** + * A small test class for the pretty printer of zweic; it reads in a + * file and prints out the abstract syntax tree. If the program is + * erroneous, the compiler will print a corresponding error message + * instead. + * + * usage: scala zweic.PrinterTest + */ +object PrinterTest { + import java.io.{FileInputStream, FileWriter, PrintWriter, IOException}; + + def main(args: Array[String]): Unit = { + if (args.length == 0) { + Console.println("usage: scala zweic.PrinterTest "); + System.exit(1) + } + try { + val in = new FileInputStream(args(0)); + val tree = new Parser(in).parse; + in.close(); + if (Report.errors > 0) + System.err.println(Report.errors + " errors found." ); + else { + val out = if (args.length < 2) new PrintWriter(System.out) + else new PrintWriter(new FileWriter(args(1))); + new Printer(out).print(tree); + out.close(); + } + System.exit(if (Report.errors > 0) 1 else 0); + } + catch { + case e: IOException => Report.fail(e.getMessage()); + } + } +}