All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.liuyehcf.framework.language.hua.cmd.Huac Maven / Gradle / Ivy

package com.github.liuyehcf.framework.language.hua.cmd;

import com.github.liuyehcf.framework.compile.engine.CompileResult;
import com.github.liuyehcf.framework.language.hua.compile.HuaCompiler;
import com.github.liuyehcf.framework.language.hua.core.IntermediateInfo;
import com.github.liuyehcf.framework.language.hua.core.io.HuaClassOutputStream;
import org.apache.commons.cli.ParseException;

import java.io.*;

import static com.github.liuyehcf.framework.common.tools.asserts.Assert.assertNotNull;
import static com.github.liuyehcf.framework.common.tools.asserts.Assert.assertTrue;
import static com.github.liuyehcf.framework.language.hua.core.io.HClassConstant.HCLASS_SUFFIX;

/**
 * Hua编译命令行工具
 *
 * @author hechenfeng
 * @date 2018/6/24
 */
public class Huac extends BaseCmd {

    /**
     * 源文件路径
     */
    private String filePath;

    /**
     * 文件名
     */
    private String fileName;

    /**
     * 输出目录,默认当前路径
     */
    private String targetPath = ".";

    /**
     * 编译器
     */
    private HuaCompiler huaCompiler;

    private Huac(String[] args) {
        super(args);
        registerOption("f", "source", false, true, "Source file path", (optValue) -> filePath = optValue);
        registerOption("d", "target", false, true, "Target directory path", (optValue) -> targetPath = optValue);
    }

    public static void main(String[] args) {
        Huac huac = new Huac(args);

        try {
            huac.init();
            huac.compile();
        } catch (Exception e) {
            System.err.println(e.getMessage());
            System.exit(0);
        }
    }

    private void init() throws ParseException {
        parseCmd();

        check();

        String[] segments = filePath.split(java.io.File.separator);
        this.fileName = segments[segments.length - 1].substring(0, segments[segments.length - 1].length() - 4);

        huaCompiler = HuaCompiler.getHuaCompiler();
    }

    private void check() {
        File file, target;

        assertNotNull(filePath == null, "Please input the source file path by -f option, use -help see more options");

        file = new File(filePath);
        target = new File(targetPath);


        assertTrue(file.exists(), "Source file '" + filePath + "' is not exist");
        assertTrue(file.isFile(), "Source file '" + filePath + "' is not file");

        assertTrue(target.exists(), "Target directory '" + targetPath + "' is not exist");
        assertTrue(target.isDirectory(), "Target directory '" + targetPath + "' is not directory");
    }

    private void compile() {
        String fileContent = loadContent();

        CompileResult result = huaCompiler.compile(fileContent);

        if (!result.isSuccess()) {
            System.err.println(result.getMessage());
            System.exit(1);
        }

        store(result.getResult());
    }

    private String loadContent() {
        StringBuilder sb = new StringBuilder();
        try {
            BufferedReader fileReader = new BufferedReader(new FileReader(filePath));
            String line;
            while ((line = fileReader.readLine()) != null) {
                sb.append(line)
                        .append('\n');
            }
            return sb.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private void store(IntermediateInfo intermediateInfo) {

        try (HuaClassOutputStream outputStream = new HuaClassOutputStream(new FileOutputStream(targetPath + File.separator + fileName + HCLASS_SUFFIX))) {

            outputStream.writeHClass(intermediateInfo);

        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy