package simple;

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 SimpleDriver extends Driver {

  /**
   * A main function to invoke driver instance.
   *
   * @param args a command line.
   **/
  public static void main(String[] args) {
	new SimpleDriver().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("C")) {
        Scanner scanner = new Scanner(new FileReader(sourceFile));
        Parser yyparser = new Parser(io.symRoot, hirRoot, io);
        try {
            yyparser.yyparse(scanner);
        } catch (Parser.yyException ye) { 
            io.msgError.put(scanner+": ");
            ye.printStackTrace();
            throw new PassException("SimpleDriver", "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();
  }
}

