Warning: session_start(): open(/var/lib/php/sessions/sess_f2s21p3guv94j3r6ff412vs4d1, 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
// keep the following line, put older transpilation after
!1400308 // JavaParser
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 com.github.javaparser.*;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.Node;
import com.github.javaparser.ast.body.*;
import com.github.javaparser.ast.visitor.*;
import com.github.javaparser.Problem;
import com.github.javaparser.ast.NodeList;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.body.EnumDeclaration;
import com.github.javaparser.ast.stmt.LocalClassDeclarationStmt;
import com.github.javaparser.printer.*;
import java.text.SimpleDateFormat;
import java.text.NumberFormat;
import javax.imageio.metadata.*;
import javax.imageio.stream.*;
import java.nio.file.Files;
import java.nio.file.Path;
class main {
// new JavaParser includes
// JavaParser
static CompilationUnit javaParseCompilationUnit(String java) {
JavaParser p = new JavaParser();
p.getParserConfiguration().setAttributeComments(false);
ParseResult r = p.parse(java);
if (!r.isSuccessful())
throw fail(str(r));
return r.getResult().get();
}
static String javaParser_makeAllPublic(String src, Object... __) {
CompilationUnit cu = javaParseCompilationUnit(src);
PrettyPrinterConfiguration ppconf = new PrettyPrinterConfiguration();
ppconf.setIndentSize(2);
ppconf.setIndentType(PrettyPrinterConfiguration.IndentType.SPACES);
ppconf.setPrintComments(false);
javaParser_makeAllPublic_Visitor visitor = new javaParser_makeAllPublic_Visitor();
visitor.notTopLevelClassDecl = boolOptPar("notTopLevelClassDecl",__);
visitor.visit(cu, null);
return cu.toString(ppconf);
}
static class javaParser_makeAllPublic_Visitor extends VoidVisitorAdapter {
boolean notTopLevelClassDecl = false; // don't make top-level class declaration public
void makePublic(NodeList modifiers) {
// XXX: does this work?
modifiers.remove(com.github.javaparser.ast.Modifier.privateModifier());
modifiers.remove(com.github.javaparser.ast.Modifier.protectedModifier());
if (!modifiers.contains(com.github.javaparser.ast.Modifier.publicModifier()))
modifiers.add(com.github.javaparser.ast.Modifier.publicModifier());
}
public void visit(ClassOrInterfaceDeclaration n, Object arg) {
Node parent = n.getParentNode().get();
if (!(parent instanceof LocalClassDeclarationStmt
|| parent instanceof CompilationUnit && neq(n.getName().asString(), "main")))
if (notTopLevelClassDecl)
notTopLevelClassDecl = false;
else
makePublic(n.getModifiers());
super.visit(n, arg);
}
public void visit(FieldDeclaration n, Object arg) {
makePublic(n.getModifiers());
super.visit(n, arg);
}
public void visit(ConstructorDeclaration n, Object arg) {
Node parent = n.getParentNode().get();
if (!(parent instanceof EnumDeclaration))
makePublic(n.getModifiers());
super.visit(n, arg);
}
public void visit(MethodDeclaration n, Object arg) {
NodeList m = n.getModifiers();
//print("Method found: " + n.getName() + " with modifiers: " + m + ", position: " + n.getRange()->begin);
if (m.contains(com.github.javaparser.ast.Modifier.privateModifier()) && !m.contains(com.github.javaparser.ast.Modifier.staticModifier()) && !m.contains(com.github.javaparser.ast.Modifier.finalModifier()))
m.add(com.github.javaparser.ast.Modifier.finalModifier());
makePublic(m);
super.visit(n, arg);
}
}
static String javaParser_reparse_keepComments(String src) {
CompilationUnit cu = javaParseCompilationUnit(src);
PrettyPrinterConfiguration ppconf = new PrettyPrinterConfiguration();
ppconf.setIndentSize(2);
ppconf.setIndentType(PrettyPrinterConfiguration.IndentType.SPACES);
return cu.toString(ppconf);
}
static String javaParser_makeAllPublic_keepComments(String src) {
CompilationUnit cu = javaParseCompilationUnit(src);
PrettyPrinterConfiguration ppconf = new PrettyPrinterConfiguration();
ppconf.setIndentSize(2);
ppconf.setIndentType(PrettyPrinterConfiguration.IndentType.SPACES);
new javaParser_makeAllPublic_Visitor().visit(cu, null);
return cu.toString(ppconf);
}
static int findCodeTokens(List tok, String... tokens) {
return findCodeTokens(tok, 1, false, tokens);
}
static int findCodeTokens(List tok, boolean ignoreCase, String... tokens) {
return findCodeTokens(tok, 1, ignoreCase, tokens);
}
static int findCodeTokens(List tok, int startIdx, boolean ignoreCase, String... tokens) {
return findCodeTokens(tok, startIdx, ignoreCase, tokens, null);
}
static HashSet findCodeTokens_specials = lithashset("*", "", "", "", "\\*");
static int findCodeTokens_bails, findCodeTokens_nonbails;
static interface findCodeTokens_Matcher {
boolean get(String token);
}
static int findCodeTokens(List tok, int startIdx, boolean ignoreCase, String[] tokens, Object condition) {
int end = tok.size()-tokens.length*2+2, nTokens = tokens.length;
int i = startIdx | 1;
findCodeTokens_Matcher[] matchers = new findCodeTokens_Matcher[nTokens];
IContentsIndexedList2 indexedList = tok instanceof IContentsIndexedList2 ? (IContentsIndexedList2) tok : null;
TreeSet indices = null;
int indicesOfs = 0;
for (int j = 0; j < nTokens; j++) {
String p = tokens[j];
findCodeTokens_Matcher matcher;
if (p.equals("*"))
matcher = t -> true;
else if (p.equals(""))
matcher = t -> isQuoted(t);
else if (p.equals(""))
matcher = t -> isIdentifier(t);
else if (p.equals(""))
matcher = t -> isInteger(t);
else if (p.equals("\\*"))
matcher = t -> t.equals("*");
else if (ignoreCase)
matcher = t -> eqic(p, t);
else {
matcher = t -> t.equals(p);
if (indexedList != null) {
TreeSet indices2 = indexedList.indicesOf_treeSetOfHasIndex(p);
if (indices2 == null) return -1;
if (indices == null || indices2.size() < indices.size()) {
// found shorter list
indices = indices2;
indicesOfs = j;
}
}
}
matchers[j] = matcher;
}
// go over shortest index
if (indices != null) {
int min = i+indicesOfs*2;
SortedSet tailSet = min == 1 ? indices : indices.tailSet(new HasIndex(min));
outer: for (HasIndex h : tailSet) {
int idx = h.idx-indicesOfs*2;
if (idx >= end) break;
for (int j = 0; j < nTokens; j++) try {
if (!matchers[j].get(tok.get(idx+j*2)))
continue outer;
} catch (IndexOutOfBoundsException e) {
print("fct indicesOfs=" + indicesOfs + ", h=" + h + ", idx=" + idx);
throw e;
}
if (condition == null || checkTokCondition(condition, tok, idx-1))
return idx;
}
return -1;
}
outer: for (; i < end; i += 2) {
for (int j = 0; j < nTokens; j++)
if (!matchers[j].get(tok.get(i+j*2)))
continue outer;
if (condition == null || checkTokCondition(condition, tok, i-1)) // pass N index
return i;
}
return -1;
}
static boolean autoQuine = true;
static int maxQuineLength = 80;
static boolean assumeTriple = true;
static boolean quickInstanceOfEnabled = false; // interferes with things
static boolean debug_jreplace = false;
// _registerThread usually costs nothing because we need
// the registerWeakHashMap mechanism anyway for ping().
// Anyway - no forced functions for now :)
static List functionsToAlwaysInclude = ll(
//"_registerThread",
//"asRuntimeException"
);
// classes with two type parameters that can written with just one
// e.g. Pair => Pair
static Set pairClasses = lithashset("Pair", "Either", "Map", "AbstractMap", "HashMap", "TreeMap", "LinkedHashMap", "MultiMap", "CompactHashMap", "WrappedMap", "F1", "IF1", "AllOnAll", "AllOnAllWithUpdates", "MultiSetMap", "AutoMap");
static String transpilingSnippetID;
//static new AtomicInteger varCount;
static ThreadLocal varCountByThread = new ThreadLocal();
static Map snippetCache = syncMap();
static boolean useIndexedList2 = false, useTokenIndexedList = true;
static boolean opt_javaTok = true;
static boolean cacheStdFunctions = true, cacheStdClasses = true;
static HashMap cachedIncludes = new HashMap();
static ExecutorService executor;
static List lclasses;
static long startTime, lastPrint;
// These variables have to be cleared manually for each transpilation
static HashSet included = new HashSet();
static Set definitions = ciSet();
static HashMap rewrites = new HashMap();
static HashSet shouldNotIncludeFunction = new HashSet();
static HashSet shouldNotIncludeClass = new HashSet();
static HashSet doNotIncludeFunction = new HashSet();
static HashSet addedFunctions = new HashSet();
static HashSet addedClasses = new HashSet();
static HashSet hardFunctionReferences = new HashSet();
static HashSet mapLikeFunctions = new HashSet();
static HashSet lambdaMapLikeFunctions = new HashSet();
static HashSet curry1LikeFunctions = new HashSet();
static HashSet mapMethodLikeFunctions = new HashSet();
static HashSet nuLikeFunctions = new HashSet();
static Map extraStandardFunctions;
static boolean quickmainDone1, quickmainDone2;
static TreeSet libs = new TreeSet();
static String mainBaseClass, mainPackage, mainClassName;
static boolean localStuffOnly = false; // for transpiling a fragment
static boolean asInclude = false; // for transpiling an include (auto-close scopes)
static boolean allowMetaCode = false; // run any embedded meta code
static List metaPostBlocks, metaTransformers;
static boolean dontPrintSource = false;
static boolean dontLoadCachedIncludesFromVM = false; // for benchmarking
static class CachedInclude {
String javax;
Future java;
String realJava;
long snippetID;
CachedInclude() {}
CachedInclude(long snippetID) {
this.snippetID = snippetID;}
String java() {
return realJava != null ? realJava : getFuture(java);
}
Future javaFuture() {
return realJava != null ? nowFuture(realJava) : java;
}
void clean() {
if (java != null) {
realJava = getFuture(java);
java = null;
}
}
}
public static void main(final String[] args) throws Exception {
startTime = lastPrint = sysNow();
try {
if (!dontLoadCachedIncludesFromVM)
vmKeepWithProgramMD5_get("cachedIncludes");
} catch (Throwable __e) { _handleException(__e); }
executor = Executors.newFixedThreadPool(numberOfCores());
transpilingSnippetID = or(getThreadLocal((ThreadLocal) getOpt(javax(), "transpilingSnippetID")), transpilingSnippetID);
print("transpilingSnippetID", transpilingSnippetID);
transpileRaw_dontCopyFromCreator = true;
final Object oldPrint = or(print_byThread().get(), "print_raw");
AutoCloseable __1 = tempInterceptPrint(new F1() {
Boolean get(String s) {
long now = sysNow();
long time = now-lastPrint; // -startTime;
lastPrint = now;
callF(oldPrint, "[" + formatInt(time/1000, 2) + ":" + formatInt(time % 1000, 3) + "] " + s);
return false;
}
}); try {
try {
_main();
} finally {
interceptPrintInThisThread(oldPrint);
if (executor != null) executor.shutdown();
executor = null;
localStuffOnly = false;
asInclude = false;
}
} finally { _close(__1); }}
static void _main() { try {
if (sameSnippetID(programID(), defaultJavaXTranslatorID())) setDefaultJavaXTranslatorID("#7");
//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;
String in = loadMainJava();
print("759 STARTING " + identityHashCode(main.class));
// clear things
includeInMainLoaded_magicComment = null;
included.clear();
definitions.clear();
rewrites.clear();
definitions.add("SymbolAsString");
shouldNotIncludeFunction.clear();
shouldNotIncludeClass.clear();
doNotIncludeFunction.clear();
addedFunctions.clear();
addedClasses.clear();
hardFunctionReferences.clear();
mapLikeFunctions = cloneHashSet(tok_mapLikeFunctions());
lambdaMapLikeFunctions = new HashSet();
curry1LikeFunctions = new HashSet();
mapMethodLikeFunctions = cloneHashSet(tok_mapMethodLikeFunctions());
nuLikeFunctions.clear();
extraStandardFunctions = new HashMap();
libs.clear();
mainBaseClass = mainPackage = mainClassName = null;
varCountByThread.set(null);
quickmainDone1 = quickmainDone2 = false;
metaPostBlocks = new ArrayList();
metaTransformers = new ArrayList();
dontPrintSource = false;
defaultMaxQuineLength_value = defaultMaxQuineLength_defaultValue;
debug_jreplace = false;
//L ts = findTranslators(toLines(join(tok)));
//print("Translators in source at start: " + structure(ts));
List tok = jtok(in);
try {
tok_definitions(tok);
// add m { }
if (!localStuffOnly && !hasCodeTokens(tok, "m", "{") && !hasCodeTokens(tok, "main", "{") && !hasCodeTokens(tok, "class", "main")) {
if (l(tok) == 1) tok = singlePlusList(first(tok), dropFirst(javaTok("m {}")));
else {
replaceTokens_reTok(tok, 1, 2, "m {\n\n" + unnull(get(tok, 1)));
replaceTokens_reTok(tok, l(tok)-2, l(tok)-1, unnull(get(tok, 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_metaTransformNow(tok);
tok_processEarlyIncludes(tok);
tok_earlyGeneralStuff(tok);
tok = tok_processIncludes(tok); // before standard functions
if (processConceptsDot(tok))
tok = tok_processIncludes(tok);
tok = localStuff1(tok);
if (!localStuffOnly) {
int safety = 0;
boolean same = false;
do { // BIG LOOP
ping();
List before = cloneList(tok);
// do the non-local stuff (flags and rewrites, things that correlate across includes like tok_selfType)
// to allow crazy stuff like "nonStatic !include #someStaticClass"
jreplace(tok, "nonStatic static", "");
tok_selfType(tok);
tok_mainClassNameAndPackage(tok);
tok_definitions(tok);
tok_ifndef(tok);
tok_ifdef(tok);
defineMapLikes(tok);
defineLambdaMapLikes(tok);
if (tok_applyMapLikeFunctions(tok, mapLikeFunctions))
functionReferences(tok);
if (tok_applyLambdaMapLikeFunctions(tok, lambdaMapLikeFunctions))
lambdaReferences(tok);
defineCurry1Likes(tok);
if (tok_applyCurry1LikeFunctions(tok, curry1LikeFunctions))
lambdaReferences(tok);
defineMapMethodLikes(tok);
tok_applyMapMethodLikeFunctions(tok, mapMethodLikeFunctions);
defineNuLikes(tok);
tok_applyNuLikeFunctions(tok, nuLikeFunctions);
tok_dropExtraCommas(tok); // from e.g. tok_applyMapMethodLikeFunctions
tok_delegateTo(tok);
tok_replaceWith(tok);
tok_findRewrites(tok);
tok_processRewrites(tok);
// main bla(...) => mainClassName.bla(...)
jreplace(tok, "main (", or(mainClassName, "main") + ".$2(");
try {
if (safety == 0) tok = quickmain(tok);
} catch (Throwable e) {
printSources(tok);
rethrow(e);
}
tok_collectMetaPostBlocks(tok, metaPostBlocks);
tok_collectTransformers(tok, metaTransformers);
tok_metaTransformNow(tok);
// Hack to allow DynModule to reimplement _registerThread
/*if (tok.contains("DynModule") && !addedClasses.contains("DynModule"))
addStandardClasses_v2(tok);*/
defineExtraSF(tok);
tok = standardFunctions(tok);
tok = stdstuff(tok); // all the keywords, standard
String diff;
long startTime = now();
//diff = unidiff(before, join(tok));
//print("unidiff: " + (now()-startTime) + " ms");
//same = eq(diff, "");
same = eq(tok, before);
if (!same) {
print("Not same " + safety + ".");
//print(indent(2, diff));
}
if (safety++ >= 10) {
//print(unidiff(before, join(tok)));
printSources(tok);
throw fail("safety 10 error!");
}
} while (!same); // END OF BIG LOOP
print("Post.");
if (mainBaseClass != null) {
jreplace1(tok, "class main", "class main extends " + mainBaseClass);
mainBaseClass = null;
}
print("moveImportsUp"); tok_moveImportsUp(tok);
print("Indexing"); tok = indexTokenList(tok);
// POST-PROCESSING after stdstuff loop
if (transpilingSnippetID != null)
jreplace_dyn(tok, "class whatever",
new F2, Integer, String>() { public String get(List tok, Integer cIndex) { try {
try { return "class " + stringToLegalIdentifier(getSnippetTitle(transpilingSnippetID)); } catch (Throwable __e) { _handleException(__e); }
return "class Whatever";
} catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "pcall { ret \"class \" + stringToLegalIdentifier(getSnippetTitle(transpilingSni..."; }});
//print("Type"); // Type to Type
print("extendClasses"); tok = extendClasses(tok);
print("libs"); libs(tok);
print("sourceCodeLine"); sourceCodeLine(tok);
tok_overridableFunctionDefs(tok, null);
// escaping for lambdas
//jreplace(tok, "-=>", "->");
// Stuff that depends on the list of inner classes (haveClasses)
HashSet haveClasses = haveClasses_actual(tok);
print("innerClassesVar"); innerClassesVar(tok, haveClasses);
fillVar_transpilationDate(tok);
haveClasses_addImported(tok, haveClasses);
print("ifclass"); tok_ifclass(tok, haveClasses);
print("slashCasts"); slashCasts(tok, haveClasses);
print("newWithoutNew"); newWithoutNew(tok, haveClasses);
if (!assumeTriple) {
print("Triple");
if (tok.contains("Triple") && !haveClasses.contains("Triple")) {
jreplace(tok, "Triple", "T3");
haveClasses.remove("Triple");
haveClasses.add("T3");
slashCasts(tok, lithashset("T3"));
tok_quickInstanceOf(tok, lithashset("T3"));
}
expandTriple(tok);
}
if (hasDef("SymbolAsString"))
jreplace(tok, "Symbol", "String");
print("classReferences"); expandClassReferences_lazy(tok, haveClasses);
if (metaCodeAllowed()) runMetaPostBlocks(tok);
// Error-checking
print("Error-checking");
Set functions = new HashSet(findFunctions(tok));
for (String f : hardFunctionReferences)
if (!functions.contains(f))
throw fail("Function " + f + " requested, but not supplied");
print("autoImports"); tok = autoImports(tok); // faster to do it at the end
if (includeInMainLoaded_magicComment != null) {
int i = lastIndexOfStartingWith(tok, includeInMainLoaded_magicComment);
if (i >= 0) tok.set(i, dropPrefix(includeInMainLoaded_magicComment, tok.get(i)));
}
print("definitions=" + sfu(definitions));
if (containsOneOfIC(definitions, "allpublic", "reparse", "PublicExceptTopClass")) {
// Fire up the Java parser & pretty printer!
print(containsIC(definitions, "allpublic")? "Making all public." : "Reparsing.");
//try {
String src = join(tok);
try {
if (containsIC(definitions, "PublicExceptTopClass"))
src = javaParser_makeAllPublic(src, "notTopLevelClassDecl" , true);
else if (containsIC(definitions, "keepComments"))
src = javaParser_makeAllPublic_keepComments(src);
else if (containsIC(definitions, "allpublic"))
src = javaParser_makeAllPublic(src);
else
src = javaParser_reparse_keepComments(src);
} catch (Throwable e) {
extractAndPrintJavaParseError(src, e);
File f = javaxCachesDir("error-source.java");
saveTextFileVerbose(f, src);
dontPrintSource = true;
throw fail("Java parsing error"); // drop the long nested parser stacktraces
}
tok = jtok(src);
/*} catch e {
S src = join(tok);
if (!dontPrintSource)
print(src);
print(f2s(saveProgramTextFile("error.java", src)));
throw rethrow(e);
}*/
}
// Do this after JavaParser (because it doesn't like package after class)
if (mainPackage != null) {
print("mainPackage");
tokPrepend(tok, 1, "package " + mainPackage + ";\n");
reTok(tok, 1, 2);
}
if (mainClassName != null) {
print("mainClassName");
jreplace(tok, "class main", "class " + mainClassName);
jreplace(tok, "main.class", mainClassName + ".class");
//tokPrepend(tok, 1, "class main {}\n"); // so main.class is generated and compiler sanity checks succeed. we can later skip it in the JavaXClassLoader
}
if (nempty(libs)) {
print("Adding libs: " + libs);
tok.add(concatMap_strings(new F1() { public String get(String s) { try { return "\n!" + s; } catch (Exception __e) { throw rethrow(__e); } }
public String toString() { return "\"\\n!\" + s"; }}, libs));
}
} // if (!localStuffOnly)
} catch (Throwable e) {
String src = join(tok);
if (!dontPrintSource)
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.");
// for dexcompile.php
if (mainClassName != null)
tokPrepend(tok, 0, "//FILENAME: "
+ (mainPackage != null ? mainPackage.replace(".", "/") + "/" : "") + mainClassName + ".java\n");
if (mainJava != null)
mainJava = join(tok);
else if (tok.contains("package"))
splitJavaFiles(tok);
else
saveMainJava(tok);
} catch (Exception __e) { throw rethrow(__e); } }
static List localStuff1(List tok) {
int safety = 0, i;
boolean same = false;
tok = indexTokenList(tok);
tok_scopes(tok, "autoCloseScopes" , true);
do {
ping();
List before = cloneList(tok);
//print("localStuff loop " + safety);
earlyStuff(tok);
// EARLY local stuff goes here
tok_dropMetaComments(tok);
tok_earlyGeneralStuff(tok);
conceptDeclarations(tok);
tok_recordDecls(tok);
tok = multilineStrings(tok);
tok_singleQuoteIdentifiersToStringConstants(tok);
tok_inStringEvals(tok);
tok_listComprehensions(tok);
tok_eventFunctions(tok);
tok_for_single(tok);
tok_for_unpair(tok); // Do this...
tok_doubleFor_v2(tok); // ...before this
tok_forUnnull(tok);
tok_ifCast(tok);
forPing(tok);
tok_directSnippetRefs(tok);
quicknu(tok);
//tok_juxtaposeCalls(tok);
jreplace(tok, "LLS", "L");
jreplace(tok, "LS", "L");
jreplace(tok, "ES", "Ext");
jreplace(tok, "ExtS", "Ext");
jreplace(tok, "dispose ;", "{ cleanUp($2); $2 = null; }");
jreplace(tok, "do ping {", "do { ping();");
replaceKeywordBlock(tok,
"swing",
"{ swing(r {",
"}); }");
replaceKeywordBlock(tok,
"null",
"{",
"null; }");
replaceKeywordBlock(tok,
"androidUI",
"{ androidUI(r {",
"}); }");
replaceKeywordBlock(tok,
"withDBLock",
"{ withDBLock(r {",
"}); }");
replaceKeywordBlock(tok, "afterwards", "temp tempAfterwards(r {", "});");
for (String keyword : ll("tokcondition", "tokCondition"))
replaceKeywordBlock(tok,
keyword,
"new TokCondition { bool get(final L tok, final int i) {",
"}}");
jreplace(tok, "synced ", "synchronized $2");
jreplace(tok, "sync ", "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",
"{ temp tempShowLoadingAnimation(); ",
"}");
replaceKeywordPlusQuotedBlock(tok, "loading",
new Object() { String[] get(List tok, int i) {
String text = tok.get(i+2);
return new String[] {
"{ temp tempShowLoadingAnimation(" + text + "); ",
"}" };
}});
while ((i = jfind(tok, "visualize as")) >= 0) {
int j = tok_findEndOfStatement(tok, i); // expression, rather
tok.set(i+2, "{ ret");
tok.set(j-1, "; }");
reTok(tok, i, j);
}
for (String pat : ll("visual {", "visualize {"))
jreplace(tok, pat, "public JComponent visualize() {", tokCondition_beginningOfMethodDeclaration());
jreplace(tok, "visualize2 {", "JComponent visualize2() {", tokCondition_beginningOfMethodDeclaration());
replaceKeywordBlock(tok, "start-thread-printDone", "start-thread {", "printDone(); }");
replaceKeywordBlock(tok, "start-thread", "start { thread \"Start\" { temp enter(); pcall {", "}}}");
jreplace(tok, "start {", "void start() ctex { super.start();", tokCondition_beginningOfMethodDeclaration());
// run { ... } => public void run() { ... }
jreplace(tok, "run {", "public void run() {", new TokCondition() { boolean get(final List tok, final int i) {
return neqGet(tok, i-1, "void");
}});
replaceKeywordBlock(tok, "html",
"static O html(S uri, fMap params) ctex " + "{\n", "}");
replaceKeywordBlock(tok, "afterVisualize",
"visualize { JComponent _c = super.visualize();",
"ret _c; }");
replaceKeywordBlock(tok, "enhanceFrame",
"void enhanceFrame(Container f) { super.enhanceFrame(f);",
"}");
if (assumeTriple) {
jreplace(tok, "Triple", "T3");
expandTriple(tok);
}
tok_shortFinals(tok);
tok_moduleClassDecls(tok);
jreplace(tok, "static sync", "static synchronized");
jreplace(tok, "sclass", "static class");
jreplace(tok, "fclass", "final class");
jreplace(tok, "fsclass", "final static class");
jreplace(tok, "srecord", "static record");
jreplace(tok, "strecord", "static transformable record");
jreplace(tok, "record noeq", "noeq record");
jreplace(tok, "asclass", "abstract static class");
jreplace(tok, "sinterface", "static interface");
jreplace(tok, "ssynchronized", "static synchronized");
jreplace(tok, "ssvoid", "static synchronized void");
jreplace(tok, "sbool", "static bool");
jreplace(tok, "fbool", "final 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");
jreplace(tok, "Sym", "Symbol");
jreplace(tok, "SymSym", "SymbolSymbol");
jreplace(tok, "SS", "Map");
jreplace(tok, "SymbolSymbol", "Map");
jreplace(tok, "MapSO", "Map");
jreplace(tok, "ByName<", "IF1>", "IF1<$3>");
jreplace(tok, "ITransform", "IF1");
jreplace(tok, "PairS", "Pair");
jreplace(tok, "LPairS", "L>");
jreplace(tok, "T3S", "T3");
jreplace(tok, "F1S", "F1");
jreplace(tok, "ItIt", "IterableIterator");
jreplace(tok, "CloseableItIt", "CloseableIterableIterator");
jreplace(tok, "class > ", "class $2 extends $4", new TokCondition() { boolean get(final List tok, final int i) {
String t = _get(tok, i+1+4*2);
return eq(t, "{") || isIdentifier(t);
}});
jreplace(tok, "class > <> {", "class $2 extends $4 $5 $6 $7 {");
jreplace(tok, "ISegmenter", "IF1>");
// IPred => IF1
jreplace(tok, "IPred<>", "IF1<$3, Bool>");
jreplace(tok, "IPred<[]>", "IF1<$3[], Bool>");
// Proposition => WithReasoning (should we really define this?)
jreplace(tok, "Proposition", "WithReasoning");
replaceKeywordBlock(tok, "print exceptions",
"try {",
"} catch print e { throw rethrow(e); }");
// "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 { _handleException(e); "
jreplace(tok, "catch print {", "catch $3 { _handleException($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 TokCondition() { boolean get(final List tok, final int i) {
String word = tok.get(i+3);
return startsWithLowerCaseOrUnderscore(word);
}});
jreplace(tok, "+ +", "+", new TokCondition() { boolean get(final List tok, final int i) {
//printStructure("++: ", subList(tok, i-1, i+6));
if (empty(_get(tok, i+2))) return false; // no space between the pluses
if (empty(_get(tok, i)) && eq("+", _get(tok, i-1))) return false; // an actual "++" at the left
if (empty(_get(tok, i+4)) && eq("+", _get(tok, i+5))) return false; // an actual "++" at the right
//print("doing it");
return true;
}});
// some crazy fancy syntax
jreplace(tok, "set ;", "$2 = true;");
// single underscore (not allowed in Java anymore) to double underscore
jreplace(tok, "_", "__");
// [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); }");
// [stdToString] -> toString { ret stdToString(this); }
jreplace(tok, "[stdToString]",
"toString { ret stdToString(this); }");
// [concepts] "concept.field!" for dereferencing references
jreplace(tok, "*!", "$1.get()", new TokCondition() { boolean get(final List tok, final int i) {
String l = tok.get(i+1);
if (!(isIdentifier(l) || eq(l, ")"))) return false;
if (tok.get(i+2).contains("\n")) return false; // no line break between and !
if (nempty(tok.get(i+4))) return true; // space after = ok
String t = _get(tok, i+5);
if (t == null) return false;
if (isIdentifier(t) || eqOneOf(t, "=", "(")) return false;
return true;
}});
jreplace(tok, "for ( )", "for ($3 $4 : list($3))");
jreplace(tok, "for (final )", "for (final $4 $5 : list($4))");
// "continue unless", "break unless"
for (String phrase : ll("continue unless", "break unless"))
while ((i = jfind(tok, phrase)) >= 0) {
String keyword = tok.get(i);
int j = scanOverExpression(tok, getBracketMap(tok), i+4, ";");
replaceTokens(tok, i, i+4, "{ if (!(");
tok.set(j, ")) " + keyword + "; }");
reTok(tok, i, j+1);
}
// S s = bla(), return if null; => S s = bla(); if (s == null) return;
// same with continue, break
while ((i = jfind(tok, ", if null;", new TokCondition() { boolean get(final List tok, final int i) {
return eqOneOf(tok.get(i+3), "return", "ret", "continue", "break");
}})) >= 0) {
String cmd = tok.get(i+2);
int j = tok_findBeginningOfStatement(tok, i);
print("Found statement " + j + "/" + i + " - " + joinSubList(tok, j-1, i+5*2-1));
String var = getVarDeclarationName(subList(tok, j-1, i));
replaceTokens_reTok(tok, i, i+5*2-1, "; if (" + var + " == null) " + cmd + ";");
}
// S s = bla(), return false if null; => S s = bla(); if (s == null) return false;
// (with false being any identifier)
while ((i = jfind(tok, ", return if null;")) >= 0) {
String returnValue = tok.get(i+4);
int j = tok_findBeginningOfStatement(tok, i);
String var = getVarDeclarationName(subList(tok, j-1, i));
replaceTokens_reTok(tok, i, i+6*2-1, "; if (" + var + " == null) return " + returnValue + ";");
}
// "continue if", "break if"
for (String phrase : ll("continue if", "break if"))
while ((i = jfind(tok, phrase)) >= 0) {
String keyword = tok.get(i);
int j = scanOverExpression(tok, getBracketMap(tok), i+4, ";");
replaceTokens(tok, i, i+4, "{ if (");
tok.set(j, ") " + keyword + "; }");
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 if"
// "return with " / "continue with " / "break with "
while ((i = jfind(tok, " with", new TokCondition() { boolean get(final List tok, final int i) { return eqOneOf(tok.get(i+1), "return", "continue", "break"); }})) >= 0) {
// XXX int j = scanOverExpression(tok, getBracketMap(tok), i+4, ";");
int j = tok_findEndOfStatement(tok, i+4)-1;
//print("Found statement: " + joinSubList(tok, i+4, j+1));
tok.set(j, "; " + tok.get(i) + "; }");
replaceTokens(tok, i, i+3, "{");
reTok(tok, i, j+1);
}
while ((i = jfind(tok, "return if")) >= 0) {
int j = scanOverExpression(tok, getBracketMap(tok), i+4, ";");
tok.set(j, ") return " + tok.get(i+2) + "; }");
replaceTokens(tok, i, i+6, "{ if (");
reTok(tok, i, j+1);
}
// return "bla" with
while ((i = jfindOneOf(tok,
"return with", "return with")) >= 0) {
String 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);
}
tok_debugStatements(tok);
// while not null (...) / if not null (...)
while ((i = jfind_check("not",tok, " not null (", new TokCondition() { boolean get(final List tok, final int i) {
return eqOneOf(_get(tok, i+1), "if", "while");
}})) >= 0) {
int closingBracket = findEndOfBracketPart(tok, i+6)-1;
replaceTokens(tok, i+2, i+6, "(");
tok.set(closingBracket, ") != null)");
reTok(tok, i, closingBracket+1);
}
// while null (...) / if null (...)
while ((i = jfind_check("null",tok, " null (", new TokCondition() { boolean get(final List tok, final int i) {
return eqOneOf(_get(tok, i+1), "if", "while");
}})) >= 0) {
int closingBracket = findEndOfBracketPart(tok, i+4)-1;
replaceTokens(tok, i+2, i+4, "(");
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) {
String 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 func1 func2 func3(...) => mapFGH(f func1, f func2, f func3, ...)
jreplace(tok, "map (", "mapFGH(f $2, f $3, f $4,");
// map func1 func2(...) => mapFG(f func1, f func2, ...)
jreplace(tok, "map (", "mapFG(f $2, f $3,");
// "ref->bla" for dereferencing Concept.Ref or ThreadLocal or other
// For lambdas, use SPACES on the left or right of the arrow!
//jreplace(tok, " ->", "$1.get().");
jreplace(tok, "->", ".get().", new TokCondition() { boolean get(final List tok, final int i) {
return empty(tok.get(i)) // no space on left of arrow
&& empty(tok.get(i+2)) // no space inside of arrow
&& empty(tok.get(i+4)) // no space on right of arrow
&& !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);
tok_extend(tok);
jreplace(tok, "pn {", "p-noconsole {");
// Do these BEFORE awt replacement! ("p-awt" contains "awt" token)
replaceKeywordBlock(tok, "r-awt", "r { awt {", "}}");
if (hasCodeTokens(tok, "p", "-")) tok_p_old(tok);
replaceKeywordBlock(tok, "awt-messagebox", "awt { pcall-messagebox {", "}}");
replaceKeywordBlock(tok, "awt", "swingLater(r {", "});");
jreplace(tok, "p-android {", "set flag Android. p {");
unswing(tok);
lockBlocks(tok);
tok_switchTo(tok);
// trim x;
jreplace(tok, "trim ;", "$2 = trim($2);");
// iterate with index
jreplace (tok, "for over :", "for (int $2 = 0; $2 < l($4); $2++)");
jreplace (tok, "for backwards over :", "for (int $2 = l($5)-1; $2 >= 0; $2--)");
jreplace (tok, "for , over : {", "for (int $2 = 0; $2 < l($7); $2++) { $4 $5 = $7.get($2);");
jreplace (tok, "for , backwards over : {", "for (int $2 = l($8)-1; $2 >= 0; $2--) { $4 $5 = $8.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);
tok_equalsCast(tok);
tok_equalsOptCast(tok);
replaceKeywordBlock(tok, "r-thread-messagebox", "r-thread { pcall-messagebox {", "}}");
replaceKeywordBlock(tok, "thread-messagebox", "thread { pcall-messagebox {", "}}");
jreplace(tok, "rThread {", "r-thread {");
jreplace(tok, "rThreadEnter {", "r-thread { temp enter(); ");
replaceKeywordBlock(tok, "r-thread", "runnableThread(r {", "})");
rNamedThread(tok);
// only works in the scope of a DynModule
jreplace(tok, "rEnter {", "r { temp enter(); ");
replaceKeywordBlock(tok, "r-pcall", "r { pcall {", "}}");
replaceKeywordBlock(tok, "r-messagebox", "r { pcall-messagebox {", "}}");
jreplace(tok, "r + r ", "r { $2(); $5(); }");
// runnable and r - now also with automatic toString if enabled
for (String keyword : ll("runnable", "r")) {
while ((i = jfind(tok, keyword + " {")) >= 0) {
int idx = findCodeTokens(tok, i, false, "{");
int j = findEndOfBracketPart(tok, idx);
List contents = subList(tok, idx+1, j-1);
replaceTokens(tok, i, j+1, "new Runnable {"
+ " public void run() ctex { " + tok_addSemicolon(contents) + "\n}"
+ (autoQuine ? tok_autoQuineFunc(contents) : "")
+ "}");
reTok(tok, i, j+1);
}
while ((i = jfind(tok, keyword + " {")) >= 0) {
int idx = findCodeTokens(tok, i, false, "{");
int j = findEndOfBracketPart(tok, idx);
List contents = subList(tok, idx+1, j-1);
replaceTokens(tok, i, j+1, "new Runnable {"
+ " public void run() ctex { " + tok_addSemicolon(contents) + "\n}"
+ " toString { ret " + tok.get(i+2) + "; }"
+ (autoQuine ? tok_autoQuineFunc(contents, "_shortenedSourceCode") : "")
+ "}");
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.
Object cond = new TokCondition() { boolean get(final List tok, final int i) {
return tok_tokenBeforeLonelyReturnValue(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);
// ok => ret "OK" with
jreplace(tok, "ok ", "return \"OK\" with $2");
replaceKeywordBlock(tok, "ok", "{", " return \"OK\"; }");
// "myFunction;" instead of "myFunction();" - quite rough
// (isolated identifier as function call)
cond = new TokCondition() {
boolean get(List tok, int i) {
String word = tok.get(i+3);
//print("single word: " + word);
return !eqOneOf(word, "break", "continue", "return", "else", "endifdef", "endif");
}
};
for (String pre : litlist("}", ";"))
jreplace(tok, pre + " ;", "$1 $2();", cond);
// shorter match syntax for answer methods
tok_expandIfQuoted(tok);
jreplace(tok, "if eq ", "if (eq($2, $4))");
tok_dropExtraCommas(tok);
// additional translations (if necessary)
jreplace(tok, "pcall ping {", "pcall { ping();");
replaceKeywordBlock(tok, ") pcall",
") { pcall {",
"}}");
replaceKeywordBlock(tok,
"pcall",
"try {",
"} catch (Throwable __e) { _handleException(__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, "bench", "*", "{")) >= 0) {
int j = findEndOfBracketPart(tok, i+4)-1;
String time = makeVar("time");
String v = makeVar("bench");
String n = tok.get(i+2);
tok.set(i, "{ long " + time + " = sysNow(); for (int " + v + " = 0; " + v + " < " + n + "; " + v + "++)");
tok.set(i+2, "");
tok.set(j, "} printBenchResult(sysNow()-" + time + ", " + n + "); }");
reTok(tok, i, j+1);
}
replaceKeywordBlockDyn(tok,
"time",
new Object() { String[] get() {
String var = makeVar("startTime");
return new String[] {
"{ long " + var + " = sysNow(); try { ",
"} finally { " + var + " = sysNow()-" + var + "; saveTiming(" + var + "); } }"};
}});
// version without { }
replaceKeywordBlockDyn(tok,
"time2",
new Object() { String[] get() {
String var = makeVar("startTime");
return new String[] {
"long " + var + " = sysNow(); ",
" " + var + " = sysNow()-" + var + "; saveTiming(" + var + "); "};
}});
// time "bla" {
// time msg {
replaceKeywordPlusQuotedOrIDBlock(tok,
"time",
new Object() { String[] get(List tok, int i) {
String var = makeVar("startTime");
return new String[] {
"long " + var + " = sysNow(); ",
" done2_always(" + tok.get(i+2) + ", " + var + "); "};
}});
if (hasCodeTokens(tok, "assertFail", "{")) {
String 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; }",
new TokCondition() { boolean get(final List tok, final int i) {
return 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 {",
"}}}");
for (String keyword : ll("autocloseable", "autoCloseable"))
/*replaceKeywordBlock(tok,
keyword,
"new AutoCloseable() { public void close() throws Exception {",
"}}");*/
replaceKeywordBlock_dyn2_legacy(tok, keyword, new Object() {
String[] get(List tok, int iOpening, int iClosing) {
List contents = subList(tok, iOpening+1, iClosing);
return new String[] {
"new AutoCloseable() { toString { ret " + quote(shorten(defaultMaxQuineLength(), trimJoin(contents))) + "; } public void close() throws Exception {",
"}}"
};
}
});
// try answer (string, test with nempty)
while ((i = findCodeTokens(tok, "try", "answer")) >= 0) {
int j = findEndOfStatement(tok, i);
String v = makeVar();
boolean needCurly = !eqGet(tok, i-2, "{");
tok.set(i, (needCurly ? "{" : "") + " S " + v);
tok.set(i+2, "=");
tok.set(j-1, "; if (!empty(" + v + ")) ret " + v + "; " + (needCurly ? "}" : ""));
reTok(tok, i, j);
}
// try bool[ean] (try answer with Bool type)
while ((i = findCodeTokens(tok, "try", "boolean")) >= 0) {
int j = findEndOfStatement(tok, i);
String v = makeVar();
tok.set(i, "{ Bool " + v);
tok.set(i+2, "=");
tok.set(j-1, "; if (" + v + " != null) ret " + v + "; }");
reTok(tok, i, j);
}
// , print "..."; => { ; print("..."); }
while ((i = jfind(tok, ", print ;")) >= 0) {
int j = tok_findBeginningOfStatement(tok, i);
replaceTokens_reTok(tok, i, i+4*2-1, "; print(" + tok.get(i+4) + "); }");
tokPrepend_reTok(tok, j, "{ ");
}
// return if null => if ( == null) return;
while ((i = jfind(tok, "return if null")) >= 0) {
int j = findEndOfStatement(tok, i);
clearTokens(tok, i, i+2);
tok.set(i+4, "((");
tok.set(j-1, ") == null) ret;");
reTok(tok, i, j);
}
// return optional (return if not null)
while ((i = jfind_check("optional",tok, "return optional =")) >= 0) {
int j = findEndOfStatement(tok, i);
String 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);
}
// try object (return if not null)
while ((i = jfind_check("object",tok, "try object")) >= 0) {
int j = findEndOfStatement(tok, i);
clearTokens(tok, i, i+3);
boolean isDecl = isIdentifier(get(tok, i+4)) && isIdentifier(get(tok, i+6)) && eqGet(tok, i+8, "=");
if (isDecl) {
String v = get(tok, i+6);
tok.set(i, "{");
tok.set(j-1, "; if (" + v + " != null) ret " + v + "; }");
} else {
String v = makeVar();
tok.set(i, "{ O " + v + "=");
tok.set(j-1, "; if (" + v + " != null) ret " + v + "; }");
}
reTok(tok, i, j);
}
// try Int i = ...; (return if not null, shorter version of "try object")
/*while ((i = jfind(tok, "try =")) >= 0) {
int j = findEndOfStatement(tok, i);
S type = tok.get(i+2), v = tok.get(i+4);
tok.set(i, "{");
tok.set(j-1, "; if (" + v + " != null) ret " + v + "; }");
reTok(tok, i, j);
}*/
while ((i = jfind(tok, "try ")) >= 0) {
int iType = i+2, iVar = tok_findEndOfType(tok, iType);
String v = tok.get(iVar);
assertEquals("try object", "=", get(tok, iVar+2));
int j = findEndOfStatement(tok, iVar);
tok.set(i, "{");
tok.set(j-1, "; if (" + v + " != null) ret " + v + "; }");
reTok(tok, i, j);
}
// debug print ...; => if (debug) print(...);
while ((i = jfind(tok, "debug print")) >= 0) {
int j = findEndOfStatement(tok, i);
replaceTokens(tok, i, i+4, "if (debug) print(");
tok.set(j-1, ");");
reTok(tok, i, j);
}
functionReferences(tok);
tok_expandLPair(tok);
tok_expandPairL(tok);
tok_expandLT3(tok);
tok_quicknew2(tok);
tempBlocks(tok); // after quicknew2 for stuff like "temp new X x;"
// X x = nu(+...) => X x = nu X(+...)
jreplace(tok, " = nu(+", "$1 $2 = nu $1(+");
// X x = nu(a := ...) => X x = nu X(a := ...)
jreplace(tok, " = nu( :=", "$1 $2 = nu $1($6 :=");
tok_expandVarCopies(tok); // AFTER the lines just above
tok_replaceColonEqualsSyntax(tok); // ditto
tok_unpair(tok);
tok_cachedFunctions(tok);
tok_simplyCachedFunctions(tok);
tok_timedCachedFunctions(tok);
tok_optPar(tok);
throwFailEtc(tok);
tok_typeAA(tok, pairClasses);
// do this after expanding sclass etc.
tok = tok_expandStarConstructors(tok);
tok_kiloConstants(tok);
//tok_colonMessages(tok);
while ((i = jfind(tok, "shit:")) >= 0) {
int j = tok_findEndOfStatement(tok, i);
tok.set(i+2, "(");
tok.set(j-1, ");");
reTok(tok, i, j);
}
jreplace(tok, "shit(", "ret with print(");
tok_virtualTypes(tok);
tok_autoLongConstants(tok);
// common misordering of type arguments
jreplace(tok, "boolean ", " boolean");
tok_unimplementedMethods(tok);
tok_switchableFields(tok);
tok_autoDisposeFields(tok);
tok_shortVisualize(tok);
tok_whileGreaterThan(tok);
tok_ifThenEnd(tok);
tok_autoInitVars(tok);
tok_fixBadTypeParameterOrder(tok);
// shortened method declarations BEFORE standardFunctions
jreplace(tok, "svoid", "static void");
jreplace(tok, "void {", "$1 $2() {");
jreplace(tok, "void thread {", "$1 $2() thread {");
jreplace(tok, "String {", "$1 $2() {");
jreplace(tok, "Object {", "$1 $2() {");
jreplace(tok, "List {", "$1 $2() {");
namedThreads(tok);
threads(tok);
//tok_maxEquals(tok);
tok_questionDot(tok);
tok_embeddedFunctions(tok);
jreplace(tok, "[] = new[", "$1[] $4 = new $1[");
jreplace(tok, " ifNull =", "if ($1 == null) $1 =");
jreplace(tok, "class extends <.>", "class $2 extends $4<$2.$7>");
tok_once(tok); // must come before next line so you can combine them
tok_ifRecordMatch(tok);
jreplace(tok, " ||=", "$1 = $1 ||");
// magicValue followed by a numerical constant
jreplace(tok, "magicValue", "", (_tok, _i) -> {
String t = _get(_tok, _i+3);
return eqOneOf(t, ".", "-") || isInteger(t);
});
lambdaReferences(tok);
tok_returnSelf(tok);
// Lua-like print statement
jreplace(tok, "print ", "print($2);");
tok_tildeCalls(tok);
tok_swappableFunctions(tok);
tok_optParLambda(tok);
tok_runnableClasses(tok);
tok_swapStatement(tok);
tok_defaultArguments(tok);
tok_persistableClasses(tok);
tok_transientClasses(tok);
// end of local stuff
tok_processMetaBlocks(tok, metaCodeAllowed());
if (metaCodeAllowed()) runMetaTransformers(tok);
same = eq(tok, before);
/*if (!same)
print("local not same " + safety + " (" + l(tok) + " tokens)");*/
if (safety++ >= 10) {
printSources(tok);
throw fail("safety 10 error!");
}
} while (!same);
return tok;
}
static List reTok_include(List tok, int i, int j) {
return reTok_modify(tok, i, j, "localStuff1");
}
static List includeInMainLoaded_reTok(List tok, int i, int j) {
return reTok_include(tok, i, j);
}
static List stdstuff(List tok) {
//if (++level >= 10) fail("woot? 10");
print("stdstuff!");
int i;
List ts = new ArrayList();
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);
//print('starConstructors); tok = tok_expandStarConstructors(tok);
// drop Java 8 annotations since we're compiling for Java 7
jreplace(tok, "@Nullable", "");
// STANDARD CLASSES & INTERFACES
print("standard classes");
final Set haveClasses = addStandardClasses_v2(tok);
tok_quickInstanceOf(tok, haveClasses);
// concept-related stuff
// auto-import concepts
boolean _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));
throw fail("Unwanted concepts import");
}
printStruct(haveClasses);
tok = includeInMainLoaded(tok, "concepts.");
reTok(tok, l(tok)-1, l(tok));
//processConceptsDot(tok);
}
return tok;
} // end of stdStuff!
static List multilineStrings(List tok) {
for (int i = 1; i < tok.size(); i += 2) {
String t = tok.get(i);
if (isQuoted(t))
if (t.startsWith("[") || t.contains("\r") || t.contains("\n"))
tok.set(i, quote(unquote(t)));
}
return tok;
}
static List quickmain(List tok) {
if (quickmainDone1 && quickmainDone2) return 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_reTok(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)-1;
List contents = subList(tok, idx+1, j);
//print("contents: " + sfu(contents));
tok.set(i, "public static void main(final String[] args) throws Exception");
replaceTokens(tok, idx+1, j, tok_addSemicolon(contents));
reTok(tok, i, j);
quickmainDone2 = true;
}
return tok;
}
static String makeVar(String name) {
AtomicInteger counter = varCountByThread.get();
if (counter == null)
varCountByThread.set(counter = new AtomicInteger());
return "_" + name + "_" + getAndInc(counter);
}
static String makeVar() { return makeVar(""); }
static List rtq(List tok, String id) {
return runTranslatorQuick(tok, id);
}
static List expandShortTypes(List 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);
}
jreplace(tok, "O", "Object");
jreplace(tok, "S", "String");
jreplace(tok, "L", "List");
jreplace(tok, "Cl", "Collection");
jreplace(tok, "ret", "return", new TokCondition() { boolean get(final List tok, final int i) {
return neqGetOneOf(tok, i+3, "=", ")", ".")
&& neqGetOneOf(tok, i-1, "int", "return");
}});
// bool -> boolean if it's not a function name
jreplace(tok, "bool", "boolean", new TokCondition() { boolean get(final List tok, final int i) {
return neqGetOneOf(tok, i+3, "(", null);
}});
jreplace(tok, "AtomicBool", "AtomicBoolean");
jreplace(tok, "AtomicInt", "AtomicInteger");
jreplace(tok, "LL< >", "L>");
jreplace(tok, "LL>", "L>");
jreplace(tok, "LL", "L");
jreplace(tok, "Clusters< >", "Map<$3, Collection<$3>>");
return tok;
}
static List autoImports(List tok) {
HashSet imports = new HashSet(tok_findImports(tok));
StringBuilder buf = new StringBuilder();
for (String c : standardImports)
if (!(imports.contains(c)))
buf.append("import " + c + ";\n");
if (buf.length() == 0) return tok;
tok.set(0, buf+tok.get(0));
return 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 List tok_processIncludes(List tok) {
int safety = 0;
while (hasCodeTokens(tok, "!", "include") && ++safety < 100)
tok = tok_processIncludesSingle(tok);
//tok_autoCloseBrackets(tok);
return tok;
}
static void tok_processEarlyIncludes(List tok) {
int i;
while ((i = jfind_check("include",tok, "!include early #")) >= 0) {
String id = tok.get(i+8);
included.add(parseLong(id));
replaceTokens_reTok(tok, i, i+10, "\n" + cacheGet(id) + "\n");
}
}
static List tok_processIncludesSingle(List tok) {
int i;
while ((i = jfind_check("include",tok, "!include #")) >= 0) {
String 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_check("include",tok, "!include once #")) >= 0) {
String id = tok.get(i+8);
boolean isNew = included.add(parseLong(id));
replaceTokens(tok, i, i+10,
isNew ? "\n" + cacheGet(id) + "\n" : "");
reTok_include(tok, i, i+10);
}
return tok;
}
static void ctex(List tok) {
replaceKeywordBlock(tok, "ctex",
"{ try {",
"} catch (Exception __e) { throw rethrow(__e); } }");
for (String keyword : ll("null on exception", "null on error"))
replaceKeywordBlock(tok, keyword,
"{ try {",
"} catch (Throwable __e) { return null; } }");
replaceKeywordBlock(tok, "false on exception",
"{ try {",
"} catch (Throwable __e) { return false; } }");
}
static List dialogHandler(List tok) {
return replaceKeywordBlock(tok,
"dialogHandler",
"new DialogHandler() {\n" +
"public void run(final DialogIO io) {",
"}}");
}
static List extendClasses(List tok) {
int i;
while ((i = jfind(tok, "extend {")) >= 0) {
String className = tok.get(i+2);
int idx = findCodeTokens(tok, i, false, "{");
int j = findEndOfBracketPart(tok, idx+2);
String content = joinSubList(tok, idx+1, j-1);
List 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);
}
return tok;
}
// for ping / while ping
static void forPing(List tok) {
int i;
for (String keyword : ll("for", "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(List tok) {
int i;
while ((i = jfind(tok, "lib ")) >= 0) {
String id = tok.get(i+2);
print("lib " + id);
if (!libs.contains(id)) {
libs.add(id);
clearAllTokens(tok, i, i+3);
/*tok.set(i, "!");
tok.set(i+1, "");*/
} else {
print("...ignoring (duplicate)");
clearAllTokens(tok, i, i+3);
reTok(tok, i, i+3);
}
}
print("libs found: " + libs);
}
// sourceCodeLine() => 1234
static void sourceCodeLine(List tok) {
int i ;
while ((i = jfind(tok, "sourceCodeLine()")) >= 0) {
replaceTokens(tok, i, i+5, str(countChar(joinSubList(tok, 0, i), '\n')+1));
reTok(tok, i, i+5);
}
}
// done before any other processing
static void earlyStuff(List tok) {
int i;
tok_scopes(tok, "autoCloseScopes" , asInclude);
tok_autosemi(tok);
tok_autoCloseBrackets(tok);
jreplace(tok, "°", "()");
// Note: this makes the word "quine" a special operator
// (unusable as a function name)
while ((i = jfind(tok, "quine(")) >= 0) {
int idx = findCodeTokens(tok, i, false, "(");
int j = findEndOfBracketPart(tok, idx+2);
tok.set(i, "new Quine");
tok.set(idx, "(" + quote(joinSubList(tok, idx+1, j-1)) + ", ");
reTok(tok, i, idx+1);
}
jreplace_check("after",tok, "void after super {", "void $2 { super.$2();");
// do this before func & voidfunc because otherwise they swallow it
jreplace(tok, "enter {", "{ temp enter();");
// func keyword for lambdas - now automatically quines toString() if enabled
// do func & voidfunc early to preserve original code as toString
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);
List contents = subList(tok, idx+1, j-1);
String returnType = "O";
if (eq(tok.get(argsTo+2), "-") && eq(tok.get(argsTo+4), ">"))
returnType = tok_toNonPrimitiveTypes(joinSubList(tok, argsTo+6, idx-1));
String toString = autoQuine ? " public S toString() { ret " + quote(shorten(defaultMaxQuineLength(), trimJoin(contents))) + "; }" : "";
List args = cloneSubList(tok, argsFrom-1, argsTo);
tok_shortFinals(args);
tok_toNonPrimitiveTypes(args);
List types = tok_typesOfParams(args);
Object type = "O";
if (l(types) <= 3)
type = "F" + l(types) + "<" + joinWithComma(types) + ", " + returnType + ">";
String body = tok_addReturn(contents);
replaceTokens_reTok(tok, i, j,
"new " + type + "() { public "
+ 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);
List contents = subList(tok, idx+1, j-1);
if (jcontains(subList(tok, argsTo+1, idx), "->"))
throw fail("voidfunc with return type: " + joinSubList(tok, i, j+1));
List args = cloneSubList(tok, argsFrom-1, argsTo);
tok_shortFinals(args);
tok_toNonPrimitiveTypes(args);
List types = tok_typesOfParams(args);
Object type = "O";
if (l(types) <= 4)
type = "VF" + l(types) + "<" + joinWithComma(types) + ">";
replaceTokens(tok, i, j, "new " + type + "() { public void get(" + trimJoin(args) + ") ctex { " + tok_addSemicolon(contents) + " }\n" +
(autoQuine ? " public S toString() { ret " + quote(shorten(defaultMaxQuineLength(), trim(join(contents)))) + "; }" : "") + "}");
reTok(tok, i, j);
}
jreplace(tok, "func {", "func -> O {");
jreplace(tok, "f {", "f -> O {");
// swing -> S { ... } => swing(func -> S { ... })
while ((i = jfind(tok, "swing ->")) >= 0) {
int bracket = findCodeTokens(tok, i, false, "{");
int j = findEndOfBracketPart(tok, bracket);
tok.set(i, "swing(func");
tok.set(j-1, "})");
reTok(tok, i, j);
}
tok_qFunctions(tok);
jreplace(tok, "func fullToString {", "func -> O fullToString {");
for (String keyword : ll(/*"f",*/ "func")) {
while ((i = jfind(tok, keyword + " ->", new TokCondition() { boolean get(final List tok, final int i) {
return isIdentifier(_get(tok, i+7)); // avoid lambda declaration like: f -> { ... }
}})) >= 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);
String returnType = tok_toNonPrimitiveTypes(joinSubList(tok, i+6, idx-1));
int quineLength = defaultMaxQuineLength();
if (eq(lastJavaToken(returnType), "fullToString")) {
quineLength = Integer.MAX_VALUE;
returnType = dropLastJavaTokenAndSpacing(returnType);
}
List