package spcl.compiler;

import java.io.*;
import java.util.*;

import spcl.data.*;

public class SPCLCompiler{


	//private Lexer lexer = new Lexer();
	//private Parser parser = new Parser();

	private static boolean success = true;
	private static String filename;

	public SPCLCompiler( String [] args ) throws Exception{

		if( args.length == 2 ){

			if( args[0].equals( "-parse" ) && args[1].endsWith( ".spcl" ) ){

				try{
					filename=args[1];
					File f = new File( filename );
					if( !f.exists() || !f.isFile() || !f.canRead() ){
						System.err.println( filename+" does not exist, is not a file, or cannot be read." );
						System.exit( -4 );
					}
					//lexer.init( f );
				 	//parser.init( lexer );
					//ASTNode p = parser.parse( tokens );
					//parser.parse( tokens );

				}catch( ParserException e ){
					success=false;
					error( e );
				}

				System.out.println("Parse "+ ( success ? "succeeded." : "failed." ) );
				//if( success ) p.acceptVisitor(new ClassRefVisitor());
				//if( success ) p.acceptVisitor(new VoidCheckVisitor());

			}else if( args[0].equals( "-lex" ) && args[1].endsWith( ".spcl" ) ){

				filename=args[1];
				File f = new File( filename );
				if( !f.exists() || !f.isFile() || !f.canRead() ){
					System.err.println( filename+" does not exist, is not a file, or cannot be read." );
					System.exit( -3 );
				}
				//lexer.scan( f );
				//Vector tokens = lexer.getTokens();
				Vector tokens = new Vector();
				if( tokens.size()==0 ){
					System.out.println( "No tokens to write from lexer." );
					System.exit( 0 );
				}

				//open the file with .lp (lexed-policy) and write out ints
				int dotPos = filename.lastIndexOf( ".spcl" );
				//dotPos cannot be -1
				String basename = filename.substring( 0, dotPos );
				basename+=".lp";
				//FileWriter writer = new FileWriter( basename, false );
				//Token token=null;

				//for( int i=0;i<tokens.size();i++ ){
					//token = ((Token)tokens.elementAt( i ));
					//switch( token.type ){
						//case LexerConstants.ID:
							//writer.write( token.text+" " );
							//break;
						//default:
						//writer.write( token.type+" " );
					//}
				//}
				//writer.close();

			}else{
				doUsage();
			}

		}else if( args.length == 1 && args[0].endsWith( ".spcl" ) ){

			//front end


			//back end
			compile();

		}else{
			doUsage();
		}

		System.exit( 0 );
	}


	public static void error( ParserException pex ){
		success = false;
		System.err.println("Error  compiling ("+filename+":"+pex.line+"). " + pex.getMessage() );
	}



	/** back end */
	private void compile(){}




	private void doUsage(){
		System.out.println( "java spcl.compiler.SPCLCompiler [-lex|-parse] [filename].spcl" );
		System.exit( -2 );
	}

	/**
	 *
	 * java spcl.compiler.SPCLCompiler [-lex|-parse] [filename].spcl
	 *
	 *
	*/
	public static void main( String [] args ){

		try{

			new SPCLCompiler( args );

		}catch( Exception e ){
			e.printStackTrace();
			System.err.println( "\n\n\t!!! Major problem with compiler: "+
							     e.getMessage()+
							     "\n\nPlease report this error to nsl@cs.columbia.edu" );
			System.exit( -1 );
		}

		System.exit( 0 );
	}


}
