Search in sources :

Example 1 with IRuntimeStatus

use of priv.bajdcc.LALR1.grammar.runtime.IRuntimeStatus in project jMiniLang by bajdcc.

the class TestGrammar3 method main.

public static void main(String[] args) {
    try {
        String a = "\n" + "var g_a = func ~(x, y) { return x + y;};\n" + "export \"g_a\";";
        String b = "import \"test1\";\n" + "var d = call g_a(1,2);\n" + "var c = g_gk;\n" + "call g_print(c);\n" + "var t = call g_print(c);";
        /*
			 * BufferedReader br = new BufferedReader(new
			 * FileReader("E:/http.c")); String line = ""; StringBuffer sb = new
			 * StringBuffer(); while ((line = br.readLine()) != null) {
			 * sb.append(line + System.lineSeparator()); }
			 * br.close();
			 */
        Grammar grammar = new Grammar(a);
        System.out.println(grammar.toString());
        RuntimeCodePage page = grammar.getCodePage();
        System.out.println(page.toString());
        RuntimeMachine machine = new RuntimeMachine();
        machine.run("test1", page);
        Grammar grammar2 = new Grammar(b);
        System.out.println(grammar2.toString());
        RuntimeCodePage page2 = grammar2.getCodePage();
        page2.getInfo().addExternalValue("g_gk", () -> new RuntimeObject("abc"));
        page2.getInfo().addExternalFunc("g_print", new IRuntimeDebugExec() {

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

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

            @Override
            public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
                System.out.println(args.get(0).getObj());
                return null;
            }
        });
        System.out.println(page2.toString());
        machine.run("test2", page2);
    // 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();
    } catch (RuntimeException e) {
        System.err.println(e.getPosition() + ": " + e.getInfo());
        e.printStackTrace();
    // } catch (IOException e) {
    // System.err.println(e.getMessage());
    // e.printStackTrace();
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    // } catch (IOException e) {
    // System.err.println(e.getMessage());
    // e.printStackTrace();
    }
}
Also used : RuntimeMachine(priv.bajdcc.LALR1.grammar.runtime.RuntimeMachine) Grammar(priv.bajdcc.LALR1.grammar.Grammar) RuntimeCodePage(priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage) IRuntimeStatus(priv.bajdcc.LALR1.grammar.runtime.IRuntimeStatus) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) RegexException(priv.bajdcc.util.lexer.error.RegexException) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) RuntimeObject(priv.bajdcc.LALR1.grammar.runtime.RuntimeObject) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RegexException(priv.bajdcc.util.lexer.error.RegexException) IRuntimeDebugExec(priv.bajdcc.LALR1.grammar.runtime.IRuntimeDebugExec)

Example 2 with IRuntimeStatus

use of priv.bajdcc.LALR1.grammar.runtime.IRuntimeStatus in project jMiniLang by bajdcc.

the class ModuleNet method buildRemoteMethods.

private void buildRemoteMethods(IRuntimeDebugInfo info) {
    info.addExternalFunc("g_net_get", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "HTTP GET";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            String text = "";
            String txt = String.valueOf(args.get(0).getObj());
            logger.debug("Request url: " + txt);
            try {
                URL url = new URL(txt);
                // 打开连接
                URLConnection urlConnection = url.openConnection();
                // 获取输入流
                BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8"));
                String line = null;
                StringBuilder sb = new StringBuilder();
                while ((line = br.readLine()) != null) {
                    sb.append(line).append("\n");
                }
                text = sb.toString();
                br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new RuntimeObject(text);
        }
    });
    info.addExternalFunc("g_net_get_json", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "HTTP GET - json";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            String text = "";
            String txt = String.valueOf(args.get(0).getObj());
            logger.debug("Request url(json): " + txt);
            try {
                URL url = new URL(txt);
                // 打开连接
                URLConnection urlConnection = url.openConnection();
                // 获取输入流
                BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8"));
                String line = null;
                StringBuilder sb = new StringBuilder();
                while ((line = br.readLine()) != null) {
                    sb.append(line).append("\n");
                }
                text = sb.toString();
                br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return parseJson(text);
        }
    });
    // -----------------------------------
    // server
    info.addExternalFunc("g_net_msg_create_server_internal", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG SERVER";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getServer() != null || getClient() != null)
                return new RuntimeObject(false);
            int port = ((BigInteger) args.get(0).getObj()).intValue();
            setServer(new ModuleNetServer(port));
            getServer().start();
            return new RuntimeObject(true);
        }
    });
    info.addExternalFunc("g_net_msg_shutdown_server", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG SERVER SHUTDOWN";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getServer() == null)
                return new RuntimeObject(false);
            getServer().exit();
            return new RuntimeObject(true);
        }
    });
    info.addExternalFunc("g_net_msg_get_server_status", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG SERVER GET STATUS";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getServer() != null) {
                ModuleNetServer.Status s = getServer().getStatus();
                if (s == ModuleNetServer.Status.ERROR) {
                    lastError = getServer().getError();
                    setServer(null);
                }
                status.getService().getProcessService().sleep(status.getPid(), MSG_QUERY_TIME);
                return new RuntimeObject(BigInteger.valueOf(s.ordinal()));
            }
            return new RuntimeObject(BigInteger.valueOf(ModuleNetServer.Status.NULL.ordinal()));
        }
    });
    // server
    // -----------------------------------
    // client
    info.addExternalFunc("g_net_msg_create_client_internal", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG CLIENT";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getServer() != null || getClient() != null)
                return new RuntimeObject(false);
            String addr = String.valueOf(args.get(0).getObj());
            setClient(new ModuleNetClient(addr));
            getClient().start();
            return new RuntimeObject(true);
        }
    });
    info.addExternalFunc("g_net_msg_shutdown_client", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG CLIENT SHUTDOWN";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getClient() == null)
                return new RuntimeObject(false);
            getClient().exit();
            return new RuntimeObject(true);
        }
    });
    info.addExternalFunc("g_net_msg_get_client_status", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG CLIENT GET STATUS";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getClient() != null) {
                ModuleNetClient.Status s = getClient().getStatus();
                if (s == ModuleNetClient.Status.ERROR) {
                    lastError = getClient().getError();
                    setClient(null);
                }
                status.getService().getProcessService().sleep(status.getPid(), MSG_QUERY_TIME);
                return new RuntimeObject(BigInteger.valueOf(s.ordinal()));
            }
            return new RuntimeObject(BigInteger.valueOf(ModuleNetClient.Status.NULL.ordinal()));
        }
    });
    info.addExternalFunc("g_net_msg_client_send", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG CLIENT SEND";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getClient() != null) {
                ModuleNetClient.Status s = getClient().getStatus();
                if (s == ModuleNetClient.Status.RUNNING) {
                    status.getService().getProcessService().sleep(status.getPid(), MSG_SEND_TIME);
                    getClient().send(String.valueOf(args.get(0).getObj()));
                    return new RuntimeObject(true);
                }
            }
            return new RuntimeObject(false);
        }
    });
    info.addExternalFunc("g_net_msg_client_send_with_origin", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG CLIENT SEND(ORIGIN)";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getClient() != null) {
                ModuleNetClient.Status s = getClient().getStatus();
                if (s == ModuleNetClient.Status.RUNNING) {
                    status.getService().getProcessService().sleep(status.getPid(), MSG_SEND_TIME);
                    getClient().send(String.valueOf(args.get(0).getObj()), String.valueOf(args.get(1).getObj()));
                    return new RuntimeObject(true);
                }
            }
            return new RuntimeObject(false);
        }
    });
    info.addExternalFunc("g_net_msg_server_send", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG SERVER SEND";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getServer() != null) {
                ModuleNetServer.Status s = getServer().getStatus();
                if (s == ModuleNetServer.Status.RUNNING) {
                    status.getService().getProcessService().sleep(status.getPid(), MSG_SEND_TIME);
                    getServer().send(String.valueOf(args.get(0).getObj()));
                    return new RuntimeObject(true);
                }
            }
            return new RuntimeObject(false);
        }
    });
    info.addExternalFunc("g_net_msg_server_send_error", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG SERVER SEND ERROR";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getServer() != null) {
                ModuleNetServer.Status s = getServer().getStatus();
                if (s == ModuleNetServer.Status.RUNNING) {
                    status.getService().getProcessService().sleep(status.getPid(), MSG_SEND_TIME);
                    getServer().send_error(String.valueOf(args.get(0).getObj()));
                    return new RuntimeObject(true);
                }
            }
            return new RuntimeObject(false);
        }
    });
    info.addExternalFunc("g_net_msg_server_send_with_origin", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG SERVER SEND(ORIGIN)";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            if (getServer() != null) {
                ModuleNetServer.Status s = getServer().getStatus();
                if (s == ModuleNetServer.Status.RUNNING) {
                    status.getService().getProcessService().sleep(status.getPid(), MSG_SEND_TIME);
                    getServer().send(String.valueOf(args.get(0).getObj()), String.valueOf(args.get(1).getObj()));
                    return new RuntimeObject(true);
                }
            }
            return new RuntimeObject(false);
        }
    });
    // client
    // -----------------------------------
    info.addExternalFunc("g_net_msg_get_error", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG SERVER GET ERROR";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            return new RuntimeObject(lastError);
        }
    });
    info.addExternalFunc("g_net_msg_get_server_msg", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG SERVER GET MESSAGE";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            return new RuntimeObject(getServer() == null ? null : getServer().getMessage());
        }
    });
    info.addExternalFunc("g_net_msg_get_client_msg", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG CLIENT GET MESSAGE";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            return new RuntimeObject(getClient() == null ? null : getClient().getMessage());
        }
    });
    info.addExternalFunc("g_net_msg_get_client_addr", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "MSG CLIENT GET ADDRESS";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            return new RuntimeObject(getClient() == null ? null : getClient().getAddr());
        }
    });
    info.addExternalFunc("g_net_parse_json", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "Parse json";
        }

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

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

        @Override
        public String getDoc() {
            return "RSS GET(BAIDU)";
        }

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

        @SuppressWarnings("unchecked")
        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
            String text = "";
            String txt = String.valueOf(args.get(0).getObj());
            logger.debug("Request url(rss): " + txt);
            RuntimeArray array = new RuntimeArray();
            final Pattern pattern = Pattern.compile("<br>(.*)<br />");
            try {
                SAXReader reader = new SAXReader();
                Document document = reader.read(txt);
                Node title = document.selectSingleNode("//title");
                array.add(new RuntimeObject(title.getText()));
                List<Node> list = document.selectNodes("//item");
                for (Node item : list) {
                    String itemTitle = item.valueOf("title");
                    array.add(new RuntimeObject(itemTitle));
                    String itemDescription = item.valueOf("description");
                    Matcher matcher = pattern.matcher(itemDescription);
                    if (matcher.find()) {
                        array.add(new RuntimeObject(matcher.group(1)));
                    } else {
                        array.add(new RuntimeObject(itemDescription.replace("<br />", "")));
                    }
                }
                return new RuntimeObject(array);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return new RuntimeObject(array);
        }
    });
}
Also used : Pattern(java.util.regex.Pattern) RuntimeArray(priv.bajdcc.LALR1.grammar.runtime.data.RuntimeArray) InputStreamReader(java.io.InputStreamReader) ModuleNetClient(priv.bajdcc.LALR1.interpret.module.net.ModuleNetClient) Matcher(java.util.regex.Matcher) SAXReader(org.dom4j.io.SAXReader) Node(org.dom4j.Node) Document(org.dom4j.Document) URL(java.net.URL) URLConnection(java.net.URLConnection) BufferedReader(java.io.BufferedReader) List(java.util.List) ModuleNetServer(priv.bajdcc.LALR1.interpret.module.net.ModuleNetServer)

Example 3 with IRuntimeStatus

use of priv.bajdcc.LALR1.grammar.runtime.IRuntimeStatus in project jMiniLang by bajdcc.

the class ModuleProc method buildMethod.

private void buildMethod(IRuntimeDebugInfo info) {
    info.addExternalFunc("g_create_process", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "创建进程";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            RuntimeFuncObject func = (RuntimeFuncObject) args.get(0).getObj();
            return new RuntimeObject(BigInteger.valueOf(status.createProcess(func)));
        }
    });
    info.addExternalFunc("g_create_process_args", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "创建进程带参数";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            RuntimeFuncObject func = (RuntimeFuncObject) args.get(0).getObj();
            RuntimeObject obj = args.get(1);
            return new RuntimeObject(BigInteger.valueOf(status.createProcess(func, obj)));
        }
    });
    info.addExternalFunc("g_create_user_process", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "创建用户态进程";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            RuntimeFuncObject func = (RuntimeFuncObject) args.get(0).getObj();
            return new RuntimeObject(BigInteger.valueOf(status.createUsrProcess(func)));
        }
    });
    info.addExternalFunc("g_create_user_process_args", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "创建用户态进程带参数";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            RuntimeFuncObject func = (RuntimeFuncObject) args.get(0).getObj();
            RuntimeObject obj = args.get(1);
            return new RuntimeObject(BigInteger.valueOf(status.createUsrProcess(func, obj)));
        }
    });
    info.addExternalFunc("g_get_user_procs", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "获取用户态进程ID列表";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            RuntimeArray arr = new RuntimeArray();
            List<Integer> list = status.getUsrProcs();
            for (Integer pid : list) {
                arr.add(new RuntimeObject(pid));
            }
            return new RuntimeObject(arr);
        }
    });
    info.addExternalFunc("g_get_pid", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "获取进程ID";
        }

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

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

        @Override
        public String getDoc() {
            return "获取父进程ID";
        }

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

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

        @Override
        public String getDoc() {
            return "获取进程优先级";
        }

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

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

        @Override
        public String getDoc() {
            return "设置进程优先级";
        }

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

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

        @Override
        public String getDoc() {
            return "进程等待";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            BigInteger pid = (BigInteger) args.get(0).getObj();
            return new RuntimeObject(status.getService().getProcessService().join(pid.intValue(), status.getPid()));
        }
    });
    info.addExternalFunc("g_live_process", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "进程存活";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            BigInteger pid = (BigInteger) args.get(0).getObj();
            return new RuntimeObject(status.getService().getProcessService().live(pid.intValue()));
        }
    });
    info.addExternalFunc("g_sleep", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "进程睡眠";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            BigInteger turn = (BigInteger) args.get(0).getObj();
            int time = turn.intValue();
            return new RuntimeObject(BigInteger.valueOf(status.getService().getProcessService().sleep(status.getPid(), time > 0 ? time : 0)));
        }
    });
    info.addExternalFunc("g_query_usr_proc", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "枚举用户态进程";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            return new RuntimeObject(ProcInfoHelper.getProcInfo(status, status.getUsrProcs()));
        }
    });
    info.addExternalFunc("g_query_sys_proc", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "枚举内核态进程";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            return new RuntimeObject(ProcInfoHelper.getProcInfo(status, status.getSysProcs()));
        }
    });
    info.addExternalFunc("g_query_all_proc", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "枚举进程";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            return new RuntimeObject(ProcInfoHelper.getProcInfoAll(status));
        }
    });
    info.addExternalFunc("g_set_process_desc", 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 {
            status.setProcDesc(String.valueOf(args.get(0).getObj()));
            return null;
        }
    });
}
Also used : BigInteger(java.math.BigInteger) RuntimeArray(priv.bajdcc.LALR1.grammar.runtime.data.RuntimeArray) BigInteger(java.math.BigInteger) RuntimeFuncObject(priv.bajdcc.LALR1.grammar.runtime.data.RuntimeFuncObject) List(java.util.List) ArrayList(java.util.ArrayList) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException)

Example 4 with IRuntimeStatus

use of priv.bajdcc.LALR1.grammar.runtime.IRuntimeStatus in project jMiniLang by bajdcc.

the class ModuleString method buildStringUtils.

private void buildStringUtils(IRuntimeDebugInfo info) {
    info.addExternalFunc("g_string_replace", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "字符串替换";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            String str = (String) args.get(0).getObj();
            String pat = (String) args.get(1).getObj();
            String sub = (String) args.get(2).getObj();
            RuntimeArray arr = new RuntimeArray();
            return new RuntimeObject(str.replaceAll(pat, sub));
        }
    });
    info.addExternalFunc("g_string_split", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "字符串分割";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            String str = (String) args.get(0).getObj();
            String split = (String) args.get(1).getObj();
            RuntimeArray arr = new RuntimeArray();
            for (String item : str.split(split, Integer.MAX_VALUE)) {
                arr.add(new RuntimeObject(item));
            }
            return new RuntimeObject(arr);
        }
    });
    info.addExternalFunc("g_string_splitn", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "字符串分割";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            String str = (String) args.get(0).getObj();
            String split = (String) args.get(1).getObj();
            int n = (int) args.get(1).getObj();
            RuntimeArray arr = new RuntimeArray();
            for (String item : str.split(split, n)) {
                arr.add(new RuntimeObject(item));
            }
            return new RuntimeObject(arr);
        }
    });
    info.addExternalFunc("g_string_trim", 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 {
            String str = (String) args.get(0).getObj();
            return new RuntimeObject(str.trim());
        }
    });
    info.addExternalFunc("g_string_length", 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 {
            String str = (String) args.get(0).getObj();
            return new RuntimeObject(BigInteger.valueOf(str.length()));
        }
    });
    info.addExternalFunc("g_string_empty", 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(args.get(0).getObj().toString().isEmpty());
        }
    });
    info.addExternalFunc("g_string_get", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "字符串查询";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            String str = (String) args.get(0).getObj();
            BigInteger index = (BigInteger) args.get(1).getObj();
            return new RuntimeObject(str.charAt(index.intValue()));
        }
    });
    info.addExternalFunc("g_string_regex", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "字符串正则匹配";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            String str = (String) args.get(0).getObj();
            String regex = (String) args.get(1).getObj();
            Matcher m = Pattern.compile(regex).matcher(str);
            RuntimeArray arr = new RuntimeArray();
            while (m.find()) {
                arr.add(new RuntimeObject(m.group()));
            }
            return new RuntimeObject(arr);
        }
    });
    info.addExternalFunc("g_string_build", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "从字节数组构造字符串";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            RuntimeArray array = (RuntimeArray) args.get(0).getObj();
            StringBuilder sb = new StringBuilder();
            for (Object obj : array.toList()) {
                sb.append(obj);
            }
            return new RuntimeObject(sb.toString());
        }
    });
    info.addExternalFunc("g_string_atoi", 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 {
            String str = (String) args.get(0).getObj();
            try {
                BigInteger bi = new BigInteger(str);
                return new RuntimeObject(new BigInteger(str));
            } catch (NumberFormatException e) {
                return new RuntimeObject(BigInteger.valueOf(-1));
            }
        }
    });
    info.addExternalFunc("g_string_atoi_s", 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 {
            String str = (String) args.get(0).getObj();
            try {
                BigInteger bi = new BigInteger(str);
                return new RuntimeObject(new BigInteger(str));
            } catch (NumberFormatException e) {
                return null;
            }
        }
    });
    info.addExternalFunc("g_string_join_array", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "字符串数组连接";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            RuntimeArray arr = (RuntimeArray) args.get(0).getObj();
            String delim = (String) args.get(1).getObj();
            return new RuntimeObject(String.join(delim, arr.toStringList()));
        }
    });
    info.addExternalFunc("g_string_toupper", 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 {
            String delim = (String) args.get(0).getObj();
            return new RuntimeObject(delim.toUpperCase());
        }
    });
    info.addExternalFunc("g_string_tolower", 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 {
            String delim = (String) args.get(0).getObj();
            return new RuntimeObject(delim.toLowerCase());
        }
    });
    info.addExternalFunc("g_string_rep", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "字符串重复构造";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            String str = (String) args.get(0).getObj();
            BigInteger dup = (BigInteger) args.get(1).getObj();
            int n = dup.intValue();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < n; i++) {
                sb.append(str);
            }
            return new RuntimeObject(sb.toString());
        }
    });
    info.addExternalFunc("g_string_to_number", 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 {
            String str = (String) args.get(0).getObj();
            try {
                return new RuntimeObject(new BigInteger(str));
            } catch (Exception e1) {
                try {
                    return new RuntimeObject(new BigDecimal(str));
                } catch (Exception e2) {
                    return null;
                }
            }
        }
    });
    info.addExternalFunc("g_string_equal", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "字符串相等比较";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            if (args.get(0).getType() == RuntimeObjectType.kString) {
                String str = (String) args.get(0).getObj();
                String cmp = (String) args.get(1).getObj();
                return new RuntimeObject(str.compareTo(cmp) == 0);
            }
            return new RuntimeObject(false);
        }
    });
    info.addExternalFunc("g_string_not_equal", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "字符串不等比较";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            if (args.get(0).getType() == RuntimeObjectType.kString) {
                String str = (String) args.get(0).getObj();
                String cmp = (String) args.get(1).getObj();
                return new RuntimeObject(str.compareTo(cmp) != 0);
            }
            return new RuntimeObject(true);
        }
    });
    info.addExternalFunc("g_string_start_with", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "字符串开头比较";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            String str = (String) args.get(0).getObj();
            String cmp = (String) args.get(1).getObj();
            return new RuntimeObject(str.startsWith(cmp));
        }
    });
    info.addExternalFunc("g_string_end_with", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "字符串结尾比较";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            String str = (String) args.get(0).getObj();
            String cmp = (String) args.get(1).getObj();
            return new RuntimeObject(str.endsWith(cmp));
        }
    });
    info.addExternalFunc("g_string_substr", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "字符串子串";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            String str = (String) args.get(0).getObj();
            BigInteger a = (BigInteger) args.get(1).getObj();
            BigInteger b = (BigInteger) args.get(2).getObj();
            return new RuntimeObject(str.substring(a.intValue(), b.intValue()));
        }
    });
    info.addExternalFunc("g_string_left", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "字符串左子串";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            String str = (String) args.get(0).getObj();
            BigInteger a = (BigInteger) args.get(1).getObj();
            return new RuntimeObject(str.substring(0, a.intValue()));
        }
    });
    info.addExternalFunc("g_string_right", new IRuntimeDebugExec() {

        @Override
        public String getDoc() {
            return "字符串右子串";
        }

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

        @Override
        public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) throws Exception {
            String str = (String) args.get(0).getObj();
            BigInteger a = (BigInteger) args.get(1).getObj();
            return new RuntimeObject(str.substring(a.intValue()));
        }
    });
}
Also used : RuntimeArray(priv.bajdcc.LALR1.grammar.runtime.data.RuntimeArray) Matcher(java.util.regex.Matcher) BigDecimal(java.math.BigDecimal) BigInteger(java.math.BigInteger)

Example 5 with IRuntimeStatus

use of priv.bajdcc.LALR1.grammar.runtime.IRuntimeStatus 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)

Aggregations

BigInteger (java.math.BigInteger)4 RuntimeException (priv.bajdcc.LALR1.grammar.runtime.RuntimeException)4 RuntimeArray (priv.bajdcc.LALR1.grammar.runtime.data.RuntimeArray)4 BufferedReader (java.io.BufferedReader)2 List (java.util.List)2 Matcher (java.util.regex.Matcher)2 Grammar (priv.bajdcc.LALR1.grammar.Grammar)2 FileReader (java.io.FileReader)1 InputStreamReader (java.io.InputStreamReader)1 BigDecimal (java.math.BigDecimal)1 URL (java.net.URL)1 URLConnection (java.net.URLConnection)1 ArrayList (java.util.ArrayList)1 Pattern (java.util.regex.Pattern)1 Document (org.dom4j.Document)1 Node (org.dom4j.Node)1 SAXReader (org.dom4j.io.SAXReader)1 IRuntimeDebugExec (priv.bajdcc.LALR1.grammar.runtime.IRuntimeDebugExec)1 IRuntimeStatus (priv.bajdcc.LALR1.grammar.runtime.IRuntimeStatus)1 RuntimeCodePage (priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage)1