Newer
Older
zweic / sources / zweic / PrinterTest.scala
@glproj03 glproj03 on 29 Nov 2005 1 KB added Printer
/*  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 <file.zwei>
 */
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 <file.zwei>");
      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());
    }
  }
}