Libraryless. Click here for Pure Java version (2069L/17K/49K).
!747 m { static S question = "Is port 80 open?"; static S answer; static Throwable error; // pattern, class name, method static String[][] entries = { {"is port * open", "ThePortOracle", "isPort_open"}, {"what time is it", "TheTimeOracle", "whatTimeIsIt"}, {"is * java compilable", "TheJavaOracle", "is_JavaCompilable"}, {"is * java-compilable", "TheJavaOracle", "is_JavaCompilable"}, {"is * a snippet", "TheSnippetOracle", "is_aSnippet"}, }; static class ThePortOracle { static S isPort_open(S s) { int port = parseInt(s); print("I'll check port " + port + "..."); boolean b = isPortOpen(port); return b ? "Yes, it is open!" : "No, it is not open."; } } static class TheTimeOracle { static S whatTimeIsIt() { return "It is " + new java.text.SimpleDateFormat("HH:mm").format(new Date(now())) + "."; } } static class TheJavaOracle { static S is_JavaCompilable(S s) { boolean b = isJavaCompilable(s); // Caveat not needed anymore - now function is actually safe /*if (b) print("This answer is only 99% safe. (See: #1000888)");*/ return b ? "Yes." : "No."; } } static S answerQuestion(S question) { L<S> tok = parse(question); for (S[] e : entries) { S[] m = match2(parse(e[0]), tok); if (m != null) ctex { Class c = Class.forName("main$" + e[1]); return "" + call(c, e[2], (Object[]) unquoteAll(m)); } } return unknown(question); } static S unknown(S question) { return "? Unknown question: " + question; } p { if (args.length != 0) question = smartJoin(args); print("Q: " + question); answer = null; error = null; try { answer = answerQuestion(question); print("A: " + answer); } catch (Throwable e) { error = e; print(getStackTrace(e)); print("Internal error!"); } } static L<S> parse(S s) { return tokensToLowerCase(dropPunctuation(javaTokPlusPeriod(s))); } static S smartJoin(S[] args) { if (args.length == 1) return args[0]; S[] a = new S[args.length]; for (int i = 0; i < a.length; i++) a[i] = args[i].indexOf(' ') >= 0 ? quote(args[i]) : args[i]; return join(" ", a); } static S[] unquoteAll(S[] tokens) { S[] a = new S[tokens.length]; for (int i = 0; i < a.length; i++) a[i] = unquote(tokens[i]); return a; } }
Began life as a copy of #1000886
download show line numbers debug dex old transpilations
Travelled to 16 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, jtubtzbbkimh, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, teubizvjbppd, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1000887 |
Snippet name: | Answer questions 4 |
Eternal ID of this version: | #1000887/1 |
Text MD5: | 68b4422ec994a686484479ca3da3316b |
Transpilation MD5: | 99aec342cff27a987ca4a2781485f7c1 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-09-05 16:23:11 |
Source code size: | 2571 bytes / 95 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 752 / 1524 |
Referenced in: | [show references] |