use of priv.bajdcc.LL1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.
the class RuntimeProcess method getPage.
public RuntimeCodePage getPage(String name) throws Exception {
String vfs = null;
if (arrCodes.containsKey(name) || (vfs = service.getFileService().getVfs(name)) != null) {
String code = vfs == null ? arrCodes.get(name) : vfs;
if (arrPages.containsKey(code)) {
return arrPages.get(code);
} else {
logger.debug("Loading page: " + name);
try {
Grammar grammar = new Grammar(code);
RuntimeCodePage page = grammar.getCodePage();
service.getFileService().addVfs(name, code);
arrPages.put(name, page);
return page;
} catch (SyntaxException e) {
String filename = pageFileMap.get(name);
e.setFileName(filename == null ? "Unknown Source" : (filename + ".txt"));
e.setPageName(name);
System.err.println("#PAGE ERROR# --> " + name);
throw e;
}
}
}
return null;
}
use of priv.bajdcc.LL1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.
the class TestGrammar2 method main.
public static void main(String[] args) {
try {
String[] exprs = new String[] { "var a = 1;", "var a = 4 + 4;", "var a = !(!true * (++4 / !4 + 4 ? 5 & 6 && true ^ !\"ddd\" | 'r' : 8.0+9*9));", "var a = 3.0 + 4.0 / 7 * 8.9;", "var a = 3000.0 << ++2.0;", "var a = true << 9 ? 1 + 3 : 2;", "var a = 5;let a = a + 4 + a ? 3 : 5;var t = func main1(){};", "var d= func [\"d\",\"t\"] ~()->a;", "var d = func ~()->4;", "var a = call (func ~(a) {call (func b(c){var b = '5'+ 66; call b(666);})();}) (6);", "var a = call (func ~(a) {\n" + "call (func b(c){\n" + "var c = '5'+ 1;\n" + "var d = c;\n" + "//call b(666);\n" + "let c = 7;\n" + "})(7);\n" + "}) (44);", "var a = call\n" + "(func ~(x, y) -> x + y)\n" + " (1, 2);\n", "var d = 4;" + "var c = d || 6;" };
/*
* BufferedReader br = new BufferedReader(new
* FileReader("E:/http.c")); String line = ""; StringBuffer sb = new
* StringBuffer(); while ((line = br.readLine()) != null) {
* sb.append(line + System.lineSeparator()); }
* br.close();
*/
Grammar grammar = new Grammar(exprs[exprs.length - 1]);
System.out.println(grammar.toString());
RuntimeCodePage page = grammar.getCodePage();
System.out.println(page.toString());
RuntimeMachine machine = new RuntimeMachine();
machine.run("test1", page);
// FileWriter fw = new FileWriter("E:/testgrammar.txt");
// fw.append(grammar.toString());
// fw.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 (IOException e) {
// System.err.println(e.getMessage());
// e.printStackTrace();
} catch (RuntimeException e) {
System.err.println(e.getPosition() + ": " + e.getInfo());
e.printStackTrace();
// } catch (IOException e) {
// System.err.println(e.getMessage());
// e.printStackTrace();
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
// } catch (IOException e) {
// System.err.println(e.getMessage());
// e.printStackTrace();
}
}
use of priv.bajdcc.LL1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.
the class TestGrammar3 method main.
public static void main(String[] args) {
try {
String a = "\n" + "var g_a = func ~(x, y) { return x + y;};\n" + "export \"g_a\";";
String b = "import \"test1\";\n" + "var d = call g_a(1,2);\n" + "var c = g_gk;\n" + "call g_print(c);\n" + "var t = call g_print(c);";
/*
* BufferedReader br = new BufferedReader(new
* FileReader("E:/http.c")); String line = ""; StringBuffer sb = new
* StringBuffer(); while ((line = br.readLine()) != null) {
* sb.append(line + System.lineSeparator()); }
* br.close();
*/
Grammar grammar = new Grammar(a);
System.out.println(grammar.toString());
RuntimeCodePage page = grammar.getCodePage();
System.out.println(page.toString());
RuntimeMachine machine = new RuntimeMachine();
machine.run("test1", page);
Grammar grammar2 = new Grammar(b);
System.out.println(grammar2.toString());
RuntimeCodePage page2 = grammar2.getCodePage();
page2.getInfo().addExternalValue("g_gk", () -> new RuntimeObject("abc"));
page2.getInfo().addExternalFunc("g_print", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "Print";
}
@Override
public RuntimeObjectType[] getArgsType() {
return new RuntimeObjectType[] { RuntimeObjectType.kObject };
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
System.out.println(args.get(0).getObj());
return null;
}
});
System.out.println(page2.toString());
machine.run("test2", page2);
// FileWriter fw = new FileWriter("E:/testgrammar.txt");
// fw.append(grammar.toString());
// fw.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 (IOException e) {
// System.err.println(e.getMessage());
// e.printStackTrace();
} catch (RuntimeException e) {
System.err.println(e.getPosition() + ": " + e.getInfo());
e.printStackTrace();
// } catch (IOException e) {
// System.err.println(e.getMessage());
// e.printStackTrace();
} catch (Exception e) {
System.err.println(e.getMessage());
e.printStackTrace();
// } catch (IOException e) {
// System.err.println(e.getMessage());
// e.printStackTrace();
}
}
use of priv.bajdcc.LL1.syntax.handler.SyntaxException 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();
}
}
use of priv.bajdcc.LL1.syntax.handler.SyntaxException in project jMiniLang by bajdcc.
the class Semantic method run.
/**
* 进行语义处理
*/
private void run() throws SyntaxException {
if (!arrErrors.isEmpty()) {
System.err.println(getTrackerError());
throw new SyntaxException(SyntaxError.COMPILE_ERROR, arrErrors.get(0).position, "出现语法错误");
}
/* 规则集合 */
ArrayList<RuleItem> items = npa.getRuleItems();
/* 符号表查询接口 */
IQuerySymbol query = getQuerySymbolService();
/* 符号表管理接口 */
IManageSymbol manage = getManageSymbolService();
/* 语义错误处理接口 */
ISemanticRecorder recorder = getSemanticRecorderService();
/* 复制单词流 */
ArrayList<Token> tokens = arrTokens.stream().map(Token::copy).collect(Collectors.toCollection(ArrayList::new));
/* 运行时自动机 */
SemanticMachine machine = new SemanticMachine(items, arrActions, tokens, query, manage, recorder, debug);
/* 遍历所有指令 */
arrInsts.forEach(machine::run);
object = machine.getObject();
if (object != null) {
Function entry = (Function) object;
manage.getManageScopeService().registerFunc(entry);
}
}
Aggregations