use of priv.bajdcc.LL1.syntax.handler.SyntaxException 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();
}
}
use of priv.bajdcc.LL1.syntax.handler.SyntaxException 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();
}
}
use of priv.bajdcc.LL1.syntax.handler.SyntaxException 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();
}
}
use of priv.bajdcc.LL1.syntax.handler.SyntaxException 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();
}
}
use of priv.bajdcc.LL1.syntax.handler.SyntaxException 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();
}
}
Aggregations