
package cz.karry.jsimdop;

import cz.karry.dop.parser.MemorySizeError;
import cz.karry.dop.parser.Parser;
import cz.karry.dop.parser.SyntaxError;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author karry
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length == 0) {
            System.err.println("Specify input file!");
            System.exit(1);
        }

        InputStreamReader in = null;
        String charsetName = Charset.defaultCharset().name();
        String filePath = args[0];
        String source = "";

        try {
            in = new InputStreamReader(new FileInputStream(new File(filePath)), charsetName);
            //Reader in = new FileReader(f);
            char[] buff = new char[4096];
            int nch;
            while ((nch = in.read(buff, 0, buff.length)) != -1) {
                source += new String(buff, 0, nch);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "file " + filePath, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "file " + filePath, ex);
        } catch (IOException ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, "file " + filePath, ex);
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        try {
            Parser parser = new Parser(source);
            parser.run();
            parser.printMemory();
        } catch (SyntaxError ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MemorySizeError ex) {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

}
