Search in sources :

Example 11 with RuntimeException

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

the class TestInterpret10 method main.

public static void main(String[] args) {
    try {
        String[] codes = new String[] { "import \"sys.base\";\n" + "import \"sys.list\";\n" + "import \"sys.proc\";\n" + "\n" + "var pc_router = 5;\n" + "call g_start_share(\"pc_router\", pc_router);\n" + "\n" + "var pc = func ~(index) {\n" + "    var pc_router = call g_query_share(\"pc_router\");\n" + "    var name = \"pc_\" + index;\n" + "    var router = index / pc_router;\n" + "    var display = \"PC #\" + index;\n" + "    call g_printn(display + \" started\");\n" + "    call g_sleep(50);\n" + "    var handle = call g_create_pipe(name);\n" + "    call g_printn(display + \" connecting...\");\n" + "    var router_connection = \"router#\" + router;\n" + "    for (;;) {\n" + "        call g_sleep(100);\n" + "        call g_lock_share(router_connection);\n" + "        var connection = call g_query_share(router_connection);\n" + "        if (call g_is_null(connection)) {\n" + "            call g_unlock_share(router_connection);\n" + "            continue;\n" + "        }\n" + "        call g_printn(display + \" connecting to #\" + router);\n" + "        call g_array_add(connection, index);\n" + "        call g_unlock_share(router_connection);\n" + "        break;\n" + "    }\n" + "    var get_id = func ~(ch) {\n" + "        if (ch == '@') {\n" + "            call g_printn(display + \" connected to router #\" + router);\n" + "        }\n" + "    };\n" + "    call g_read_pipe(handle, get_id);\n" + "    call g_sleep(1000);\n" + "    call g_printn(display + \" stopped\");\n" + "};\n" + "\n" + "var router = func ~(index) {\n" + "    var pc_router = call g_query_share(\"pc_router\");\n" + "    var name = \"router_\" + index;\n" + "    var display = \"Router #\" + index;\n" + "    call g_printn(display + \" started\");\n" + "    var router_connection = \"router#\" + index;\n" + "    var connection = [];\n" + "    var list = [];\n" + "    call g_start_share(router_connection, connection);\n" + "    var connected = 0;\n" + "    var handle_pc = func ~(args) {\n" + "        var connected = call g_array_get(args, 0);\n" + "        var pc_router = call g_array_get(args, 1);\n" + "        var router_connection = call g_array_get(args, 2);\n" + "        var display = call g_array_get(args, 3);\n" + "        var list = call g_array_get(args, 4);\n" + "        for (;;) {\n" + "            call g_sleep(100);\n" + "            call g_lock_share(router_connection);\n" + "            var connection = call g_query_share(router_connection);\n" + "            var new_pc = call g_array_pop(connection);\n" + "            if (call g_is_null(new_pc)) {\n" + "                call g_unlock_share(router_connection);\n" + "                continue;\n" + "            }\n" + "            connected++;\n" + "            call g_array_add(list, new_pc);\n" + "            call g_printn(display + \" connecting to pc #\" + new_pc);\n" + "            call g_unlock_share(router_connection);\n" + "            var name = \"pc_\" + new_pc;\n" + "            var handle = call g_create_pipe(name);\n" + "            call g_destroy_pipe(handle);\n" + "            call g_printn(display + \" connected to #\" + new_pc);\n" + "            if (connected == pc_router) { break; }\n" + "        }\n" + "    };\n" + "    var args = [];\n" + "    call g_array_add(args, connected);\n" + "    call g_array_add(args, pc_router);\n" + "    call g_array_add(args, router_connection);\n" + "    call g_array_add(args, display);\n" + "    call g_array_add(args, list);\n" + "    call g_join_process(call g_create_process_args(handle_pc, args));\n" + "    var stop_pc = func ~(args) {\n" + "        var display = call g_array_get(args, 3);\n" + "        var list = call g_array_get(args, 4);\n" + "        var size = call g_array_size(list);\n" + "        for (var i = 0; i < size; i++) {\n" + "            call g_sleep(10);\n" + "            var name = \"pc_\" + call g_array_get(list, i);\n" + "            var handle = call g_create_pipe(name);\n" + "            call g_write_pipe(handle, \"!\");\n" + "            call g_printn(display + \" disconnected with #\" + i);\n" + "        }\n" + "    };\n" + "    call g_sleep(100);\n" + "    call g_join_process(call g_create_process_args(stop_pc, args));\n" + "    call g_printn(display + \" stopped\");\n" + "};\n" + "\n" + "var create_pc = func ~(n) {\n" + "    var handles = [];\n" + "    foreach (var i : call g_range(0, n - 1)) {\n" + "        var h = call g_create_process_args(pc, i);\n" + "        call g_array_add(handles, h);\n" + "        call g_printn(\"Create pc: #\" + i);\n" + "    }\n" + "    return handles;\n" + "};\n" + "var create_router = func ~(n) {\n" + "    var handles = [];\n" + "    foreach (var i : call g_range(0, n - 1)) {\n" + "        var h = call g_create_process_args(router, i);\n" + "        call g_array_add(handles, h);\n" + "        call g_printn(\"Create router: #\" + i);\n" + "    }\n" + "    return handles;\n" + "};\n" + "\n" + "call g_printn(\"Starting pc...\");\n" + "var pcs = call create_pc(5);\n" + "call g_printn(\"Starting router...\");\n" + "var routers = call create_router(1);\n" + "call g_join_process_array(pcs);\n" + "call g_join_process_array(routers);\n" + "\n" };
        System.out.println(codes[codes.length - 1]);
        Interpreter interpreter = new Interpreter();
        Grammar grammar = new Grammar(codes[codes.length - 1]);
        // System.out.println(grammar.toString());
        RuntimeCodePage page = grammar.getCodePage();
        // System.out.println(page.toString());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        RuntimeCodePage.exportFromStream(page, baos);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        interpreter.run("test_1", bais);
    } catch (RegexException e) {
        System.err.println();
        System.err.println(e.getPosition() + "," + e.getMessage());
        e.printStackTrace();
    } catch (SyntaxException e) {
        System.err.println();
        System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
        e.printStackTrace();
    } catch (RuntimeException e) {
        System.err.println();
        System.err.println(e.getPosition() + ": " + e.getInfo());
        e.printStackTrace();
    } catch (Exception e) {
        System.err.println();
        System.err.println(e.getMessage());
        e.printStackTrace();
    }
}
Also used : Interpreter(priv.bajdcc.LALR1.interpret.Interpreter) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) ByteArrayInputStream(java.io.ByteArrayInputStream) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RegexException(priv.bajdcc.util.lexer.error.RegexException) Grammar(priv.bajdcc.LALR1.grammar.Grammar) RuntimeCodePage(priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) RegexException(priv.bajdcc.util.lexer.error.RegexException)

Example 12 with RuntimeException

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

the class UIMainFrame method startOS.

private void startOS(UIGraphics g) {
    IOSCodePage[] pages = new IOSCodePage[] { // OS
    new OSEntry(), new OSIrq(), new OSTask(), // IRQ
    new IRPrint(), new IRRemote(), new IRTask(), new IRSignal(), // TASK
    new TKSystem(), new TKUtil(), new TKUI(), new TKNet(), // UI
    new UIMain(), new UIClock(), new UIHitokoto(), new UIMonitor(), // USER
    new UserMain(), // USER ROUTINE
    new URShell(), new UREcho(), new URPipe(), new URDup(), new URGrep(), new URRange(), new URProc(), new URTask(), new URSleep(), new URTime(), new URCount(), new URTest(), new URMsg(), new URNews(), new URBash(), new URReplace(), new URUtil(), new URAI(), // USER FILE
    new URFileLoad(), new URFileSave(), new URFileAppend() };
    try {
        String code = "import \"sys.base\";\n" + "import \"sys.proc\";\n" + "call g_load_sync_x(\"/kern/entry\");\n";
        interpreter = new Interpreter();
        for (IOSCodePage page : pages) {
            interpreter.load(page);
        }
        Grammar grammar = new Grammar(code);
        // System.out.println(grammar.toString());
        RuntimeCodePage page = grammar.getCodePage();
        // System.out.println(page.toString());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        RuntimeCodePage.exportFromStream(page, baos);
        ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
        interpreter.run("@main", bais);
    } catch (RegexException e) {
        System.err.println();
        System.err.println(e.getPosition() + "," + e.getMessage());
        e.printStackTrace();
    } catch (SyntaxException e) {
        System.err.println();
        System.err.println(String.format("模块名:%s. 位置:%s. 错误:%s-%s(%s:%d)", e.getPageName(), e.getPosition(), e.getMessage(), e.getInfo(), e.getFileName(), e.getPosition().iLine + 1));
        e.printStackTrace();
    } catch (RuntimeException e) {
        System.err.println();
        System.err.println(e.getkError().getMessage() + " " + e.getPosition() + ": " + e.getInfo());
        e.printStackTrace();
    } catch (Exception e) {
        System.err.println();
        System.err.println(e.getMessage());
        e.printStackTrace();
    }
}
Also used : OSTask(priv.bajdcc.LALR1.interpret.os.kern.OSTask) IOSCodePage(priv.bajdcc.LALR1.interpret.os.IOSCodePage) URFileSave(priv.bajdcc.LALR1.interpret.os.user.routine.file.URFileSave) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) UIHitokoto(priv.bajdcc.LALR1.interpret.os.ui.UIHitokoto) UserMain(priv.bajdcc.LALR1.interpret.os.user.UserMain) URFileAppend(priv.bajdcc.LALR1.interpret.os.user.routine.file.URFileAppend) IRPrint(priv.bajdcc.LALR1.interpret.os.irq.IRPrint) ByteArrayInputStream(java.io.ByteArrayInputStream) TKUtil(priv.bajdcc.LALR1.interpret.os.task.TKUtil) UIClock(priv.bajdcc.LALR1.interpret.os.ui.UIClock) IRSignal(priv.bajdcc.LALR1.interpret.os.irq.IRSignal) OSEntry(priv.bajdcc.LALR1.interpret.os.kern.OSEntry) TKSystem(priv.bajdcc.LALR1.interpret.os.task.TKSystem) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) UIMain(priv.bajdcc.LALR1.interpret.os.ui.UIMain) Interpreter(priv.bajdcc.LALR1.interpret.Interpreter) OSIrq(priv.bajdcc.LALR1.interpret.os.kern.OSIrq) TKNet(priv.bajdcc.LALR1.interpret.os.task.TKNet) IRRemote(priv.bajdcc.LALR1.interpret.os.irq.IRRemote) Grammar(priv.bajdcc.LALR1.grammar.Grammar) RuntimeCodePage(priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TKUI(priv.bajdcc.LALR1.interpret.os.task.TKUI) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) RegexException(priv.bajdcc.util.lexer.error.RegexException) UIMonitor(priv.bajdcc.LALR1.interpret.os.ui.UIMonitor) IRTask(priv.bajdcc.LALR1.interpret.os.irq.IRTask) URFileLoad(priv.bajdcc.LALR1.interpret.os.user.routine.file.URFileLoad) RegexException(priv.bajdcc.util.lexer.error.RegexException)

Example 13 with RuntimeException

use of priv.bajdcc.LALR1.grammar.runtime.RuntimeException 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 14 with RuntimeException

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

the class RuntimeMachine method opLoadFunc.

@Override
public void opLoadFunc() throws RuntimeException {
    int idx = loadInt();
    RuntimeFuncObject func = new RuntimeFuncObject(pageName, idx);
    RuntimeObject obj = new RuntimeObject(func);
    int envSize = loadInt();
    FOR_LOOP: for (int i = 0; i < envSize; i++) {
        int id = loadInt();
        if (id == -1) {
            id = loadInt();
            String name = fetchFromGlobalData(id).getObj().toString();
            IRuntimeDebugValue value = currentPage.getInfo().getValueCallByName(name);
            if (value != null) {
                func.addEnv(id, value.getRuntimeObject());
                continue;
            }
            int index = currentPage.getInfo().getAddressOfExportFunc(name);
            if (index != -1) {
                func.addEnv(id, new RuntimeObject(new RuntimeFuncObject(pageName, index)));
                continue;
            }
            List<RuntimeCodePage> refers = pageRefer.get(currentPage.getInfo().getDataMap().get("name").toString());
            for (RuntimeCodePage page : refers) {
                value = page.getInfo().getValueCallByName(name);
                if (value != null) {
                    func.addEnv(id, value.getRuntimeObject());
                    continue FOR_LOOP;
                }
                index = page.getInfo().getAddressOfExportFunc(name);
                if (index != -1) {
                    func.addEnv(id, new RuntimeObject(new RuntimeFuncObject(page.getInfo().getDataMap().get("name").toString(), index)));
                    continue FOR_LOOP;
                }
            }
            err(RuntimeError.WRONG_LOAD_EXTERN, name);
        } else {
            func.addEnv(id, stack.findVariable(func.getPage(), id));
        }
    }
    stack.pushData(obj);
}
Also used : RuntimeFuncObject(priv.bajdcc.LALR1.grammar.runtime.data.RuntimeFuncObject)

Example 15 with RuntimeException

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

the class RuntimeMachine method opMap.

@Override
public void opMap() throws RuntimeException {
    int size = current();
    next();
    RuntimeMap map = new RuntimeMap();
    for (int i = 0; i < size; i++) {
        map.put(loadString(), load());
    }
    stack.pushData(new RuntimeObject(map));
}
Also used : RuntimeMap(priv.bajdcc.LALR1.grammar.runtime.data.RuntimeMap)

Aggregations

Grammar (priv.bajdcc.LALR1.grammar.Grammar)17 RuntimeException (priv.bajdcc.LALR1.grammar.runtime.RuntimeException)17 RuntimeCodePage (priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage)16 RegexException (priv.bajdcc.util.lexer.error.RegexException)16 SyntaxException (priv.bajdcc.LALR1.syntax.handler.SyntaxException)15 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 Interpreter (priv.bajdcc.LALR1.interpret.Interpreter)14 RuntimeMachine (priv.bajdcc.LALR1.grammar.runtime.RuntimeMachine)2 RuntimeFuncObject (priv.bajdcc.LALR1.grammar.runtime.data.RuntimeFuncObject)2 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 BigInteger (java.math.BigInteger)1 Scanner (java.util.Scanner)1 IRuntimeDebugExec (priv.bajdcc.LALR1.grammar.runtime.IRuntimeDebugExec)1 IRuntimeStatus (priv.bajdcc.LALR1.grammar.runtime.IRuntimeStatus)1 RuntimeObject (priv.bajdcc.LALR1.grammar.runtime.RuntimeObject)1 RuntimeArray (priv.bajdcc.LALR1.grammar.runtime.data.RuntimeArray)1 RuntimeMap (priv.bajdcc.LALR1.grammar.runtime.data.RuntimeMap)1 IOSCodePage (priv.bajdcc.LALR1.interpret.os.IOSCodePage)1