use of priv.bajdcc.LALR1.grammar.runtime.RuntimeException 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.LALR1.grammar.runtime.RuntimeException 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();
}
}
use of priv.bajdcc.LALR1.grammar.runtime.RuntimeException in project jMiniLang by bajdcc.
the class TestInterpret5 method main.
public static void main(String[] args) {
try {
String[] codes = new String[] { "import \"sys.base\";\n" + "var to_list = func ~(a, b, c) {\n" + " foreach (var i : call a(b, c)) {\n" + " call g_printn(i);\n" + " }\n" + "};\n" + "call to_list(\"g_range\", 1, 100);\n" + "//call g_range(1, 6);\n" + "\n", "import \"sys.base\";\n" + "var a = 1;" + "if (a == 1) {" + " call g_printn('x');" + "} else if (a == 2) {" + " call g_printn('y');" + "}" + "\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.LALR1.grammar.runtime.RuntimeException in project jMiniLang by bajdcc.
the class TestInterpret8 method main.
public static void main(String[] args) {
try {
String[] codes = new String[] { "import \"sys.base\"; import \"sys.list\";\n" + "var a = [];\n" + "call g_array_add(a, 5);\n" + "call g_array_set(a, 0, 4);\n" + "call g_printn(call g_array_get(a, 0));\n" + "call g_array_remove(a, 0);\n" + "call g_array_add(a, 50);\n" + "call g_array_add(a, 100);\n" + "call g_array_set(a, 1, 400);\n" + "call g_printn(call g_array_get(a, 1));\n" + "call g_array_pop(a);\n" + "call g_array_pop(a);\n" + "call g_printn(call g_array_size(a));\n" + "\n" + "let a = {};\n" + "call g_map_put(a, \"x\", 5);\n" + "call g_map_put(a, \"y\", 10);\n" + "call g_map_put(a, \"x\", 50);\n" + "call g_printn(call g_map_size(a));\n" + "call g_printn(call g_map_get(a, \"x\"));\n" + "call g_printn(call g_map_get(a, \"y\"));\n" + "call g_printn(call g_map_contains(a, \"x\"));\n" + "call g_map_remove(a, \"x\");\n" + "call g_printn(call g_map_contains(a, \"x\"));\n" + "call g_printn(call g_map_size(a));\n" + "\n", "import \"sys.base\"; import \"sys.list\";\n" + "var create_node = func ~(data) {\n" + " var new_node = g_new_map;\n" + " call g_map_put(new_node, \"data\", data);\n" + " call g_map_put(new_node, \"prev\", g_null);\n" + " call g_map_put(new_node, \"next\", g_null);\n" + " return new_node;\n" + "};\n" + "var append = func ~(head, obj) {\n" + " var new_node = call create_node(obj);\n" + " call g_map_put(new_node, \"next\", head);\n" + " call g_map_put(head, \"prev\", new_node);\n" + " return new_node;" + "};\n" + "var head = call create_node(0);\n" + "foreach (var i : call g_range(1, 10)) {\n" + " let head = call append(head, i);\n" + "}\n" + "var p = head;\n" + "while (!call g_is_null(p)) {\n" + " call g_printn(call g_map_get(p, \"data\"));\n" + " let p = call g_map_get(p, \"next\");\n" + "}\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();
}
}
use of priv.bajdcc.LALR1.grammar.runtime.RuntimeException in project jMiniLang by bajdcc.
the class TestInterpret method main.
public static void main(String[] args) {
try {
String[] codes = new String[] { "import \"sys.base\";" + "call g_print(call g_is_null(g_null));", "import \"sys.base\";" + "call g_print(\"Hello World!\\\n\");" + "call g_print_err(\"Hello World!\\\n\");", "import \"sys.base\";\n" + "call g_print(\"请输入:\");\n" + "call g_print(call g_stdin_read_line() + g_endl);\n" + "call g_print(\"输入两个数字:\");\n" + "call g_print(\n" + " \"较大数是:\" +" + " call g_to_string(\n" + " call g_max(call g_stdin_read_int(), call g_stdin_read_int()))\n" + " + \"\\n\"\n" + ");\n" + "call g_print(\"输入两个数字:\");\n" + "call g_print(\n" + " \"较小数是:\" +" + " call g_to_string(\n" + " call g_min(call g_stdin_read_int(), call g_stdin_read_int()))\n" + " + \"\\n\"\n" + ");\n", "import \"sys.base\";\n" + "call g_print(call g_doc(\"g_author\") + g_endl);\n" + "call g_print(call g_doc(\"g_print\") + g_endl);\n" + "call g_print(call g_doc(\"g_stdin_read_int\") + g_endl);\n" + "call g_print(call g_doc(\"g_new\") + g_endl);\n" + "call g_print(call g_to_string(call g_new(g_endl)));\n" + "call g_print(call g_to_string(call g_new(5)));\n" // "import \"sys.base\";\n"
// + "var f = func ~(n) ->\n"
// + " n <= 2 ? 1 : call f(n-1) + call f(n-2);"
// + "call g_print(call f(6));\n"
// ,
// "import \"sys.base\";\n"
// + "call g_print(call g_load_func(\"g_max\"));\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();
}
}
Aggregations