package pl0front;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;

import coins.HirRoot;
import coins.IoRoot;
import coins.PassException;
import coins.driver.Driver;
import coins.driver.Suffix;
import coins.ir.hir.Program;

/**
 * A simple driver implementation using the COINS Compiler Driver API.
 **/
public class PL0Driver extends Driver {

  /**
   * A main function to invoke driver instance.
   *
   * @param args a command line.
   **/
  public static void main(String[] args) {
	new PL0Driver().go(args);
  }
  
  /**
   * HIR tree creation from source code.
   *
   * @param hirRoot an HirRoot object
   * @param suffix suffix rule of the source file.
   * @param in an input stream from which the C source program can be read
   * @param io an IoRoot object
   * @throws IOException any IO error
   * @throws PassException unrecoverable error(s) found in processing
   **/
  protected void makeHirFromSource(File sourceFile, HirRoot hirRoot, Suffix suffix, InputStream in, IoRoot io)
	throws IOException, PassException {
    if (suffix.getLanguageName().equals("PL0")) {
        PL0Parser parser = new PL0Parser(io.symRoot, hirRoot, io, new FileReader(sourceFile));	
        try {
            parser.program();
        } catch (Exception e) {
            io.msgError.put("in PL0 parser: ");
            e.printStackTrace();
            throw new PassException("PL0Driver", "Error(s) in parsing "+sourceFile.getName());
        }
    } else {
      io.msgError.put(sourceFile + ": Unknown programming language: " + suffix.getLanguageName());
      throw new PassException(myName, "Unknown language " + suffix.getLanguageName());
    }
    ((Program)hirRoot.programRoot).finishHir();
  }
}

