Search in sources :

Example 11 with Interpreter

use of priv.bajdcc.LALR1.interpret.Interpreter in project jMiniLang by bajdcc.

the class TestInterpret2 method main.

@SuppressWarnings("resource")
public static void main(String[] args) {
    try {
        Scanner scanner = new Scanner(System.in);
        Interpreter interpreter = new Interpreter();
        int i = 0;
        while (true) {
            System.out.print(">> ");
            String code = scanner.nextLine();
            try {
                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("test_" + i++, bais);
            } catch (RegexException e) {
                System.err.println(e.getPosition() + "," + e.getMessage());
            // e.printStackTrace();
            } catch (RuntimeException e) {
                System.err.println(e.getPosition() + ": " + e.getInfo());
                if (e.getkError() == RuntimeError.EXIT) {
                    break;
                }
            // e.printStackTrace();
            } catch (Exception e) {
                System.err.println(e.getMessage());
            // e.printStackTrace();
            }
        }
    } catch (Exception e) {
        System.err.println(e.getMessage());
    // e.printStackTrace();
    }
}
Also used : Scanner(java.util.Scanner) Interpreter(priv.bajdcc.LALR1.interpret.Interpreter) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) ByteArrayInputStream(java.io.ByteArrayInputStream) RegexException(priv.bajdcc.util.lexer.error.RegexException) Grammar(priv.bajdcc.LALR1.grammar.Grammar) RuntimeCodePage(priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) RegexException(priv.bajdcc.util.lexer.error.RegexException)

Example 12 with Interpreter

use of priv.bajdcc.LALR1.interpret.Interpreter in project jMiniLang by bajdcc.

the class TestInterpret4 method main.

public static void main(String[] args) {
    try {
        String[] codes = new String[] { // 高级语言特性之迭代返回,即协程
        "import \"sys.base\";\n" + "var get = yield ~(a, b, c) {\n" + "    for (var i = a; i < b; i++) {\n" + "        if (i % c == 0) {\n" + "            continue;\n" + "        }\n" + "        yield i;\n" + "    }\n" + "};\n" + "foreach (var i : call get(0, 10, 2) + 1) {\n" + "   call g_print(i);\n" + "}\n", // 高级语言特性之函数闭包
        "import \"sys.base\";\n" + "var gen_eq = func ~(a) {\n" + "    var eq = func ~(b) -> a == b;\n" + "    return eq;\n" + "};\n" + // 函数Curry,保存词法上下文
        "var ex_eq = call gen_eq(5);\n" + "call g_print(call ex_eq(6));\n" + "call g_print(call ex_eq(6));\n", "import \"sys.base\";\n" + "var lt = call g_curry_1(\"g_lt\", 5);\n" + "call g_print(call lt(7));\n" + "call g_println();\n" + "call g_print(call lt(4));\n" + "call g_println();\n" + "\n", "import \"sys.base\";\n" + "var get = yield ~(a, b, c) {\n" + "    for (var i = a; i < b; i++) {\n" + "        if (i % c == 0) {\n" + "            continue;\n" + "        }\n" + "        yield i;\n" + "    }\n" + "};\n" + "foreach (var i : call get(0, 10, 2) * 10) {\n" + "   call g_print(i);\n" + "   call g_println();\n" + "}\n", "import \"sys.base\";\n" + "var a = 5;\n" + "var b = 4;\n" + "\n" + "    if (a == 5) {\n" + "       var b = 7;\n" + "       call g_print(b);\n" + "    }\n" + "call g_print(b);\n", "import \"sys.base\";\n" + "var mod_swap = call g_swap(\"g_mod\");\n" + "var mod_3 = call g_curry_1(mod_swap, 3);\n" + "foreach (var i : call g_range(1, 10) * 8) {\n" + "    if (call mod_3(i)) {\n" + "       call g_print(i);\n" + "       call g_println();\n" + "    }\n" + "}\n", // 打印质数版本1
        "import \"sys.base\";\n" + "call g_print(\"输入下限:\");\n" + "var lower_bound = call g_stdin_read_int();\n" + "call g_print(\"输入上限:\");\n" + "var upper_bound = call g_stdin_read_int();\n" + "var prime = func ~(a) {\n" + "    if (a <= 1) {\n" + "        return false;\n" + "    }\n" + "    var mod_a = call g_curry_1(\"g_mod\", a);\n" + "    var mod_any = func ~(x) -> call mod_a(x);\n" + "    if (!call g_range_any(2, a - 1, mod_any)) {\n" + "        call g_print(a);\n" + "        call g_println();\n" + "    }\n" + "};\n" + "call g_range_foreach(lower_bound, upper_bound, prime);\n" + "\n", // 打印质数版本2
        "import \"sys.base\";\n" + "call g_print(\"输入下限:\");\n" + "var lower_bound = call g_stdin_read_int();\n" + "call g_print(\"输入上限:\");\n" + "var upper_bound = call g_stdin_read_int();\n" + "var mod_swap = call g_swap(\"g_mod\");\n" + "var prime = func ~(a) {\n" + "    if (a <= 1) {\n" + "        return false;\n" + "    }\n" + "    foreach (var i : call g_range(2, a - 1)) {\n" + "        var mod_i = call g_curry_1(mod_swap, i);\n" + "        if (call mod_i(a)) {\n" + "            return;" + "        }\n" + "    }\n" + "    call g_print(a);\n" + "    call g_println();\n" + "};\n" + "call g_range_foreach(lower_bound, upper_bound, prime);\n" + "\n", // 打印质数版本3
        "import \"sys.base\";\nimport \"sys.math\";\n" + "call g_print(\"输入下限:\");\n" + "var lower_bound = call g_stdin_read_int();\n" + "call g_print(\"输入上限:\");\n" + "var upper_bound = call g_stdin_read_int();\n" + "var print_prime = func ~(a, b) {\n" + "    for (var i = a; i <= b; i++) {\n" + "        if (i <= 1) {\n" + "            continue;\n" + "        }\n" + "        var sq = call g_sqrt(i);\n" + "        for (var j = 2; j <= sq; j++) {\n" + "            if (i % j == 0) {\n" + "                break;\n" + "            }\n" + "        }\n" + "        if (j > sq) {\n" + "            call g_print(i);\n" + "            call g_println();\n" + "        }\n" + "    }\n" + "};\n" + "call print_prime(lower_bound, upper_bound);\n" + "\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 13 with Interpreter

use of priv.bajdcc.LALR1.interpret.Interpreter in project jMiniLang by bajdcc.

the class TestInterpret6 method main.

public static void main(String[] args) {
    try {
        String[] codes = new String[] { "import \"sys.base\";\n" + "var ff = func ~(f) {\n" + "    var fh = func ~(h) {\n" + "        return call h(h);\n" + "    };\n" + "    var fx = func ~(x) {\n" + "        var fn = func ~(n) {\n" + "            var vx = call x(x);\n" + "            var vf = call f(vx);\n" + "            return call vf(n);\n" + "        };\n" + "        return fn;\n" + "    };\n" + "    return call fh(fx);\n" + "};\n" + "var fact = func ~(f) {\n" + "    var fn = func ~(n) -> (n > 0) ? (n * call f(n - 1)) : 1;\n" + "    return fn;\n" + "};\n" + "var ffact = call ff(fact);\n" + "var fact_5 = call ffact(5);\n" + "call g_printn(fact_5);\n" + "\n", "import \"sys.base\";\n" + "var ff = func ~(f) {\n" + "    var fh = func ~(h) {\n" + "        return call h(h);\n" + "    };\n" + "    var fx = func ~(x) {\n" + "        var fn = func ~(n) {\n" + "            var vx = call x(x);\n" + "            var vf = call f(vx);\n" + "            return call vf(n);\n" + "        };\n" + "        return fn;\n" + "    };\n" + "    return call fh(fx);\n" + "};\n" + "var fact = func ~(f) {\n" + "    var fk = func ~(n) {\n" + "        if (n > 0) {\n" + "            return n * call f(n - 1);\n" + "        } else {\n" + "            return 1;\n" + "        };\n" + "    };\n" + "    return fk;\n" + "};\n" + "var ffact = call ff(fact);\n" + "var fact_5 = call ffact(5);\n" + "call g_printn(fact_5);\n" + "\n" };
        Interpreter interpreter = new Interpreter();
        Grammar grammar = new Grammar(codes[codes.length - 1]);
        System.out.println(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 14 with Interpreter

use of priv.bajdcc.LALR1.interpret.Interpreter in project jMiniLang by bajdcc.

the class TestInterpret7 method main.

public static void main(String[] args) {
    try {
        String[] codes = new String[] { "import \"sys.base\";\n" + "var f = func ~(a) -> a;\n" + "call g_printn(call g_get_type(f));" + "\n", "import \"sys.base\";\n" + "//a var i = 0;\n" + "//let i=7;\n" + "var i=0;\n" + "//for(;i<10;i++){\n" + "//call g_printn(\"\" + i);\n" + "//}\n" + "while (i < 10) {call g_printn(\"\"+ ++i);}", "import \"sys.base\";\n" + "var move = func ~(i, x, y) {\n" + "    call g_printn(\"\" + i + \": \" + x + \" -> \" + y);\n" + "};\n" + "var hanoi = func ~(f) {\n" + "    var fk = func ~(i, a, b, c) {\n" + "        if (i == 1) {\n" + "            call move(i, a, c);\n" + "        } else {\n" + "            call f(i - 1, a, c, b);\n" + "            call move(i, a, c);\n" + "            call f(i - 1, b, a, c);\n" + "        }\n" + "    };\n" + "    return fk;\n" + "};\n" + "var h = call (func ~(f) {\n" + "    var fx = func ~(x) {\n" + "        var fn = func ~(i, a, b, c) {\n" + "            var vf = call f(call x(x));\n" + "            return call vf(i, a, b, c);\n" + "        };\n" + "        return fn;\n" + "    };\n" + "    return call (func ~(h) -> call h(h))(fx);\n" + "})(hanoi);\n" + "call h(3, 'A', 'B', 'C');\n" + "\n", "import \"sys.base\";\n" + "call g_printn(\"Trampoline example:\");\n" + "var repeat = func ~(f) {\n" + "    var repeat0 = func ~(operation, count) {\n" + "        var k = func ~() {\n" + "            if (count <= 0) { return; }\n" + "            call operation();\n" + "            return call f(operation, --count);\n" + "        };\n" + "        return k;\n" + "    };\n" + "    return repeat0;\n" + "};\n" + "var REPEAT = call (func ~(f) {\n" + "    var fx = func ~(x) {\n" + "        var fn = func ~(operation, count) {\n" + "            var vf = call f(call x(x));\n" + "            return call vf(operation, count);\n" + "        };\n" + "        return fn;\n" + "    };\n" + "    return call (func ~(h) -> call h(h))(fx);\n" + "})(repeat);" + "var trampoline = func ~(f) {\n" + "    while (!(call g_is_null(f)) && (call g_get_type(f) == \"函数\")) {\n" + "        let f = call f();\n" + "    }\n" + "};\n" + "var print = func ~() -> call g_printn(\"\"+5);\n" + "var tfun = func ~() -> call REPEAT(print, 10);\n" + "call trampoline(tfun);" + "\n", "import \"sys.base\";\n" + "call g_printn(\"Trampoline example:\");\n" + "var repeat = func ~(operation, count) {\n" + "    var repeat0 = func ~() {\n" + "        if (count <= 0) { return; }\n" + "        call operation(count);\n" + "        return call repeat(operation, --count);\n" + "    };\n" + "    return repeat0;\n" + "};\n" + "var print = func ~(n) -> call g_printn(\"n: \" + n);\n" + "var tfun = func ~() -> call repeat(print, 5);\n" + "call(func ~(f) {\n" + "    while (!(call g_is_null(f)) && (call g_get_type(f) == \"函数\")) {\n" + "        let f = call f(); // Trampoline, like CPS.\n" + "    }\n" + "})(tfun);\n" + "\n", "import \"sys.base\";\n" + "import \"sys.list\";\n" + "import \"sys.proc\";\n" + "import \"sys.task\";\n" + "import \"sys.string\";\n" + "/* 创建场景 */\n" + "var create_stage = func ~(f) -> call f();\n" + "\n" + "/* 重复操作 */\n" + "var real_repeat = func ~(_operation, _arg, _start, _end) {\n" + "    var repeat = func ~(operation, arg, start, end) {\n" + "        var index = start;\n" + "        var repeat0 = func ~() {\n" + "            if (index >= end) { return; }\n" + "            call g_print(\"ttt\");call operation(arg, index);\n" + "            return call repeat(operation, arg, ++index, end);\n" + "        };\n" + "        return repeat0;\n" + "    };\n" + "    var repear_f = func ~() -> call repeat(_operation, _arg, _start, _end);\n" + "     call g_print(\"ttt2\");call(func ~(f) {\n" + "call g_print(call g_to_string(call g_get_type_ordinal(f)));" + "        while (!(call g_is_null(f)) && (call g_get_type_ordinal(f) == 8)) {\n" + "            let f = call f();\n" + "        }\n" + "    })(repear_f); call g_print(\"tttr\");\n" + "};\n" + "    \n" + "/* 打字效果 */\n" + "var word_fadein = func ~(str, span) {\n" + "    var print = func ~(a, n) {\n" + "        call g_print(call g_string_char(a, n));\n" + "        call g_sleep(span);\n" + "    };\n" + "    call real_repeat(print, str, 0, call g_string_length(str));\n" + "};\n" + "\n" + "/* 场景一 */\n" + "var stage_1 = func ~() {\n" + "    call word_fadein(\"Hello world!\\n\", 200);\n" + "};\n" + "\n" + "call create_stage(stage_1);" };
        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)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 Grammar (priv.bajdcc.LALR1.grammar.Grammar)14 RuntimeCodePage (priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage)14 RuntimeException (priv.bajdcc.LALR1.grammar.runtime.RuntimeException)14 Interpreter (priv.bajdcc.LALR1.interpret.Interpreter)14 RegexException (priv.bajdcc.util.lexer.error.RegexException)14 SyntaxException (priv.bajdcc.LALR1.syntax.handler.SyntaxException)13 Scanner (java.util.Scanner)1 IOSCodePage (priv.bajdcc.LALR1.interpret.os.IOSCodePage)1 IRPrint (priv.bajdcc.LALR1.interpret.os.irq.IRPrint)1 IRRemote (priv.bajdcc.LALR1.interpret.os.irq.IRRemote)1 IRSignal (priv.bajdcc.LALR1.interpret.os.irq.IRSignal)1 IRTask (priv.bajdcc.LALR1.interpret.os.irq.IRTask)1 OSEntry (priv.bajdcc.LALR1.interpret.os.kern.OSEntry)1 OSIrq (priv.bajdcc.LALR1.interpret.os.kern.OSIrq)1 OSTask (priv.bajdcc.LALR1.interpret.os.kern.OSTask)1 TKNet (priv.bajdcc.LALR1.interpret.os.task.TKNet)1 TKSystem (priv.bajdcc.LALR1.interpret.os.task.TKSystem)1 TKUI (priv.bajdcc.LALR1.interpret.os.task.TKUI)1