Search in sources :

Example 16 with RuntimeException

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

the class RuntimeMachine method opArr.

@Override
public void opArr() throws RuntimeException {
    int size = current();
    next();
    RuntimeArray arr = new RuntimeArray();
    for (int i = 0; i < size; i++) {
        arr.add(load());
    }
    stack.pushData(new RuntimeObject(arr));
}
Also used : RuntimeArray(priv.bajdcc.LALR1.grammar.runtime.data.RuntimeArray)

Example 17 with RuntimeException

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

the class RuntimeMachine method opReloadFunc.

@Override
public void opReloadFunc() throws RuntimeException {
    int idx = loadInt();
    RuntimeFuncObject func = new RuntimeFuncObject(stack.reg.pageId, stack.reg.execId);
    RuntimeObject obj = new RuntimeObject(func);
    obj.setSymbol(currentPage.getData().get(idx));
    stack.storeVariableDirect(idx, obj);
}
Also used : RuntimeFuncObject(priv.bajdcc.LALR1.grammar.runtime.data.RuntimeFuncObject)

Example 18 with RuntimeException

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

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

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

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