import java.io.BufferedReader;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import pascal.PascalParser;
import pascal.PascalTokenizer;

import rebelsky.compiler.misc.AdvCharStream;
import rebelsky.compiler.misc.StandardCharStream;
import rebelsky.compiler.parser.Node;
import rebelsky.compiler.parser.TreePrinter;

/**
 * Build a parse tree for a Pascal program and then traverse the tree
 * looking for true and false values.
 *
 * @author Samuel A. Rebelsky
 * @version 1.0 of April 2004
 */
public class TestBC
{
  public static void main(String[] args) 
    throws Exception
  {
    String fname;
    InputStreamReader isr;
  
    // No parameters: Read from standard input
    if (args.length == 0) {
      System.out.print("Reading Pascal from stdin.\n");
      isr = new InputStreamReader(System.in);
    } // read from stdin
    else 
      isr = new FileReader(args[0]);

    // Create the character stream
    AdvCharStream acs =
      new AdvCharStream(
        new StandardCharStream(
          new BufferedReader(
            isr)));

    // Prepare for output.
    PrintWriter out = new PrintWriter(System.out, true);

    // Create the tokenizer
    PascalTokenizer tokenizer = new PascalTokenizer(acs);

    // Build the parse tree
    PascalParser parser = new PascalParser();
    Node tree = parser.parse(tokenizer);

    // Print the parse tree
    out.println("****** PARSE TREE ******");
    TreePrinter.print(tree, out);
   
    // Traverse the parse tree
    BooleanCounter bc = new BooleanCounter();
    bc.traverse(tree);
 
    // Print the results
    out.println("****** RESULTS ******");
    out.println("True constants: " + bc.truecount);
    out.println("False constants: " + bc.falsecount);

    // That's it, we're done.
    System.exit(0);
  } // main(String[])
} // class TestBC

