Search in sources :

Example 26 with Grammar

use of priv.bajdcc.LL1.grammar.Grammar in project jMiniLang by bajdcc.

the class ModuleBase method getCodePage.

@Override
public RuntimeCodePage getCodePage() throws Exception {
    if (runtimeCodePage != null)
        return runtimeCodePage;
    String base = ResourceLoader.load(getClass());
    Grammar grammar = new Grammar(base);
    RuntimeCodePage page = grammar.getCodePage();
    IRuntimeDebugInfo info = page.getInfo();
    info.addExternalValue("g_null", () -> new RuntimeObject(null));
    final BigInteger MINUS_ONE = new BigInteger("-1");
    info.addExternalValue("g_minus_1", () -> new RuntimeObject(MINUS_ONE));
    info.addExternalValue("g_true", () -> new RuntimeObject(true));
    info.addExternalValue("g_false", () -> new RuntimeObject(false));
    final String NEWLINE = System.lineSeparator();
    info.addExternalValue("g_endl", () -> new RuntimeObject(NEWLINE));
    info.addExternalValue("g_nullptr", () -> new RuntimeObject(-1));
    info.addExternalFunc("g_is_null", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "判断是否为空";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kObject };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            return new RuntimeObject(args.get(0).getObj() == null);
        }
    });
    info.addExternalFunc("g_is_valid_handle", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "判断句柄合法";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kPtr };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            return new RuntimeObject(((int) args.get(0).getObj()) >= 0);
        }
    });
    info.addExternalFunc("g_set_flag", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "设置对象属性";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kObject, RuntimeObjectType.kInt };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            BigInteger flag = (BigInteger) args.get(1).getObj();
            args.get(0).setFlag(flag.longValue());
            return args.get(0);
        }
    });
    info.addExternalFunc("g_get_flag", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "获取对象属性";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kObject };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            return new RuntimeObject(BigInteger.valueOf(args.get(0).getFlag()));
        }
    });
    info.addExternalFunc("g_is_flag", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "判断对象属性";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kObject, RuntimeObjectType.kInt };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            BigInteger flag = (BigInteger) args.get(1).getObj();
            return new RuntimeObject(args.get(0).getFlag() == flag.longValue());
        }
    });
    info.addExternalFunc("g_set_debug", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "输出调试信息";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kBool };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            boolean debug = (boolean) args.get(0).getObj();
            status.getService().getProcessService().setDebug(status.getPid(), debug);
            return null;
        }
    });
    info.addExternalFunc("g_not_null", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "判断是否有效";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kObject };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            return new RuntimeObject(args.get(0).getObj() != null);
        }
    });
    info.addExternalFunc("g_print", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "标准输出";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kObject };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            logger.info(args.get(0).getObj());
            return null;
        }
    });
    info.addExternalFunc("g_print_info", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "标准输出";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kObject };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            logger.info(args.get(0));
            return null;
        }
    });
    info.addExternalFunc("g_printn", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "标准输出并换行";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kObject };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            Object[] info = status.getProcInfo();
            logger.info(String.format("#%03d [%s] %s", status.getPid(), info[2], args.get(0).getObj()));
            return null;
        }
    });
    info.addExternalFunc("g_printdn", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "调试输出并换行";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kObject };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            Object[] info = status.getProcInfo();
            logger.debug(String.format("#%03d [%s] %s", status.getPid(), info[2], args.get(0).getObj()));
            return null;
        }
    });
    info.addExternalFunc("g_print_err", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "错误输出";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kObject };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            logger.error(args.get(0).getObj());
            return null;
        }
    });
    info.addExternalFunc("g_to_string", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "将对象转换成字符串";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kObject };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            RuntimeObject obj = args.get(0);
            if (obj == null) {
                return new RuntimeObject(null);
            }
            return new RuntimeObject(String.valueOf(args.get(0).getObj()));
        }
    });
    info.addExternalFunc("g_new", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "深拷贝";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kObject };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            return args.get(0).clone();
        }
    });
    info.addExternalFunc("g_doc", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "文档";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kString };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            return new RuntimeObject(status.getHelpString(args.get(0).getObj().toString()));
        }
    });
    info.addExternalFunc("g_get_type", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "获取类型";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kObject };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            return new RuntimeObject(args.get(0).getTypeName());
        }
    });
    info.addExternalFunc("g_get_type_ordinal", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "获取类型(索引)";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kObject };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            return new RuntimeObject(BigInteger.valueOf(args.get(0).getTypeIndex()));
        }
    });
    info.addExternalFunc("g_type", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "获取类型";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kObject };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            return new RuntimeObject(args.get(0).getTypeString());
        }
    });
    info.addExternalFunc("g_hash", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "获取哈希";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kObject };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            if (args.get(0).getObj() == null)
                return new RuntimeObject("NULL");
            return new RuntimeObject(String.valueOf(args.get(0).getObj().hashCode()));
        }
    });
    info.addExternalFunc("g_exit", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "程序退出";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return null;
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            throw new RuntimeException(RuntimeError.EXIT, -1, "用户自行退出");
        }
    });
    info.addExternalFunc("g_load", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "载入并运行程序";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kString };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            return new RuntimeObject(BigInteger.valueOf(status.runProcess(args.get(0).getObj().toString())));
        }
    });
    info.addExternalFunc("g_load_x", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "载入并运行程序";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kString };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            return new RuntimeObject(BigInteger.valueOf(status.runProcessX(args.get(0).getObj().toString())));
        }
    });
    info.addExternalFunc("g_load_user", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "载入并运行用户态程序";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kString };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            return new RuntimeObject(BigInteger.valueOf(status.runUsrProcess(args.get(0).getObj().toString())));
        }
    });
    info.addExternalFunc("g_load_user_x", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "载入并运行用户态程序";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kString };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            return new RuntimeObject(BigInteger.valueOf(status.runUsrProcessX(args.get(0).getObj().toString())));
        }
    });
    info.addExternalFunc("g_print_file", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "载入并运行程序";
        }

        @Override
        public RuntimeObjectType[] getArgsType() {
            return new RuntimeObjectType[] { RuntimeObjectType.kString };
        }

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            try {
                BufferedReader br = new BufferedReader(new FileReader(args.get(0).getObj().toString()));
                String line;
                while ((line = br.readLine()) != null) {
                    System.out.println(line);
                }
                br.close();
            } catch (Exception e) {
                System.err.println(e.getMessage());
            }
            return null;
        }
    });
    buildIORead(info);
    return runtimeCodePage = page;
}
Also used : Grammar(priv.bajdcc.LALR1.grammar.Grammar) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) BufferedReader(java.io.BufferedReader) BigInteger(java.math.BigInteger) FileReader(java.io.FileReader)

Example 27 with Grammar

use of priv.bajdcc.LL1.grammar.Grammar in project jMiniLang by bajdcc.

the class RuntimeMachine method runUsrProcess.

@Override
public int runUsrProcess(String name) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader(name));
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = br.readLine()) != null) {
        sb.append(line);
        sb.append(System.lineSeparator());
    }
    br.close();
    Grammar grammar = new Grammar(sb.toString());
    return process.createProcess(pid, false, name, grammar.getCodePage(), 0, null);
}
Also used : BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Grammar(priv.bajdcc.LALR1.grammar.Grammar)

Example 28 with Grammar

use of priv.bajdcc.LL1.grammar.Grammar in project jMiniLang by bajdcc.

the class RuntimeMachine method runPage.

@Override
public void runPage(String name) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader(name));
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = br.readLine()) != null) {
        sb.append(line);
        sb.append(System.lineSeparator());
    }
    br.close();
    logger.debug("Loading file: " + name);
    Grammar grammar = new Grammar(sb.toString());
    run(name, grammar.getCodePage());
}
Also used : BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Grammar(priv.bajdcc.LALR1.grammar.Grammar)

Example 29 with Grammar

use of priv.bajdcc.LL1.grammar.Grammar in project jMiniLang by bajdcc.

the class TestGrammar method main.

public static void main(String[] args) {
    try {
        // String expr = "void main(int argc, char** argv) {return 0;}";
        // BufferedReader br = new BufferedReader(new FileReader("E:/http.c"));
        BufferedReader br = new BufferedReader(new FileReader("E:/a.c"));
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line).append(System.lineSeparator());
        }
        br.close();
        Grammar grammar = new Grammar(sb.toString());
        // System.out.println(grammar.getNGAString());
        System.out.println(grammar.getNPAString());
        System.out.println(grammar.getInst());
        System.out.println(grammar.getTrackerError());
        System.out.println(grammar.getTokenList());
        System.out.println(grammar.getObject());
    // FileWriter fw = new FileWriter("E:/testgrammar.txt");
    // fw.append(grammar.toString());
    // fw.close();
    } catch (RegexException e) {
        System.err.println(e.getPosition() + "," + e.getMessage());
        e.printStackTrace();
    } catch (SyntaxException e) {
        System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    }
}
Also used : SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RegexException(priv.bajdcc.util.lexer.error.RegexException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Grammar(priv.bajdcc.LALR1.grammar.Grammar) IOException(java.io.IOException)

Example 30 with Grammar

use of priv.bajdcc.LL1.grammar.Grammar in project jMiniLang by bajdcc.

the class ModuleRemote method getCodePage.

@Override
public RuntimeCodePage getCodePage() throws Exception {
    if (runtimeCodePage != null)
        return runtimeCodePage;
    String base = ResourceLoader.load(getClass());
    Grammar grammar = new Grammar(base);
    RuntimeCodePage page = grammar.getCodePage();
    IRuntimeDebugInfo info = page.getInfo();
    buildRemoteMethods(info);
    return runtimeCodePage = page;
}
Also used : Grammar(priv.bajdcc.LALR1.grammar.Grammar)

Aggregations

Grammar (priv.bajdcc.LALR1.grammar.Grammar)37 RuntimeCodePage (priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage)21 RegexException (priv.bajdcc.util.lexer.error.RegexException)20 RuntimeException (priv.bajdcc.LALR1.grammar.runtime.RuntimeException)17 SyntaxException (priv.bajdcc.LALR1.syntax.handler.SyntaxException)17 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 Interpreter (priv.bajdcc.LALR1.interpret.Interpreter)14 BufferedReader (java.io.BufferedReader)5 FileReader (java.io.FileReader)5 IRuntimeDebugInfo (priv.bajdcc.LALR1.grammar.runtime.IRuntimeDebugInfo)5 RuntimeMachine (priv.bajdcc.LALR1.grammar.runtime.RuntimeMachine)2 RuntimeObject (priv.bajdcc.LALR1.grammar.runtime.RuntimeObject)2 Grammar (priv.bajdcc.LL1.grammar.Grammar)2 GrammarException (priv.bajdcc.LL1.grammar.error.GrammarException)2 SyntaxException (priv.bajdcc.LL1.syntax.handler.SyntaxException)2 IOException (java.io.IOException)1 BigInteger (java.math.BigInteger)1 Scanner (java.util.Scanner)1 IRuntimeDebugExec (priv.bajdcc.LALR1.grammar.runtime.IRuntimeDebugExec)1