Warning: session_start(): open(/var/lib/php/sessions/sess_61s7c2a5ha07k1fbflq94mcjvk, O_RDWR) failed: No space left on device (28) in /var/www/tb-usercake/models/config.php on line 51
Warning: session_start(): Failed to read session data: files (path: /var/lib/php/sessions) in /var/www/tb-usercake/models/config.php on line 51
!1006722
static bool autoQuine = true;
static int maxQuineLength = 80;
static L functionsToAlwaysInclude = ll("_registerThread", "asRuntimeException");
static Set pairClasses = lithashset("Pair", "Either", "Map", "HashMap", "TreeMap", "LinkedHashMap", "MultiMap"); // classes with two type parameters
!include #1001496 // Matches
!include #1000882 // EGDiff
!include #1000883 // BlockDiffer
!include #1001065 // DialoGIO
!include #1004045 // IndexedList2
//include #1001296 // MultiMap
!include #1000988 // MultiSet
!include #1004543 // DynamicObject
!include #1005575 // CompilerBot
!include #1002662 // isTrue
!include #1010581 // unquote
static int varCount;
static new Map snippetCache;
static bool useIndexedList = true, opt_javaTok = true;
static bool cacheStdFunctions = true, cacheStdClasses = true;
static new HashMap cachedIncludes;
static ExecutorService executor;
static L lclasses;
static long startTime, lastPrint;
// Remember these variables are NOT cleared automatically
static new HashSet included;
static new HashSet definitions;
static new HashSet shouldNotIncludeFunction, shouldNotIncludeClass;
static new HashSet addedFunctions;
static new HashSet addedClasses;
static new HashSet hardFunctionReferences;
sbool quickmainDone1, quickmainDone2;
sclass CachedInclude {
S javax;
Future java;
S realJava;
S java() {
ret realJava != null ? realJava : getFuture(java);
}
Future javaFuture() {
ret realJava != null ? nowFuture(realJava) : java;
}
void clean {
if (java != null) {
realJava = getFuture(java);
java = null;
}
}
}
p {
startTime = lastPrint = sysNow();
pcall { vmKeepWithProgramMD5_get('cachedIncludes); }
executor = Executors.newFixedThreadPool(numberOfCores());
F1 oldPrint = interceptPrintInThisThread(new F1() {
Bool get(S s) {
long now = sysNow();
long time = now-lastPrint; // -startTime;
lastPrint = now;
print_raw("[" + formatInt(time/1000, 2) + ":" + formatInt(time % 1000, 3) + "] " + s);
false;
}
});
try {
_main();
} finally {
interceptPrintInThisThread(oldPrint);
executor.shutdown();
executor = null;
}
}
svoid _main() ctex {
//reTok_modify_check = true;
if (useIndexedList) findCodeTokens_debug = true;
javaTok_opt = opt_javaTok;
findCodeTokens_indexed = findCodeTokens_unindexed = 0;
findCodeTokens_bails = findCodeTokens_nonbails = 0;
javaTok_n = javaTok_elements = 0;
S in = loadMainJava();
print("759 STARTING " + identityHashCode(main.class));
included.clear();
definitions.clear();
shouldNotIncludeFunction.clear();
shouldNotIncludeClass.clear();
addedFunctions.clear();
addedClasses.clear();
hardFunctionReferences.clear();
varCount = 0;
quickmainDone1 = quickmainDone2 = false;
//L ts = findTranslators(toLines(join(tok)));
//print("Translators in source at start: " + structure(ts));
// "duplicate" statement - unused
/*L lines = toLines(in);
call(getJavaX(), "findTranslators", lines);
in = fromLines(lines);
new Matches m;
if (match("duplicate *", in, m)) {
// actual copying - unused
// tok = jtok(loadSnippet(m.get(0)));
// reference by include()
in = "m { p { callMain(include(" + quote(m.get(0)) + ")); } }";
}*/
L tok = jtok(in);
// add m { }
if (!hasCodeTokens(tok, "m", "{") && !hasCodeTokens(tok, "main", "{") && !hasCodeTokens(tok, "class", "main")) {
//tok = jtok(moveImportsUp("m {\n" + in + "\n}"));
replaceTokens_reTok(tok, 1, 2, "m {\n" + tok.get(1));
replaceTokens_reTok(tok, l(tok)-2, l(tok)-1, tok.get(l(tok)-2) + "}");
tok_moveImportsUp(tok);
}
// standard translate
//ts = findTranslators(toLines(join(tok)));
//print("Translators in source: " + structure(ts));
if (tok_hasTranslators(tok))
tok = jtok(defaultTranslate(join(tok)));
//print("end of default translate");
//print(join(tok));
//tok_autoCloseBrackets(tok);
tok = tok_processIncludes(tok); // before standard functions
if (processConceptsDot(tok))
tok = tok_processIncludes(tok);
tok = localStuff1(tok);
int safety = 0;
boolean same;
do {
S before = join(tok);
//tok = localStuff1(tok);
// shortened method declarations BEFORE standardFunctions
jreplace(tok, "svoid", "static void");
jreplace(tok, "void {", "$1 $2() {");
jreplace(tok, "String {", "$1 $2() {");
jreplace(tok, "Object {", "$1 $2() {");
jreplace(tok, "List {", "$1 $2() {");
tok_definitions(tok);
tok_ifndef(tok);
tok_ifdef(tok);
tok = standardFunctions(tok);
tok = stdstuff(tok); // standard functions and all the keywords
S diff;
long startTime = now();
//diff = unidiff(before, join(tok));
//print("unidiff: " + (now()-startTime) + " ms");
//same = eq(diff, "");
same = tok_sameTest(tok, before);
if (!same) {
print("Not same " + safety + ".");
//print(indent(2, diff));
}
if (safety++ >= 10) {
//print(unidiff(before, join(tok)));
printSources(tok);
fail("safety 10 error!");
}
} while (!same);
print("Post.");
print('moveImportsUp); tok_moveImportsUp(tok);
print('Indexing); tok = indexedList2(tok);
// POST-PROCESSING after stdstuff loop
//print("Type"); // Type to Type
print('extendClasses); tok = extendClasses(tok);
print('libs); libs(tok);
print('sourceCodeLine); sourceCodeLine(tok);
// Stuff that depends on the list of inner classes (haveClasses)
HashSet haveClasses = haveClasses(tok);
print('innerClassesVar); innerClassesVar(tok, haveClasses);
print('ifclass); tok_ifclass(tok, haveClasses);
print('slashCasts); slashCasts(tok, haveClasses);
print('newWithoutNew); newWithoutNew(tok, haveClasses);
print('Triple);
if (tok.contains("Triple") && !haveClasses.contains("Triple"))
jreplace(tok, "Triple", "T3");
jreplace(tok, "T3< >", "T3<$3, $3, $3>");
print('classReferences); expandClassReferences_lazy(tok, haveClasses);
// Error-checking
print("Error-checking");
Set functions = new HashSet(findFunctions(tok));
for (S f : hardFunctionReferences)
if (!functions.contains(f))
fail("Function " + f + " requested, but not supplied");
print('autoImports); tok = autoImports(tok); // faster to do it at the end
print("definitions=" + sfu(definitions));
if (containsIC(definitions, "allpublic")) {
// Fire up the Java parser & pretty printer!
print("Making all public.");
try {
tok = jtok(javaParser_makeAllPublic(join(tok)));
} catch e {
S src = join(tok);
print(src);
print(f2s(saveProgramTextFile("error.java", src)));
throw rethrow(e);
}
}
/*if (useIndexedList)
print("Indexed/unindexed lookups: " + findCodeTokens_indexed + "/" + findCodeTokens_unindexed + ", lists made: " + IndexedList2.instances);
print("findCodeToken bails: " + findCodeTokens_bails + "/" + findCodeTokens_nonbails);
print("javaToks: " + javaTok_n + "/" + javaTok_elements);*/
print("Saving.");
if (tok.contains("package"))
splitJavaFiles(tok);
else
saveMainJava(tok);
}
static L localStuff1(L tok) {
int safety = 0, i;
boolean same;
tok = indexedList2(tok);
do {
S before = join(tok);
earlyStuff(tok);
conceptDeclarations(tok);
tok = multilineStrings(tok);
tok_singleQuoteIdentifiersToStringConstants(tok);
inStringEvals(tok);
listComprehensions(tok);
doubleFor_simple(tok);
forPing(tok);
directSnippetRefs(tok);
quicknu(tok);
//tok_juxtaposeCalls(tok);
expandVarCopies(tok);
jreplace(tok, "do ping {", "do { ping();");
replaceKeywordBlock(tok,
"swing",
"{ swing(r {",
"}); }");
jreplace(tok, "synced ", "synchronized $2");
replaceKeywordBlock(tok, "answer",
"static S answer(S s) {\nfinal new Matches m;\n",
"\nret null;\n}");
replaceKeywordBlock(tok, "static-pcall",
"static { pcall {",
"}}");
replaceKeywordBlock(tok, "loading",
"{ JWindow _loading_window = showLoadingAnimation(); try {",
"} finally { disposeWindow(_loading_window); }}");
replaceKeywordPlusQuotedBlock(tok, "loading",
func(L tok, int i) {
S text = tok.get(i+2);
ret new S[] {
"{ JWindow _loading_window = showLoadingAnimation(" + text + "); try {",
"} finally { disposeWindow(_loading_window); }}" };
});
replaceKeywordBlock(tok, "html",
"static O html(S uri, fMap params) ctex " + "{\n", "}");
// "static sync" => static synchronized
jreplace(tok, "static sync", "static synchronized");
// "sclass" => static class
jreplace(tok, "sclass", "static class");
// "asclass" => abstract static class
jreplace(tok, "asclass", "abstract static class");
// "sinterface" => static interface
jreplace(tok, "sinterface", "static interface");
// "ssynchronized" => static synchronized
jreplace(tok, "ssynchronized", "static synchronized");
jreplace(tok, "ssvoid", "static synchronized void");
jreplace(tok, "sbool", "static bool");
jreplace(tok, "sint", "static int");
jreplace(tok, "snew", "static new");
jreplace(tok, "sv ", "static void $2");
jreplace(tok, "pvoid", "public void");
// "sS" => static S
jreplace(tok, "sS", "static S");
// "sO" => static O
jreplace(tok, "sO", "static O");
// "sL" => static L
jreplace(tok, "sL", "static L");
// "toString {" => "public S toString() {"
jreplace(tok, "toString {", "public S toString() {");
jreplace(tok, "Int", "Integer");
jreplace(tok, "Bool", "Boolean");
jreplace(tok, "BigInt", "BigInteger");
jreplace(tok, "Char", "Character");
// I REALLY wanted to avoid this, but eh...
jreplace(tok, "SS", "Map");
// "on fail {" => "catch (Throwable _e) { ... rethrow(_e); }"
replaceKeywordBlock(tok, "on fail",
"catch (Throwable _e) {",
"\nthrow rethrow(_e); }");
// "catch {" => "catch (Throwable _e) {"
jreplace(tok, "catch {", "catch (Throwable _e) {");
// "catch print e {" => "catch e { printException(e); "
jreplace(tok, "catch print {", "catch $3 { printException($3);");
// "catch print short e {" => "catch e { printExceptionShort(e); "
jreplace(tok, "catch print short {", "catch $4 { printExceptionShort($4);");
// "catch X e {" => "catch (X e) {"
jreplace(tok, "catch {", "catch ($2 $3) {");
// "catch e {" => "catch (Throwable e) {" (if e is lowercase)
jreplace(tok, "catch {", "catch (Throwable $2) {", new O() {
bool get(L tok, int i) {
S word = tok.get(i+3);
ret startsWithLowerCaseOrUnderscore(word);
}
});
jreplace(tok, "+ +", "+", new O() {
bool get(L tok, int i) {
//printStructure("++: ", subList(tok, i-1, i+6));
if (empty(_get(tok, i+2))) ret false; // no space between the pluses
if (empty(_get(tok, i)) && eq("+", _get(tok, i-1))) ret false; // an actual "++" at the left
if (empty(_get(tok, i+4)) && eq("+", _get(tok, i+5))) ret false; // an actual "++" at the right
//print("doing it");
ret true;
}
});
// some crazy fancy syntax
jreplace(tok, "set ;", "$2 = true;");
// [stdEq] -> implementation of equals() and hashCode()
jreplace(tok, "[stdEq]",
"public bool equals(O o) { ret stdEq2(this, o); }\n" +
"public int hashCode() { ret stdHash2(this); }");
// [concepts] "concept.field!" for dereferencing references
jreplace(tok, "*!", "$1.get()", func(L tok, int i) {
S l = tok.get(i+1);
if (!(isIdentifier(l) || eq(l, ")"))) false;
if (tok.get(i+2).contains("\n")) false; // no line break between and !
if (nempty(tok.get(i+4))) true; // space after = ok
S t = _get(tok, i+5);
if (t == null) ret false;
if (isIdentifier(t) || eqOneOf(t, "=", "(")) false;
true;
});
// [concepts] "field := value" for defining fields e.g. in "uniq"
while ((i = jfind(tok, " :=")) >= 0) {
tok.set(i, quote(tok.get(i)));
tok.set(i+2, ",");
tok.set(i+4, "");
reTok(tok, i, i+5);
}
// "quoted" := value
while ((i = jfind(tok, " :=")) >= 0) {
tok.set(i, tok.get(i));
tok.set(i+2, ",");
tok.set(i+4, "");
reTok(tok, i, i+5);
}
jreplace(tok, "for ( )", "for ($3 $4 : list($3))");
jreplace(tok, "for (final )", "for (final $4 $5 : list($4))");
// more shortening
jreplace(tok, "fS", "final S");
jreplace(tok, "fO", "final O");
jreplace(tok, "fL", "final L");
jreplace(tok, "fMap", "final Map");
jreplace(tok, "fRunnable", "final Runnable");
jreplace(tok, "f int", "final int");
// "continue unless"
while ((i = jfind(tok, "continue unless")) >= 0) {
int j = scanOverExpression(tok, getBracketMap(tok), i+4, ";");
replaceTokens(tok, i, i+4, "{ if (!(");
tok.set(j, ")) continue; }");
reTok(tok, i, j+1);
}
// "continue if"
while ((i = jfind(tok, "continue if")) >= 0) {
int j = scanOverExpression(tok, getBracketMap(tok), i+4, ";");
replaceTokens(tok, i, i+4, "{ if (");
tok.set(j, ") continue; }");
reTok(tok, i, j+1);
}
// "return if"
while ((i = jfind(tok, "return if")) >= 0) {
int j = scanOverExpression(tok, getBracketMap(tok), i+4, ";");
replaceTokens(tok, i, i+4, "{ if (");
tok.set(j, ") return; }");
reTok(tok, i, j+1);
}
// "return unless"
while ((i = jfind(tok, "return unless")) >= 0) {
int j = scanOverExpression(tok, getBracketMap(tok), i+4, ";");
replaceTokens(tok, i, i+4, "{ if (!(");
tok.set(j, ")) return; }");
reTok(tok, i, j+1);
}
// "return with " / "continue with "
while ((i = jfind(tok, " with", func(L tok, int i) { eqOneOf(tok.get(i+1), "return", "continue") })) >= 0) {
int j = scanOverExpression(tok, getBracketMap(tok), i+4, ";");
tok.set(j, "; " + tok.get(i) + "; }");
replaceTokens(tok, i, i+3, "{");
reTok(tok, i, j+1);
}
// return "bla" with
while ((i = jfind(tok, "return with")) >= 0) {
S result = tok.get(i+2);
int j = scanOverExpression(tok, getBracketMap(tok), i+6, ";");
replaceTokens(tok, i, i+5, "{");
tok.set(j, "; return " + result + "; }");
reTok(tok, i, j+1);
}
// while not null ()
while ((i = jfind(tok, "while not null (")) >= 0) {
int closingBracket = findEndOfBracketPart(tok, i+6)-1;
replaceTokens(tok, i+2, i+6, "(");
tok.set(closingBracket, ") != null)");
reTok(tok, i, closingBracket+1);
}
// Replace $1 with m.unq(0) etc. - caveat: this blocks identifiers $1, $2, ...
for (i = 1; i < l(tok); i += 2) {
S s = tok.get(i);
if (s.startsWith("$")) {
s = substring(s, 1);
if (isInteger(s)) {
tok.set(i, "m.unq(" + (parseInt(s)-1) + ")");
reTok(tok, i);
}
}
}
// instanceof trickery
jreplace(tok, "is a ", "instanceof $3");
jreplace(tok, "! instanceof .", "!($2 instanceof $4.$6)");
jreplace(tok, "! instanceof ", "!($2 instanceof $4)");
jreplace(tok, " !instanceof ", "!($1 instanceof $4)");
// map funcname(...) => map(f funcname, ...)
jreplace(tok, "map (", "map(f $2,");
// func keyword for lambdas - now automatically quines toString() if enabled
while ((i = jfind(tok, "func(")) >= 0) {
int argsFrom = i+4, argsTo = findCodeTokens(tok, i, false, ")");
int idx = findCodeTokens(tok, argsTo, false, "{");
int j = findEndOfBracketPart(tok, idx);
L contents = subList(tok, idx+1, j-1);
S returnType = "O";
if (eq(tok.get(argsTo+2), "-") && eq(tok.get(argsTo+4), ">"))
returnType = join(subList(tok, argsTo+6, idx-1));
S toString = autoQuine ? " public S toString() { ret " + quote(shorten(maxQuineLength, trim(join(contents)))) + "; }" : "";
L args = subList(tok, argsFrom-1, argsTo);
L types = tok_typesOfParams(args);
O type = "O";
if (l(types) == 1)
type = "F1<" + first(types) + ", " + returnType + ">";
S body = tok_addReturn(contents);
replaceTokens_reTok(tok, i, j,
"new " + type + "() { "
+ returnType + " get(" + trimJoin(args) + ") ctex { "
+ body
+ " }\n" +
+ toString + "}");
}
while ((i = jfind(tok, "voidfunc(")) >= 0) {
int argsFrom = i+4, argsTo = findCodeTokens(tok, i, false, ")");
int idx = findCodeTokens(tok, argsTo, false, "{");
int j = findEndOfBracketPart(tok, idx);
L contents = subList(tok, idx+1, j-1);
replaceTokens(tok, i, j, "new O { void get(" + join(subList(tok, argsFrom, argsTo-1)) + ") ct" + "ex { " + tok_addSemicolon(contents) + " }\n" +
(autoQuine ? " public S toString() { ret " + quote(shorten(maxQuineLength, trim(join(contents)))) + "; }" : "") + "}");
reTok(tok, i, j);
}
for (S keyword : ll("f", "func")) {
while ((i = jfind(tok, keyword + " {")) >= 0) {
int idx = findCodeTokens(tok, i, false, "{");
int j = findEndOfBracketPart(tok, idx+2); // XXX - not idx???
L contents = subList(tok, idx+1, j-1);
replaceTokens(tok, i, j, "new O { O get() ctex { " + tok_addReturn(contents) + " }\n" +
(autoQuine ? " public S toString() { ret " + quote(shorten(maxQuineLength, trim(join(contents)))) + "; }" : "") + "}");
reTok(tok, i, j);
}
while ((i = jfind(tok, keyword + " ->")) >= 0) {
// I think there is a bug here for something like func -> x { new x { } }
int idx = findCodeTokens(tok, i, false, "{");
int j = findEndOfBracketPart(tok, idx);
S returnType = join(subList(tok, i+6, idx-1));
L contents = subList(tok, idx+1, j-1);
replaceTokens(tok, i, j, "new F0<" + returnType + ">() { " + returnType + " get() ctex { " + tok_addReturn(contents) + " }\n" +
(autoQuine ? " public S toString() { ret " + quote(shorten(maxQuineLength, trim(join(contents)))) + "; }" : "") + "}");
reTok(tok, i, j);
}
}
// "ref -> bla" for dereferencing Concept.Ref or ThreadLocal or other
//jreplace(tok, " ->", "$1.get().");
jreplace(tok, "->", ".get().", func(L tok, int i) {
empty(tok.get(i+2))
&& !(empty(_get(tok, i)) && eq(_get(tok, i-1), "-")) // i-->0
});
// shortened subconcept declaration (before star constructors!)
shortenedSubconcepts(tok);
// "case" as a variable name ( => _case)
caseAsVariableName(tok);
// "do" as a function name ( => dO)
tok_doAsMethodName(tok);
// "continue" as a function name ( => _continue)
continueAsFunctionName(tok);
// Do this BEFORE awt replacement! ("p-awt" contains "awt" token)
if (hasCodeTokens(tok, "p", "-")) {
jreplace(tok, "p-pretty {", "p-noconsole {");
replaceKeywordBlock(tok, "p-awt-noconsole", "p-awt {", "\nhideConsole(); }");
replaceKeywordBlock(tok, "p-substance-noconsole", "p-substance {", "\nhideConsole(); }");
replaceKeywordBlock(tok, "p-nimbus-noconsole", "p-nimbus {", "\nhideConsole(); }");
replaceKeywordBlock(tok, "p-subst-noconsole", "p-subst {", "\nhideConsole(); }");
replaceKeywordBlock(tok, "p-noconsole", "p-subst {", "\nhideConsole(); }");
replaceKeywordBlock(tok, "p-subst", "p-substance-thread {", "}");
replaceKeywordBlock(tok, "p-substance-thread", "p { substance();", "}");
replaceKeywordBlock(tok, "p-magellan-thread", "p { magellan();", "}");
replaceKeywordBlock(tok, "p-substance", "p-awt { substance();", "}");
replaceKeywordBlock(tok, "p-nimbus", "p-awt { nimbus();", "}");
replaceKeywordBlock(tok, "p-center", "p { centerConsole(); ", "}");
jreplace(tok, "p-type {", "p-typewriter {");
jreplace(tok, "p-tt {", "p-typewriter {");
replaceKeywordBlock(tok, "p-awt", "p { swing {", "}}");
replaceKeywordBlock(tok, "p-typewriter", "p { typeWriterConsole();", "}");
replaceKeywordBlock(tok, "p-lowprio", "p { lowPriorityThread(r " + "{", "}); }");
tok_p_repeatWithSleep(tok);
}
replaceKeywordBlock(tok,
"awt",
"swingLater(r {",
"});");
unswing(tok);
lockBlocks(tok);
// crazy stuff
jreplace (tok, "for over :", "for (int $2 = 0; $2 < l($4); $2++)");
jreplace (tok, "for , over : {", "for (int $2 = 0; $2 < l($7); $2++) { $4 $5 = $7.get($2);");
jreplace (tok, "for to :", "for (int $2 = 0; $2 < $4; $2++)");
jreplace (tok, "for to :", "for (int $2 = 0; $2 < $4; $2++)");
tok = expandShortTypes(tok);
if (containsToken(tok, "cast")) {
// TODO: use tokens
S s = join(tok);
s = s.replaceAll("(\\w+<[\\w\\s,\\[\\]]+>|\\w+|\\w+\\[\\]|\\w+\\[\\]\\[\\])\\s+(\\w+)\\s*=\\s*cast(\\W[^;]*);", "$1 $2 = ($1) ($3);");
tok = jtok(s);
}
replaceKeywordBlock(tok, "r-thread", "runnableThread(r " + "{", "})");
rNamedThread(tok);
// runnable and r - now also with automatic toString if enabled
for (S keyword : ll("runnable", "r"))
while ((i = jfind(tok, keyword + " {")) >= 0) {
int idx = findCodeTokens(tok, i, false, "{");
int j = findEndOfBracketPart(tok, idx);
L contents = subList(tok, idx+1, j-1);
//print("r contents: " + structure(contents));
replaceTokens(tok, i, j+1, "new Runnable() { public void run() { try { " + tok_addSemicolon(contents) +
"\n} catch (Exception __e) { throw rethrow(__e); } }" +
(autoQuine ? " public S toString() { return " + quote(shorten(maxQuineLength, trim(join(contents)))) + "; }" : "") +
"}");
reTok(tok, i, j+1);
}
replaceKeywordBlock(tok,
"expectException",
"{ bool __ok = false; try {",
"} catch { __ok = true; } assertTrue(\"expected exception\", __ok); }");
while ((i = tok.indexOf("tex")) >= 0) {
tok.set(i, "throws Exception");
tok = jtok(tok);
}
// shorter & smarter whiles
jreplace(tok, "while true", "while (true)");
jreplace(tok, "while licensed", "while (licensed())");
jreplace(tok, "repeat {", "while (licensed()) {");
tok_repeatWithSleep(tok);
// null; => return null; etc.
O cond = func(L tok, int i) {
tok_tokenBeforeLonelyReturnValue(_get(tok, i-1))
};
jreplace(tok, "null;", "return null;", cond);
jreplace(tok, "false;", "return false;", cond);
jreplace(tok, "true;", "return true;", cond);
jreplace(tok, "this;", "return this;", cond);
// "myFunction;" instead of "myFunction();" - quite rough
cond = new O() {
bool get(L tok, int i) {
S word = tok.get(i+3);
//print("single word: " + word);
ret !litlist("break", "continue", "return", "else").contains(word);
}
};
for (S pre : litlist("}", ";"))
jreplace(tok, pre + " ;", "$1 $2();", cond);
// shorter match syntax for answer methods
jreplace(tok, "if || ",
"if (matchOneOf(s, m, $2, $5))");
// "bla..."
jreplace(tok, "if ", "if (matchStartX($2, s, m))",
new O() { bool get(L tok, int i) {
ret unquote(tok.get(i+3)).endsWith("...");
}});
// "bla * bla | blubb * blubb"
jreplace_dyn(tok, "if ", func(L tok, int cIdx) {
S s = unquote(tok.get(cIdx+2));
//print("multimatch: " + quote(s));
new L l;
for (S pat : splitAtJavaToken(s, "|")) {
//print("multimatch part: " + quote(pat));
if (javaTok(pat).contains("*"))
l.add("match(" + quote(trim(pat)) + ", s, m)");
else
l.add("match(" + quote(trim(pat)) + ", s)");
}
ret "if (" + join(" || ", l) + ")";
}, func(L tok, int i) {
javaTokC(unquote(tok.get(i+3))).contains("|")
});
// "bla"
jreplace(tok, "if ", "if (match($2, s))",
new O() { bool get(L tok, int i) {
ret !javaTokC(unquote(tok.get(i+3))).contains("*");
}});
// "bla * bla"
jreplace(tok, "if ", "if (match($2, s, m))");
jreplace(tok, "if match ", "if (match($3, s, m))");
// extra commas ("litlist(1, 2,)")
jreplace(tok, ",)", ")");
// additional translations (if necessary)
jreplace(tok, "pcall ping {", "pcall { ping();");
replaceKeywordBlock(tok,
"pcall",
"try {",
"} catch (Throwable __e) { printStackTrace2(__e); }");
replaceKeywordBlock(tok,
"pcall-short",
"try {",
"} catch (Throwable __e) { print(exceptionToStringShort(__e)); }");
replaceKeywordBlock(tok,
"pcall-silent",
"try {",
"} catch (Throwable __e) { silentException(__e); }");
replaceKeywordBlock(tok,
"pcall-messagebox",
"try {",
"} catch __e { messageBox(__e); }");
replaceKeywordBlock(tok,
"pcall-infobox",
"try {",
"} catch __e { infoBox(__e); }");
tok = dialogHandler(tok);
replaceKeywordBlock(tok, "exceptionToUser",
"try {",
"} catch (Throwable __e) { ret exceptionToUser(__e); }");
if (hasCodeTokens(tok, "twice", "{"))
replaceKeywordBlock(tok, "twice",
"for (int __twice = 0; __twice < 2; __twice++) {",
"}");
while ((i = findCodeTokens(tok, "repeat", "*", "{")) >= 0) {
S v = makeVar("repeat");
tok.set(i, "for (int " + v + " = 0; " + v + " < " + tok.get(i+2) + "; " + v + "++)");
tok.set(i+2, "");
tok = jtok(tok);
}
replaceKeywordBlockDyn(tok,
"time",
new O() { S[] get() {
S var = makeVar("startTime");
ret new S[] {
"{ long " + var + " = sysNow(); try { ",
"} finally { " + var + " = sysNow()-" + var + "; saveTiming(" + var + "); } }"};
}});
// version without { }
replaceKeywordBlockDyn(tok,
"time2",
new O() { S[] get() {
S var = makeVar("startTime");
ret new S[] {
"long " + var + " = sysNow(); ",
" " + var + " = sysNow()-" + var + "; saveTiming(" + var + "); "};
}});
// time "bla" {
replaceKeywordPlusQuotedBlock(tok,
"time",
func(L tok, int i) {
S var = makeVar("startTime");
ret new S[] {
"long " + var + " = sysNow(); ",
" done2_always(" + tok.get(i+2) + ", " + var + "); "};
});
if (hasCodeTokens(tok, "assertFail", "{")) {
S var = makeVar("oops");
replaceKeywordBlock(tok,
"assertFail",
"boolean " + var + " = false; try {",
"\n" + var + " = true; } catch (Exception e) { /* ok */ } assertFalse(" + var + ");");
}
replaceKeywordBlock(tok,
"yo",
"try {",
"} catch (Exception " + makeVar("e") + ") { ret false; }",
func(L tok, int i) {
neqOneOf(_get(tok, i-1), "svoid", "void")
});
replaceKeywordBlock(tok,
"awtIfNecessary",
"swingNowOrLater(r " + "{",
"});");
ctex(tok);
replaceKeywordBlock(tok,
"actionListener",
"new java.awt.event.ActionListener() { " +
"public void actionPerformed(java.awt.event.ActionEvent _evt) { pcall-messagebox {",
"}}}");
namedThreads(tok);
threads(tok);
// try answer
while ((i = findCodeTokens(tok, "try", "answer")) >= 0) {
int j = findEndOfStatement(tok, i);
S v = makeVar("a");
tok.set(i, "{ S " + v);
tok.set(i+2, "=");
tok.set(j-1, "; if (!empty(" + v + ")) ret " + v + "; }");
reTok(tok, i, j);
}
// return optional (return if not null)
while ((i = jfind(tok, "return optional =")) >= 0) {
int j = findEndOfStatement(tok, i);
S v = tok.get(i+4);
clearTokens(tok, i+2, i+4);
tok.set(i, "{");
tok.set(j-1, "; if (" + v + " != null) ret " + v + "; }");
reTok(tok, i, j);
}
functionReferences(tok);
quicknew2(tok);
tok_unpair(tok);
tok_cachedFunctions(tok);
throwFail(tok);
tok_typeAA(tok);
// end of localStuff1
same = tok_sameTest(tok, before);
/*if (!same)
print("local not same " + safety + " (" + l(tok) + " tokens)");*/
if (safety++ >= 10) {
print("----");
print(join(tok));
print("----");
fail("safety 10 error!");
}
} while (!same);
ret tok;
}
static L reTok_include(L tok, int i, int j) {
ret reTok_modify(tok, i, j, f localStuff1);
}
static L includeInMainLoaded_reTok(L tok, int i, int j) {
ret reTok_include(tok, i, j);
}
static L stdstuff(L tok) {
//if (++level >= 10) fail("woot? 10");
print("stdstuff!");
int i;
new L ts;
tok_findTranslators(tok, ts);
if (nempty(ts))
print("DROPPING TRANSLATORS: " + structure(ts));
print('quickmain); tok = quickmain(tok);
print('includes); tok = tok_processIncludes(tok);
print('conceptsDot); if (processConceptsDot(tok))
tok = tok_processIncludes(tok);
// * constructors
print('starConstructors); if (hasCodeTokens(tok, "\\*", "("))
tok = expandStarConstructors(tok);
// STANDARD CLASSES & INTERFACES
if (lclasses == null) {
S sc = cacheGet("#1003674");
lclasses = new L;
for (S line : toLinesFullTrim(sc)) {
line = javaDropComments(line).trim();
int idx = line.indexOf('/');
lclasses.addAll(ll(line.substring(0, idx), line.substring(idx+1)));
}
}
//final Set haveClasses = addStandardClasses(tok, toStringArray(lclasses));
print("standard classes");
final Set haveClasses = addStandardClasses_v2(tok, lclasses);
// "x << X" or "x >> X" => "x instanceof X"
for (S op : ll("<<", ">>"))
jreplace(tok, " " + op + " ", "$1 instanceof $4", func(L tok, int i) {
haveClasses.contains(tok.get(i+7))
});
// concept-related stuff
// auto-import concepts
bool _a = tok_hasClassRef2(tok, /*"extends",*/ "Concept") || tok_hasClassRef2(tok, "Concepts"), _b = !haveClasses.contains("Concept");
//print("auto-import: " + _a + ", " + _b);
if (_a && _b) {
print("Auto-including concepts.");
if (shouldNotIncludeClass.contains("Concepts")) {
print(join(tok));
fail("Unwanted concepts import");
}
printStruct(haveClasses);
tok = includeInMainLoaded(tok, "concepts.");
reTok(tok, l(tok)-1, l(tok));
//processConceptsDot(tok);
}
ret tok;
} // end of stdStuff!
static L multilineStrings(L tok) {
for (int i = 1; i < tok.size(); i += 2) {
S t = tok.get(i);
if (isQuoted(t))
if (t.startsWith("[") || t.contains("\r") || t.contains("\n"))
tok.set(i, quote(unquote(t)));
}
ret tok;
}
static void inStringEvals(L tok) {
bool change = false;
for (int i = 1; i < tok.size(); i += 2) {
S t = tok.get(i);
if (!isQuoted(t)) continue;
if (t.contains("\\*") && !t.contains("\\\\")) { // << rough
tok.set(i, inStringEval(t));
change = true;
}
}
if (change) reTok(tok);
}
static S inStringEval(S t) {
t = dropPrefix("\"", dropSuffix("\"", t));
new L l;
int idx;
while ((idx = t.indexOf("\\*")) >= 0) {
int j = indexOf(t, idx, "*/");
if (j < 0) break;
if (idx > 0)
l.add("\"" + substring(t, 0, idx) + "\"");
l.add("(" + trim(substring(t, idx+2, j)) + ")");
t = substring(t, j+2);
}
if (nempty(t))
l.add("\"" + t + "\"");
ret "(" + join(" + ", l) + ")";
}
static L quickmain(L tok) {
if (quickmainDone1 && quickmainDone2) ret tok;
int i = findCodeTokens(tok, "main", "{");
if (i < 0) i = findCodeTokens(tok, "m", "{");
if (i >= 0 && !(i-2 > 0 && tok.get(i-2).equals("class"))) {
tokSet(tok, i, "class main");
quickmainDone1 = true;
}
i = findCodeTokens(tok, "psvm", "{");
if (i < 0) i = findCodeTokens(tok, "p", "{");
if (i >= 0) {
int idx = i+2;
int j = findEndOfBracketPart(tok, idx);
L contents = subList(tok, idx+1, j-1);
tok.set(i, "public static void main(final String[] args) throws Exception");
replaceTokens(tok, idx+1, j-1, tok_addSemicolon(contents));
reTok(tok, i, j-1);
quickmainDone2 = true;
}
ret tok;
}
static S makeVar(S name) {
ret "_" + name + "_" + varCount++;
}
static S makeVar() { ret makeVar(""); }
/*static L standardFunctions(L tok) {
ret rtq(tok, "#1002474");
}*/
static L rtq(L tok, S id) {
ret runTranslatorQuick(tok, id);
}
static L expandShortTypes(L tok) {
// replace with
for (int i = 1; i+4 < tok.size(); i += 2)
if (tok.get(i).equals("<")
&& litlist(">", ",").contains(tok.get(i+4))) {
String type = tok.get(i+2);
if (type.equals("int")) type = "Integer";
else if (type.equals("long")) type = "Long";
tok.set(i+2, type);
}
// O = Object, S = String, ret = return
for (int i = 1; i < tok.size(); i += 2) {
String t = tok.get(i);
if (t.equals("O")) t = "Object";
if (t.equals("S")) t = "String";
else if (t.equals("L")) t = "List";
//else if (t.equals("F")) t = "Function";
else if (t.equals("ret") && neqOneOf(get(tok, i+2), "=", ")", ".")) t = "return";
else if (t.equals("bool") && i+2 < tok.size() && neq(tok.get(i+2), "(")) t = "boolean"; // bool -> boolean if it's not a function name
tok.set(i, t);
}
jreplace(tok, "LL< >", "L>");
jreplace(tok, "Clusters< >", "Map<$3, Collection<$3>>");
ret tok;
}
static L autoImports(L tok) {
HashSet imports = new HashSet(tok_findImports(tok));
new StringBuilder buf;
for (S c : standardImports)
if (!(imports.contains(c)))
buf.append("import " + c + ";\n");
if (buf.length() == 0) ret tok;
tok.set(0, buf+tok.get(0));
ret reTok(tok, 0, 1);
}
static String[] standardImports = {
"java.util.*",
"java.util.zip.*",
"java.util.List",
"java.util.regex.*",
"java.util.concurrent.*",
"java.util.concurrent.atomic.*",
"java.util.concurrent.locks.*",
"javax.swing.*",
"javax.swing.event.*",
"javax.swing.text.*",
"javax.swing.table.*",
"java.io.*",
"java.net.*",
"java.lang.reflect.*",
"java.lang.ref.*",
"java.lang.management.*",
"java.security.*",
"java.security.spec.*",
"java.awt.*",
"java.awt.event.*",
"java.awt.image.*",
"javax.imageio.*",
"java.math.*"
};
static L expandStarConstructors(L tok) {
mainLoop: for (int i = 3; i+6 < tok.size(); i += 2) {
String t = tok.get(i), l = tok.get(i-2);
if (!t.equals("*"))
continue;
if (!tok.get(i+2).equals("("))
continue;
if (!eqOneOf(l, "}", "public", "private", "protected", ";", "{", "endif") && neq(get(tok, i-4), "ifclass")) // is this correct...??
continue;
// ok, it seems like a constructor declaration.
// Now find class name by going backwards.
int j = i, level = 1;
while (j > 0 && level > 0) {
t = tok.get(j);
if (t.equals("}")) ++level;
if (t.equals("{")) --level;
j -= 2;
}
while (j > 0) {
t = tok.get(j);
if (t.equals("class")) {
String className = tok.get(j+2);
tok.set(i, className); // exchange constructor name!
// now for the parameters.
// Syntax: *(Learner *learner) {
// We will surely add type inference here in time... :)
j = i+2;
while (!tok.get(j).equals("{"))
j += 2;
int block = j+1;
for (int k = i+2; k < block-1; k += 2)
if (tok.get(k).equals("*")) {
tok.remove(k);
tok.remove(k);
block -= 2;
String name = tok.get(k);
tok.addAll(block, Arrays.asList(new String[] {
"\n ", "this", "", ".", "", name, " ", "=", " ", name, "", ";" }));
}
continue mainLoop;
}
j -= 2;
}
}
ret tok;
}
static L tok_processIncludes(L tok) {
int safety = 0;
while (hasCodeTokens(tok, "!", "include") && ++safety < 100)
tok = tok_processIncludesSingle(tok);
//tok_autoCloseBrackets(tok);
ret tok;
}
static L tok_processIncludesSingle(L tok) {
int i;
while ((i = jfind(tok, "!include #")) >= 0) {
S id = tok.get(i+6);
included.add(parseLong(id));
replaceTokens(tok, i, i+8, "\n" + cacheGet(id) + "\n");
reTok_include(tok, i, i+8);
}
while ((i = jfind(tok, "!include once #")) >= 0) {
S id = tok.get(i+8);
bool isNew = included.add(parseLong(id));
replaceTokens(tok, i, i+10,
isNew ? "\n" + cacheGet(id) + "\n" : "");
reTok_include(tok, i, i+10);
}
ret tok;
}
static void ctex(L tok) {
replaceKeywordBlock(tok, "ctex",
"{ try {",
"} catch (Exception __e) { throw rethrow(__e); } }");
replaceKeywordBlock(tok, "null on exception",
"{ try {",
"} catch (Throwable __e) { return null; } }");
replaceKeywordBlock(tok, "false on exception",
"{ try {",
"} catch (Throwable __e) { return false; } }");
}
static L dialogHandler(L tok) {
ret replaceKeywordBlock(tok,
"dialogHandler",
"new DialogHandler() {\n" +
"public void run(final DialogIO io) {",
"}}");
}
static void quicknew2(L tok) {
jreplace(tok, "new ;", "$2 $3 = new $2;");
jreplace(tok, "new <> ;", "$2<$4> $6 = new $2;");
jreplace(tok, "new <> , ;", "$2<$4> $6 = new $2, $8 = new $2;");
jreplace(tok, "new <,> ;", "$2<$4,$6> $8 = new $2;");
jreplace(tok, "new <<>> ;", "$2 $3 $4 $5 $6 $7 $8 $9 = new $2;");
jreplace(tok, "new <[]> ;", "$2 $3 $4 $5 $6 $7 $8 = new $2;");
jreplace(tok, "new < <>, > ;", "$2 $3 $4 $5 $6 $7 $8 $9 $10 $11 = new $2;");
jreplace(tok, "new < <,> > ;", "$2 $3 $4 $5 $6 $7 $8 $9 $10 $11 = new $2;");
jreplace(tok, "new < , <> > ;", "$2 $3 $4 $5 $6 $7 $8 $9 $10 $11 = new $2;");
jreplace(tok, "new < <,,> > ;", "$2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 = new $2;");
jreplace(tok, "new < <,>, > ;", "$2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 = new $2;");
// [abandoned, confusing, looks like a function definition] with arguments - new A a(...); => A a = new A(...);
//jreplace(tok, "new (", "$2 $3 = new $2(");
jreplace(tok, "for args " + "{", "for (int i = 0; i < args.length; i++) { final String arg = args[i];");
// Constructor calls without parentheses
// So you can say something like: predictors.add(new P1);
jreplace1(tok, "new ", "new $2()", func(L tok, int i) {
eqOneOf(_get(tok, i+5), "{", ",", ")", ";", ":")
});
jreplace(tok, "new List(", "new ArrayList(");
jreplace(tok, "new Map(", "new HashMap(");
jreplace(tok, "new Set(", "new HashSet(");
jreplace(tok, "new (Hash)Set", "new HashSet"); // rough
jreplace(tok, "new (Tree)Set", "new TreeSet");
jreplace(tok, "new (Hash)Map", "new HashMap");
jreplace(tok, "new (Tree)Map", "new TreeMap");
jreplace(tok, "\\*[] ;", "$2[] $6 = new $2[$4];");
// X x = new(...) => X x = new X(...)
// X x = new => X x = new
jreplace(tok, " = new", "$1 $2 = new $1", func(L tok, int i) {
eqOneOf(_get(tok, i+9), "(", ";", ",", ")")
});
jreplace(tok, " = new \\*", "$1 $2 = new $1");
jreplace(tok, "\\* = new ", "$5 $2 = new $5");
jreplace(tok, "<> = new", "$1 $2 $3 $4 $5 = new $1", func(L tok, int i) {
eqOneOf(_get(tok, i+9+3*2), "(", ";", ",", ")")
});
jreplace(tok, "<,> = new", "$1 $2 $3 $4 $5 $6 $7 = new $1", func(L tok, int i) {
eqOneOf(_get(tok, i+9+5*2), "(", ";", ",", ")")
});
}
static L extendClasses(L tok) {
int i;
while ((i = jfind(tok, "extend {")) >= 0) {
S className = tok.get(i+2);
int idx = findCodeTokens(tok, i, false, "{");
int j = findEndOfBracketPart(tok, idx+2);
S content = join(subList(tok, idx+1, j-1));
L c = findInnerClassOfMain(tok, className);
print("Extending class " + className);
clearTokens(tok.subList(i, j+1));
if (c == null) {
print("Warning: Can't extend class " + className + ", not found");
continue;
}
int startOfClass = indexOfSubList(tok, c); // magicIndexOfSubList is broken
int endOfClass = startOfClass + l(c)-1;
//print("Extending class " + className + " ==> " + join(subList(tok, startOfClass, endOfClass)));
while (neq(tok.get(endOfClass), "}")) --endOfClass;
//print("Extending class " + className + " ==> " + join(subList(tok, startOfClass, endOfClass)));
tok.set(endOfClass, content + "\n" + tok.get(endOfClass));
doubleReTok(tok, i, j+1, endOfClass, endOfClass+1);
}
ret tok;
}
static void listComprehensions(L tok) {
int i;
for (S op : ll(":", "in"))
while ((i = jfind(tok, "[ " + op)) >= 0) {
Map bracketMap = getBracketMap(tok); // XXX - optimize
S type = tok.get(i+2), id = tok.get(i+4);
int j = scanOverExpression(tok, bracketMap, i+8, "|");
S exp = join(tok.subList(i+8, j));
j += 2;
int k = scanOverExpression(tok, bracketMap, j, "]");
S where = join(tok.subList(j, k));
++k;
S code = "filter(" + exp + ", func(" + type + " " + id + ") { " + where + " })";
replaceTokens(tok, i, k, code);
reTok(tok, i, k);
}
}
static void doubleFor_simple(L tok) {
jreplace(tok, "for ( , : ) {",
"for ($3 $4 : keys($9)) { $6 $7 = $9.get($4);");
jreplace(tok, "for ( , <> : ) {",
"for ($3 $4 : keys($12)) { $6 $7 $8 $9 $10 = $12.get($4);");
jreplace(tok, "for ( , <,> : ) {",
"for ($3 $4 : keys($14)) { $6 $7 $8 $9 $10 $11 $12 = $14.get($4);");
jreplace(tok, "for ( , <,,> : ) {",
"for ($3 $4 : keys($16)) { $6 $7 $8 $9 $10 $11 $12 $13 $14 = $16.get($4);");
jreplace(tok, "for ( , : ) {",
"for ($3 $4 : keys($8)) { $3 $6 = $8.get($4);");
}
// for ping / while ping
static void forPing(L tok) {
int i;
for (S keyword : ll("for", "while"))
while ((i = jfind(tok, keyword + " ping (")) >= 0) {
int bracketEnd = findEndOfBracketPart(tok, i+4)-1;
int iStatement = bracketEnd+2;
int iEnd = findEndOfStatement(tok, iStatement);
tok.set(i+2, "");
// turn into block
if (!eq(get(tok, iStatement), "{")) {
tok.set(iStatement, "{ " + tok.get(iStatement));
tok.set(iEnd-1, tok.get(iEnd-1) + " }");
}
// add ping
tok.set(iStatement, "{ ping(); " + dropPrefixTrim("{", tok.get(iStatement)));
reTok(tok, i+2, iEnd);
}
}
// lib 123 => !123
static void libs(L