Search in sources :

Example 21 with Grammar

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

the class TestInterpret method main.

public static void main(String[] args) {
    try {
        String[] codes = new String[] { "import \"sys.base\";" + "call g_print(call g_is_null(g_null));", "import \"sys.base\";" + "call g_print(\"Hello World!\\\n\");" + "call g_print_err(\"Hello World!\\\n\");", "import \"sys.base\";\n" + "call g_print(\"请输入:\");\n" + "call g_print(call g_stdin_read_line() + g_endl);\n" + "call g_print(\"输入两个数字:\");\n" + "call g_print(\n" + "  \"较大数是:\" +" + "  call g_to_string(\n" + "    call g_max(call g_stdin_read_int(), call g_stdin_read_int()))\n" + "    + \"\\n\"\n" + ");\n" + "call g_print(\"输入两个数字:\");\n" + "call g_print(\n" + "  \"较小数是:\" +" + "  call g_to_string(\n" + "    call g_min(call g_stdin_read_int(), call g_stdin_read_int()))\n" + "    + \"\\n\"\n" + ");\n", "import \"sys.base\";\n" + "call g_print(call g_doc(\"g_author\") + g_endl);\n" + "call g_print(call g_doc(\"g_print\") + g_endl);\n" + "call g_print(call g_doc(\"g_stdin_read_int\") + g_endl);\n" + "call g_print(call g_doc(\"g_new\") + g_endl);\n" + "call g_print(call g_to_string(call g_new(g_endl)));\n" + "call g_print(call g_to_string(call g_new(5)));\n" // "import \"sys.base\";\n"
        // + "var f = func ~(n) ->\n"
        // + "    n <= 2 ? 1 : call f(n-1) + call f(n-2);"
        // + "call g_print(call f(6));\n"
        // ,
        // "import \"sys.base\";\n"
        // + "call g_print(call g_load_func(\"g_max\"));\n"
        // ,
        };
        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 22 with Grammar

use of priv.bajdcc.LALR1.grammar.Grammar 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 23 with Grammar

use of priv.bajdcc.LALR1.grammar.Grammar 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 24 with Grammar

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

the class TestGrammar 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);
        // Grammar grammar = new Grammar("(i * i) * (i + i) - i");
        Grammar grammar = new Grammar("i + i * i");
        grammar.addTerminal("SYMBOL", TokenType.ID, "i");
        grammar.addTerminal("PLUS", TokenType.OPERATOR, OperatorType.PLUS);
        grammar.addTerminal("MINUS", TokenType.OPERATOR, OperatorType.MINUS);
        grammar.addTerminal("TIMES", TokenType.OPERATOR, OperatorType.TIMES);
        grammar.addTerminal("DIVIDE", TokenType.OPERATOR, OperatorType.DIVIDE);
        grammar.addTerminal("LPA", TokenType.OPERATOR, OperatorType.LPARAN);
        grammar.addTerminal("RPA", TokenType.OPERATOR, OperatorType.RPARAN);
        grammar.setEpsilonName("epsilon");
        String[] nons = new String[] { "E", "E1", "T", "T1", "F", "A", "M" };
        for (String non : nons) {
            grammar.addNonTerminal(non);
        }
        grammar.infer("E -> T E1");
        grammar.infer("E1 -> A T E1 | @epsilon");
        grammar.infer("T -> F T1");
        grammar.infer("T1 -> M F T1 | @epsilon");
        grammar.infer("F -> @LPA E @RPA | @SYMBOL");
        grammar.infer("A -> @PLUS | @MINUS");
        grammar.infer("M -> @TIMES | @DIVIDE");
        grammar.initialize("E");
        System.out.println(grammar.toString());
        System.out.println(grammar.getPredictionString());
        grammar.run();
        System.out.println(grammar.getTokenString());
    // 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();
    } catch (GrammarException e) {
        System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
        e.printStackTrace();
    }
}
Also used : SyntaxException(priv.bajdcc.LL1.syntax.handler.SyntaxException) GrammarException(priv.bajdcc.LL1.grammar.error.GrammarException) RegexException(priv.bajdcc.util.lexer.error.RegexException) Grammar(priv.bajdcc.LL1.grammar.Grammar)

Example 25 with Grammar

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

the class TestGrammar2 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);
        Grammar syntax = new Grammar("b b b b ");
        syntax.addTerminal("a", TokenType.ID, "a");
        syntax.addTerminal("b", TokenType.ID, "b");
        syntax.setEpsilonName("epsilon");
        String[] nons = new String[] { "S", "A", "B" };
        for (String non : nons) {
            syntax.addNonTerminal(non);
        }
        syntax.infer("S -> A B A B");
        syntax.infer("A -> @a A | @epsilon");
        syntax.infer("B -> @b B | @epsilon");
        syntax.initialize("S");
        System.out.println(syntax.toString());
        System.out.println(syntax.getPredictionString());
        syntax.run();
        System.out.println(syntax.getTokenString());
    // 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();
    } catch (GrammarException e) {
        System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
        e.printStackTrace();
    }
}
Also used : SyntaxException(priv.bajdcc.LL1.syntax.handler.SyntaxException) GrammarException(priv.bajdcc.LL1.grammar.error.GrammarException) RegexException(priv.bajdcc.util.lexer.error.RegexException) Grammar(priv.bajdcc.LL1.grammar.Grammar)

Aggregations

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