Search in sources :

Example 1 with RegexException

use of priv.bajdcc.util.lexer.error.RegexException in project jMiniLang by bajdcc.

the class TestGrammar2 method main.

public static void main(String[] args) {
    try {
        String[] exprs = new String[] { "var a = 1;", "var a = 4 + 4;", "var a = !(!true * (++4 / !4 + 4 ? 5 & 6 && true ^ !\"ddd\" | 'r' : 8.0+9*9));", "var a = 3.0 + 4.0 / 7 * 8.9;", "var a = 3000.0 << ++2.0;", "var a = true << 9 ? 1 + 3 : 2;", "var a = 5;let a = a + 4 + a ? 3 : 5;var t = func main1(){};", "var d= func [\"d\",\"t\"] ~()->a;", "var d = func ~()->4;", "var a = call (func ~(a) {call (func b(c){var b = '5'+ 66; call b(666);})();}) (6);", "var a = call (func ~(a) {\n" + "call (func b(c){\n" + "var c = '5'+ 1;\n" + "var d = c;\n" + "//call b(666);\n" + "let c = 7;\n" + "})(7);\n" + "}) (44);", "var a = call\n" + "(func ~(x, y) -> x + y)\n" + " (1, 2);\n", "var d = 4;" + "var c = d || 6;" };
        /*
			 * BufferedReader br = new BufferedReader(new
			 * FileReader("E:/http.c")); String line = ""; StringBuffer sb = new
			 * StringBuffer(); while ((line = br.readLine()) != null) {
			 * sb.append(line + System.lineSeparator()); }
			 * br.close();
			 */
        Grammar grammar = new Grammar(exprs[exprs.length - 1]);
        System.out.println(grammar.toString());
        RuntimeCodePage page = grammar.getCodePage();
        System.out.println(page.toString());
        RuntimeMachine machine = new RuntimeMachine();
        machine.run("test1", page);
    // FileWriter fw = new FileWriter("E:/testgrammar.txt");
    // fw.append(grammar.toString());
    // fw.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 (IOException e) {
    // System.err.println(e.getMessage());
    // e.printStackTrace();
    } catch (RuntimeException e) {
        System.err.println(e.getPosition() + ": " + e.getInfo());
        e.printStackTrace();
    // } catch (IOException e) {
    // System.err.println(e.getMessage());
    // e.printStackTrace();
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    // } catch (IOException e) {
    // System.err.println(e.getMessage());
    // e.printStackTrace();
    }
}
Also used : RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RegexException(priv.bajdcc.util.lexer.error.RegexException) RuntimeMachine(priv.bajdcc.LALR1.grammar.runtime.RuntimeMachine) Grammar(priv.bajdcc.LALR1.grammar.Grammar) RuntimeCodePage(priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) RegexException(priv.bajdcc.util.lexer.error.RegexException)

Example 2 with RegexException

use of priv.bajdcc.util.lexer.error.RegexException in project jMiniLang by bajdcc.

the class TestGrammar3 method main.

public static void main(String[] args) {
    try {
        String a = "\n" + "var g_a = func ~(x, y) { return x + y;};\n" + "export \"g_a\";";
        String b = "import \"test1\";\n" + "var d = call g_a(1,2);\n" + "var c = g_gk;\n" + "call g_print(c);\n" + "var t = call g_print(c);";
        /*
			 * BufferedReader br = new BufferedReader(new
			 * FileReader("E:/http.c")); String line = ""; StringBuffer sb = new
			 * StringBuffer(); while ((line = br.readLine()) != null) {
			 * sb.append(line + System.lineSeparator()); }
			 * br.close();
			 */
        Grammar grammar = new Grammar(a);
        System.out.println(grammar.toString());
        RuntimeCodePage page = grammar.getCodePage();
        System.out.println(page.toString());
        RuntimeMachine machine = new RuntimeMachine();
        machine.run("test1", page);
        Grammar grammar2 = new Grammar(b);
        System.out.println(grammar2.toString());
        RuntimeCodePage page2 = grammar2.getCodePage();
        page2.getInfo().addExternalValue("g_gk", () -> new RuntimeObject("abc"));
        page2.getInfo().addExternalFunc("g_print", new IRuntimeDebugExec() {

            @Override
            public String getDoc() {
                return "Print";
            }

            @Override
            public RuntimeObjectType[] getArgsType() {
                return new RuntimeObjectType[] { RuntimeObjectType.kObject };
            }

            @Override
            public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
                System.out.println(args.get(0).getObj());
                return null;
            }
        });
        System.out.println(page2.toString());
        machine.run("test2", page2);
    // FileWriter fw = new FileWriter("E:/testgrammar.txt");
    // fw.append(grammar.toString());
    // fw.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 (IOException e) {
    // System.err.println(e.getMessage());
    // e.printStackTrace();
    } catch (RuntimeException e) {
        System.err.println(e.getPosition() + ": " + e.getInfo());
        e.printStackTrace();
    // } catch (IOException e) {
    // System.err.println(e.getMessage());
    // e.printStackTrace();
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    // } catch (IOException e) {
    // System.err.println(e.getMessage());
    // e.printStackTrace();
    }
}
Also used : RuntimeMachine(priv.bajdcc.LALR1.grammar.runtime.RuntimeMachine) Grammar(priv.bajdcc.LALR1.grammar.Grammar) RuntimeCodePage(priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage) IRuntimeStatus(priv.bajdcc.LALR1.grammar.runtime.IRuntimeStatus) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) RegexException(priv.bajdcc.util.lexer.error.RegexException) RuntimeException(priv.bajdcc.LALR1.grammar.runtime.RuntimeException) RuntimeObject(priv.bajdcc.LALR1.grammar.runtime.RuntimeObject) SyntaxException(priv.bajdcc.LALR1.syntax.handler.SyntaxException) RegexException(priv.bajdcc.util.lexer.error.RegexException) IRuntimeDebugExec(priv.bajdcc.LALR1.grammar.runtime.IRuntimeDebugExec)

Example 3 with RegexException

use of priv.bajdcc.util.lexer.error.RegexException in project jMiniLang by bajdcc.

the class TestInterpret9 method main.

public static void main(String[] args) {
    try {
        String[] codes = new String[] { "import \"sys.base\"; import \"sys.proc\";\n" + "var a = func ~() {\n" + "    foreach (var i : call g_range(1, 10)) {\n" + "        call g_printn(\"[\" + call g_get_pid() + \"] Hello world!\");\n" + "    }\n" + "};\n" + "call g_create_process(a);\n" + "call a();\n" + "\n", "import \"sys.base\"; import \"sys.proc\";\n" + "var proc = func ~() {\n" + "    var handle = call g_create_pipe(\"test\");\n" + "    var print = func ~(ch) -> call g_print(ch);\n" + "    var pid = call g_get_pid();\n" + "    if (pid == 0) {\n" + "        foreach (var i : call g_range(1, 10)) {\n" + "            call g_printn(\"[\" + call g_get_pid() + \"] Hello world!\");\n" + "            call g_write_pipe(handle, \"\" + i);\n" + "        }\n" + "        call g_destroy_pipe(handle);\n" + "    } else {\n" + "        call g_printn(\"[\" + call g_get_pid() + \"] Hello world!\");\n" + "        call g_read_pipe(handle, print);\n" + "    }\n" + "};\n" + "call g_create_process(proc);\n" + "call proc();\n" + "\n", "import \"sys.base\";\n" + "import \"sys.list\";\n" + "import \"sys.proc\";\n" + "\n" + "var goods = [];\n" + "call g_start_share(\"goods\", goods);\n" + "var index = 1;\n" + "call g_start_share(\"index\", index);\n" + "var consumer = func ~() {\n" + "    for (;;) {\n" + "        var goods = call g_query_share(\"goods\");\n" + "        if (call g_is_null(goods)) {\n" + "            break;\n" + "        }\n" + "        var g = call g_array_pop(goods);\n" + "        if (!call g_is_null(g)) {\n" + "            call g_printn(\"Consumer#\" + call g_get_pid() + \" ---- get \" + g);\n" + "        }\n" + "    }\n" + "    call g_printn(\"Consumer#\" + call g_get_pid() + \" exit\");\n" + "};\n" + "var producer = func ~() {\n" + "    foreach (var i : call g_range(1, 5)) {\n" + "        var goods = call g_reference_share(\"goods\");\n" + "        call g_lock_share(\"index\");\n" + "        var index = call g_reference_share(\"index\");\n" + "        call g_printn(\"Producer#\" + call g_get_pid() + \" ++++ put \" + index);\n" + "        call g_array_add(goods, index);\n" + "        index++;\n" + "        call g_stop_share(\"index\");\n" + "        call g_unlock_share(\"index\");\n" + "        call g_stop_share(\"goods\");\n" + "    }\n" + "    call g_printn(\"Producer#\" + call g_get_pid() + \" exit\");\n" + "};\n" + "var create_consumer = func ~(n) {\n" + "    var handles = [];\n" + "    foreach (var i : call g_range(1, n)) {\n" + "        var h = call g_create_process(consumer);\n" + "        call g_array_add(handles, h);\n" + "        call g_printn(\"[\" + i + \"] Create consumer: #\" + h);\n" + "    }\n" + "    return handles;\n" + "};\n" + "var create_producer = func ~(n) {\n" + "    var handles = [];\n" + "    foreach (var i : call g_range(1, n)) {\n" + "        var h = call g_create_process(producer);\n" + "        call g_array_add(handles, h);\n" + "        call g_printn(\"[\" + i + \"] Create producer: #\" + h);\n" + "    }\n" + "    return handles;\n" + "};\n" + "\n" + "var consumers = call create_consumer(3);\n" + "var producers = call create_producer(4);\n" + "call g_printn(\"Waiting for producers to exit...\");\n" + "call g_join_process_array(producers);\n" + "call g_printn(\"Producers exit\");\n" + "call g_printn(\"Waiting for consumers to exit...\");\n" + "call g_stop_share(\"index\");\n" + "call g_stop_share(\"goods\");\n" + "call g_join_process_array(consumers);\n" + "call g_printn(\"Consumers exit\");\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 4 with RegexException

use of priv.bajdcc.util.lexer.error.RegexException in project jMiniLang by bajdcc.

the class TestSemantic2 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);
        // String expr = "( i )";
        String expr = "v + d * d";
        Semantic semantic = new Semantic(expr);
        semantic.addTerminal("PLUS", TokenType.OPERATOR, OperatorType.PLUS);
        semantic.addTerminal("TIMES", TokenType.OPERATOR, OperatorType.TIMES);
        semantic.addTerminal("LPA", TokenType.OPERATOR, OperatorType.LPARAN);
        semantic.addTerminal("RPA", TokenType.OPERATOR, OperatorType.RPARAN);
        semantic.addTerminal("v", TokenType.ID, "v");
        semantic.addTerminal("d", TokenType.ID, "d");
        semantic.addNonTerminal("S");
        semantic.addNonTerminal("E");
        semantic.addNonTerminal("T");
        semantic.addNonTerminal("F");
        semantic.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>");
        semantic.infer("S -> E");
        semantic.infer("E -> E @PLUS<+> T");
        semantic.infer("E -> T");
        semantic.infer("T -> T @TIMES<*> F");
        semantic.infer("T -> F");
        semantic.infer("F -> @v<v>");
        semantic.infer("F -> @d<d>");
        semantic.infer("F -> @LPA@<(> E @RPA<)>");
        semantic.initialize("S");
        System.out.println(semantic.toString());
        System.out.println(semantic.getNGAString());
        System.out.println(semantic.getNPAString());
        System.out.println(semantic.getInst());
        System.out.println(semantic.getTrackerError());
        System.out.println(semantic.getTokenList());
    // 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) Semantic(priv.bajdcc.LALR1.semantic.Semantic) RegexException(priv.bajdcc.util.lexer.error.RegexException)

Example 5 with RegexException

use of priv.bajdcc.util.lexer.error.RegexException in project jMiniLang by bajdcc.

the class TestInterpret11 method main.

public static void main(String[] args) {
    try {
        String[] codes = new String[] { "import \"sys.base\";\n" + "import \"sys.func\";\n" + "import \"sys.list\";\n" + "import \"sys.string\";\n" + "\n" + "var a = call g_array_range(1, 10);\n" + "var b = call g_func_apply(\"g_func_add\", a);\n" + "call g_printn(b);\n" + "var b1 = call g_func_length(a);\n" + "call g_printn(b1);\n" + "var c = call g_func_apply(\"g_func_add\", a);\n" + "call g_printn(c);\n" + "var c1 = call g_func_map(a, \"g_to_string\");\n" + "var c2 = call g_func_applyr(\"g_func_add\", c1);\n" + "call g_printn(c2);\n" + "let c1 = call g_func_mapr(a, \"g_to_string\");\n" + "let c2 = call g_func_apply(\"g_func_add\", c1);\n" + "call g_printn(c2);\n" + "var c3 = call g_func_applyr(\"g_func_sub\", a);\n" + "call g_printn(c3);\n" + "var f4 = func ~(x) -> x % 2 == 0;\n" + "var c4 = call g_func_filter(a, f4);\n" + "let c4 = call g_func_map(c4, \"g_to_string\");\n" + "let c4 = call g_func_apply(\"g_func_add\", c4);\n" + "call g_printn(c4);\n" + "var c5 = call g_func_take(c1, 5);\n" + "let c5 = call g_func_apply(\"g_func_add\", c5);\n" + "call g_printn(c5);\n" + "let c5 = call g_func_taker(c1, 5);\n" + "let c5 = call g_func_apply(\"g_func_add\", c5);\n" + "call g_printn(c5);\n" + "let c5 = call g_func_drop(c1, 5);\n" + "let c5 = call g_func_apply(\"g_func_add\", c5);\n" + "call g_printn(c5);\n" + "let c5 = call g_func_dropr(c1, 5);\n" + "let c5 = call g_func_apply(\"g_func_add\", c5);\n" + "call g_printn(c5);\n" + "var c6 = call g_func_zip(\"g_func_mul\" ,a, a);\n" + "let c6 = call g_func_map(c6, \"g_to_string\");\n" + "let c6 = call g_func_apply(\"g_func_add\", c6);\n" + "call g_printn(c6);\n" + "var d = call g_func_apply_arg(\"g_func_add\", call g_string_split(\"12345\", \"\"), \"g_func_swap\");\n" + "call g_printn(d);\n" + "call g_func_import_string_module();\n" + "var e = call g_func_applicative(\"g_func_eq\", \"12321\", \"g_string_reverse\");\n" + "call g_printn(e);\n" + "call g_printn(call g_doc(\"g_func_fold\"));\n" + "var xx = func ~(l) {\n" + "    var idx = call g_array_size(l) - 1;\n" + "    var _xsr = func ~() ->\n" + "        idx < 0 ? g_null : call g_array_get(l, idx--);\n" + "    return _xsr;\n" + "};\n" + "var x1 = call xx(a);\n" + "var x2 = call x1();\n" + "while (!call g_is_null(x2)) {\n" + "    call g_printn(x2);\n" + "    let x2 = call x1();\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)

Aggregations

RegexException (priv.bajdcc.util.lexer.error.RegexException)40 SyntaxException (priv.bajdcc.LALR1.syntax.handler.SyntaxException)24 Grammar (priv.bajdcc.LALR1.grammar.Grammar)18 RuntimeCodePage (priv.bajdcc.LALR1.grammar.runtime.RuntimeCodePage)16 RuntimeException (priv.bajdcc.LALR1.grammar.runtime.RuntimeException)16 ByteArrayInputStream (java.io.ByteArrayInputStream)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)14 Interpreter (priv.bajdcc.LALR1.interpret.Interpreter)14 Scanner (java.util.Scanner)6 Token (priv.bajdcc.util.lexer.token.Token)5 Semantic (priv.bajdcc.LALR1.semantic.Semantic)4 SyntaxException (priv.bajdcc.OP.syntax.handler.SyntaxException)4 OperatorType (priv.bajdcc.util.lexer.token.OperatorType)4 Syntax (priv.bajdcc.LALR1.syntax.Syntax)3 GrammarException (priv.bajdcc.OP.grammar.error.GrammarException)3 IPatternHandler (priv.bajdcc.OP.grammar.handler.IPatternHandler)3 RegexStringIteratorData (priv.bajdcc.util.lexer.regex.RegexStringIteratorData)3 RegexStringUtility (priv.bajdcc.util.lexer.regex.RegexStringUtility)3 RuntimeMachine (priv.bajdcc.LALR1.grammar.runtime.RuntimeMachine)2 ISemanticAnalyzer (priv.bajdcc.LALR1.semantic.token.ISemanticAnalyzer)2