Search in sources :

Example 1 with Interpreter

use of priv.bajdcc.LALR1.interpret.Interpreter 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 2 with Interpreter

use of priv.bajdcc.LALR1.interpret.Interpreter 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)

Example 3 with Interpreter

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

the class TestInterpret12 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" + "import \"module.lisp\";\n" + "\n" + "var env = call g_lisp_env();\n" + "call g_lisp_repl(env, \"(define circle-area (lambda (r) (* PI (* r r))))\");\n" + "call g_print(call g_lisp_repl(env, \"(circle-area 10)\"));\n" + "call g_lisp_repl(env, \"(define fact (lambda (n) (if (<= n 1) 1 (* n (fact (- n 1))))))\");\n" + "call g_print(call g_lisp_repl(env, \"(fact 10)\"));\n" + "call g_print(call g_lisp_repl(env, \"(list 1 2 3 4 5)\"));\n" + "call g_print(\"-----\");\n" + "call g_print(call g_lisp_repl(env, \"(define L (list 1 2 3 4 5))\"));\n" + "call g_print(call g_lisp_repl(env, \"(car L)\"));\n" + "call g_print(call g_lisp_repl(env, \"(cdr L)\"));\n" + "call g_print(call g_lisp_repl(env, \"(count 0 (list 0 1 2 3 0 0))\"));\n" + "call g_print(call g_lisp_repl(env, \"(count (quote the) (quote (the more the merrier the bigger the better)))\"));\n" + "call g_print(call g_lisp_repl(env, \"(null? (list))\"));\n" + "call g_print(call g_lisp_repl(env, \"(number? 5.0)\"));\n" + "call g_print(call g_lisp_repl(env, \"(number? (list))\"));\n" + "call g_print(call g_lisp_repl(env, \"(type (quote hello))\"));\n" + "call g_print(call g_lisp_repl(env, \"(list? (list))\"));\n" + "call g_print(call g_lisp_repl(env, \"(car (quote (a b c)))\"));\n" + "call g_print(call g_lisp_repl(env, \"(type (car (quote (a b c))))\"));\n" + "call g_print(call g_lisp_repl(env, \"(cdr (cons (quote a) (quote (b c))))\"));\n" + "call g_print(call g_lisp_repl(env, \"(define repeat (lambda (f) (lambda (x) (f (f x)))))\"));\n" + "call g_print(call g_lisp_repl(env, \"(define twice (lambda (x) (* 2 x)))\"));\n" + "call g_print(call g_lisp_repl(env, \"((repeat twice) 10)\"));\n" + "call g_print(call g_lisp_repl(env, \"(define sum (lambda (n) (if (< n 2) 1 (+ n (sum (- n 1))))))\"));\n" + "call g_print(call g_lisp_repl(env, \"(sum 10)\"));\n" + "call g_print(call g_lisp_repl(env, \"(min 50 60)\"));\n" + "call g_print(call g_lisp_repl(env, \"(range 0 10)\"));\n" + "call g_print(\"-----\");\n" + "call g_print(call g_lisp_repl(env, \"(define fib (lambda (n) (if (<= n 2) 1 (+ (fib (- n 1)) (fib (- n 2))))))\"));\n" + "call g_print(call g_lisp_repl(env, \"(fib 10)\"));\n" + "call g_print(\"-----\");\n" + "call g_print(call g_lisp_repl(env, \"(map fib (list 3 2 3 4 5))\"));\n" + "call g_print(call g_lisp_repl(env, \"(map fib (range 0 10))\"));\n" + "call g_print(call g_lisp_repl(env, \"(map (lambda (n) ((repeat twice) n)) (range 1 10))\"));\n" + "call g_print(\"-----\");\n" + "call g_lisp_repl(env, \"(print \\\"ab_cd\\\")\");\n" + "call g_print(call g_lisp_repl(env, \"(car (cons 'a '(b c)))\"));\n" + "call g_print(call g_lisp_repl(env, \"(cdr (cons 'a '(b c)))\"));\n" + "call g_print(call g_lisp_repl(env, \"(apply + (range 1 10))\"));\n" + "call g_print(call g_lisp_repl(env, \"(apply + (list \\\"hello\\\" #s \\\"world\\\" #s \\\"bajdcc\\\"))\"));\n" + "call g_print(call g_lisp_repl(env, \"(append '(a b) '(c d))\"));\n" + "call g_print(call g_lisp_repl(env, \"(apply 'append '('(a b) '(c d)))\"));\n" + "call g_print(call g_lisp_repl(env, \"(apply max (range 1 10))\"));\n" + "call g_print(\"-----\");\n" + "call g_print(call g_lisp_repl(env, \"(define fib_Y (lambda (f) (lambda (n) (if (<= n 2) 1 (+ (f (- n 1)) (f (- n 2)))))))\"));\n" + "call g_print(call g_lisp_repl(env, \"(apply + (map (Y fib_Y) (range 1 10)))\"));\n" + "call g_print(\"-----\");\n" + "call g_print(call g_lisp_repl(env, \"(cond ((== 1 2) 3 7) ((== 4 4) 6))\"));\n" + "call g_print(call g_lisp_repl(env, \"(cond ((== 1 2) 3) ((== 4 4) 6))\"));\n" + "call g_print(call g_lisp_repl(env, \"(define N 8)\"));\n" + "call g_print(call g_lisp_repl(env, \"(case N (1 2) (8 9))\"));\n" + "call g_print(call g_lisp_repl(env, \"(case N (3 2) (2 9) ('(4 8) 5))\"));\n" + "call g_print(call g_lisp_repl(env, \"(when (> N 5) 6)\"));\n" + "call g_print(call g_lisp_repl(env, \"(when (> N 50) 6)\"));\n" + "call g_print(call g_lisp_repl(env, \"(while (< N 12) (set! N (++ N)))\"));\n" + "call g_print(call g_lisp_repl(env, \"(val N)\"));\n" + "call g_print(\"-----\");\n" + "call g_print(call g_lisp_repl(env, \"(call/cc (lambda (k) (* 5 4)) (lambda (c) c))\"));\n" + "call g_print(call g_lisp_repl(env, \"(call/cc (lambda (k) (k 4)) (lambda (c) c))\"));\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 4 with Interpreter

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

the class TestInterpret13 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" + "import \"sys.math\";\n" + "import \"sys.class\";\n" + "\n" + "var ctx = call g_create_context();\n" + "call g_register_class(ctx, \"shape\", lambda(this){\n" + "call g_create_property(this, \"type\", \"shape\");\n" + "call g_create_method(this, \"get_area\", lambda(this)->0);\n" + "call g_create_method(this, \"get_index\", lambda(this,i)->i);\n" + "}, g_null);\n" + "call g_register_class(ctx, \"square\", lambda(this){\n" + "call g_create_property(this, \"type\", \"square\");\n" + "call g_create_property(this, \"a\", 0);\n" + "call g_create_property(this, \"b\", 0);\n" + "call g_create_method(this, \"get_area\", lambda(this){\n" + "return call g_get_property(this, \"a\") * call g_get_property(this, \"b\");\n" + "});\n" + "call g_create_method(this, \"to_string\", lambda(this){\n" + "return \"\" + call g_get_property(this, \"type\")\n" + "+ \" a=\" + call g_get_property(this, \"a\")\n" + "+ \" b=\" + call g_get_property(this, \"b\")\n" + "+ \" area=\"\n" + "+ call g_invoke_method(this, \"get_area\");\n" + "});\n" + "}, \"shape\");\n" + "call g_register_class(ctx, \"circle\", lambda(this){\n" + "call g_create_property(this, \"type\", \"circle\");\n" + "call g_create_property(this, \"r\", 0);\n" + "call g_create_method(this, \"get_area\", lambda(this){\n" + "var r = call g_get_property(this, \"r\");\n" + "return 3.14 * r * r;\n" + "});\n" + "call g_create_method(this, \"to_string\", lambda(this){\n" + "return \"\" + call g_get_property(this, \"type\")\n" + "+ \" r=\" + call g_get_property(this, \"r\")\n" + "+ \" area=\"\n" + "+ call g_invoke_method(this, \"get_area\");\n" + "});\n" + "}, \"shape\");\n" + "\n" + "\n" + "var square = call g_create_class(ctx, \"square\");\n" + "call g_set_property(square, \"a\", 5);\n" + "call g_set_property(square, \"b\", 6);\n" + "call g_printn(\"\" + call g_get_property(square, \"type\")\n" + "+ \" a=\" + call g_get_property(square, \"a\")\n" + "+ \" b=\" + call g_get_property(square, \"b\")\n" + "+ \" area=\"\n" + "+ call g_invoke_method(square, \"get_area\"));\n" + "var circle = call g_create_class(ctx, \"circle\");\n" + "call g_set_property(circle, \"r\", 10);\n" + "call g_set_property(circle, \"s\", square);\n" + "call g_printn(\"\" + call g_get_property(circle, \"type\")\n" + "+ \" r=\" + call g_get_property(circle, \"r\")\n" + "+ \" area=\"\n" + "+ call g_invoke_method(circle, \"get_area\"));\n" + "call g_printn(call g_invoke_method(square, \"to_string\"));\n" + "call g_printn(call g_invoke_method(circle, \"to_string\"));\n" + "call g_printn(circle.\"r\");\n" + "call g_printn(circle.\"s\".\"__type__\");\n" + "call g_printn(circle.\"s\".\"a\");\n" + "call g_printn(circle.\"s\".\"b\");\n" + "call g_printn(circle.\"__type__\");\n" + "call g_printn(square.\"__type__\");\n" + "set square::\"a\" = 100;\n" + "set square::\"b\" = 120;\n" + "call g_printn(circle.\"s\".\"a\");\n" + "call g_printn(circle.\"s\".\"b\");\n" + "call g_printn(invoke circle::\"get_area\"());\n" + "call g_printn(invoke square::\"get_area\"());\n" + "call g_printn(invoke circle::\"get_index\"(1));\n" + "call g_printn(invoke square::\"get_index\"(2));\n" + "", "import \"sys.base\";\n" + "var c = yield ~(){throw 3;};\n" + "var b = yield ~(){\n" + "try{foreach(var k : c()){}yield 1;}\n" + "catch{\n" + "  yield 4;throw 2;}};\n" + "/*g_set_debug(true);*/\n" + "try{foreach(var k : b()){g_printn(k);}}catch{g_printn(5);}\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 5 with Interpreter

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

the class TestInterpret3 method main.

public static void main(String[] args) {
    try {
        String[] codes = new String[] { "import \"sys.base\";\n" + "var a = true;\n" + "if (a) {call g_print(\"ok\");}\n" + "else {call g_print(\"failed\");}", "import \"sys.base\";\n" + "call g_print(\n" + "    call (func~(a,b,c) -> call a(b,c))(\"g_max\",5,6));\n", "import \"sys.base\";\n" + "var t = 0;\n" + "for (var i = 0; i < 10; i++) {\n" + "    if (i % 2 == 0) {\n" + "        continue;\n" + "    }\n" + "    let t = t + i;\n" + "}\n" + "call g_print(t);\n", "import \"sys.base\";\n" + "var enumerator = func ~(f, t, v) {\n" + "    for (var i = f; i < t; i++) {\n" + "        if (i % 2 == 0) {\n" + "            continue;\n" + "        }\n" + "        call v(i);\n" + "    }\n" + "};\n" + "var sum = 0;\n" + "var set = func ~(v) {\n" + "   let sum = sum + v;\n" + "};\n" + "call enumerator(0, 10, set);\n" + "call g_print(sum);\n", "import \"sys.base\";" + "var a=func~(){var f=func~()->call g_print(\"af\");call f();};" + "var b=func~(){var f=func~()->call g_print(\"bf\");call f();};" + "call a();call b();" };
        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