Search in sources :

Example 16 with SyntaxException

use of priv.bajdcc.LALR1.syntax.handler.SyntaxException 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 17 with SyntaxException

use of priv.bajdcc.LALR1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.

the class TestSyntax method main.

public static void main(String[] args) {
    // System.out.println("Z -> `a`<,> | B | [`a` `b` Z B]");
    try {
        // Scanner scanner = new Scanner(System.in);
        Syntax syntax = new Syntax();
        syntax.addTerminal("PLUS", TokenType.OPERATOR, OperatorType.PLUS);
        syntax.addTerminal("MINUS", TokenType.OPERATOR, OperatorType.MINUS);
        syntax.addTerminal("TIMES", TokenType.OPERATOR, OperatorType.TIMES);
        syntax.addTerminal("DIVIDE", TokenType.OPERATOR, OperatorType.DIVIDE);
        syntax.addTerminal("LPA", TokenType.OPERATOR, OperatorType.LPARAN);
        syntax.addTerminal("RPA", TokenType.OPERATOR, OperatorType.RPARAN);
        syntax.addTerminal("SYMBOL", TokenType.ID, "i");
        syntax.addNonTerminal("E");
        syntax.addNonTerminal("T");
        syntax.addNonTerminal("F");
        syntax.addErrorHandler("sample", null);
        // syntax.infer("E -> T `PLUS`<+> E | T `MINUS`<-> E | T");
        // syntax.infer("T -> F `TIMES`<*> T | F `DIVIDE`</> T | F");
        // syntax.infer("F -> `LPA`<(> E `RPA`<)>  | `SYMBOL`<i>");
        syntax.infer("E -> E @PLUS<+> T");
        syntax.infer("E -> E @MINUS<-> T");
        syntax.infer("E -> T");
        syntax.infer("T -> T @TIMES<*> F");
        syntax.infer("T -> T @DIVIDE</> F");
        syntax.infer("T -> F");
        syntax.infer("F -> @LPA<(> E @RPA<)>");
        syntax.infer("F -> @SYMBOL<i>");
        syntax.initialize("E");
        System.out.println(syntax.toString());
        System.out.println(syntax.getNGAString());
        System.out.println(syntax.getNPAString());
    // scanner.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();
    }
}
Also used : SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RegexException(priv.bajdcc.util.lexer.error.RegexException) Syntax(priv.bajdcc.LALR1.syntax.Syntax)

Example 18 with SyntaxException

use of priv.bajdcc.LALR1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.

the class TestSyntax2 method main.

public static void main(String[] args) {
    try {
        // Scanner scanner = new Scanner(System.in);
        Syntax syntax = new Syntax();
        syntax.addTerminal("a", TokenType.ID, "a");
        syntax.addTerminal("b", TokenType.ID, "b");
        syntax.addNonTerminal("Z");
        syntax.addNonTerminal("S");
        syntax.addNonTerminal("B");
        syntax.addErrorHandler("sample", null);
        syntax.infer("Z -> S");
        syntax.infer("S -> B B");
        syntax.infer("B -> @a B");
        syntax.infer("B -> @b");
        syntax.initialize("Z");
        System.out.println(syntax.toString());
        System.out.println(syntax.getNGAString());
        System.out.println(syntax.getNPAString());
    // scanner.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();
    }
}
Also used : SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RegexException(priv.bajdcc.util.lexer.error.RegexException) Syntax(priv.bajdcc.LALR1.syntax.Syntax)

Example 19 with SyntaxException

use of priv.bajdcc.LALR1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.

the class TestSyntax3 method main.

public static void main(String[] args) {
    try {
        // Scanner scanner = new Scanner(System.in);
        Syntax syntax = new Syntax();
        syntax.addTerminal("a", TokenType.ID, "a");
        syntax.addTerminal("c", TokenType.ID, "c");
        syntax.addTerminal("d", TokenType.ID, "d");
        syntax.addNonTerminal("S");
        syntax.addNonTerminal("A");
        syntax.addErrorHandler("sample", null);
        syntax.infer("S -> @c A @d");
        syntax.infer("A -> @a");
        syntax.infer("A -> A @a");
        syntax.initialize("S");
        System.out.println(syntax.toString());
        System.out.println(syntax.getNGAString());
        System.out.println(syntax.getNPAString());
    // scanner.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();
    }
}
Also used : SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RegexException(priv.bajdcc.util.lexer.error.RegexException) Syntax(priv.bajdcc.LALR1.syntax.Syntax)

Example 20 with SyntaxException

use of priv.bajdcc.LALR1.syntax.handler.SyntaxException 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)

Aggregations

RegexException (priv.bajdcc.util.lexer.error.RegexException)28 SyntaxException (priv.bajdcc.LALR1.syntax.handler.SyntaxException)26 Grammar (priv.bajdcc.LALR1.grammar.Grammar)18 RuntimeCodePage (priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage)15 RuntimeException (priv.bajdcc.LALR1.grammar.runtime.RuntimeException)15 ByteArrayInputStream (java.io.ByteArrayInputStream)13 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 Interpreter (priv.bajdcc.LALR1.interpret.Interpreter)13 Semantic (priv.bajdcc.LALR1.semantic.Semantic)5 ISemanticAnalyzer (priv.bajdcc.LALR1.semantic.token.ISemanticAnalyzer)3 Syntax (priv.bajdcc.LALR1.syntax.Syntax)3 RuleItem (priv.bajdcc.LALR1.syntax.rule.RuleItem)3 RuntimeMachine (priv.bajdcc.LALR1.grammar.runtime.RuntimeMachine)2 PropertyExp (priv.bajdcc.LALR1.syntax.exp.PropertyExp)2 Grammar (priv.bajdcc.LL1.grammar.Grammar)2 GrammarException (priv.bajdcc.LL1.grammar.error.GrammarException)2 SyntaxException (priv.bajdcc.LL1.syntax.handler.SyntaxException)2 OperatorType (priv.bajdcc.util.lexer.token.OperatorType)2 Token (priv.bajdcc.util.lexer.token.Token)2 TokenType (priv.bajdcc.util.lexer.token.TokenType)2