use of priv.bajdcc.util.lexer.error.RegexException 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.util.lexer.error.RegexException 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.util.lexer.error.RegexException in project jMiniLang by bajdcc.
the class TestGrammar 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);
Grammar grammar = new Grammar("3 - (28 / (4 * 7)) * (2 + 4) + 5");
grammar.addTerminal("i", TokenType.INTEGER, null);
grammar.addTerminal("PLUS", TokenType.OPERATOR, OperatorType.PLUS);
grammar.addTerminal("MINUS", TokenType.OPERATOR, OperatorType.MINUS);
grammar.addTerminal("TIMES", TokenType.OPERATOR, OperatorType.TIMES);
grammar.addTerminal("DIVIDE", TokenType.OPERATOR, OperatorType.DIVIDE);
grammar.addTerminal("LPA", TokenType.OPERATOR, OperatorType.LPARAN);
grammar.addTerminal("RPA", TokenType.OPERATOR, OperatorType.RPARAN);
String[] nons = new String[] { "E", "T", "F" };
for (String non : nons) {
grammar.addNonTerminal(non);
}
grammar.addPatternHandler("1", new IPatternHandler() {
@Override
public Object handle(List<Token> tokens, List<Object> symbols) {
return Integer.parseInt(tokens.get(0).object.toString());
}
@Override
public String getPatternName() {
return "操作数转换";
}
});
grammar.addPatternHandler("010", new IPatternHandler() {
@Override
public Object handle(List<Token> tokens, List<Object> symbols) {
int lop = (int) symbols.get(0);
int rop = (int) symbols.get(1);
Token op = tokens.get(0);
if (op.kToken == TokenType.OPERATOR) {
OperatorType kop = (OperatorType) op.object;
switch(kop) {
case PLUS:
return lop + rop;
case MINUS:
return lop - rop;
case TIMES:
return lop * rop;
case DIVIDE:
if (rop == 0) {
return lop;
} else {
return lop / rop;
}
default:
return 0;
}
} else {
return 0;
}
}
@Override
public String getPatternName() {
return "二元运算";
}
});
grammar.addPatternHandler("101", new IPatternHandler() {
@Override
public Object handle(List<Token> tokens, List<Object> symbols) {
Token ltok = tokens.get(0);
Token rtok = tokens.get(1);
Object exp = symbols.get(0);
if (ltok.object == OperatorType.LPARAN && rtok.object == OperatorType.RPARAN) {
// 判断括号
return exp;
}
return null;
}
@Override
public String getPatternName() {
return "括号运算";
}
});
grammar.infer("E -> E @PLUS T | E @MINUS T | T");
grammar.infer("T -> T @TIMES F | T @DIVIDE F | F");
grammar.infer("F -> @LPA E @RPA | @i");
grammar.initialize("E");
System.out.println(grammar.getPrecedenceString());
System.out.println(grammar.toString());
grammar.run();
System.out.println(grammar.getTokenString());
// 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();
} catch (GrammarException e) {
System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
e.printStackTrace();
}
}
use of priv.bajdcc.util.lexer.error.RegexException in project jMiniLang by bajdcc.
the class TestGrammar2 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);
Grammar grammar = new Grammar("!3 - (28 / (!4 * 7)) * (2 + 4) + 5");
grammar.addTerminal("i", TokenType.INTEGER, null);
grammar.addTerminal("NEGATIVE", TokenType.OPERATOR, OperatorType.LOGICAL_NOT);
grammar.addTerminal("PLUS", TokenType.OPERATOR, OperatorType.PLUS);
grammar.addTerminal("MINUS", TokenType.OPERATOR, OperatorType.MINUS);
grammar.addTerminal("TIMES", TokenType.OPERATOR, OperatorType.TIMES);
grammar.addTerminal("DIVIDE", TokenType.OPERATOR, OperatorType.DIVIDE);
grammar.addTerminal("LPA", TokenType.OPERATOR, OperatorType.LPARAN);
grammar.addTerminal("RPA", TokenType.OPERATOR, OperatorType.RPARAN);
String[] nons = new String[] { "E", "T", "F", "G" };
for (String non : nons) {
grammar.addNonTerminal(non);
}
grammar.addPatternHandler("1", new IPatternHandler() {
@Override
public Object handle(List<Token> tokens, List<Object> symbols) {
return Integer.parseInt(tokens.get(0).object.toString());
}
@Override
public String getPatternName() {
return "操作数转换";
}
});
grammar.addPatternHandler("010", new IPatternHandler() {
@Override
public Object handle(List<Token> tokens, List<Object> symbols) {
int lop = (int) symbols.get(0);
int rop = (int) symbols.get(1);
Token op = tokens.get(0);
if (op.kToken == TokenType.OPERATOR) {
OperatorType kop = (OperatorType) op.object;
switch(kop) {
case PLUS:
return lop + rop;
case MINUS:
return lop - rop;
case TIMES:
return lop * rop;
case DIVIDE:
if (rop == 0) {
return lop;
} else {
return lop / rop;
}
default:
return 0;
}
} else {
return 0;
}
}
@Override
public String getPatternName() {
return "二元运算";
}
});
grammar.addPatternHandler("101", new IPatternHandler() {
@Override
public Object handle(List<Token> tokens, List<Object> symbols) {
Token ltok = tokens.get(0);
Token rtok = tokens.get(1);
Object exp = symbols.get(0);
if (ltok.object == OperatorType.LPARAN && rtok.object == OperatorType.RPARAN) {
// 判断括号
return exp;
}
return null;
}
@Override
public String getPatternName() {
return "括号运算";
}
});
grammar.addPatternHandler("10", new IPatternHandler() {
@Override
public Object handle(List<Token> tokens, List<Object> symbols) {
Token unary = tokens.get(0);
int op = (int) symbols.get(0);
if (unary.object == OperatorType.LOGICAL_NOT) {
// 判断取反
return -op;
}
return null;
}
@Override
public String getPatternName() {
return "一元运算";
}
});
grammar.infer("E -> E @PLUS T | E @MINUS T | T");
grammar.infer("T -> T @TIMES F | T @DIVIDE F | F");
grammar.infer("F -> @NEGATIVE G | G");
grammar.infer("G -> @LPA E @RPA | @i");
grammar.initialize("E");
System.out.println(grammar.getPrecedenceString());
System.out.println(grammar.toString());
grammar.run();
System.out.println(grammar.getTokenString());
// 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();
} catch (GrammarException e) {
System.err.println(e.getPosition() + "," + e.getMessage() + " " + e.getInfo());
e.printStackTrace();
}
}
use of priv.bajdcc.util.lexer.error.RegexException 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