<commit_before>package kalang.tool;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.logging.Level;
import java.util.logging.Logger;
import kalang.KalangClassLoader;
import kalang.compiler.Diagnosis;
import kalang.compiler.StandardCompileHandler;
import kalang.util.ClassNameUtil;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
 *
 * @author <NAME> <<EMAIL>>
 */
public class Compiler {

    private final static String APP_NAME = "kalang";

    private final static Options OPTIONS;
    
    private boolean hasError = false;

    static {
        OPTIONS = new Options();
        OPTIONS.addOption("h", false, "show this help message");
        OPTIONS.addOption("cp", true, "compile classpath");
        OPTIONS.addOption("s", true, "source directory");
        OPTIONS.addOption("o", true, "output directory");
        OPTIONS.addOption("f",true,"output format");
        OPTIONS.addOption("run", true, "run the class with special name");
        //OPTIONS.addOption("t",true,"set the output type,should be one of class,java");
    }

    public static void printUsage() {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(APP_NAME, OPTIONS);
    }
    
    public static void main(String[] args) throws ParseException, IOException {
        Compiler cper = new Compiler();
        cper.compile(args);
    }

    public boolean compile(String[] args) throws ParseException, IOException {
        DefaultParser parser = new DefaultParser();
        CommandLine cli = parser.parse(OPTIONS, args);
        if (args.length == 0 || cli.hasOption("h")) {
            printUsage();
            return false;
        }
        if(cli.hasOption("run")){
            return run(cli);
        }else{
            JointFileSystemCompiler fsc = new JointFileSystemCompiler(){
                @Override
                protected void reportDiagnosis(Diagnosis dn) {
                    if(dn.getKind().isError()){
                        Compiler.this.hasError = true;
                    }
                    super.reportDiagnosis(dn);
                }
            };
            File[] cps = parseClassPath(cli);
            if(cps!=null){
                for (File cp : cps) {
                        fsc.addClassPath(cp);
                }
            }
            String outPath = ".";
            if (cli.hasOption("s")) {
                String srcPath = cli.getOptionValue("s");
                fsc.addKalangAndJavaSourceDir(new File(srcPath));
            }
            if (cli.hasOption("o")) {
                outPath = cli.getOptionValue("o");
            }
            String outputFormat = "class";
            if(cli.hasOption("f")){
                outputFormat = cli.getOptionValue("f");
            }
            FileSystemOutputManager outputManager = new FileSystemOutputManager(new File(outPath), outputFormat);
            switch(outputFormat){
                case "class":
                    fsc.setJavaOutputManager(outputManager);
                    fsc.setCodeGenerator(new ClassWriter(outputManager));
                    break;
                case "ast":
                    fsc.setCodeGenerator(new AstWriter(outputManager));
                    break;
                default:
                    throw new UnsupportedOperationException("unknown format:" + outputFormat);
            }
            File currentDir = new File(".");
            fsc.addJavaSourcePath(currentDir);
            fsc.addSourcePath(currentDir);
            String[] srcs = cli.getArgs();
            for(String s:srcs){
                File srcFile = new File(s);
                if(srcFile.isDirectory()){
                    fsc.addKalangAndJavaSourceDir(srcFile);
                }else{
                    //TODO here should be currenDir?
                    fsc.addKalangOrJavaSource(currentDir , srcFile);
                }
            }
            hasError = false;
            fsc.compile();
            return hasError;
        }
    }
    
    private File[] parseClassPath(CommandLine cli){
        if (cli.hasOption("cp")) {
            String[] cps = cli.getOptionValue("cp").split(";");
            File[] file = new File[cps.length];
            for (int i=0;i<cps.length;i++) {
                file[i] = new File(cps[i]);
            }
            return file;
        }
        return new File[0];
    }
    
    public boolean run(CommandLine cli){
        String clsName = cli.getOptionValue("run");
        KalangClassLoader kcl = new KalangClassLoader();
        kcl.addClassPath(new File("."));<commit_msg>Fix return value of Compiler.compile<commit_after>package kalang.tool;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.logging.Level;
import java.util.logging.Logger;
import kalang.KalangClassLoader;
import kalang.compiler.Diagnosis;
import kalang.compiler.StandardCompileHandler;
import kalang.util.ClassNameUtil;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

/**
 *
 * @author <NAME> <<EMAIL>>
 */
public class Compiler {

    private final static String APP_NAME = "kalang";

    private final static Options OPTIONS;
    
    private boolean hasError = false;

    static {
        OPTIONS = new Options();
        OPTIONS.addOption("h", false, "show this help message");
        OPTIONS.addOption("cp", true, "compile classpath");
        OPTIONS.addOption("s", true, "source directory");
        OPTIONS.addOption("o", true, "output directory");
        OPTIONS.addOption("f",true,"output format");
        OPTIONS.addOption("run", true, "run the class with special name");
        //OPTIONS.addOption("t",true,"set the output type,should be one of class,java");
    }

    public static void printUsage() {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(APP_NAME, OPTIONS);
    }
    
    public static void main(String[] args) throws ParseException, IOException {
        Compiler cper = new Compiler();
        cper.compile(args);
    }

    public boolean compile(String[] args) throws ParseException, IOException {
        DefaultParser parser = new DefaultParser();
        CommandLine cli = parser.parse(OPTIONS, args);
        if (args.length == 0 || cli.hasOption("h")) {
            printUsage();
            return false;
        }
        if(cli.hasOption("run")){
            return run(cli);
        }else{
            JointFileSystemCompiler fsc = new JointFileSystemCompiler(){
                @Override
                protected void reportDiagnosis(Diagnosis dn) {
                    if(dn.getKind().isError()){
                        Compiler.this.hasError = true;
                    }
                    super.reportDiagnosis(dn);
                }
            };
            File[] cps = parseClassPath(cli);
            if(cps!=null){
                for (File cp : cps) {
                        fsc.addClassPath(cp);
                }
            }
            String outPath = ".";
            if (cli.hasOption("s")) {
                String srcPath = cli.getOptionValue("s");
                fsc.addKalangAndJavaSourceDir(new File(srcPath));
            }
            if (cli.hasOption("o")) {
                outPath = cli.getOptionValue("o");
            }
            String outputFormat = "class";
            if(cli.hasOption("f")){
                outputFormat = cli.getOptionValue("f");
            }
            FileSystemOutputManager outputManager = new FileSystemOutputManager(new File(outPath), outputFormat);
            switch(outputFormat){
                case "class":
                    fsc.setJavaOutputManager(outputManager);
                    fsc.setCodeGenerator(new ClassWriter(outputManager));
                    break;
                case "ast":
                    fsc.setCodeGenerator(new AstWriter(outputManager));
                    break;
                default:
                    throw new UnsupportedOperationException("unknown format:" + outputFormat);
            }
            File currentDir = new File(".");
            fsc.addJavaSourcePath(currentDir);
            fsc.addSourcePath(currentDir);
            String[] srcs = cli.getArgs();
            for(String s:srcs){
                File srcFile = new File(s);
                if(srcFile.isDirectory()){
                    fsc.addKalangAndJavaSourceDir(srcFile);
                }else{
                    //TODO here should be currenDir?
                    fsc.addKalangOrJavaSource(currentDir , srcFile);
                }
            }
            hasError = false;
            fsc.compile();
            return !hasError;
        }
    }
    
    private File[] parseClassPath(CommandLine cli){
        if (cli.hasOption("cp")) {
            String[] cps = cli.getOptionValue("cp").split(";");
            File[] file = new File[cps.length];
            for (int i=0;i<cps.length;i++) {
                file[i] = new File(cps[i]);
            }
            return file;
        }
        return new File[0];
    }
    
    public boolean run(CommandLine cli){
        String clsName = cli.getOptionValue("run");
        KalangClassLoader kcl = new KalangClassLoader();
        kcl.addClassPath(new File("."));
{'fraction_non_alphanumeric': 0.1114519427402863, 'fraction_numerical': 0.0006134969325153375, 'mean_word_length': 1.900652431791222, 'pattern_counts': {'":': 4, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4540947', 'n_tokens_mistral': 2683, 'n_tokens_neox': 2564, 'n_words': 555}