import java.util.*;
import java.util.zip.*;
import java.util.List;
import java.util.regex.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.locks.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.*;
import javax.swing.table.*;
import java.io.*;
import java.net.*;
import java.lang.reflect.*;
import java.lang.ref.*;
import java.lang.management.*;
import java.security.*;
import java.security.spec.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.imageio.*;
import java.math.*;
import javax.net.ssl.*;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.text.SimpleDateFormat;
import java.text.*;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import java.text.NumberFormat;
import java.awt.datatransfer.StringSelection;
class main {
public static void main(final String[] args) throws Exception { pExperiment();
printStruct(nlLogic_parseRule("\n [id: abcdabcdabcd, other option]\n what's => up\n "));
printStruct(nlLogic_parseRule("\n [id: dkflfwrpxmpsnsmu] i(give me a question) => evalOutput(random(mL(\"Question Examples\")))\n "));
}
static void pExperiment() {
if (!isMain()) return;
//substance();
autoRestart(5);
experimentIcon();
centerHigherConsole();
tt();
}
static A printStruct(String prefix, A a) {
printStructure(prefix, a);
return a;
}
static A printStruct(A a) {
printStructure(a);
return a;
}
static IfThen nlLogic_parseRule(String s) {
IfThen rule = new IfThen();
rule.originalText = s;
List tok = javaTokPlusBrackets2(s);
if (isSquareBracketed(get(tok, 1))) {
List tokOptions = javaTokPlusBrackets2(deSquareBracket(get(tok, 1)));
Matches m = new Matches();
for (String option : tok_splitAtComma(tokOptions)) {
if (startsWith_trim(option, "id: ", m))
rule.globalID = m.rest();
else
rule.options = linkedHashSet_addWithAutoInit(rule.options, option);
}
tok = dropFirst(2, tok);
}
List l = splitAtDoubleArrow(tok);
if (l(l) > 2 || l(l) == 0) return null;
rule.out = nlLogic_parseExpression(last(l));
rule.in = nlLogic_parseExpression(nextToLast(l));
if (rule.globalID == null)
//rule.globalID = aGlobalID();
rule.globalID = textMD5ToGlobalID(str(rule));
return rule;
}
static boolean isMain() {
return isMainProgram();
}
static double autoRestart_interval = 10;
static boolean autoRestart_on, autoRestart_debug, autoRestart_simulate;
static java.util.Timer autoRestart_timer;
static void autoRestart(double interval) {
autoRestart_interval = interval;
autoRestart();
}
static void autoRestart() {
if (!isMain() || autoRestart_on) return;
autoRestart_on = true;
autoRestart_schedule();
preloadProgramTitle();
}
static void autoRestart_off() {
if (!autoRestart_on) return;
stopTimer(autoRestart_timer);
autoRestart_timer = null;
}
static void autoRestart_schedule() {
autoRestart_timer = doLater_daemon(toMS(autoRestart_interval), "autoRestart_check");
}
static void autoRestart_check() {
try {
String newMD5;
try {
newMD5 = serverAutoRestartMD5(programID());
} catch (Throwable e) {
return;
}
if (!isMD5(newMD5)) { if (autoRestart_debug) print("autoRestart: no server transpilation"); return; }
if (autoRestart_localMD5 == null)
autoRestart_localMD5 = md5(loadCachedTranspilation(programID()));
String localMD5 = autoRestart_localMD5();
if (neq(localMD5, newMD5)) {
if (autoRestart_simulate)
print("Would upgrade now. " + localMD5 + " -> " + newMD5);
else {
infoBox("Upgrading " + programTitle());
restartWithDelay(500);
sleep();
}
}
} finally {
if (autoRestart_debug) print("autoRestart: Done");
autoRestart_schedule();
}
}
static void experimentIcon() {
consoleAndDefaultFrameIcon("#1101173" /*#1101171*/);
}
static void centerHigherConsole() {
if (headless()) return;
setConsoleHeight(600);
centerConsole();
}
static void tt() {
typeWriterConsole();
}
static String tt(Object contents, Object... params) {
return tag("tt", contents, params);
}
static void printStructure(String prefix, Object o) {
if (endsWithLetter(prefix)) prefix += ": ";
print(prefix + structureForUser(o));
}
static void printStructure(Object o) {
print(structureForUser(o));
}
static List javaTokPlusBrackets2(String s) {
return tok_combineRoundCurlySquareBrackets_keep(javaTok(s));
}
static boolean isSquareBracketed(String s) {
return s != null && s.startsWith("[") && s.endsWith("]");
}
// get purpose 1: access a list/array/map (safer version of x.get(y))
static A get(List l, int idx) {
return l != null && idx >= 0 && idx < l(l) ? l.get(idx) : null;
}
// seems to conflict with other signatures
/*static B get(Map map, A key) {
ret map != null ? map.get(key) : null;
}*/
static A get(A[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : null;
}
// default to false
static boolean get(boolean[] l, int idx) {
return idx >= 0 && idx < l(l) ? l[idx] : false;
}
// get purpose 2: access a field by reflection or a map
static Object get(Object o, String field) {
try {
if (o == null) return null;
if (o instanceof Class) return get((Class) o, field);
if (o instanceof Map)
return ((Map) o).get(field);
Field f = getOpt_findField(o.getClass(), field);
if (f != null) {
f.setAccessible(true);
return f.get(o);
}
if (o instanceof DynamicObject)
return ((DynamicObject) o).fieldValues.get(field);
} catch (Exception e) {
throw asRuntimeException(e);
}
throw new RuntimeException("Field '" + field + "' not found in " + o.getClass().getName());
}
static Object get_raw(Object o, String field) {
try {
Field f = get_findField(o.getClass(), field);
f.setAccessible(true);
return f.get(o);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Object get(Class c, String field) {
try {
Field f = get_findStaticField(c, field);
f.setAccessible(true);
return f.get(null);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
static Field get_findStaticField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field) && (f.getModifiers() & java.lang.reflect.Modifier.STATIC) != 0)
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Static field '" + field + "' not found in " + c.getName());
}
static Field get_findField(Class> c, String field) {
Class _c = c;
do {
for (Field f : _c.getDeclaredFields())
if (f.getName().equals(field))
return f;
_c = _c.getSuperclass();
} while (_c != null);
throw new RuntimeException("Field '" + field + "' not found in " + c.getName());
}
static String deSquareBracket(String s) {
if (startsWith(s, "[") && endsWith(s, "]"))
return substring(s, 1, l(s)-1);
return s;
}
static List tok_splitAtComma(String s) {
return tok_splitAtComma(javaTok(s));
}
static List tok_splitAtComma(List tok) {
List out = new ArrayList();
for (int i = 0; i < l(tok); i++) {
int j = smartIndexOf(tok, ",", i);
out.add(joinSubList(tok, i+1, j-1));
i = j;
}
return out;
}
static boolean startsWith_trim(String a, String b, Matches m) {
if (!startsWith(a, b)) return false;
m.m = new String[] {trim(substring(a, l(b)))};
return true;
}
static Set linkedHashSet_addWithAutoInit(Set set, A a) {
if (set == null) set = new LinkedHashSet();
set.add(a);
return set;
}
static String[] dropFirst(int n, String[] a) {
return drop(n, a);
}
static String[] dropFirst(String[] a) {
return drop(1, a);
}
static Object[] dropFirst(Object[] a) {
return drop(1, a);
}
static List dropFirst(List l) {
return dropFirst(1, l);
}
static List dropFirst(int n, Iterable i) { return dropFirst(n, toList(i)); }
static List dropFirst(Iterable i) { return dropFirst(toList(i)); }
static List dropFirst(int n, List l) {
return n <= 0 ? l : new ArrayList(l.subList(Math.min(n, l.size()), l.size()));
}
static List dropFirst(List l, int n) {
return dropFirst(n, l);
}
static String dropFirst(int n, String s) { return substring(s, n); }
static String dropFirst(String s) { return substring(s, 1); }
static List splitAtDoubleArrow(String s) {
return splitAtDoubleArrow(javaTok(s));
}
static List splitAtDoubleArrow(List tok) {
List l = new ArrayList();
int i = 0;
while (i < l(tok)) {
int j = indexOfSubList(tok, ll("=", "", ">"), i);
if (i >= l(tok)) break;
if (j < 0) j = l(tok);
l.add(trimJoin(tok.subList(i, j)));
i = j+3;
}
return l;
}
static int l(Object[] a) { return a == null ? 0 : a.length; }
static int l(boolean[] a) { return a == null ? 0 : a.length; }
static int l(byte[] a) { return a == null ? 0 : a.length; }
static int l(short[] a) { return a == null ? 0 : a.length; }
static int l(long[] a) { return a == null ? 0 : a.length; }
static int l(int[] a) { return a == null ? 0 : a.length; }
static int l(float[] a) { return a == null ? 0 : a.length; }
static int l(double[] a) { return a == null ? 0 : a.length; }
static int l(char[] a) { return a == null ? 0 : a.length; }
static int l(Collection c) { return c == null ? 0 : c.size(); }
static int l(Map m) { return m == null ? 0 : m.size(); }
static int l(CharSequence s) { return s == null ? 0 : s.length(); } static long l(File f) { return f == null ? 0 : f.length(); }
static int l(Object o) {
return o == null ? 0
: o instanceof String ? l((String) o)
: o instanceof Map ? l((Map) o)
: o instanceof Collection ? l((Collection) o)
: o instanceof Object[] ? l((Object[]) o)
: (Integer) call(o, "size");
}
static Exp nlLogic_parseExpression(String s) {
if (s == null) return null;
List tok = javaTokPlusAllThreeBrackets(s);
// a && b
Pair p = splitAtTokens_once(tok, ll("&", "", "&"));
if (p != null)
return new And(nlLogic_parseExpression(first(p)), nlLogic_parseExpression(second(p)));
// !a
if (eqGet(tok, 1, "!"))
return new ExpNot(nlLogic_parseExpression(join(dropFirst(2, tok))));
// function with options - a[bla, blubb](...)
if (l(tok) == 7 && isIdentifier(tok.get(1))
&& isSquareBracketed(tok.get(3))
&& isRoundBracketed(tok.get(5)))
return new Func(tok.get(1),
tok_splitAtComma(deSquareBracket(tok.get(3))),
nlLogic_parseExpression(deRoundBracket(tok.get(5))));
// function - a(...)
if (l(tok) == 5 && isIdentifier(tok.get(1)) && isRoundBracketed(tok.get(3))) {
String name = tok.get(1), arg = deRoundBracket(tok.get(3));
boolean parseInnerPart = false;
return new Func(name, parseInnerPart ? nlLogic_parseExpression(arg)
: new Sentence2(arg));
}
// a = b
List l = splitAtTokens(tok, ll("="));
if (l(l) == 2)
return new Eq(nlLogic_parseExpression(first(l)), nlLogic_parseExpression(second(l)));
// default
return new Sentence2(l(tok) == 3 && isQuoted(tok.get(1)) ? unquote(second(tok)) : join(tok));
}
static A last(List l) {
return empty(l) ? null : l.get(l.size()-1);
}
static char last(String s) {
return empty(s) ? '#' : s.charAt(l(s)-1);
}
static int last(int[] a) {
return l(a) != 0 ? a[l(a)-1] : 0;
}
static A last(A[] a) {
return l(a) != 0 ? a[l(a)-1] : null;
}
static A last(Iterator it) {
A a = null;
while (it.hasNext()) { ping(); a = it.next(); }
return a;
}
static A nextToLast(List l) {
return get(l, l(l)-2);
}
static String textMD5ToGlobalID(String s) {
return md5ToGlobalID(md5(s));
}
static String str(Object o) {
return o == null ? "null" : o.toString();
}
static String str(char[] c) {
return new String(c);
}
static boolean isMainProgram() {
return creator() == null;
}
static void preloadProgramTitle() {
{ Thread _t_0 = new Thread() {
public void run() { try { programTitle(); } catch (Throwable __e) { _handleException(__e); } }
};
startThread(_t_0); }
}
static void stopTimer(java.util.Timer timer) {
if (timer != null) timer.cancel();
}
static java.util.Timer doLater_daemon(long delay, final Object r) {
final java.util.Timer timer = new java.util.Timer(true);
timer.schedule(timerTask(r), delay);
return timer;
}
static java.util.Timer doLater_daemon(double delaySeconds, final Object r) {
return doLater_daemon(toMS(delaySeconds), r);
}
static long toMS(double seconds) {
return (long) (seconds*1000);
}
static int serverAutoRestartMD5_timeout = 10000;
static String serverAutoRestartMD5(String programID) {
return loadPageSilentlyWithTimeout("http://botcompany.de/1010693/raw?id=" + psI(programID), serverAutoRestartMD5_timeout);
}
static String serverAutoRestartMD5() {
return serverAutoRestartMD5(programID());
}
static String programID() {
return getProgramID();
}
static String programID(Object o) {
return getProgramID(o);
}
static boolean isMD5(String s) {
return l(s) == 32 && isLowerHexString(s);
}
static volatile StringBuffer local_log = new StringBuffer(); // not redirected
static volatile Appendable print_log = local_log; // might be redirected, e.g. to main bot
// in bytes - will cut to half that
static volatile int print_log_max = 1024*1024;
static volatile int local_log_max = 100*1024;
//static int print_maxLineLength = 0; // 0 = unset
static boolean print_silent; // total mute if set
static Object print_byThread_lock = new Object();
static volatile ThreadLocal