package natlab;

import natlab.options.Options;
import natlab.ast.*;
import beaver.Parser;
import java.io.*;
import java.util.*;



public class Main
{
    private static Options options;
    
    public static void main(String[] args)
    {

        
        options = new Options();

        if( processCmdLine( args ) ){

            if( options.help() ){
                System.out.println(options.getUsage());
            }
            else{
                if( options.e() ){
                    System.out.println("exhaustive list");
                }
                if( options.d() ){
                    System.out.println("dynamic linking");
                }
                if( options.getFiles().size() == 0 ){
                    System.err.println("No files provided, must have at least one file.");
                }
                else{
                    for( Object o : options.getFiles() ){
                        String file = (String) o;
                        System.out.println(file);
                    }
                }
            }
        }
    }

    private static Program parseFile(String fName) throws IOException
    {
        //Parse a given file and return a Program ast node
        //if file does not exist, exit program
        NatlabParser parser = new NatlabParser();
        NatlabScanner scanner;
        CommentBuffer cb = new CommentBuffer();

        parser.setCommentBuffer(cb);

        try{
            scanner = new NatlabScanner(new FileReader( fName ) );
            scanner.setCommentBuffer( cb );
            try{
                return (Program)parser.parse(scanner);        
            }catch(Parser.Exception e){
                System.err.println("**ERROR**");
                System.err.println(e.getMessage());
                for(String error : parser.getErrors()) {
                    System.err.println(error);
                }
                System.err.println("Aborting");
                System.exit(1);
            }            
        }catch(FileNotFoundException e){
            System.err.println("File "+fName+" not found!\nAborting");
            System.exit(1);
        }
        //should be dead due to exit calls
        return null;
    }
    private static boolean processCmdLine(String[] args)
    {
        try{
            options.parse( args );

            if( args.length == 0 ){
                System.out.println("No options given\n" +
                                   "Try -help for usage");
                return false;
            }
            return true;
        }catch( NullPointerException e ){
            System.err.println("options variable not initialized");
            throw e;
        }
    }

}
@@ -841,32 +841,96 @@
           else%7B%0A
+%09%09    LinkedList%3CProgram%3E programs = new LinkedList%3CProgram%3E();%0A
                 
@@ -1064,20 +1064,72 @@
 println(
-file
+%22Parsing: %22 + file);%0A%09%09%09programs.add( parseFile( file ) 
 );%0A     
@@ -1198,88 +1198,8 @@
  %7D%0A%0A
-    private static Program parseFile(String fName) throws IOException%0A    %7B%0A    
     
@@ -1253,20 +1253,16 @@
 ode%0A    
- 
-   
 //if fil
@@ -1281,22 +1281,97 @@
 xist
-, exit program
+ or other problems, exit program%0A    private static Program parseFile(String fName)%0A    %7B
 %0A   
@@ -2250,32 +2250,247 @@
 t(1);%0A        %7D%0A
+%09catch(IOException e)%7B%0A%09    System.err.println(%22Problem parsing %22+fName);%0A%09    if( e.getMessage() != null )%0A%09%09System.err.println( e.getMessage() );%0A%09    System.err.println(%22Aborting%22);%0A%09    System.exit(1);%0A%09%7D%0A%09    %0A
         //should

update to Main entry point