use of priv.bajdcc.LL1.syntax.handler.SyntaxException 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.LL1.syntax.handler.SyntaxException 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.LL1.syntax.handler.SyntaxException 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();
}
}
use of priv.bajdcc.LL1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.
the class TestInterpret10 method main.
public static void main(String[] args) {
try {
String[] codes = new String[] { "import \"sys.base\";\n" + "import \"sys.list\";\n" + "import \"sys.proc\";\n" + "\n" + "var pc_router = 5;\n" + "call g_start_share(\"pc_router\", pc_router);\n" + "\n" + "var pc = func ~(index) {\n" + " var pc_router = call g_query_share(\"pc_router\");\n" + " var name = \"pc_\" + index;\n" + " var router = index / pc_router;\n" + " var display = \"PC #\" + index;\n" + " call g_printn(display + \" started\");\n" + " call g_sleep(50);\n" + " var handle = call g_create_pipe(name);\n" + " call g_printn(display + \" connecting...\");\n" + " var router_connection = \"router#\" + router;\n" + " for (;;) {\n" + " call g_sleep(100);\n" + " call g_lock_share(router_connection);\n" + " var connection = call g_query_share(router_connection);\n" + " if (call g_is_null(connection)) {\n" + " call g_unlock_share(router_connection);\n" + " continue;\n" + " }\n" + " call g_printn(display + \" connecting to #\" + router);\n" + " call g_array_add(connection, index);\n" + " call g_unlock_share(router_connection);\n" + " break;\n" + " }\n" + " var get_id = func ~(ch) {\n" + " if (ch == '@') {\n" + " call g_printn(display + \" connected to router #\" + router);\n" + " }\n" + " };\n" + " call g_read_pipe(handle, get_id);\n" + " call g_sleep(1000);\n" + " call g_printn(display + \" stopped\");\n" + "};\n" + "\n" + "var router = func ~(index) {\n" + " var pc_router = call g_query_share(\"pc_router\");\n" + " var name = \"router_\" + index;\n" + " var display = \"Router #\" + index;\n" + " call g_printn(display + \" started\");\n" + " var router_connection = \"router#\" + index;\n" + " var connection = [];\n" + " var list = [];\n" + " call g_start_share(router_connection, connection);\n" + " var connected = 0;\n" + " var handle_pc = func ~(args) {\n" + " var connected = call g_array_get(args, 0);\n" + " var pc_router = call g_array_get(args, 1);\n" + " var router_connection = call g_array_get(args, 2);\n" + " var display = call g_array_get(args, 3);\n" + " var list = call g_array_get(args, 4);\n" + " for (;;) {\n" + " call g_sleep(100);\n" + " call g_lock_share(router_connection);\n" + " var connection = call g_query_share(router_connection);\n" + " var new_pc = call g_array_pop(connection);\n" + " if (call g_is_null(new_pc)) {\n" + " call g_unlock_share(router_connection);\n" + " continue;\n" + " }\n" + " connected++;\n" + " call g_array_add(list, new_pc);\n" + " call g_printn(display + \" connecting to pc #\" + new_pc);\n" + " call g_unlock_share(router_connection);\n" + " var name = \"pc_\" + new_pc;\n" + " var handle = call g_create_pipe(name);\n" + " call g_destroy_pipe(handle);\n" + " call g_printn(display + \" connected to #\" + new_pc);\n" + " if (connected == pc_router) { break; }\n" + " }\n" + " };\n" + " var args = [];\n" + " call g_array_add(args, connected);\n" + " call g_array_add(args, pc_router);\n" + " call g_array_add(args, router_connection);\n" + " call g_array_add(args, display);\n" + " call g_array_add(args, list);\n" + " call g_join_process(call g_create_process_args(handle_pc, args));\n" + " var stop_pc = func ~(args) {\n" + " var display = call g_array_get(args, 3);\n" + " var list = call g_array_get(args, 4);\n" + " var size = call g_array_size(list);\n" + " for (var i = 0; i < size; i++) {\n" + " call g_sleep(10);\n" + " var name = \"pc_\" + call g_array_get(list, i);\n" + " var handle = call g_create_pipe(name);\n" + " call g_write_pipe(handle, \"!\");\n" + " call g_printn(display + \" disconnected with #\" + i);\n" + " }\n" + " };\n" + " call g_sleep(100);\n" + " call g_join_process(call g_create_process_args(stop_pc, args));\n" + " call g_printn(display + \" stopped\");\n" + "};\n" + "\n" + "var create_pc = func ~(n) {\n" + " var handles = [];\n" + " foreach (var i : call g_range(0, n - 1)) {\n" + " var h = call g_create_process_args(pc, i);\n" + " call g_array_add(handles, h);\n" + " call g_printn(\"Create pc: #\" + i);\n" + " }\n" + " return handles;\n" + "};\n" + "var create_router = func ~(n) {\n" + " var handles = [];\n" + " foreach (var i : call g_range(0, n - 1)) {\n" + " var h = call g_create_process_args(router, i);\n" + " call g_array_add(handles, h);\n" + " call g_printn(\"Create router: #\" + i);\n" + " }\n" + " return handles;\n" + "};\n" + "\n" + "call g_printn(\"Starting pc...\");\n" + "var pcs = call create_pc(5);\n" + "call g_printn(\"Starting router...\");\n" + "var routers = call create_router(1);\n" + "call g_join_process_array(pcs);\n" + "call g_join_process_array(routers);\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.LL1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.
the class TestSyntax 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);
Syntax syntax = new Syntax();
syntax.addTerminal("PLUS", TokenType.OPERATOR, OperatorType.PLUS);
syntax.addTerminal("MINUS", TokenType.OPERATOR, OperatorType.MINUS);
syntax.addTerminal("TIMES", TokenType.OPERATOR, OperatorType.TIMES);
syntax.addTerminal("DIVIDE", TokenType.OPERATOR, OperatorType.DIVIDE);
syntax.addTerminal("LPA", TokenType.OPERATOR, OperatorType.LPARAN);
syntax.addTerminal("RPA", TokenType.OPERATOR, OperatorType.RPARAN);
syntax.addTerminal("SYMBOL", TokenType.ID, "i");
syntax.addNonTerminal("E");
syntax.addNonTerminal("T");
syntax.addNonTerminal("F");
syntax.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>");
syntax.infer("E -> E @PLUS<+> T");
syntax.infer("E -> E @MINUS<-> T");
syntax.infer("E -> T");
syntax.infer("T -> T @TIMES<*> F");
syntax.infer("T -> T @DIVIDE</> F");
syntax.infer("T -> F");
syntax.infer("F -> @LPA<(> E @RPA<)>");
syntax.infer("F -> @SYMBOL<i>");
syntax.initialize("E");
System.out.println(syntax.toString());
System.out.println(syntax.getNGAString());
System.out.println(syntax.getNPAString());
// 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();
}
}
Aggregations