use of priv.bajdcc.LALR1.grammar.runtime.RuntimeObject in project jMiniLang by bajdcc.
the class RuntimeMachine method opCallExtern.
@Override
public void opCallExtern(boolean invoke) throws Exception {
int idx = loadInt();
String name = "";
if (invoke) {
RuntimeStack itStack = stack;
RuntimeObject obj = null;
while (obj == null && itStack != null) {
obj = itStack.findVariable(pageName, idx);
itStack = itStack.prev;
}
if (obj == null) {
err(RuntimeError.WRONG_LOAD_EXTERN, String.valueOf(idx));
}
if (obj.getType() == RuntimeObjectType.kFunc) {
RuntimeFuncObject func = (RuntimeFuncObject) obj.getObj();
Map<Integer, RuntimeObject> env = func.getEnv();
if (env != null) {
for (Entry<Integer, RuntimeObject> entry : env.entrySet()) {
int id = entry.getKey();
RuntimeObject o = entry.getValue();
if (o != null) {
if (o.getSymbol() == null)
o.setSymbol(currentPage.getData().get(id));
o.setReadonly(false);
}
stack.storeClosure(id, o);
}
stack.pushData(obj);
}
int address = func.getAddr();
stack.opCall(address, func.getPage(), stack.reg.execId, pageName, pageMap.get(func.getPage()).getInfo().getFuncNameByAddress(address));
stack.reg.execId = address;
stack.reg.pageId = func.getPage();
switchPage();
pop();
return;
} else if (obj.getType() == RuntimeObjectType.kString) {
name = obj.getObj().toString();
} else {
err(RuntimeError.WRONG_LOAD_EXTERN, obj.toString());
}
} else {
RuntimeObject obj = fetchFromGlobalData(idx);
name = obj.getObj().toString();
}
List<RuntimeCodePage> refers = pageRefer.get(pageName);
for (RuntimeCodePage page : refers) {
int address = page.getInfo().getAddressOfExportFunc(name);
if (address != -1) {
String jmpPage = page.getInfo().getDataMap().get("name").toString();
stack.opCall(address, jmpPage, stack.reg.execId, stack.reg.pageId, name);
stack.reg.execId = address;
stack.reg.pageId = jmpPage;
switchPage();
return;
}
}
for (RuntimeCodePage page : refers) {
IRuntimeDebugExec exec = page.getInfo().getExecCallByName(name);
if (exec != null) {
int argsCount = stack.getFuncArgsCount();
RuntimeObjectType[] types = exec.getArgsType();
if ((types == null && argsCount != 0) || (types != null && types.length != argsCount)) {
err(RuntimeError.WRONG_ARGCOUNT, name + " " + String.valueOf(argsCount));
}
List<RuntimeObject> args = new ArrayList<>();
for (int i = 0; i < argsCount; i++) {
RuntimeObjectType type = types[i];
RuntimeObject objParam = stack.loadFuncArgs(i);
if (type != RuntimeObjectType.kObject) {
RuntimeObjectType objType = objParam.getType();
if (objType != type) {
Token token = Token.createFromObject(objParam.getObj());
TokenType objTokenType = RuntimeObject.toTokenType(type);
if (objTokenType == TokenType.ERROR) {
err(RuntimeError.WRONG_ARGTYPE, name + " " + objTokenType.getName());
}
if (!TokenTools.promote(objTokenType, token)) {
err(RuntimeError.UNDEFINED_CONVERT, name + " " + token.toString() + " " + objTokenType.getName());
} else {
objParam.setObj(token.object);
}
}
}
args.add(objParam);
}
stack.opCall(stack.reg.execId, stack.reg.pageId, stack.reg.execId, stack.reg.pageId, name);
RuntimeObject retVal = exec.ExternalProcCall(args, this);
if (retVal == null) {
store(new RuntimeObject(null));
} else {
store(retVal);
}
opReturn();
return;
}
}
err(RuntimeError.WRONG_LOAD_EXTERN, name);
}
use of priv.bajdcc.LALR1.grammar.runtime.RuntimeObject 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.LALR1.grammar.runtime.RuntimeObject in project jMiniLang by bajdcc.
the class ModuleList method getCodePage.
@Override
public RuntimeCodePage getCodePage() throws Exception {
if (runtimeCodePage != null)
return runtimeCodePage;
String base = ResourceLoader.load(getClass());
Grammar grammar = new Grammar(base);
RuntimeCodePage page = grammar.getCodePage();
IRuntimeDebugInfo info = page.getInfo();
info.addExternalValue("g_new_array", () -> new RuntimeObject(new RuntimeArray()));
info.addExternalValue("g_new_map", () -> new RuntimeObject(new RuntimeMap()));
buildMethod(info);
return runtimeCodePage = page;
}
use of priv.bajdcc.LALR1.grammar.runtime.RuntimeObject in project jMiniLang by bajdcc.
the class ModuleMath method getCodePage.
@Override
public RuntimeCodePage getCodePage() throws Exception {
if (runtimeCodePage != null)
return runtimeCodePage;
String base = ResourceLoader.load(getClass());
Grammar grammar = new Grammar(base);
RuntimeCodePage page = grammar.getCodePage();
IRuntimeDebugInfo info = page.getInfo();
info.addExternalValue("g_PI", () -> new RuntimeObject(BigDecimal.valueOf(Math.PI)));
info.addExternalValue("g_PI_2", () -> new RuntimeObject(BigDecimal.valueOf(Math.PI * 2.0)));
info.addExternalValue("g_E", () -> new RuntimeObject(BigDecimal.valueOf(Math.E)));
info.addExternalValue("g_random", () -> new RuntimeObject(BigDecimal.valueOf(rand.nextDouble())));
buildUnaryFunc(info);
return runtimeCodePage = page;
}
use of priv.bajdcc.LALR1.grammar.runtime.RuntimeObject in project jMiniLang by bajdcc.
the class ModuleNet method buildRemoteMethods.
private void buildRemoteMethods(IRuntimeDebugInfo info) {
info.addExternalFunc("g_net_get", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "HTTP GET";
}
@Override
public RuntimeObjectType[] getArgsType() {
return new RuntimeObjectType[] { RuntimeObjectType.kString };
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
String text = "";
String txt = String.valueOf(args.get(0).getObj());
logger.debug("Request url: " + txt);
try {
URL url = new URL(txt);
// 打开连接
URLConnection urlConnection = url.openConnection();
// 获取输入流
BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8"));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
text = sb.toString();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return new RuntimeObject(text);
}
});
info.addExternalFunc("g_net_get_json", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "HTTP GET - json";
}
@Override
public RuntimeObjectType[] getArgsType() {
return new RuntimeObjectType[] { RuntimeObjectType.kString };
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
String text = "";
String txt = String.valueOf(args.get(0).getObj());
logger.debug("Request url(json): " + txt);
try {
URL url = new URL(txt);
// 打开连接
URLConnection urlConnection = url.openConnection();
// 获取输入流
BufferedReader br = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8"));
String line = null;
StringBuilder sb = new StringBuilder();
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
}
text = sb.toString();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return parseJson(text);
}
});
// -----------------------------------
// server
info.addExternalFunc("g_net_msg_create_server_internal", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "MSG SERVER";
}
@Override
public RuntimeObjectType[] getArgsType() {
return new RuntimeObjectType[] { RuntimeObjectType.kInt };
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
if (getServer() != null || getClient() != null)
return new RuntimeObject(false);
int port = ((BigInteger) args.get(0).getObj()).intValue();
setServer(new ModuleNetServer(port));
getServer().start();
return new RuntimeObject(true);
}
});
info.addExternalFunc("g_net_msg_shutdown_server", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "MSG SERVER SHUTDOWN";
}
@Override
public RuntimeObjectType[] getArgsType() {
return null;
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
if (getServer() == null)
return new RuntimeObject(false);
getServer().exit();
return new RuntimeObject(true);
}
});
info.addExternalFunc("g_net_msg_get_server_status", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "MSG SERVER GET STATUS";
}
@Override
public RuntimeObjectType[] getArgsType() {
return null;
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
if (getServer() != null) {
ModuleNetServer.Status s = getServer().getStatus();
if (s == ModuleNetServer.Status.ERROR) {
lastError = getServer().getError();
setServer(null);
}
status.getService().getProcessService().sleep(status.getPid(), MSG_QUERY_TIME);
return new RuntimeObject(BigInteger.valueOf(s.ordinal()));
}
return new RuntimeObject(BigInteger.valueOf(ModuleNetServer.Status.NULL.ordinal()));
}
});
// server
// -----------------------------------
// client
info.addExternalFunc("g_net_msg_create_client_internal", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "MSG CLIENT";
}
@Override
public RuntimeObjectType[] getArgsType() {
return new RuntimeObjectType[] { RuntimeObjectType.kString };
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
if (getServer() != null || getClient() != null)
return new RuntimeObject(false);
String addr = String.valueOf(args.get(0).getObj());
setClient(new ModuleNetClient(addr));
getClient().start();
return new RuntimeObject(true);
}
});
info.addExternalFunc("g_net_msg_shutdown_client", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "MSG CLIENT SHUTDOWN";
}
@Override
public RuntimeObjectType[] getArgsType() {
return null;
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
if (getClient() == null)
return new RuntimeObject(false);
getClient().exit();
return new RuntimeObject(true);
}
});
info.addExternalFunc("g_net_msg_get_client_status", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "MSG CLIENT GET STATUS";
}
@Override
public RuntimeObjectType[] getArgsType() {
return null;
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
if (getClient() != null) {
ModuleNetClient.Status s = getClient().getStatus();
if (s == ModuleNetClient.Status.ERROR) {
lastError = getClient().getError();
setClient(null);
}
status.getService().getProcessService().sleep(status.getPid(), MSG_QUERY_TIME);
return new RuntimeObject(BigInteger.valueOf(s.ordinal()));
}
return new RuntimeObject(BigInteger.valueOf(ModuleNetClient.Status.NULL.ordinal()));
}
});
info.addExternalFunc("g_net_msg_client_send", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "MSG CLIENT SEND";
}
@Override
public RuntimeObjectType[] getArgsType() {
return new RuntimeObjectType[] { RuntimeObjectType.kString };
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
if (getClient() != null) {
ModuleNetClient.Status s = getClient().getStatus();
if (s == ModuleNetClient.Status.RUNNING) {
status.getService().getProcessService().sleep(status.getPid(), MSG_SEND_TIME);
getClient().send(String.valueOf(args.get(0).getObj()));
return new RuntimeObject(true);
}
}
return new RuntimeObject(false);
}
});
info.addExternalFunc("g_net_msg_client_send_with_origin", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "MSG CLIENT SEND(ORIGIN)";
}
@Override
public RuntimeObjectType[] getArgsType() {
return new RuntimeObjectType[] { RuntimeObjectType.kString, RuntimeObjectType.kString };
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
if (getClient() != null) {
ModuleNetClient.Status s = getClient().getStatus();
if (s == ModuleNetClient.Status.RUNNING) {
status.getService().getProcessService().sleep(status.getPid(), MSG_SEND_TIME);
getClient().send(String.valueOf(args.get(0).getObj()), String.valueOf(args.get(1).getObj()));
return new RuntimeObject(true);
}
}
return new RuntimeObject(false);
}
});
info.addExternalFunc("g_net_msg_server_send", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "MSG SERVER SEND";
}
@Override
public RuntimeObjectType[] getArgsType() {
return new RuntimeObjectType[] { RuntimeObjectType.kString };
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
if (getServer() != null) {
ModuleNetServer.Status s = getServer().getStatus();
if (s == ModuleNetServer.Status.RUNNING) {
status.getService().getProcessService().sleep(status.getPid(), MSG_SEND_TIME);
getServer().send(String.valueOf(args.get(0).getObj()));
return new RuntimeObject(true);
}
}
return new RuntimeObject(false);
}
});
info.addExternalFunc("g_net_msg_server_send_error", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "MSG SERVER SEND ERROR";
}
@Override
public RuntimeObjectType[] getArgsType() {
return new RuntimeObjectType[] { RuntimeObjectType.kString };
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
if (getServer() != null) {
ModuleNetServer.Status s = getServer().getStatus();
if (s == ModuleNetServer.Status.RUNNING) {
status.getService().getProcessService().sleep(status.getPid(), MSG_SEND_TIME);
getServer().send_error(String.valueOf(args.get(0).getObj()));
return new RuntimeObject(true);
}
}
return new RuntimeObject(false);
}
});
info.addExternalFunc("g_net_msg_server_send_with_origin", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "MSG SERVER SEND(ORIGIN)";
}
@Override
public RuntimeObjectType[] getArgsType() {
return new RuntimeObjectType[] { RuntimeObjectType.kString, RuntimeObjectType.kString };
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
if (getServer() != null) {
ModuleNetServer.Status s = getServer().getStatus();
if (s == ModuleNetServer.Status.RUNNING) {
status.getService().getProcessService().sleep(status.getPid(), MSG_SEND_TIME);
getServer().send(String.valueOf(args.get(0).getObj()), String.valueOf(args.get(1).getObj()));
return new RuntimeObject(true);
}
}
return new RuntimeObject(false);
}
});
// client
// -----------------------------------
info.addExternalFunc("g_net_msg_get_error", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "MSG SERVER GET ERROR";
}
@Override
public RuntimeObjectType[] getArgsType() {
return null;
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
return new RuntimeObject(lastError);
}
});
info.addExternalFunc("g_net_msg_get_server_msg", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "MSG SERVER GET MESSAGE";
}
@Override
public RuntimeObjectType[] getArgsType() {
return null;
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
return new RuntimeObject(getServer() == null ? null : getServer().getMessage());
}
});
info.addExternalFunc("g_net_msg_get_client_msg", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "MSG CLIENT GET MESSAGE";
}
@Override
public RuntimeObjectType[] getArgsType() {
return null;
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
return new RuntimeObject(getClient() == null ? null : getClient().getMessage());
}
});
info.addExternalFunc("g_net_msg_get_client_addr", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "MSG CLIENT GET ADDRESS";
}
@Override
public RuntimeObjectType[] getArgsType() {
return null;
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
return new RuntimeObject(getClient() == null ? null : getClient().getAddr());
}
});
info.addExternalFunc("g_net_parse_json", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "Parse json";
}
@Override
public RuntimeObjectType[] getArgsType() {
return new RuntimeObjectType[] { RuntimeObjectType.kString };
}
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
return parseJsonSafe(String.valueOf(args.get(0).getObj()));
}
});
info.addExternalFunc("g_net_get_rss", new IRuntimeDebugExec() {
@Override
public String getDoc() {
return "RSS GET(BAIDU)";
}
@Override
public RuntimeObjectType[] getArgsType() {
return new RuntimeObjectType[] { RuntimeObjectType.kString };
}
@SuppressWarnings("unchecked")
@Override
public RuntimeObject ExternalProcCall(List<RuntimeObject> args, IRuntimeStatus status) {
String text = "";
String txt = String.valueOf(args.get(0).getObj());
logger.debug("Request url(rss): " + txt);
RuntimeArray array = new RuntimeArray();
final Pattern pattern = Pattern.compile("<br>(.*)<br />");
try {
SAXReader reader = new SAXReader();
Document document = reader.read(txt);
Node title = document.selectSingleNode("//title");
array.add(new RuntimeObject(title.getText()));
List<Node> list = document.selectNodes("//item");
for (Node item : list) {
String itemTitle = item.valueOf("title");
array.add(new RuntimeObject(itemTitle));
String itemDescription = item.valueOf("description");
Matcher matcher = pattern.matcher(itemDescription);
if (matcher.find()) {
array.add(new RuntimeObject(matcher.group(1)));
} else {
array.add(new RuntimeObject(itemDescription.replace("<br />", "")));
}
}
return new RuntimeObject(array);
} catch (Exception e) {
e.printStackTrace();
}
return new RuntimeObject(array);
}
});
}
Aggregations