Download Jar. Uses 2039K of libraries. Click here for Pure Java version (18571L/138K).
1 | !1006722 |
2 | //!7 |
3 | |
4 | !include once #1025802 // optimized findCodeTokens |
5 | |
6 | set flag AllowMetaCode. |
7 | |
8 | //set flag tok_forUnnull_debug. |
9 | |
10 | // ifdef in cached includes? => i think cached includes still contain the ifdefs, so it's fine. tok_ifdef is not called within localStuff1 |
11 | |
12 | set flag noAI. |
13 | set flag isQuoted_dirty. // should be OK, we're dealing with tokens |
14 | |
15 | static bool autoQuine = true; |
16 | static int maxQuineLength = 80; |
17 | sbool assumeTriple = true; |
18 | sbool quickInstanceOfEnabled; // interferes with things |
19 | |
20 | // _registerThread usually costs nothing because we need |
21 | // the registerWeakHashMap mechanism anyway for ping(). |
22 | // Anyway - no forced functions for now :) |
23 | static L<S> functionsToAlwaysInclude = ll( |
24 | //"_registerThread", |
25 | //"asRuntimeException" |
26 | ); |
27 | |
28 | // classes with two type parameters that can written with just one |
29 | // e.g. Pair<S> => Pair<S, S> |
30 | static Set<S> pairClasses = lithashset("Pair", "Either", "Map", "AbstractMap", "HashMap", "TreeMap", "LinkedHashMap", "MultiMap", "CompactHashMap", "WrappedMap", "F1", "IF1", "AllOnAll", "AllOnAllWithUpdates"); |
31 | |
32 | sS transpilingSnippetID; |
33 | //static new AtomicInteger varCount; |
34 | static new ThreadLocal<AtomicInteger> varCountByThread; |
35 | static new Map<S, S> snippetCache; |
36 | sbool useIndexedList2 = false, useTokenIndexedList = true; |
37 | sbool opt_javaTok = true; |
38 | static bool cacheStdFunctions = true, cacheStdClasses = true; |
39 | static new HashMap<Long, CachedInclude> cachedIncludes; |
40 | static ExecutorService executor; |
41 | static L lclasses; |
42 | static long startTime, lastPrint; |
43 | |
44 | // These variables have to be cleared manually for each transpilation |
45 | |
46 | static new HashSet<Long> included; |
47 | static Set<S> definitions = ciSet(); |
48 | static new HashMap<S> rewrites; |
49 | static new HashSet<S> shouldNotIncludeFunction; |
50 | static new HashSet<S> shouldNotIncludeClass; |
51 | static new HashSet<S> doNotIncludeFunction; |
52 | static new HashSet<S> addedFunctions; |
53 | static new HashSet<S> addedClasses; |
54 | static new HashSet<S> hardFunctionReferences; |
55 | static new HashSet<S> mapLikeFunctions; |
56 | static new HashSet<S> mapMethodLikeFunctions; |
57 | static new HashSet<S> nuLikeFunctions; |
58 | static SS extraStandardFunctions; |
59 | sbool quickmainDone1, quickmainDone2; |
60 | static new TreeSet<S> libs; |
61 | sS mainBaseClass, mainPackage, mainClassName; |
62 | sbool localStuffOnly; // for transpiling a fragment |
63 | sbool asInclude; // for transpiling an include (auto-close scopes) |
64 | sbool allowMetaCode = false; // run any embedded meta code |
65 | static LS metaPostBlocks, metaTransformers; |
66 | sbool dontPrintSource; |
67 | sbool dontLoadCachedIncludesFromVM; // for benchmarking |
68 | |
69 | sclass CachedInclude { |
70 | S javax; |
71 | Future<S> java; |
72 | S realJava; |
73 | |
74 | S java() { |
75 | ret realJava != null ? realJava : getFuture(java); |
76 | } |
77 | |
78 | Future<S> javaFuture() { |
79 | ret realJava != null ? nowFuture(realJava) : java; |
80 | } |
81 | |
82 | void clean { |
83 | if (java != null) { |
84 | realJava = getFuture(java); |
85 | java = null; |
86 | } |
87 | } |
88 | } |
89 | |
90 | p { |
91 | startTime = lastPrint = sysNow(); |
92 | pcall { |
93 | if (!dontLoadCachedIncludesFromVM) |
94 | vmKeepWithProgramMD5_get('cachedIncludes); |
95 | } |
96 | executor = Executors.newFixedThreadPool(numberOfCores()); |
97 | transpilingSnippetID = or(getThreadLocal((ThreadLocal<S>) getOpt(javax(), 'transpilingSnippetID)), transpilingSnippetID); |
98 | print(+transpilingSnippetID); |
99 | set transpileRaw_dontCopyFromCreator; |
100 | |
101 | fO oldPrint = or(print_byThread()!, f print_raw); |
102 | temp tempInterceptPrint(new F1<S, Bool>() { |
103 | Bool get(S s) { |
104 | long now = sysNow(); |
105 | long time = now-lastPrint; // -startTime; |
106 | lastPrint = now; |
107 | callF(oldPrint, "[" + formatInt(time/1000, 2) + ":" + formatInt(time % 1000, 3) + "] " + s); |
108 | false; |
109 | } |
110 | }); |
111 | |
112 | try { |
113 | _main(); |
114 | } finally { |
115 | interceptPrintInThisThread(oldPrint); |
116 | if (executor != null) executor.shutdown(); |
117 | executor = null; |
118 | localStuffOnly = false; |
119 | asInclude = false; |
120 | } |
121 | } |
122 | |
123 | svoid _main() ctex { |
124 | if (sameSnippetID(programID(), defaultJavaXTranslatorID())) setDefaultJavaXTranslatorID(#7); |
125 | |
126 | //reTok_modify_check = true; |
127 | //if (useIndexedList) findCodeTokens_debug = true; |
128 | javaTok_opt = opt_javaTok; |
129 | //findCodeTokens_indexed = findCodeTokens_unindexed = 0; |
130 | findCodeTokens_bails = findCodeTokens_nonbails = 0; |
131 | javaTok_n = javaTok_elements = 0; |
132 | S in = loadMainJava(); |
133 | |
134 | print("759 STARTING " + identityHashCode(main.class)); |
135 | included.clear(); |
136 | definitions.clear(); |
137 | rewrites.clear(); |
138 | definitions.add("SymbolAsString"); |
139 | shouldNotIncludeFunction.clear(); |
140 | shouldNotIncludeClass.clear(); |
141 | doNotIncludeFunction.clear(); |
142 | addedFunctions.clear(); |
143 | addedClasses.clear(); |
144 | hardFunctionReferences.clear(); |
145 | mapLikeFunctions = cloneHashSet(tok_mapLikeFunctions()); |
146 | mapMethodLikeFunctions = cloneHashSet(tok_mapMethodLikeFunctions()); |
147 | nuLikeFunctions.clear(); |
148 | extraStandardFunctions = new HashMap; |
149 | libs.clear(); |
150 | mainBaseClass = mainPackage = mainClassName = null; |
151 | varCountByThread.set(null); |
152 | quickmainDone1 = quickmainDone2 = false; |
153 | metaPostBlocks = new L; |
154 | metaTransformers = new L; |
155 | dontPrintSource = false; |
156 | |
157 | //L ts = findTranslators(toLines(join(tok))); |
158 | //print("Translators in source at start: " + structure(ts)); |
159 | |
160 | L<S> tok = jtok(in); |
161 | |
162 | try { |
163 | tok_definitions(tok); |
164 | |
165 | // add m { } |
166 | |
167 | if (!localStuffOnly && !hasCodeTokens(tok, "m", "{") && !hasCodeTokens(tok, "main", "{") && !hasCodeTokens(tok, "class", "main")) { |
168 | //tok = jtok(moveImportsUp("m {\n" + in + "\n}")); |
169 | if (l(tok) == 1) tok = singlePlusList(first(tok), dropFirst(javaTok("m {}"))); |
170 | else { |
171 | replaceTokens_reTok(tok, 1, 2, "m {\n\n" + unnull(get(tok, 1))); |
172 | replaceTokens_reTok(tok, l(tok)-2, l(tok)-1, unnull(get(tok, l(tok)-2)) + "}"); |
173 | } |
174 | tok_moveImportsUp(tok); |
175 | } |
176 | |
177 | // standard translate |
178 | |
179 | //ts = findTranslators(toLines(join(tok))); |
180 | //print("Translators in source: " + structure(ts)); |
181 | |
182 | if (tok_hasTranslators(tok)) |
183 | tok = jtok(defaultTranslate(join(tok))); |
184 | |
185 | //print("end of default translate"); |
186 | //print(join(tok)); |
187 | |
188 | //tok_autoCloseBrackets(tok); |
189 | |
190 | tok_metaTransformNow(tok); |
191 | |
192 | tok_processEarlyIncludes(tok); |
193 | |
194 | tok_earlyGeneralStuff(tok); |
195 | |
196 | tok = tok_processIncludes(tok); // before standard functions |
197 | if (processConceptsDot(tok)) |
198 | tok = tok_processIncludes(tok); |
199 | tok = localStuff1(tok); |
200 | |
201 | if (!localStuffOnly) { |
202 | int safety = 0; |
203 | boolean same; |
204 | do { // BIG LOOP |
205 | S before = join(tok); |
206 | |
207 | // do the non-local stuff (flags and rewrites, things that correlate across includes like tok_selfType) |
208 | tok_selfType(tok); |
209 | tok_mainClassNameAndPackage(tok); |
210 | tok_definitions(tok); |
211 | tok_ifndef(tok); |
212 | tok_ifdef(tok); |
213 | defineMapLikes(tok); |
214 | if (tok_applyMapLikeFunctions(tok, mapLikeFunctions)) |
215 | functionReferences(tok); |
216 | defineMapMethodLikes(tok); |
217 | tok_applyMapMethodLikeFunctions(tok, mapMethodLikeFunctions); |
218 | defineNuLikes(tok); |
219 | tok_applyNuLikeFunctions(tok, nuLikeFunctions); |
220 | tok_dropExtraCommas(tok); // from e.g. tok_applyMapMethodLikeFunctions |
221 | tok_delegateTo(tok); |
222 | tok_replaceWith(tok); |
223 | tok_findRewrites(tok); |
224 | tok_processRewrites(tok); |
225 | |
226 | // main bla(...) => mainClassName.bla(...) |
227 | jreplace(tok, "main <id>(", or(mainClassName, "main") + ".$2("); |
228 | |
229 | try { |
230 | if (safety == 0) tok = quickmain(tok); |
231 | } catch e { |
232 | printSources(tok); |
233 | rethrow(e); |
234 | } |
235 | tok_collectMetaPostBlocks(tok, metaPostBlocks); |
236 | tok_collectTransformers(tok, metaTransformers); |
237 | tok_metaTransformNow(tok); |
238 | |
239 | // Hack to allow DynModule to reimplement _registerThread |
240 | /*if (tok.contains("DynModule") && !addedClasses.contains("DynModule")) |
241 | addStandardClasses_v2(tok);*/ |
242 | |
243 | defineExtraSF(tok); |
244 | tok = standardFunctions(tok); |
245 | tok = stdstuff(tok); // all the keywords, standard |
246 | S diff; |
247 | long startTime = now(); |
248 | //diff = unidiff(before, join(tok)); |
249 | //print("unidiff: " + (now()-startTime) + " ms"); |
250 | //same = eq(diff, ""); |
251 | same = tok_sameTest(tok, before); |
252 | if (!same) { |
253 | print("Not same " + safety + "."); |
254 | //print(indent(2, diff)); |
255 | } |
256 | if (safety++ >= 10) { |
257 | //print(unidiff(before, join(tok))); |
258 | printSources(tok); |
259 | fail("safety 10 error!"); |
260 | } |
261 | } while (!same); // END OF BIG LOOP |
262 | |
263 | print("Post."); |
264 | |
265 | if (mainBaseClass != null) { |
266 | jreplace1(tok, "class main", "class main extends " + mainBaseClass); |
267 | mainBaseClass = null; |
268 | } |
269 | |
270 | print('moveImportsUp); tok_moveImportsUp(tok); |
271 | |
272 | print('Indexing); tok = indexTokenList(tok); |
273 | |
274 | // POST-PROCESSING after stdstuff loop |
275 | |
276 | if (transpilingSnippetID != null) |
277 | jreplace_dyn(tok, "class whatever", |
278 | func(L<S> tok, int cIndex) -> S { |
279 | pcall { ret "class " + stringToLegalIdentifier(getSnippetTitle(transpilingSnippetID)); } |
280 | ret "class Whatever"; |
281 | }); |
282 | |
283 | //print("Type<A, A>"); // Type<A> to Type<A, A> |
284 | |
285 | print('extendClasses); tok = extendClasses(tok); |
286 | print('libs); libs(tok); |
287 | print('sourceCodeLine); sourceCodeLine(tok); |
288 | |
289 | tok_overridableFunctionDefs(tok, null); |
290 | |
291 | // escaping for lambdas |
292 | //jreplace(tok, "-=>", "->"); |
293 | |
294 | // Stuff that depends on the list of inner classes (haveClasses) |
295 | HashSet<S> haveClasses = haveClasses_actual(tok); |
296 | print('innerClassesVar); innerClassesVar(tok, haveClasses); |
297 | fillVar_transpilationDate(tok); |
298 | haveClasses_addImported(tok, haveClasses); |
299 | print('ifclass); tok_ifclass(tok, haveClasses); |
300 | print('slashCasts); slashCasts(tok, haveClasses); |
301 | print('newWithoutNew); newWithoutNew(tok, haveClasses); |
302 | |
303 | if (!assumeTriple) { |
304 | print('Triple); |
305 | if (tok.contains("Triple") && !haveClasses.contains("Triple")) { |
306 | jreplace(tok, "Triple", "T3"); |
307 | haveClasses.remove("Triple"); |
308 | haveClasses.add("T3"); |
309 | slashCasts(tok, lithashset("T3")); |
310 | tok_quickInstanceOf(tok, lithashset("T3")); |
311 | } |
312 | expandTriple(tok); |
313 | } |
314 | |
315 | if (hasDef("SymbolAsString")) |
316 | jreplace(tok, "Symbol", "String"); |
317 | |
318 | print('classReferences); expandClassReferences_lazy(tok, haveClasses); |
319 | |
320 | if (metaCodeAllowed()) runMetaPostBlocks(tok); |
321 | |
322 | // Error-checking |
323 | print("Error-checking"); |
324 | Set<S> functions = new HashSet(findFunctions(tok)); |
325 | for (S f : hardFunctionReferences) |
326 | if (!functions.contains(f)) |
327 | fail("Function " + f + " requested, but not supplied"); |
328 | |
329 | print('autoImports); tok = autoImports(tok); // faster to do it at the end |
330 | |
331 | print("definitions=" + sfu(definitions)); |
332 | if (containsOneOfIC(definitions, "allpublic", "reparse", "PublicExceptTopClass")) { |
333 | // Fire up the Java parser & pretty printer! |
334 | print(containsIC(definitions, "allpublic")? "Making all public." : "Reparsing."); |
335 | //try { |
336 | S src = join(tok); |
337 | try { |
338 | if (containsIC(definitions, "PublicExceptTopClass")) |
339 | src = javaParser_makeAllPublic(src, notTopLevelClassDecl := true); |
340 | else if (containsIC(definitions, "keepComments")) |
341 | src = javaParser_makeAllPublic_keepComments(src); |
342 | else if (containsIC(definitions, "allpublic")) |
343 | src = javaParser_makeAllPublic(src); |
344 | else |
345 | src = javaParser_reparse_keepComments(src); |
346 | } catch e { |
347 | extractAndPrintJavaParseError(src, e); |
348 | File f = javaxCachesDir("error-source.java"); |
349 | saveTextFileVerbose(f, src); |
350 | dontPrintSource = true; |
351 | throw e; |
352 | } |
353 | tok = jtok(src); |
354 | /*} catch e { |
355 | S src = join(tok); |
356 | if (!dontPrintSource) |
357 | print(src); |
358 | print(f2s(saveProgramTextFile("error.java", src))); |
359 | throw rethrow(e); |
360 | }*/ |
361 | } |
362 | |
363 | // Do this after JavaParser (because it doesn't like package after class) |
364 | |
365 | if (mainPackage != null) { |
366 | print('mainPackage); |
367 | tokPrepend(tok, 1, "package " + mainPackage + ";\n"); |
368 | reTok(tok, 1, 2); |
369 | } |
370 | |
371 | if (mainClassName != null) { |
372 | print('mainClassName); |
373 | jreplace(tok, "class main", "class " + mainClassName); |
374 | jreplace(tok, "main.class", mainClassName + ".class"); |
375 | //tokPrepend(tok, 1, "class main {}\n"); // so main.class is generated and compiler sanity checks succeed. we can later skip it in the JavaXClassLoader |
376 | } |
377 | |
378 | if (nempty(libs)) { |
379 | print("Adding libs: " + libs); |
380 | tok.add(concatMap_strings(func(S s) -> S { "\n!" + s }, libs)); |
381 | } |
382 | } // if (!localStuffOnly) |
383 | } catch e { |
384 | S src = join(tok); |
385 | if (!dontPrintSource) |
386 | print(src); |
387 | print(f2s(saveProgramTextFile("error.java", src))); |
388 | throw rethrow(e); |
389 | } |
390 | |
391 | /*if (useIndexedList) |
392 | print("Indexed/unindexed lookups: " + findCodeTokens_indexed + "/" + findCodeTokens_unindexed + ", lists made: " + IndexedList2.instances); |
393 | print("findCodeToken bails: " + findCodeTokens_bails + "/" + findCodeTokens_nonbails); |
394 | print("javaToks: " + javaTok_n + "/" + javaTok_elements);*/ |
395 | |
396 | print("Saving."); |
397 | |
398 | // for dexcompile.php |
399 | if (mainClassName != null) |
400 | tokPrepend(tok, 0, "//FILENAME: " |
401 | + (mainPackage != null ? mainPackage.replace(".", "/") + "/" : "") + mainClassName + ".java\n"); |
402 | |
403 | if (mainJava != null) |
404 | mainJava = join(tok); |
405 | else if (tok.contains("package")) |
406 | splitJavaFiles(tok); |
407 | else |
408 | saveMainJava(tok); |
409 | } |
410 | |
411 | static L<S> localStuff1(L<S> tok) { |
412 | int safety = 0, i; |
413 | boolean same; |
414 | tok = indexTokenList(tok); |
415 | |
416 | tok_scopes(tok, autoCloseScopes := true); |
417 | |
418 | do { |
419 | S before = join(tok); |
420 | |
421 | //print("localStuff loop " + safety); |
422 | |
423 | earlyStuff(tok); |
424 | |
425 | // EARLY local stuff goes here |
426 | |
427 | tok_earlyGeneralStuff(tok); |
428 | |
429 | conceptDeclarations(tok); |
430 | tok_recordDecls(tok); |
431 | |
432 | tok = multilineStrings(tok); |
433 | tok_singleQuoteIdentifiersToStringConstants(tok); |
434 | inStringEvals(tok); |
435 | tok_listComprehensions(tok); |
436 | |
437 | tok_for_single(tok); |
438 | |
439 | tok_for_unpair(tok); // Do this... |
440 | tok_doubleFor_v2(tok); // ...before this |
441 | |
442 | tok_forUnnull(tok); |
443 | tok_ifCast(tok); |
444 | forPing(tok); |
445 | tok_directSnippetRefs(tok); |
446 | quicknu(tok); |
447 | //tok_juxtaposeCalls(tok); |
448 | |
449 | jreplace(tok, "LLS", "L<LS>"); |
450 | jreplace(tok, "LS", "L<S>"); |
451 | jreplace(tok, "ES", "Ext<S>"); |
452 | jreplace(tok, "ExtS", "Ext<S>"); |
453 | |
454 | jreplace(tok, "dispose <id>;", "{ cleanUp($2); $2 = null; }"); |
455 | |
456 | jreplace(tok, "do ping {", "do { ping();"); |
457 | |
458 | replaceKeywordBlock(tok, |
459 | "swing", |
460 | "{ swing(r {", |
461 | "}); }"); |
462 | |
463 | replaceKeywordBlock(tok, |
464 | "androidUI", |
465 | "{ androidUI(r {", |
466 | "}); }"); |
467 | |
468 | replaceKeywordBlock(tok, |
469 | "withDBLock", |
470 | "{ withDBLock(r {", |
471 | "}); }"); |
472 | |
473 | replaceKeywordBlock(tok, "afterwards", "temp tempAfterwards(r {", "});"); |
474 | |
475 | for (S keyword : ll("tokcondition", "tokCondition")) |
476 | replaceKeywordBlock(tok, |
477 | keyword, |
478 | "new TokCondition { bool get(final L<S> tok, final int i) {", |
479 | "}}"); |
480 | |
481 | jreplace(tok, "synced <id>", "synchronized $2"); |
482 | jreplace(tok, "sync <id>", "synchronized $2"); |
483 | |
484 | replaceKeywordBlock(tok, "answer", |
485 | "static S answer(S s) {\nfinal new Matches m;\n", |
486 | "\nret null;\n}"); |
487 | |
488 | replaceKeywordBlock(tok, "static-pcall", |
489 | "static { pcall {", |
490 | "}}"); |
491 | |
492 | replaceKeywordBlock(tok, "loading", |
493 | "{ temp tempShowLoadingAnimation(); ", |
494 | "}"); |
495 | |
496 | replaceKeywordPlusQuotedBlock(tok, "loading", |
497 | new O { S[] get(L<S> tok, int i) { |
498 | S text = tok.get(i+2); |
499 | ret new S[] { |
500 | "{ temp tempShowLoadingAnimation(" + text + "); ", |
501 | "}" }; |
502 | }}); |
503 | |
504 | while ((i = jfind(tok, "visualize as")) >= 0) { |
505 | int j = tok_findEndOfStatement(tok, i); // expression, rather |
506 | tok.set(i+2, "{ ret"); |
507 | tok.set(j-1, "; }"); |
508 | reTok(tok, i, j); |
509 | } |
510 | |
511 | jreplace(tok, "visualize {", "public JComponent visualize() {", tokCondition_beginningOfMethodDeclaration()); |
512 | |
513 | jreplace(tok, "visualize2 {", "JComponent visualize2() {", tokCondition_beginningOfMethodDeclaration()); |
514 | |
515 | replaceKeywordBlock(tok, "start-thread-printDone", "start-thread {", "printDone(); }"); |
516 | |
517 | replaceKeywordBlock(tok, "start-thread", [[start { thread "Start" { temp enter(); pcall {]], "}}}"); |
518 | |
519 | jreplace(tok, "start {", "void start() { super.start();", tokCondition_beginningOfMethodDeclaration()); |
520 | |
521 | // run { ... } => public void run() { ... } |
522 | jreplace(tok, "run {", "public void run() {", tokcondition { |
523 | ret neqGet(tok, i-1, "void"); |
524 | }); |
525 | |
526 | replaceKeywordBlock(tok, "html", |
527 | "static O html(S uri, fMap<S, S> params) ctex " + "{\n", "}"); |
528 | |
529 | replaceKeywordBlock(tok, "afterVisualize", |
530 | "visualize { JComponent _c = super.visualize();", |
531 | "ret _c; }"); |
532 | |
533 | replaceKeywordBlock(tok, "enhanceFrame", |
534 | "void enhanceFrame(Container f) { super.enhanceFrame(f);", |
535 | "}"); |
536 | |
537 | if (assumeTriple) { |
538 | jreplace(tok, "Triple", "T3"); |
539 | expandTriple(tok); |
540 | } |
541 | |
542 | tok_shortFinals(tok); |
543 | |
544 | tok_moduleClassDecls(tok); |
545 | |
546 | jreplace(tok, "static sync", "static synchronized"); |
547 | jreplace(tok, "sclass", "static class"); |
548 | jreplace(tok, "srecord", "static record"); |
549 | jreplace(tok, "record noeq", "noeq record"); |
550 | jreplace(tok, "asclass", "abstract static class"); |
551 | jreplace(tok, "sinterface", "static interface"); |
552 | jreplace(tok, "ssynchronized", "static synchronized"); |
553 | |
554 | jreplace(tok, "ssvoid", "static synchronized void"); |
555 | jreplace(tok, "sbool", "static bool"); |
556 | jreplace(tok, "fbool", "final bool"); |
557 | jreplace(tok, "sint", "static int"); |
558 | jreplace(tok, "snew", "static new"); |
559 | jreplace(tok, "sv <id>", "static void $2"); |
560 | jreplace(tok, "pvoid", "public void"); |
561 | |
562 | // "sS" => static S |
563 | jreplace(tok, "sS", "static S"); |
564 | |
565 | // "sO" => static O |
566 | jreplace(tok, "sO", "static O"); |
567 | |
568 | // "sL" => static L |
569 | jreplace(tok, "sL", "static L"); |
570 | |
571 | // "toString {" => "public S toString() {" |
572 | jreplace(tok, "toString {", "public S toString() {"); |
573 | |
574 | jreplace(tok, "Int", "Integer"); |
575 | jreplace(tok, "Bool", "Boolean"); |
576 | jreplace(tok, "BigInt", "BigInteger"); |
577 | jreplace(tok, "Char", "Character"); |
578 | |
579 | jreplace(tok, "Sym", "Symbol"); |
580 | jreplace(tok, "SymSym", "SymbolSymbol"); |
581 | |
582 | jreplace(tok, "SS", "Map<S>"); |
583 | jreplace(tok, "SymbolSymbol", "Map<Symbol>"); |
584 | |
585 | jreplace(tok, "PairS", "Pair<S>"); |
586 | jreplace(tok, "LPairS", "L<Pair<S>>"); |
587 | |
588 | jreplace(tok, "T3S", "T3<S>"); |
589 | jreplace(tok, "F1S", "F1<S>"); |
590 | |
591 | jreplace(tok, "ItIt", "IterableIterator"); |
592 | jreplace(tok, "CloseableItIt", "CloseableIterableIterator"); |
593 | |
594 | jreplace(tok, "class <id> > <id> {", "class $2 extends $4 {"); |
595 | jreplace(tok, "class <id> > <id><<id>> {", "class $2 extends $4 $5 $6 $7 {"); |
596 | |
597 | jreplace(tok, "ISegmenter", "IF1<BufferedImage, L<Rect>>"); |
598 | |
599 | // IPred<A> => IF1<A, Bool> |
600 | jreplace(tok, "IPred<<id>>", "IF1<$3, Bool>"); |
601 | |
602 | // "on fail {" => "catch (Throwable _e) { ... rethrow(_e); }" |
603 | replaceKeywordBlock(tok, "on fail", |
604 | "catch (Throwable _e) {", |
605 | "\nthrow rethrow(_e); }"); |
606 | |
607 | // "catch {" => "catch (Throwable _e) {" |
608 | jreplace(tok, "catch {", "catch (Throwable _e) {"); |
609 | |
610 | // "catch print e {" => "catch e { _handleException(e); " |
611 | jreplace(tok, "catch print <id> {", "catch $3 { _handleException($3);"); |
612 | |
613 | // "catch print short e {" => "catch e { printExceptionShort(e); " |
614 | jreplace(tok, "catch print short <id> {", "catch $4 { printExceptionShort($4);"); |
615 | |
616 | // "catch X e {" => "catch (X e) {" |
617 | jreplace(tok, "catch <id> <id> {", "catch ($2 $3) {"); |
618 | |
619 | // "catch e {" => "catch (Throwable e) {" (if e is lowercase) |
620 | jreplace(tok, "catch <id> {", "catch (Throwable $2) {", tokcondition { |
621 | S word = tok.get(i+3); |
622 | ret startsWithLowerCaseOrUnderscore(word); |
623 | }); |
624 | |
625 | jreplace(tok, "+ +", "+", tokcondition { |
626 | //printStructure("++: ", subList(tok, i-1, i+6)); |
627 | if (empty(_get(tok, i+2))) ret false; // no space between the pluses |
628 | if (empty(_get(tok, i)) && eq("+", _get(tok, i-1))) ret false; // an actual "++" at the left |
629 | if (empty(_get(tok, i+4)) && eq("+", _get(tok, i+5))) ret false; // an actual "++" at the right |
630 | //print("doing it"); |
631 | ret true; |
632 | }); |
633 | |
634 | // some crazy fancy syntax |
635 | jreplace(tok, "set <id>;", "$2 = true;"); |
636 | |
637 | // single underscore (not allowed in Java anymore) to double underscore |
638 | jreplace(tok, "_", "__"); |
639 | |
640 | // [stdEq] -> implementation of equals() and hashCode() |
641 | jreplace(tok, "[stdEq]", |
642 | "public bool equals(O o) { ret stdEq2(this, o); }\n" + |
643 | "public int hashCode() { ret stdHash2(this); }"); |
644 | |
645 | // [concepts] "concept.field!" for dereferencing references |
646 | |
647 | jreplace(tok, "*!", "$1.get()", tokcondition { |
648 | S l = tok.get(i+1); |
649 | if (!(isIdentifier(l) || eq(l, ")"))) false; |
650 | if (tok.get(i+2).contains("\n")) false; // no line break between <id> and ! |
651 | if (nempty(tok.get(i+4))) true; // space after = ok |
652 | S t = _get(tok, i+5); |
653 | if (t == null) ret false; |
654 | if (isIdentifier(t) || eqOneOf(t, "=", "(")) false; |
655 | true; |
656 | }); |
657 | |
658 | jreplace(tok, "for (<id> <id>)", "for ($3 $4 : list($3))"); |
659 | jreplace(tok, "for (final <id> <id>)", "for (final $4 $5 : list($4))"); |
660 | |
661 | // "continue unless", "break unless" |
662 | |
663 | for (S phrase : ll("continue unless", "break unless")) |
664 | while ((i = jfind(tok, phrase)) >= 0) { |
665 | S keyword = tok.get(i); |
666 | int j = scanOverExpression(tok, getBracketMap(tok), i+4, ";"); |
667 | replaceTokens(tok, i, i+4, "{ if (!("); |
668 | tok.set(j, ")) " + keyword + "; }"); |
669 | reTok(tok, i, j+1); |
670 | } |
671 | |
672 | // "continue if", "break if" |
673 | |
674 | for (S phrase : ll("continue if", "break if")) |
675 | while ((i = jfind(tok, phrase)) >= 0) { |
676 | S keyword = tok.get(i); |
677 | int j = scanOverExpression(tok, getBracketMap(tok), i+4, ";"); |
678 | replaceTokens(tok, i, i+4, "{ if ("); |
679 | tok.set(j, ") " + keyword + "; }"); |
680 | reTok(tok, i, j+1); |
681 | } |
682 | |
683 | // "return if" |
684 | |
685 | while ((i = jfind(tok, "return if")) >= 0) { |
686 | int j = scanOverExpression(tok, getBracketMap(tok), i+4, ";"); |
687 | replaceTokens(tok, i, i+4, "{ if ("); |
688 | tok.set(j, ") return; }"); |
689 | reTok(tok, i, j+1); |
690 | } |
691 | |
692 | // "return unless" |
693 | |
694 | while ((i = jfind(tok, "return unless")) >= 0) { |
695 | int j = scanOverExpression(tok, getBracketMap(tok), i+4, ";"); |
696 | replaceTokens(tok, i, i+4, "{ if (!("); |
697 | tok.set(j, ")) return; }"); |
698 | reTok(tok, i, j+1); |
699 | } |
700 | |
701 | // "return <id> if" |
702 | |
703 | while ((i = jfind(tok, "return <id> if")) >= 0) { |
704 | int j = scanOverExpression(tok, getBracketMap(tok), i+4, ";"); |
705 | tok.set(j, ") return " + tok.get(i+2) + "; }"); |
706 | replaceTokens(tok, i, i+6, "{ if ("); |
707 | reTok(tok, i, j+1); |
708 | } |
709 | |
710 | // "return with <statement>" / "continue with <statement>" / "break with <statement>" |
711 | |
712 | while ((i = jfind(tok, "<id> with", tokcondition { ret eqOneOf(tok.get(i+1), "return", "continue", "break"); })) >= 0) { |
713 | int j = scanOverExpression(tok, getBracketMap(tok), i+4, ";"); |
714 | tok.set(j, "; " + tok.get(i) + "; }"); |
715 | replaceTokens(tok, i, i+3, "{"); |
716 | reTok(tok, i, j+1); |
717 | } |
718 | |
719 | // return "bla" with <statement> |
720 | |
721 | while ((i = jfindOneOf(tok, |
722 | "return <quoted> with", "return <id> with")) >= 0) { |
723 | S result = tok.get(i+2); |
724 | int j = scanOverExpression(tok, getBracketMap(tok), i+6, ";"); |
725 | replaceTokens(tok, i, i+5, "{"); |
726 | tok.set(j, "; return " + result + "; }"); |
727 | reTok(tok, i, j+1); |
728 | } |
729 | |
730 | tok_debugStatements(tok); |
731 | |
732 | // while not null (...) / if not null (...) |
733 | |
734 | while ((i = jfind_check not(tok, "<id> not null (", tokcondition { |
735 | ret eqOneOf(_get(tok, i+1), "if", "while"); |
736 | })) >= 0) { |
737 | int closingBracket = findEndOfBracketPart(tok, i+6)-1; |
738 | replaceTokens(tok, i+2, i+6, "("); |
739 | tok.set(closingBracket, ") != null)"); |
740 | reTok(tok, i, closingBracket+1); |
741 | } |
742 | |
743 | // while null (...) / if null (...) |
744 | |
745 | while ((i = jfind_check null(tok, "<id> null (", tokcondition { |
746 | ret eqOneOf(_get(tok, i+1), "if", "while"); |
747 | })) >= 0) { |
748 | int closingBracket = findEndOfBracketPart(tok, i+4)-1; |
749 | replaceTokens(tok, i+2, i+4, "("); |
750 | tok.set(closingBracket, ") == null)"); |
751 | reTok(tok, i, closingBracket+1); |
752 | } |
753 | |
754 | // Replace $1 with m.unq(0) etc. - caveat: this blocks identifiers $1, $2, ... |
755 | for (i = 1; i < l(tok); i += 2) { |
756 | S s = tok.get(i); |
757 | if (s.startsWith("$")) { |
758 | s = substring(s, 1); |
759 | if (isInteger(s)) { |
760 | tok.set(i, "m.unq(" + (parseInt(s)-1) + ")"); |
761 | reTok(tok, i); |
762 | } |
763 | } |
764 | } |
765 | |
766 | // instanceof trickery |
767 | |
768 | jreplace(tok, "is a <id>", "instanceof $3"); |
769 | jreplace(tok, "!<id> instanceof <id>.<id>", "!($2 instanceof $4.$6)"); |
770 | jreplace(tok, "!<id> instanceof <id>", "!($2 instanceof $4)"); |
771 | jreplace(tok, "<id> !instanceof <id>", "!($1 instanceof $4)"); |
772 | |
773 | // map func1 func2 func3(...) => mapFGH(f func1, f func2, f func3, ...) |
774 | jreplace(tok, "map <id> <id> <id>(", "mapFGH(f $2, f $3, f $4,"); |
775 | |
776 | // map func1 func2(...) => mapFG(f func1, f func2, ...) |
777 | jreplace(tok, "map <id> <id>(", "mapFG(f $2, f $3,"); |
778 | |
779 | // "ref->bla" for dereferencing Concept.Ref or ThreadLocal or other |
780 | // For lambdas, use SPACES on the left or right of the arrow! |
781 | //jreplace(tok, "<id> ->", "$1.get()."); |
782 | jreplace(tok, "->", ".get().", tokcondition { |
783 | ret empty(tok.get(i)) // no space on left of arrow |
784 | && empty(tok.get(i+2)) // no space inside of arrow |
785 | && empty(tok.get(i+4)) // no space on right of arrow |
786 | && !eq(_get(tok, i-1), "-"); // i-->0; |
787 | }); |
788 | |
789 | // shortened subconcept declaration (before star constructors!) |
790 | shortenedSubconcepts(tok); |
791 | |
792 | // "case" as a variable name ( => _case) |
793 | |
794 | caseAsVariableName(tok); |
795 | |
796 | // "do" as a function name ( => dO) |
797 | |
798 | tok_doAsMethodName(tok); |
799 | |
800 | // "continue" as a function name ( => _continue) |
801 | continueAsFunctionName(tok); |
802 | |
803 | tok_extend(tok); |
804 | |
805 | jreplace(tok, "pn {", "p-noconsole {"); |
806 | |
807 | // Do these BEFORE awt replacement! ("p-awt" contains "awt" token) |
808 | replaceKeywordBlock(tok, "r-awt", "r { awt {", "}}"); |
809 | if (hasCodeTokens(tok, "p", "-")) tok_p_old(tok); |
810 | |
811 | replaceKeywordBlock(tok, "awt-messagebox", "awt { pcall-messagebox {", "}}"); |
812 | replaceKeywordBlock(tok, "awt", "swingLater(r {", "});"); |
813 | |
814 | jreplace(tok, "p-android {", "set flag Android. p {"); |
815 | |
816 | unswing(tok); |
817 | lockBlocks(tok); |
818 | tempBlocks(tok); |
819 | tok_switchTo(tok); |
820 | |
821 | // trim x; |
822 | |
823 | jreplace(tok, "trim <id>;", "$2 = trim($2);"); |
824 | |
825 | // iterate with index |
826 | |
827 | jreplace (tok, "for <id> over <id>:", "for (int $2 = 0; $2 < l($4); $2++)"); |
828 | jreplace (tok, "for <id> backwards over <id>:", "for (int $2 = l($5)-1; $2 >= 0; $2--)"); |
829 | jreplace (tok, "for <id>, <id> <id> over <id>: {", "for (int $2 = 0; $2 < l($7); $2++) { $4 $5 = $7.get($2);"); |
830 | jreplace (tok, "for <id>, <id> <id> backwards over <id>: {", "for (int $2 = l($8)-1; $2 >= 0; $2--) { $4 $5 = $8.get($2);"); |
831 | jreplace (tok, "for <id> to <id>:", "for (int $2 = 0; $2 < $4; $2++)"); |
832 | jreplace (tok, "for <id> to <int>:", "for (int $2 = 0; $2 < $4; $2++)"); |
833 | |
834 | tok = expandShortTypes(tok); |
835 | |
836 | tok_equalsCast(tok); |
837 | tok_equalsOptCast(tok); |
838 | |
839 | replaceKeywordBlock(tok, "r-thread-messagebox", "r-thread { pcall-messagebox {", "}}"); |
840 | |
841 | replaceKeywordBlock(tok, "thread-messagebox", "thread { pcall-messagebox {", "}}"); |
842 | |
843 | jreplace(tok, "rThread {", "r-thread {"); |
844 | jreplace(tok, "rThreadEnter {", "r-thread { temp enter(); "); |
845 | |
846 | replaceKeywordBlock(tok, "r-thread", "runnableThread(r {", "})"); |
847 | rNamedThread(tok); |
848 | |
849 | // only works in the scope of a DynModule |
850 | jreplace(tok, "rEnter {", "r { temp enter(); "); |
851 | |
852 | replaceKeywordBlock(tok, "r-messagebox", "r { pcall-messagebox {", "}}"); |
853 | |
854 | jreplace(tok, "r <id> + r <id>", "r { $2(); $5(); }"); |
855 | |
856 | // runnable and r - now also with automatic toString if enabled |
857 | for (S keyword : ll("runnable", "r")) { |
858 | while ((i = jfind(tok, keyword + " {")) >= 0) { |
859 | int idx = findCodeTokens(tok, i, false, "{"); |
860 | int j = findEndOfBracketPart(tok, idx); |
861 | L<S> contents = subList(tok, idx+1, j-1); |
862 | replaceTokens(tok, i, j+1, "new Runnable {" |
863 | + " public void run() ctex { " + tok_addSemicolon(contents) + "\n}" |
864 | + (autoQuine ? tok_autoQuineFunc(contents) : "") |
865 | + "}"); |
866 | reTok(tok, i, j+1); |
867 | } |
868 | |
869 | while ((i = jfind(tok, keyword + " <quoted> {")) >= 0) { |
870 | int idx = findCodeTokens(tok, i, false, "{"); |
871 | int j = findEndOfBracketPart(tok, idx); |
872 | L<S> contents = subList(tok, idx+1, j-1); |
873 | replaceTokens(tok, i, j+1, "new Runnable {" |
874 | + " public void run() ctex { " + tok_addSemicolon(contents) + "\n}" |
875 | + " toString { ret " + tok.get(i+2) + "; }" |
876 | + (autoQuine ? tok_autoQuineFunc(contents, '_shortenedSourceCode) : "") |
877 | + "}"); |
878 | reTok(tok, i, j+1); |
879 | } |
880 | } |
881 | |
882 | replaceKeywordBlock(tok, |
883 | "expectException", |
884 | "{ bool __ok = false; try {", |
885 | "} catch { __ok = true; } assertTrue(\"expected exception\", __ok); }"); |
886 | |
887 | while ((i = tok.indexOf("tex")) >= 0) { |
888 | tok.set(i, "throws Exception"); |
889 | tok = jtok(tok); |
890 | } |
891 | |
892 | // shorter & smarter whiles |
893 | |
894 | jreplace(tok, "while true", "while (true)"); |
895 | jreplace(tok, "while licensed", "while (licensed())"); |
896 | jreplace(tok, "repeat {", "while (licensed()) {"); |
897 | tok_repeatWithSleep(tok); |
898 | |
899 | // null; => return null; etc. |
900 | |
901 | O cond = tokcondition { |
902 | ret tok_tokenBeforeLonelyReturnValue(tok, i-1); |
903 | }; |
904 | jreplace(tok, "null;", "return null;", cond); |
905 | jreplace(tok, "false;", "return false;", cond); |
906 | jreplace(tok, "true;", "return true;", cond); |
907 | jreplace(tok, "this;", "return this;", cond); |
908 | |
909 | // ok <cmd> => ret "OK" with <cmd> |
910 | jreplace(tok, "ok <id>", "return \"OK\" with $2"); |
911 | replaceKeywordBlock(tok, "ok", "{", " return \"OK\"; }"); |
912 | |
913 | // "myFunction;" instead of "myFunction();" - quite rough |
914 | // (isolated identifier as function call) |
915 | cond = new TokCondition { |
916 | bool get(L<S> tok, int i) { |
917 | S word = tok.get(i+3); |
918 | //print("single word: " + word); |
919 | ret !eqOneOf(word, "break", "continue", "return", "else", "endifdef", "endif"); |
920 | } |
921 | }; |
922 | for (S pre : litlist("}", ";")) |
923 | jreplace(tok, pre + " <id>;", "$1 $2();", cond); |
924 | |
925 | // shorter match syntax for answer methods |
926 | |
927 | jreplace(tok, "if <quoted> || <quoted>", |
928 | "if (matchOneOf(s, m, $2, $5))"); |
929 | |
930 | // "bla * bla | blubb * blubb" |
931 | jreplace_dyn(tok, "if <quoted>", func(L<S> tok, int cIdx) -> S { |
932 | S s = unquote(tok.get(cIdx+2)); |
933 | //print("multimatch: " + quote(s)); |
934 | new L<S> l; |
935 | for (S pat : splitAtJavaToken(s, "|")) { |
936 | //print("multimatch part: " + quote(pat)); |
937 | if (pat.contains("...")) |
938 | l.add("matchX(" + quote(trim(pat)) + ", s, m)"); |
939 | else if (javaTok(pat).contains("*")) |
940 | l.add("match(" + quote(trim(pat)) + ", s, m)"); |
941 | else |
942 | l.add("match(" + quote(trim(pat)) + ", s)"); |
943 | } |
944 | ret "if (" + join(" || ", l) + ")"; |
945 | }, tokcondition { |
946 | ret javaTokC(unquote(tok.get(i+3))).contains("|"); |
947 | }); |
948 | |
949 | // "...bla..." |
950 | jreplace(tok, "if <quoted>", "if (find3plusRestsX($2, s, m))", |
951 | tokcondition { |
952 | ret startsAndEndsWith(unquote(tok.get(i+3)), "..."); |
953 | }); |
954 | |
955 | // "bla..." |
956 | jreplace(tok, "if <quoted>", "if (matchStartX($2, s, m))", |
957 | tokcondition { |
958 | ret unquote(tok.get(i+3)).endsWith("..."); |
959 | }); |
960 | |
961 | // "bla" |
962 | jreplace(tok, "if <quoted>", "if (match($2, s))", |
963 | tokcondition { |
964 | ret !javaTokC(unquote(tok.get(i+3))).contains("*"); |
965 | }); |
966 | // "bla * bla" |
967 | jreplace(tok, "if <quoted>", "if (match($2, s, m))"); |
968 | jreplace(tok, "if match <quoted>", "if (match($3, s, m))"); |
969 | |
970 | jreplace(tok, "if <id> eq <quoted>", "if (eq($2, $4))"); |
971 | |
972 | tok_dropExtraCommas(tok); |
973 | |
974 | // additional translations (if necessary) |
975 | |
976 | jreplace(tok, "pcall ping {", "pcall { ping();"); |
977 | |
978 | replaceKeywordBlock(tok, ") pcall", |
979 | ") { pcall {", |
980 | "}}"); |
981 | |
982 | replaceKeywordBlock(tok, |
983 | "pcall", |
984 | "try {", |
985 | "} catch (Throwable __e) { _handleException(__e); }"); |
986 | |
987 | replaceKeywordBlock(tok, |
988 | "pcall-short", |
989 | "try {", |
990 | "} catch (Throwable __e) { print(exceptionToStringShort(__e)); }"); |
991 | |
992 | replaceKeywordBlock(tok, |
993 | "pcall-silent", |
994 | "try {", |
995 | "} catch (Throwable __e) { silentException(__e); }"); |
996 | |
997 | replaceKeywordBlock(tok, |
998 | "pcall-messagebox", |
999 | "try {", |
1000 | "} catch __e { messageBox(__e); }"); |
1001 | |
1002 | replaceKeywordBlock(tok, |
1003 | "pcall-infobox", |
1004 | "try {", |
1005 | "} catch __e { infoBox(__e); }"); |
1006 | |
1007 | tok = dialogHandler(tok); |
1008 | |
1009 | replaceKeywordBlock(tok, "exceptionToUser", |
1010 | "try {", |
1011 | "} catch (Throwable __e) { ret exceptionToUser(__e); }"); |
1012 | |
1013 | if (hasCodeTokens(tok, "twice", "{")) |
1014 | replaceKeywordBlock(tok, "twice", |
1015 | "for (int __twice = 0; __twice < 2; __twice++) {", |
1016 | "}"); |
1017 | |
1018 | while ((i = findCodeTokens(tok, "bench", "*", "{")) >= 0) { |
1019 | int j = findEndOfBracketPart(tok, i+4)-1; |
1020 | S time = makeVar("time"); |
1021 | S v = makeVar("bench"); |
1022 | S n = tok.get(i+2); |
1023 | tok.set(i, "{ long " + time + " = sysNow(); for (int " + v + " = 0; " + v + " < " + n + "; " + v + "++)"); |
1024 | tok.set(i+2, ""); |
1025 | tok.set(j, "} printBenchResult(sysNow()-" + time + ", " + n + "); }"); |
1026 | reTok(tok, i, j+1); |
1027 | } |
1028 | |
1029 | replaceKeywordBlockDyn(tok, |
1030 | "time", |
1031 | new O { S[] get() { |
1032 | S var = makeVar("startTime"); |
1033 | ret new S[] { |
1034 | "{ long " + var + " = sysNow(); try { ", |
1035 | "} finally { " + var + " = sysNow()-" + var + "; saveTiming(" + var + "); } }"}; |
1036 | }}); |
1037 | |
1038 | // version without { } |
1039 | replaceKeywordBlockDyn(tok, |
1040 | "time2", |
1041 | new O { S[] get() { |
1042 | S var = makeVar("startTime"); |
1043 | ret new S[] { |
1044 | "long " + var + " = sysNow(); ", |
1045 | " " + var + " = sysNow()-" + var + "; saveTiming(" + var + "); "}; |
1046 | }}); |
1047 | |
1048 | // time "bla" { |
1049 | // time msg { |
1050 | replaceKeywordPlusQuotedOrIDBlock(tok, |
1051 | "time", |
1052 | new O { S[] get(L<S> tok, int i) { |
1053 | S var = makeVar("startTime"); |
1054 | ret new S[] { |
1055 | "long " + var + " = sysNow(); ", |
1056 | " done2_always(" + tok.get(i+2) + ", " + var + "); "}; |
1057 | }}); |
1058 | |
1059 | if (hasCodeTokens(tok, "assertFail", "{")) { |
1060 | S var = makeVar("oops"); |
1061 | |
1062 | replaceKeywordBlock(tok, |
1063 | "assertFail", |
1064 | "boolean " + var + " = false; try {", |
1065 | "\n" + var + " = true; } catch (Exception e) { /* ok */ } assertFalse(" + var + ");"); |
1066 | } |
1067 | |
1068 | replaceKeywordBlock(tok, |
1069 | "yo", |
1070 | "try {", |
1071 | "} catch (Exception " + makeVar("e") + ") { ret false; }", |
1072 | tokcondition { |
1073 | ret neqOneOf(_get(tok, i-1), "svoid", "void"); |
1074 | }); |
1075 | |
1076 | replaceKeywordBlock(tok, |
1077 | "awtIfNecessary", |
1078 | "swingNowOrLater(r " + "{", |
1079 | "});"); |
1080 | |
1081 | ctex(tok); |
1082 | |
1083 | replaceKeywordBlock(tok, |
1084 | "actionListener", |
1085 | "new java.awt.event.ActionListener() { " + |
1086 | "public void actionPerformed(java.awt.event.ActionEvent _evt) { pcall-messagebox {", |
1087 | "}}}"); |
1088 | |
1089 | for (S keyword : ll("autocloseable", "autoCloseable")) |
1090 | /*replaceKeywordBlock(tok, |
1091 | keyword, |
1092 | "new AutoCloseable() { public void close() throws Exception {", |
1093 | "}}");*/ |
1094 | replaceKeywordBlock_dyn2_legacy(tok, keyword, new O { |
1095 | S[] get(LS tok, int iOpening, int iClosing) { |
1096 | LS contents = subList(tok, iOpening+1, iClosing); |
1097 | ret new S[] { |
1098 | "new AutoCloseable() { toString { ret " + quote(shorten(maxQuineLength, trimJoin(contents))) + "; } public void close() throws Exception {", |
1099 | "}}" |
1100 | }; |
1101 | } |
1102 | }); |
1103 | |
1104 | // try answer (string, test with nempty) |
1105 | while ((i = findCodeTokens(tok, "try", "answer")) >= 0) { |
1106 | int j = findEndOfStatement(tok, i); |
1107 | S v = makeVar(); |
1108 | bool needCurly = !eqGet(tok, i-2, "{"); |
1109 | tok.set(i, (needCurly ? "{" : "") + " S " + v); |
1110 | tok.set(i+2, "="); |
1111 | tok.set(j-1, "; if (!empty(" + v + ")) ret " + v + "; " + (needCurly ? "}" : "")); |
1112 | reTok(tok, i, j); |
1113 | } |
1114 | |
1115 | // try bool[ean] (try answer with Bool type) |
1116 | while ((i = findCodeTokens(tok, "try", "boolean")) >= 0) { |
1117 | int j = findEndOfStatement(tok, i); |
1118 | S v = makeVar(); |
1119 | tok.set(i, "{ Bool " + v); |
1120 | tok.set(i+2, "="); |
1121 | tok.set(j-1, "; if (" + v + " != null) ret " + v + "; }"); |
1122 | reTok(tok, i, j); |
1123 | } |
1124 | |
1125 | // S s = bla(), return if null; => S s = bla(); if (s == null) return; |
1126 | while ((i = jfind(tok, ", return if null;")) >= 0) { |
1127 | int j = tok_findBeginningOfStatement(tok, i); |
1128 | print("Found statement " + j + "/" + i + " - " + joinSubList(tok, j-1, i+5*2-1)); |
1129 | S var = getVarDeclarationName(subList(tok, j-1, i)); |
1130 | replaceTokens_reTok(tok, i, i+5*2-1, "; if (" + var + " == null) ret;"); |
1131 | } |
1132 | |
1133 | // <statement>, print "..."; => { <statement>; print("..."); } |
1134 | while ((i = jfind(tok, ", print <quoted>;")) >= 0) { |
1135 | int j = tok_findBeginningOfStatement(tok, i); |
1136 | replaceTokens_reTok(tok, i, i+4*2-1, "; print(" + tok.get(i+4) + "); }"); |
1137 | tokPrepend_reTok(tok, j, "{ "); |
1138 | } |
1139 | |
1140 | // return if null <expression> => if (<expression> == null) return; |
1141 | while ((i = jfind(tok, "return if null")) >= 0) { |
1142 | int j = findEndOfStatement(tok, i); |
1143 | clearTokens(tok, i, i+2); |
1144 | tok.set(i+4, "(("); |
1145 | tok.set(j-1, ") == null) ret;"); |
1146 | reTok(tok, i, j); |
1147 | } |
1148 | |
1149 | // return optional (return if not null) |
1150 | while ((i = jfind_check optional(tok, "return optional <id> =")) >= 0) { |
1151 | int j = findEndOfStatement(tok, i); |
1152 | S v = tok.get(i+4); |
1153 | clearTokens(tok, i+2, i+4); |
1154 | tok.set(i, "{"); |
1155 | tok.set(j-1, "; if (" + v + " != null) ret " + v + "; }"); |
1156 | reTok(tok, i, j); |
1157 | } |
1158 | |
1159 | // try object (return if not null) |
1160 | while ((i = jfind_check object(tok, "try object")) >= 0) { |
1161 | int j = findEndOfStatement(tok, i); |
1162 | clearTokens(tok, i, i+3); |
1163 | bool isDecl = isIdentifier(get(tok, i+4)) && isIdentifier(get(tok, i+6)) && eqGet(tok, i+8, "="); |
1164 | if (isDecl) { |
1165 | S v = get(tok, i+6); |
1166 | tok.set(i, "{"); |
1167 | tok.set(j-1, "; if (" + v + " != null) ret " + v + "; }"); |
1168 | } else { |
1169 | S v = makeVar(); |
1170 | tok.set(i, "{ O " + v + "="); |
1171 | tok.set(j-1, "; if (" + v + " != null) ret " + v + "; }"); |
1172 | } |
1173 | reTok(tok, i, j); |
1174 | } |
1175 | |
1176 | // try Int i = ...; (return if not null, shorter version of "try object") |
1177 | while ((i = jfind(tok, "try <id> <id> =")) >= 0) { |
1178 | int j = findEndOfStatement(tok, i); |
1179 | S type = tok.get(i+2), v = tok.get(i+4); |
1180 | tok.set(i, "{"); |
1181 | tok.set(j-1, "; if (" + v + " != null) ret " + v + "; }"); |
1182 | reTok(tok, i, j); |
1183 | } |
1184 | |
1185 | // debug print ...; => if (debug) print(...); |
1186 | while ((i = jfind(tok, "debug print")) >= 0) { |
1187 | int j = findEndOfStatement(tok, i); |
1188 | replaceTokens(tok, i, i+4, "if (debug) print("); |
1189 | tok.set(j-1, ");"); |
1190 | reTok(tok, i, j); |
1191 | } |
1192 | |
1193 | functionReferences(tok); |
1194 | tok_expandLPair(tok); |
1195 | tok_expandPairL(tok); |
1196 | tok_expandLT3(tok); |
1197 | quicknew2(tok); |
1198 | |
1199 | // X x = nu(+...) => X x = nu X(+...) |
1200 | jreplace(tok, "<id> <id> = nu(+", "$1 $2 = nu $1(+"); |
1201 | // X x = nu(a := ...) => X x = nu X(a := ...) |
1202 | jreplace(tok, "<id> <id> = nu(<id> :=", "$1 $2 = nu $1($6 :="); |
1203 | |
1204 | tok_expandVarCopies(tok); // AFTER the lines just above |
1205 | tok_replaceColonEqualsSyntax(tok); // ditto |
1206 | |
1207 | tok_unpair(tok); |
1208 | tok_cachedFunctions(tok); |
1209 | tok_simplyCachedFunctions(tok); |
1210 | |
1211 | tok_optPar(tok); |
1212 | |
1213 | throwFailEtc(tok); |
1214 | tok_typeAA(tok, pairClasses); |
1215 | |
1216 | // do this after expanding sclass etc. |
1217 | tok = tok_expandStarConstructors(tok); |
1218 | |
1219 | tok_kiloConstants(tok); |
1220 | //tok_colonMessages(tok); |
1221 | |
1222 | while ((i = jfind(tok, "shit:")) >= 0) { |
1223 | int j = tok_findEndOfStatement(tok, i); |
1224 | tok.set(i+2, "("); |
1225 | tok.set(j-1, ");"); |
1226 | reTok(tok, i, j); |
1227 | } |
1228 | |
1229 | jreplace("shit(", "ret with print("); |
1230 | |
1231 | tok_virtualTypes(tok); |
1232 | tok_autoLongConstants(tok); |
1233 | |
1234 | // common misordering of type arguments |
1235 | jreplace("boolean <A>", "<A> boolean"); |
1236 | |
1237 | tok_unimplementedMethods(tok); |
1238 | tok_switchableFields(tok); |
1239 | tok_autoDisposeFields(tok); |
1240 | tok_shortVisualize(tok); |
1241 | tok_whileGreaterThan(tok); |
1242 | tok_ifThenEnd(tok); |
1243 | |
1244 | tok_autoInitVars(tok); |
1245 | |
1246 | tok_fixBadTypeParameterOrder(tok); |
1247 | |
1248 | // shortened method declarations BEFORE standardFunctions |
1249 | jreplace(tok, "svoid", "static void"); |
1250 | jreplace(tok, "void <id> {", "$1 $2() {"); |
1251 | jreplace(tok, "void <id> thread {", "$1 $2() thread {"); |
1252 | jreplace(tok, "String <id> {", "$1 $2() {"); |
1253 | jreplace(tok, "Object <id> {", "$1 $2() {"); |
1254 | jreplace(tok, "List <id> {", "$1 $2() {"); |
1255 | |
1256 | namedThreads(tok); |
1257 | threads(tok); |
1258 | |
1259 | //tok_maxEquals(tok); |
1260 | |
1261 | tok_questionDot(tok); |
1262 | |
1263 | tok_embeddedFunctions(tok); |
1264 | |
1265 | jreplace("<id>[] <id> = new[", "$1[] $4 = new $1["); |
1266 | |
1267 | jreplace("<id> ifNull =", "if ($1 == null) $1 ="); |
1268 | |
1269 | jreplace("class <id> extends <id><.<id>>", "class $2 extends $4<$2.$7>"); |
1270 | |
1271 | tok_once(tok); // must come before next line so you can combine them |
1272 | tok_ifRecordMatch(tok); |
1273 | |
1274 | jreplace(tok, "<id> ||=", "$1 = $1 ||"); |
1275 | |
1276 | // magicValue followed by a numerical constant |
1277 | jreplace("magicValue", "", (_tok, _i) -> { |
1278 | S t = _get(_tok, _i+3); |
1279 | ret eqOneOf(t, ".", "-") || isInteger(t); |
1280 | }); |
1281 | |
1282 | // end of local stuff |
1283 | |
1284 | tok_processMetaBlocks(tok, metaCodeAllowed()); |
1285 | if (metaCodeAllowed()) runMetaTransformers(tok); |
1286 | |
1287 | same = tok_sameTest(tok, before); |
1288 | /*if (!same) |
1289 | print("local not same " + safety + " (" + l(tok) + " tokens)");*/ |
1290 | if (safety++ >= 10) { |
1291 | printSources(tok); |
1292 | fail("safety 10 error!"); |
1293 | } |
1294 | } while (!same); |
1295 | |
1296 | ret tok; |
1297 | } |
1298 | |
1299 | static L<S> reTok_include(L<S> tok, int i, int j) { |
1300 | ret reTok_modify(tok, i, j, f localStuff1); |
1301 | } |
1302 | |
1303 | static L<S> includeInMainLoaded_reTok(L<S> tok, int i, int j) { |
1304 | ret reTok_include(tok, i, j); |
1305 | } |
1306 | |
1307 | static L<S> stdstuff(L<S> tok) { |
1308 | //if (++level >= 10) fail("woot? 10"); |
1309 | |
1310 | print("stdstuff!"); |
1311 | int i; |
1312 | |
1313 | new L<S> ts; |
1314 | tok_findTranslators(tok, ts); |
1315 | if (nempty(ts)) |
1316 | print("DROPPING TRANSLATORS: " + structure(ts)); |
1317 | |
1318 | print('quickmain); tok = quickmain(tok); |
1319 | print('includes); tok = tok_processIncludes(tok); |
1320 | print('conceptsDot); if (processConceptsDot(tok)) |
1321 | tok = tok_processIncludes(tok); |
1322 | |
1323 | //print('starConstructors); tok = tok_expandStarConstructors(tok); |
1324 | |
1325 | // drop Java 8 annotations since we're compiling for Java 7 |
1326 | jreplace(tok, "@Nullable", ""); |
1327 | |
1328 | // STANDARD CLASSES & INTERFACES |
1329 | |
1330 | print("standard classes"); |
1331 | final Set<S> haveClasses = addStandardClasses_v2(tok); |
1332 | |
1333 | tok_quickInstanceOf(tok, haveClasses); |
1334 | |
1335 | // concept-related stuff |
1336 | |
1337 | // auto-import concepts |
1338 | bool _a = tok_hasClassRef2(tok, /*"extends",*/ "Concept") || tok_hasClassRef2(tok, "Concepts"), _b = !haveClasses.contains("Concept"); |
1339 | //print("auto-import: " + _a + ", " + _b); |
1340 | if (_a && _b) { |
1341 | print("Auto-including concepts."); |
1342 | if (shouldNotIncludeClass.contains("Concepts")) { |
1343 | print(join(tok)); |
1344 | fail("Unwanted concepts import"); |
1345 | } |
1346 | printStruct(haveClasses); |
1347 | tok = includeInMainLoaded(tok, "concepts."); |
1348 | reTok(tok, l(tok)-1, l(tok)); |
1349 | //processConceptsDot(tok); |
1350 | } |
1351 | |
1352 | ret tok; |
1353 | } // end of stdStuff! |
1354 | |
1355 | static L<S> multilineStrings(L<S> tok) { |
1356 | for (int i = 1; i < tok.size(); i += 2) { |
1357 | S t = tok.get(i); |
1358 | if (isQuoted(t)) |
1359 | if (t.startsWith("[") || t.contains("\r") || t.contains("\n")) |
1360 | tok.set(i, quote(unquote(t))); |
1361 | } |
1362 | ret tok; |
1363 | } |
1364 | |
1365 | static void inStringEvals(L<S> tok) { |
1366 | bool change = false; |
1367 | for (int i = 1; i < tok.size(); i += 2) { |
1368 | S t = tok.get(i); |
1369 | if (!isQuoted(t)) continue; |
1370 | if (t.contains("\\*") && !t.contains("\\\\")) { // << rough |
1371 | tok.set(i, inStringEval(t)); |
1372 | change = true; |
1373 | } |
1374 | } |
1375 | if (change) reTok(tok); |
1376 | } |
1377 | |
1378 | static S inStringEval(S t) { |
1379 | t = dropPrefix("\"", dropSuffix("\"", t)); |
1380 | new L<S> l; |
1381 | int idx; |
1382 | while ((idx = t.indexOf("\\*")) >= 0) { |
1383 | int j = indexOf(t, idx, "*/"); |
1384 | if (j < 0) break; |
1385 | if (idx > 0) |
1386 | l.add("\"" + substring(t, 0, idx) + "\""); |
1387 | l.add("(" + trim(substring(t, idx+2, j)) + ")"); |
1388 | t = substring(t, j+2); |
1389 | } |
1390 | if (nempty(t)) |
1391 | l.add("\"" + t + "\""); |
1392 | ret "(" + join(" + ", l) + ")"; |
1393 | } |
1394 | |
1395 | static L<S> quickmain(L<S> tok) { |
1396 | if (quickmainDone1 && quickmainDone2) ret tok; |
1397 | |
1398 | int i = findCodeTokens(tok, "main", "{"); |
1399 | if (i < 0) i = findCodeTokens(tok, "m", "{"); |
1400 | if (i >= 0 && !(i-2 > 0 && tok.get(i-2).equals("class"))) { |
1401 | tokSet_reTok(tok, i, "class main"); |
1402 | quickmainDone1 = true; |
1403 | } |
1404 | |
1405 | i = findCodeTokens(tok, "psvm", "{"); |
1406 | if (i < 0) i = findCodeTokens(tok, "p", "{"); |
1407 | if (i >= 0) { |
1408 | int idx = i+2; |
1409 | int j = findEndOfBracketPart(tok, idx)-1; |
1410 | L<S> contents = subList(tok, idx+1, j); |
1411 | //print("contents: " + sfu(contents)); |
1412 | tok.set(i, "public static void main(final String[] args) throws Exception"); |
1413 | replaceTokens(tok, idx+1, j, tok_addSemicolon(contents)); |
1414 | reTok(tok, i, j); |
1415 | quickmainDone2 = true; |
1416 | } |
1417 | |
1418 | ret tok; |
1419 | } |
1420 | |
1421 | sS makeVar(S name) { |
1422 | AtomicInteger counter = varCountByThread!; |
1423 | if (counter == null) |
1424 | varCountByThread.set(counter = new AtomicInteger); |
1425 | ret "_" + name + "_" + getAndInc(counter); |
1426 | } |
1427 | |
1428 | static S makeVar() { ret makeVar(""); } |
1429 | |
1430 | static L<S> rtq(L<S> tok, S id) { |
1431 | ret runTranslatorQuick(tok, id); |
1432 | } |
1433 | |
1434 | static L<S> expandShortTypes(L<S> tok) { |
1435 | // replace <int> with <Integer> |
1436 | for (int i = 1; i+4 < tok.size(); i += 2) |
1437 | if (tok.get(i).equals("<") |
1438 | && litlist(">", ",").contains(tok.get(i+4))) { |
1439 | String type = tok.get(i+2); |
1440 | if (type.equals("int")) type = "Integer"; |
1441 | else if (type.equals("long")) type = "Long"; |
1442 | tok.set(i+2, type); |
1443 | } |
1444 | |
1445 | // O = Object, S = String, ret = return |
1446 | for (int i = 1; i < tok.size(); i += 2) { |
1447 | String t = tok.get(i); |
1448 | if (t.equals("O")) t = "Object"; |
1449 | if (t.equals("S")) t = "String"; |
1450 | else if (t.equals("L")) t = "List"; |
1451 | else if (t.equals("Cl")) t = "Collection"; |
1452 | //else if (t.equals("F")) t = "Function"; |
1453 | else if (t.equals("ret") && neqOneOf(get(tok, i+2), "=", ")", ".")) t = "return"; |
1454 | 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 |
1455 | else if (t.equals("AtomicBool")) t = "AtomicBoolean"; |
1456 | tok.set(i, t); |
1457 | } |
1458 | |
1459 | jreplace(tok, "LL< <id> >", "L<L<$3>>"); |
1460 | jreplace(tok, "LL<?>", "L<L<?>>"); |
1461 | jreplace(tok, "LL", "L<L>"); |
1462 | |
1463 | jreplace(tok, "Clusters< <id> >", "Map<$3, Collection<$3>>"); |
1464 | |
1465 | ret tok; |
1466 | } |
1467 | |
1468 | static L<S> autoImports(L<S> tok) { |
1469 | HashSet<S> imports = new HashSet(tok_findImports(tok)); |
1470 | new StringBuilder buf; |
1471 | for (S c : standardImports) |
1472 | if (!(imports.contains(c))) |
1473 | buf.append("import " + c + ";\n"); |
1474 | if (buf.length() == 0) ret tok; |
1475 | tok.set(0, buf+tok.get(0)); |
1476 | ret reTok(tok, 0, 1); |
1477 | } |
1478 | |
1479 | static String[] standardImports = { |
1480 | "java.util.*", |
1481 | "java.util.zip.*", |
1482 | "java.util.List", |
1483 | "java.util.regex.*", |
1484 | "java.util.concurrent.*", |
1485 | "java.util.concurrent.atomic.*", |
1486 | "java.util.concurrent.locks.*", |
1487 | "javax.swing.*", |
1488 | "javax.swing.event.*", |
1489 | "javax.swing.text.*", |
1490 | "javax.swing.table.*", |
1491 | "java.io.*", |
1492 | "java.net.*", |
1493 | "java.lang.reflect.*", |
1494 | "java.lang.ref.*", |
1495 | "java.lang.management.*", |
1496 | "java.security.*", |
1497 | "java.security.spec.*", |
1498 | "java.awt.*", |
1499 | "java.awt.event.*", |
1500 | "java.awt.image.*", |
1501 | "javax.imageio.*", |
1502 | "java.math.*" |
1503 | }; |
1504 | |
1505 | static L<S> tok_processIncludes(L<S> tok) { |
1506 | int safety = 0; |
1507 | while (hasCodeTokens(tok, "!", "include") && ++safety < 100) |
1508 | tok = tok_processIncludesSingle(tok); |
1509 | |
1510 | //tok_autoCloseBrackets(tok); |
1511 | ret tok; |
1512 | } |
1513 | |
1514 | svoid tok_processEarlyIncludes(L<S> tok) { |
1515 | int i; |
1516 | while ((i = jfind_check include(tok, "!include early #<int>")) >= 0) { |
1517 | S id = tok.get(i+8); |
1518 | included.add(parseLong(id)); |
1519 | replaceTokens_reTok(tok, i, i+10, "\n" + cacheGet(id) + "\n"); |
1520 | } |
1521 | } |
1522 | |
1523 | static L<S> tok_processIncludesSingle(L<S> tok) { |
1524 | int i; |
1525 | while ((i = jfind_check include(tok, "!include #<int>")) >= 0) { |
1526 | S id = tok.get(i+6); |
1527 | included.add(parseLong(id)); |
1528 | replaceTokens(tok, i, i+8, "\n" + cacheGet(id) + "\n"); |
1529 | reTok_include(tok, i, i+8); |
1530 | } |
1531 | while ((i = jfind_check include(tok, "!include once #<int>")) >= 0) { |
1532 | S id = tok.get(i+8); |
1533 | bool isNew = included.add(parseLong(id)); |
1534 | replaceTokens(tok, i, i+10, |
1535 | isNew ? "\n" + cacheGet(id) + "\n" : ""); |
1536 | reTok_include(tok, i, i+10); |
1537 | } |
1538 | ret tok; |
1539 | } |
1540 | |
1541 | static void ctex(L<S> tok) { |
1542 | replaceKeywordBlock(tok, "ctex", |
1543 | "{ try {", |
1544 | "} catch (Exception __e) { throw rethrow(__e); } }"); |
1545 | for (S keyword : ll("null on exception", "null on error")) |
1546 | replaceKeywordBlock(tok, keyword, |
1547 | "{ try {", |
1548 | "} catch (Throwable __e) { return null; } }"); |
1549 | replaceKeywordBlock(tok, "false on exception", |
1550 | "{ try {", |
1551 | "} catch (Throwable __e) { return false; } }"); |
1552 | } |
1553 | |
1554 | static L<S> dialogHandler(L<S> tok) { |
1555 | ret replaceKeywordBlock(tok, |
1556 | "dialogHandler", |
1557 | "new DialogHandler() {\n" + |
1558 | "public void run(final DialogIO io) {", |
1559 | "}}"); |
1560 | } |
1561 | |
1562 | static void quicknew2(L<S> tok) { |
1563 | tok_quicknew(tok); |
1564 | |
1565 | /*jreplace(tok, "new <id> <id>;", "$2 $3 = new $2;"); |
1566 | jreplace(tok, "new <id><<id>> <id>;", "$2<$4> $6 = new $2;"); |
1567 | jreplace(tok, "new <id>.<id> <id>;", "$2.$4 $5 = new $2.$4();"); |
1568 | jreplace(tok, "new <id><<id>> <id>, <id>;", "$2<$4> $6 = new $2, $8 = new $2;"); |
1569 | jreplace(tok, "new <id><<id>,<id>> <id>;", "$2<$4,$6> $8 = new $2;"); |
1570 | jreplace(tok, "new <id><<id><<id>>> <id>;", "$2<$4<$6>> $9 = new $2;"); |
1571 | jreplace(tok, "new <id><<id>[]> <id>;", "$2 $3 $4 $5 $6 $7 $8 = new $2;"); |
1572 | jreplace(tok, "new <id>< <id><<id>>, <id>> <id>;", "$2 $3 $4 $5 $6 $7 $8 $9 $10 $11 = new $2;"); |
1573 | jreplace(tok, "new <id>< <id><<id>,<id>> > <id>;", "$2 $3 $4 $5 $6 $7 $8 $9 $10 $11 = new $2;"); |
1574 | jreplace(tok, "new <id>< <id>, <id><<id>> > <id>;", "$2 $3 $4 $5 $6 $7 $8 $9 $10 $11 = new $2;"); |
1575 | jreplace(tok, "new <id>< <id><<id>,<id>,<id>> > <id>;", "$2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 = new $2;"); |
1576 | jreplace(tok, "new <id>< <id><<id>,<id>>, <id>> <id>;", "$2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 = new $2;"); |
1577 | jreplace(tok, "new <id>< <id>, <id><<id>,<id>>> <id>;", "$2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 = new $2;"); |
1578 | jreplace(tok, "new <id>< <id>< <id>, <id><<id>,<id>>>> <id>;", "$2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15 $16 = new $2;"); |
1579 | jreplace(tok, "new <id>< <id>< <id>.<id>, <id><<id>,<id>>>> <id>;", "$2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15 $16 $17 $18 = new $2;"); |
1580 | jreplace(tok, "new <id>< <id>< <id><<id>>, <id><<id>>>> <id>;", "$2 $3 $4 $5 $6 $7 $8 $9 $10 $11 $12 $13 $14 $15 $16 $17 = new $2;");*/ |
1581 | |
1582 | // [abandoned, confusing, looks like a function definition] with arguments - new A a(...); => A a = new A(...); |
1583 | //jreplace(tok, "new <id> <id>(", "$2 $3 = new $2("); |
1584 | |
1585 | jreplace(tok, "for args " + "{", "for (int i = 0; i < args.length; i++) { final String arg = args[i];"); |
1586 | |
1587 | // Constructor calls without parentheses |
1588 | // So you can say something like: predictors.add(new P1); |
1589 | |
1590 | jreplace1(tok, "new <id>", "new $2()", tokcondition { |
1591 | ret eqOneOf(_get(tok, i+5), "{", ",", ")", ";", ":"); |
1592 | }); |
1593 | |
1594 | jreplace1(tok, "new <id> < <id> >", "new $2<$4>()", tokcondition { |
1595 | ret eqOneOf(_get(tok, i+11), "{", ",", ")", ";", ":"); |
1596 | }); |
1597 | |
1598 | jreplace(tok, "new List(", "new ArrayList("); |
1599 | jreplace(tok, "new Map(", "new HashMap("); |
1600 | jreplace(tok, "new Set(", "new HashSet("); |
1601 | jreplace(tok, "new (Hash)Set", "new HashSet"); // rough |
1602 | jreplace(tok, "new (Tree)Set", "new TreeSet"); |
1603 | jreplace(tok, "new (Hash)Map", "new HashMap"); |
1604 | jreplace(tok, "new (Tree)Map", "new TreeMap"); |
1605 | jreplace(tok, "\\*<id>[<id>] <id>;", "$2[] $6 = new $2[$4];"); |
1606 | |
1607 | // X x = new(...) => X x = new X(...) |
1608 | // X x = new {...} => X x = new X {...} |
1609 | // X x = new => X x = new |
1610 | jreplace(tok, "<id> <id> = new", "$1 $2 = new $1", tokcondition { |
1611 | ret eqOneOf(_get(tok, i+9), "(", ";", ",", ")", "{"); |
1612 | }); |
1613 | jreplace(tok, "<id> <id> = new \\*", "$1 $2 = new $1"); |
1614 | jreplace(tok, "\\* <id> = new <id>", "$5 $2 = new $5"); |
1615 | |
1616 | // TODO: use backward type scanning |
1617 | |
1618 | jreplace(tok, "<id><<id>> <id> = new", "$1 $2 $3 $4 $5 = new $1", tokcondition { |
1619 | ret eqOneOf(_get(tok, i+9+3*2), "(", ";", ",", ")"); |
1620 | }); |
1621 | jreplace(tok, "<id><<id>,<id>> <id> = new", "$1 $2 $3 $4 $5 $6 $7 = new $1", tokcondition { |
1622 | ret eqOneOf(_get(tok, i+9+5*2), "(", ";", ",", ")"); |
1623 | }); |
1624 | jreplace(tok, "<id><<id><<id>>> <id> = new", "$1 $2 $3 $4 $5 $6 $7 $8 = new $1", tokcondition { |
1625 | ret eqOneOf(_get(tok, i+9+6*2), "(", ";", ",", ")"); |
1626 | }); |
1627 | } |
1628 | |
1629 | static L<S> extendClasses(L<S> tok) { |
1630 | int i; |
1631 | while ((i = jfind(tok, "extend <id> {")) >= 0) { |
1632 | S className = tok.get(i+2); |
1633 | int idx = findCodeTokens(tok, i, false, "{"); |
1634 | int j = findEndOfBracketPart(tok, idx+2); |
1635 | S content = join(subList(tok, idx+1, j-1)); |
1636 | L<S> c = findInnerClassOfMain(tok, className); |
1637 | print("Extending class " + className); |
1638 | clearTokens(tok.subList(i, j+1)); |
1639 | if (c == null) { |
1640 | print("Warning: Can't extend class " + className + ", not found"); |
1641 | continue; |
1642 | } |
1643 | int startOfClass = indexOfSubList(tok, c); // magicIndexOfSubList is broken |
1644 | int endOfClass = startOfClass + l(c)-1; |
1645 | //print("Extending class " + className + " ==> " + join(subList(tok, startOfClass, endOfClass))); |
1646 | while (neq(tok.get(endOfClass), "}")) --endOfClass; |
1647 | //print("Extending class " + className + " ==> " + join(subList(tok, startOfClass, endOfClass))); |
1648 | tok.set(endOfClass, content + "\n" + tok.get(endOfClass)); |
1649 | |
1650 | doubleReTok(tok, i, j+1, endOfClass, endOfClass+1); |
1651 | } |
1652 | ret tok; |
1653 | } |
1654 | |
1655 | // for ping / while ping |
1656 | static void forPing(L<S> tok) { |
1657 | int i; |
1658 | for (S keyword : ll("for", "fOr", "while")) |
1659 | while ((i = jfind(tok, keyword + " ping (")) >= 0) { |
1660 | int bracketEnd = findEndOfBracketPart(tok, i+4)-1; |
1661 | int iStatement = bracketEnd+2; |
1662 | int iEnd = findEndOfStatement(tok, iStatement); |
1663 | tok.set(i+2, ""); |
1664 | |
1665 | // turn into block |
1666 | if (!eq(get(tok, iStatement), "{")) { |
1667 | tok.set(iStatement, "{ " + tok.get(iStatement)); |
1668 | tok.set(iEnd-1, tok.get(iEnd-1) + " }"); |
1669 | } |
1670 | |
1671 | // add ping |
1672 | tok.set(iStatement, "{ ping(); " + dropPrefixTrim("{", tok.get(iStatement))); |
1673 | reTok(tok, i+2, iEnd); |
1674 | } |
1675 | } |
1676 | |
1677 | // lib 123 => !123 |
1678 | static void libs(L<S> tok) { |
1679 | int i; |
1680 | while ((i = jfind(tok, "lib <int>")) >= 0) { |
1681 | S id = tok.get(i+2); |
1682 | print("lib " + id); |
1683 | if (!libs.contains(id)) { |
1684 | libs.add(id); |
1685 | clearAllTokens(tok, i, i+3); |
1686 | /*tok.set(i, "!"); |
1687 | tok.set(i+1, "");*/ |
1688 | } else { |
1689 | print("...ignoring (duplicate)"); |
1690 | clearAllTokens(tok, i, i+3); |
1691 | reTok(tok, i, i+3); |
1692 | } |
1693 | } |
1694 | print("libs found: " + libs); |
1695 | } |
1696 | |
1697 | // sourceCodeLine() => 1234 |
1698 | static void sourceCodeLine(L<S> tok) { |
1699 | int i ; |
1700 | while ((i = jfind(tok, "sourceCodeLine()")) >= 0) { |
1701 | replaceTokens(tok, i, i+5, str(countChar(join(subList(tok, 0, i)), '\n')+1)); |
1702 | reTok(tok, i, i+5); |
1703 | } |
1704 | } |
1705 | |
1706 | // done before any other processing |
1707 | static void earlyStuff(L<S> tok) { |
1708 | int i; |
1709 | |
1710 | tok_scopes(tok, autoCloseScopes := asInclude); |
1711 | tok_autosemi(tok); |
1712 | tok_autoCloseBrackets(tok); |
1713 | jreplace(tok, "°", "()"); |
1714 | |
1715 | // Note: this makes the word "quine" a special operator |
1716 | // (unusable as a function name) |
1717 | |
1718 | while ((i = jfind(tok, "quine(")) >= 0) { |
1719 | int idx = findCodeTokens(tok, i, false, "("); |
1720 | int j = findEndOfBracketPart(tok, idx+2); |
1721 | tok.set(i, "new Quine"); |
1722 | tok.set(idx, "(" + quote(join(subList(tok, idx+1, j-1))) + ", "); |
1723 | reTok(tok, i, idx+1); |
1724 | } |
1725 | |
1726 | jreplace_check after(tok, "void <id> after super {", "void $2 { super.$2();"); |
1727 | |
1728 | // do this before func & voidfunc because otherwise they swallow it |
1729 | jreplace(tok, "enter {", "{ temp enter();"); |
1730 | |
1731 | // func keyword for lambdas - now automatically quines toString() if enabled |
1732 | |
1733 | // do func & voidfunc early to preserve original code as toString |
1734 | |
1735 | while ((i = jfind(tok, "func(")) >= 0) { |
1736 | int argsFrom = i+4, argsTo = findCodeTokens(tok, i, false, ")"); |
1737 | int idx = findCodeTokens(tok, argsTo, false, "{"); |
1738 | int j = findEndOfBracketPart(tok, idx); |
1739 | L<S> contents = subList(tok, idx+1, j-1); |
1740 | |
1741 | S returnType = "O"; |
1742 | if (eq(tok.get(argsTo+2), "-") && eq(tok.get(argsTo+4), ">")) |
1743 | returnType = tok_toNonPrimitiveTypes(join(subList(tok, argsTo+6, idx-1))); |
1744 | |
1745 | S toString = autoQuine ? " public S toString() { ret " + quote(shorten(maxQuineLength, trimJoin(contents))) + "; }" : ""; |
1746 | |
1747 | L<S> args = cloneSubList(tok, argsFrom-1, argsTo); |
1748 | tok_shortFinals(args); |
1749 | tok_toNonPrimitiveTypes(args); |
1750 | |
1751 | L<S> types = tok_typesOfParams(args); |
1752 | O type = "O"; |
1753 | if (l(types) <= 3) |
1754 | type = "F" + l(types) + "<" + joinWithComma(types) + ", " + returnType + ">"; |
1755 | S body = tok_addReturn(contents); |
1756 | |
1757 | replaceTokens_reTok(tok, i, j, |
1758 | "new " + type + "() { " |
1759 | + returnType + " get(" + trimJoin(args) + ") ctex { " |
1760 | + body |
1761 | + " }\n" + |
1762 | + toString + "}"); |
1763 | } |
1764 | |
1765 | while ((i = jfind(tok, "voidfunc(")) >= 0) { |
1766 | int argsFrom = i+4, argsTo = findCodeTokens(tok, i, false, ")"); |
1767 | int idx = findCodeTokens(tok, argsTo, false, "{"); |
1768 | int j = findEndOfBracketPart(tok, idx); |
1769 | L<S> contents = subList(tok, idx+1, j-1); |
1770 | |
1771 | L<S> args = cloneSubList(tok, argsFrom-1, argsTo); |
1772 | tok_shortFinals(args); |
1773 | tok_toNonPrimitiveTypes(args); |
1774 | |
1775 | L<S> types = tok_typesOfParams(args); |
1776 | O type = "O"; |
1777 | if (l(types) <= 4) |
1778 | type = "VF" + l(types) + "<" + joinWithComma(types) + ">"; |
1779 | |
1780 | replaceTokens(tok, i, j, "new " + type + "() { public void get(" + trimJoin(args) + ") ctex { " + tok_addSemicolon(contents) + " }\n" + |
1781 | (autoQuine ? " public S toString() { ret " + quote(shorten(maxQuineLength, trim(join(contents)))) + "; }" : "") + "}"); |
1782 | reTok(tok, i, j); |
1783 | } |
1784 | |
1785 | jreplace(tok, "func {", "func -> O {"); |
1786 | jreplace(tok, "f {", "f -> O {"); |
1787 | |
1788 | // swing -> S { ... } => swing(func -> S { ... }) |
1789 | while ((i = jfind(tok, "swing ->")) >= 0) { |
1790 | int bracket = findCodeTokens(tok, i, false, "{"); |
1791 | int j = findEndOfBracketPart(tok, bracket); |
1792 | tok.set(i, "swing(func"); |
1793 | tok.set(j-1, "})"); |
1794 | reTok(tok, i, j); |
1795 | } |
1796 | |
1797 | tok_qFunctions(tok); |
1798 | |
1799 | for (S keyword : ll(/*"f",*/ "func")) { |
1800 | while ((i = jfind(tok, keyword + " ->", tokcondition { |
1801 | ret isIdentifier(_get(tok, i+7)); // avoid lambda declaration like: f -> { ... } |
1802 | })) >= 0) { |
1803 | // I think there is a bug here for something like func -> x { new x { } } |
1804 | int idx = findCodeTokens(tok, i, false, "{"); |
1805 | int j = findEndOfBracketPart(tok, idx); |
1806 | S returnType = tok_toNonPrimitiveTypes(join(subList(tok, i+6, idx-1))); |
1807 | L<S> contents = subList(tok, idx+1, j-1); |
1808 | replaceTokens(tok, i, j, "new F0<" + returnType + ">() { " + returnType + " get() ctex { " + tok_addReturn(contents) + " }\n" + |
1809 | (autoQuine ? " public S toString() { ret " + quote(shorten(maxQuineLength, trim(join(contents)))) + "; }" : "") + "}"); |
1810 | reTok(tok, i, j); |
1811 | } |
1812 | } |
1813 | |
1814 | while ((i = jfind(tok, "time repeat * {")) >= 0) { |
1815 | int j = findEndOfBlock(tok, i+6)-1; |
1816 | tok.set(i, "time {"); |
1817 | tok.set(j, "}}"); |
1818 | reTok(tok, i, j+1); |
1819 | } |
1820 | |
1821 | // do this before "m {" stuff because "repeat m" [doesn't seem to work yet though) |
1822 | while ((i = findCodeTokens(tok, "repeat", "*", "{")) >= 0) { |
1823 | S v = makeVar("repeat"); |
1824 | tok.set(i, "for (int " + v + " = 0; " + v + " < " + tok.get(i+2) + "; " + v + "++)"); |
1825 | tok.set(i+2, ""); |
1826 | reTok(tok, i, i+3); |
1827 | } |
1828 | } |
1829 | |
1830 | static void throwFailEtc(L<S> tok) { |
1831 | bool anyChange = false; |
1832 | for (int i = 1; i+4 < l(tok); i += 2) |
1833 | if (eqOneOf(get(tok, i+2), "fail", "todo") |
1834 | && eq(get(tok, i+4), "(") |
1835 | && !eqOneOf(get(tok, i), "throw", "RuntimeException", "return", "f", "function")) { |
1836 | tok.set(i+2, "throw " + tok.get(i+2)); |
1837 | anyChange = true; |
1838 | } |
1839 | if (anyChange) |
1840 | reTok(tok); |
1841 | } |
1842 | |
1843 | static void namedThreads(L<S> tok) { |
1844 | for (int i = 0; i < 100; i++) { |
1845 | int idx = findCodeTokens(tok, "thread", "<quoted>", "{"); |
1846 | if (idx < 0) idx = findCodeTokens(tok, "thread", "<id>", "{"); |
1847 | if (idx < 0) |
1848 | break; |
1849 | int j = findEndOfBracketPart(tok, idx+4); |
1850 | S tName = tok.get(idx+2); |
1851 | |
1852 | S pre = "startThread(" + tName + ", r { "; |
1853 | S post = "})"; |
1854 | if (!tok_tokenLeftOfExpression(tok, idx-2)) post += ";"; |
1855 | |
1856 | tok.set(idx, pre); |
1857 | tok.set(idx+2, ""); |
1858 | tok.set(idx+4, ""); |
1859 | tok.set(j-1, post); |
1860 | //print(join(subList(tok, idx, j))); |
1861 | reTok(tok, idx, j); |
1862 | } |
1863 | } |
1864 | |
1865 | static void rNamedThread(L<S> tok) { |
1866 | for (int i = 0; i < 100; i++) { |
1867 | int idx = findCodeTokens(tok, "r", "-", "thread", "<quoted>", "{"); |
1868 | if (idx < 0) idx = findCodeTokens(tok, "r", "-", "thread", "<id>", "{"); |
1869 | if (idx < 0) |
1870 | break; |
1871 | int j = findEndOfBracketPart(tok, idx+8); |
1872 | S tName = tok.get(idx+6); |
1873 | |
1874 | S pre = "r { thread " + tName + " {"; |
1875 | S post = "}}"; |
1876 | |
1877 | replaceTokens(tok, idx, idx+9, pre); |
1878 | tok.set(j-1, post); |
1879 | reTok(tok, idx, j); |
1880 | } |
1881 | } |
1882 | |
1883 | static void threads(L<S> tok) { |
1884 | replaceKeywordBlock(tok, "daemon", "startDaemonThread(r {", "});"); |
1885 | //replaceKeywordBlock(tok, "thread", "startThread(r {", "});"); |
1886 | |
1887 | // now enclose in { } to allow using like "void bla() thread { ... }" - if it's not used as an expression |
1888 | |
1889 | replaceKeywordBlock_dyn2_legacy(tok, "thread", new O { // don't use func here, it can't be transpiled |
1890 | S[] get (LS tok, int iOpening, int iClosing) { |
1891 | // is the thread clause used as an expression? |
1892 | bool isExpression = tok_tokenLeftOfExpression(tok, iOpening-4); |
1893 | ret new S[] { |
1894 | (isExpression ? "" : "{ ") + "startThread(r {", |
1895 | "})" + |
1896 | (isExpression ? "" : "; }") |
1897 | }; |
1898 | } |
1899 | }); |
1900 | } |
1901 | |
1902 | static SS sf; // standard standard functions (haha) |
1903 | static Bool _761ok; |
1904 | |
1905 | static L<S> standardFunctions(L<S> tok) { |
1906 | if (sf == null) { |
1907 | S _761 = cacheGet("#761"); |
1908 | if (_761ok == null) |
1909 | _761ok = isBracketHygienic(_761); |
1910 | assertTrue("Whoa! #761 truncated?", _761ok); |
1911 | L<S> standardFunctions = concatLists( |
1912 | (L) loadVariableDefinition(_761, "standardFunctions"), |
1913 | (L) loadVariableDefinition(cacheGet("#1006654"), "standardFunctions")); |
1914 | |
1915 | sf = new HashMap; |
1916 | for (S x : standardFunctions) { |
1917 | S[] f = x.split("/"); |
1918 | sf.put(f[1], f[0]); |
1919 | } |
1920 | } |
1921 | |
1922 | SS sfMap = combinedMap(extraStandardFunctions, sf); |
1923 | |
1924 | for (int i = 0; ; i++) { |
1925 | Set<S> defd = new HashSet(findFunctions(tok)); |
1926 | |
1927 | int j; |
1928 | while ((j = jfind(tok, "should not include function *.")) >= 0) { |
1929 | S fname = tok.get(j+8); |
1930 | shouldNotIncludeFunction.add(fname); |
1931 | clearAllTokens(tok.subList(j, j+12)); |
1932 | } |
1933 | |
1934 | while ((j = jfind(tok, "do not include function *.")) >= 0) { |
1935 | S fname = tok.get(j+8); |
1936 | doNotIncludeFunction.add(fname); |
1937 | clearAllTokens(tok.subList(j, j+12)); |
1938 | } |
1939 | |
1940 | doNotIncludeFunction.addAll(tok_importedStaticFunctionNames(tok)); |
1941 | |
1942 | // changes tok |
1943 | Set<String> invocations = findFunctionInvocations(tok, sfMap, hardFunctionReferences, defd, true); |
1944 | /*if (invocations.contains("str")) |
1945 | print("==STR==" + join(tok) + "==STR==");*/ |
1946 | if (!cic(definitions, "leanMode")) |
1947 | invocations.addAll(functionsToAlwaysInclude); |
1948 | |
1949 | //print("Functions invoked: " + structure(invocations)); |
1950 | List<String> needed = diff(invocations, defd); |
1951 | for (S f : doNotIncludeFunction) needed.remove(f); |
1952 | if (needed.isEmpty()) |
1953 | break; |
1954 | print("Adding functions: " + join(" " , needed)); |
1955 | |
1956 | HashSet neededSet = new HashSet(needed); |
1957 | Collection<S> bad = setIntersection(neededSet, shouldNotIncludeFunction); |
1958 | if (nempty(bad)) { |
1959 | S msg = "INCLUDING BAD FUNCTIONS: " + sfu(bad); |
1960 | print(msg); |
1961 | print(join(tok)); |
1962 | fail(msg); |
1963 | } |
1964 | |
1965 | new L<S> added; |
1966 | new L<Future<S>> parts; |
1967 | new L<S> preload; |
1968 | |
1969 | for (S x : needed) |
1970 | if (sfMap.containsKey(x)) |
1971 | preload.add(sfMap.get(x)); |
1972 | print("Preloading: " + preload); |
1973 | cachePreload(preload); |
1974 | |
1975 | for (String x : cloneList(needed)) { |
1976 | if (defd.contains(x)) continue; |
1977 | |
1978 | String id = sfMap.get(x); |
1979 | if (id == null) { |
1980 | print("Standard function " + x + " not found."); |
1981 | needed.remove(x); |
1982 | continue; |
1983 | } |
1984 | //print("Adding function: " + x + " (" + id + ")"); |
1985 | |
1986 | S function = cacheGet(id); |
1987 | //if (("\n" + function).contains("\n!")) print("Warning: " + id + " contains translators."); |
1988 | |
1989 | if (cacheStdFunctions) { |
1990 | long _id = psI(id); |
1991 | CachedInclude ci = cachedIncludes.get(_id); |
1992 | if (ci == null) { |
1993 | cachedIncludes.put(_id, ci = new CachedInclude); |
1994 | //println("Caching include " + _id + ", l=" + l(cachedIncludes)); |
1995 | } |
1996 | function += "\n"; |
1997 | fS _function = function; |
1998 | if (neq(ci.javax, function)) { |
1999 | print("Compiling function: " + x); |
2000 | ci.javax = function; |
2001 | ci.java = executor.submit(new Callable<S>() { |
2002 | public S call() { |
2003 | // We assume that variables made with makeVar() will always be local to some block |
2004 | varCountByThread.set(null); |
2005 | ret join(localStuff1(jtok(_function))); |
2006 | } |
2007 | }); |
2008 | ci.realJava = null; |
2009 | } |
2010 | parts.add(ci.javaFuture()); |
2011 | } else |
2012 | parts.add(nowFuture(function + "\n")); |
2013 | |
2014 | added.add(x); |
2015 | Collection<S> newFunctions = new HashSet(findFunctionDefs(javaTok(function))); |
2016 | defd.addAll(newFunctions); |
2017 | for (S f : newFunctions) |
2018 | if (!addedFunctions.add(f)) { |
2019 | printSources(tok); |
2020 | fail("Trying to add function " + f + " again - main class syntax broken!"); |
2021 | } |
2022 | } |
2023 | |
2024 | S text = join(getAllFutures(parts)); |
2025 | tok = includeInMainLoaded(tok, text); |
2026 | |
2027 | // defd = new HashSet(findFunctions(tok)); |
2028 | //print("Functions added: " + joinWithSpace(added)); |
2029 | |
2030 | for (String x : needed) |
2031 | if (!defd.contains(x)) { |
2032 | print(join(tok)); |
2033 | fail("Function not defined properly: " + x); |
2034 | } |
2035 | //print("Iteration " + (i+2)); |
2036 | |
2037 | tok_definitions(tok); |
2038 | tok_ifndef(tok); |
2039 | tok_ifdef(tok); |
2040 | |
2041 | if (i >= 1000) fail("Too many iterations"); |
2042 | } |
2043 | |
2044 | ret tok; |
2045 | } |
2046 | |
2047 | static L<S> findFunctions(L<S> tok) { |
2048 | //ret findFunctionDefinitions(join(findMainClass(tok))); |
2049 | ret findFunctionDefs(findMainClass(tok)); |
2050 | } |
2051 | |
2052 | static S cacheGet(S snippetID) { |
2053 | snippetID = formatSnippetID(snippetID); |
2054 | S text = snippetCache.get(snippetID); |
2055 | if (text == null) { |
2056 | text = loadSnippet(snippetID); |
2057 | // very early processing/checks for includes |
2058 | |
2059 | if (hasUnclosedStringLiterals(text)) |
2060 | fail("Unclosed string literals in " + snippetID); |
2061 | |
2062 | if (regexpContains("\\bscope\\b", text)) { |
2063 | LS tok = javaTok(text); |
2064 | tok_scopes(tok, autoCloseScopes := true); |
2065 | text = join(tok); |
2066 | } |
2067 | |
2068 | snippetCache.put(snippetID, text); |
2069 | } |
2070 | ret text; |
2071 | } |
2072 | |
2073 | static void cachePreload(Collection<S> ids) { |
2074 | new L<S> needed; |
2075 | for (S id : ids) |
2076 | if (!snippetCache.containsKey(formatSnippetID(id))) |
2077 | needed.add(formatSnippetID(id)); |
2078 | if (l(needed) > 1) { |
2079 | L<S> texts = loadSnippets(needed); |
2080 | for (int i = 0; i < l(needed); i++) |
2081 | if (texts.get(i) != null) |
2082 | snippetCache.put(needed.get(i), texts.get(i)); |
2083 | } |
2084 | } |
2085 | |
2086 | static L<S> jtok(L<S> tok) { |
2087 | ret jtok(join(tok)); |
2088 | } |
2089 | |
2090 | static L<S> jtok(S s) { |
2091 | ret indexTokenList(javaTok(s)); |
2092 | } |
2093 | |
2094 | static HashSet<S> haveClasses_actual(L<S> tok) { |
2095 | new HashSet<S> have; |
2096 | for (L<S> c : innerClassesOfMain(tok)) |
2097 | have.add(getClassDeclarationName(c)); |
2098 | ret have; |
2099 | } |
2100 | |
2101 | static HashSet<S> haveClasses_addImported(L<S> tok, HashSet<S> have) { |
2102 | have.addAll(tok_importedClassNames(tok)); |
2103 | have.addAll(usualJavaClassNames()); // for S => S.class |
2104 | ret have; |
2105 | } |
2106 | |
2107 | // works on Java level (no "sclass" etc) |
2108 | // returns list of classes we have (useful for other processing) |
2109 | static Set<S> addStandardClasses_v2(L<S> tok) { |
2110 | if (lclasses == null) { |
2111 | S sc = cacheGet("#1003674"); |
2112 | lclasses = new L; |
2113 | for (S line : tlft_j(sc)) { |
2114 | int idx = line.indexOf('/'); |
2115 | lclasses.addAll(ll(line.substring(0, idx), line.substring(idx+1))); |
2116 | } |
2117 | } |
2118 | L<S> definitions = lclasses; |
2119 | |
2120 | for (int safety = 0; safety < 10; safety++) { |
2121 | HashSet<S> have = haveClasses_actual(tok); |
2122 | have.addAll(tok_importedClassNames(tok)); |
2123 | |
2124 | int j; |
2125 | while ((j = jfind(tok, "should not include class *.")) >= 0) { |
2126 | S cname = tok.get(j+8); |
2127 | shouldNotIncludeClass.add(cname); |
2128 | clearAllTokens(tok.subList(j, j+12)); |
2129 | } |
2130 | |
2131 | new SS need; |
2132 | Set<S> idx = tokenIndexWithoutIfclass_forStdClasses(tok); |
2133 | |
2134 | while ((j = jfind(tok, "please include class *.")) >= 0) { |
2135 | S cname = tok.get(j+6); |
2136 | idx.add(cname); |
2137 | clearAllTokens(tok.subList(j, j+10)); |
2138 | } |
2139 | |
2140 | for (int i = 0; i+1 < l(definitions); i += 2) { |
2141 | S className = definitions.get(i); |
2142 | if (idx.contains(className) && !have.contains(className)) |
2143 | need.put(className, definitions.get(i+1)); |
2144 | } |
2145 | if (hasDef("SymbolAsString")) { |
2146 | print("Have SymbolAsString."); |
2147 | if (need.containsKey("Symbol")) { |
2148 | print("Have Symbol."); |
2149 | need.remove("Symbol"); |
2150 | } |
2151 | } else |
2152 | print("No SymbolAsString."); |
2153 | |
2154 | if (empty(need)) ret have; |
2155 | |
2156 | for (S className : keys(need)) |
2157 | if (shouldNotIncludeClass.contains(className)) { |
2158 | S msg = "INCLUDING BAD CLASS: " + className; |
2159 | print(msg); |
2160 | print(join(tok)); |
2161 | fail(msg); |
2162 | } |
2163 | |
2164 | cachePreload(values(need)); |
2165 | |
2166 | new StringBuilder buf; |
2167 | print("Adding classes: " + joinWithSpace(keys(need))); |
2168 | for (S className : keys(need)) { |
2169 | if (have.contains(className)) continue; // intermittent add |
2170 | S snippetID = need.get(className); |
2171 | //print("Adding class " + className + " / " + snippetID); |
2172 | snippetID = fsI(snippetID); |
2173 | S text = cacheGet(snippetID); |
2174 | |
2175 | assertTrue(cacheStdClasses); |
2176 | long _id = psI(snippetID); |
2177 | CachedInclude ci = cachedIncludes.get(_id); |
2178 | if (ci == null) cachedIncludes.put(_id, ci = new CachedInclude); |
2179 | if (neq(ci.javax, text)) { |
2180 | // TODO: do it in multiple threads |
2181 | print("Compiling class: " + className); |
2182 | ci.javax = text; |
2183 | ci.java = null; |
2184 | ci.realJava = join(localStuff1(jtok(text))); |
2185 | } |
2186 | buf.append(ci.java()); |
2187 | |
2188 | L<S> ct = javaTok(ci.java()); |
2189 | for (L<S> c : allClasses(ct)) { |
2190 | S name = getClassDeclarationName(c); |
2191 | have.add(name); |
2192 | } |
2193 | if (!have.contains(className)) |
2194 | fail("Wrongly defined class: " + className + " / " + snippetID); |
2195 | if (!addedClasses.add(className)) { |
2196 | printSources(tok); |
2197 | fail("Trying to add class " + className + " again - main class syntax broken!"); |
2198 | } |
2199 | } // for className |
2200 | |
2201 | tok = includeInMainLoaded(tok, str(buf)); |
2202 | } |
2203 | fail("safety 10"); |
2204 | } |
2205 | |
2206 | static Set<S> expandableClassNames = lithashset("BigInteger"); |
2207 | |
2208 | // no reTok - leaves tok dirty |
2209 | // magically append ".class" to class name references |
2210 | static bool expandClassReferences_lazy(L<S> tok, Set<S> classNames) { |
2211 | bool change = false; |
2212 | for (int i = 3; i+2 < l(tok); i += 2) { |
2213 | S t = tok.get(i); |
2214 | |
2215 | // skip implements/extends/throws lists |
2216 | if (eqOneOf(t, "implements", "extends", "throws")) { |
2217 | i = tok_endOfImplementsList(tok, i); |
2218 | continue; |
2219 | } |
2220 | |
2221 | if (classNames.contains(t) || expandableClassNames.contains(t)) { |
2222 | S s = tok.get(i-2); t = tok.get(i+2); |
2223 | // TODO: This whole logic ain't very good |
2224 | // (Hard to distinguish between "Int.class" as an argument |
2225 | // and "Int" as a type parameter.) |
2226 | if (eqOneOf(s, "instanceof", "new", ".", "<", ">", "/", "nu")) continue; |
2227 | if (isIdentifier(s) || isInteger(s)) continue; |
2228 | if (eq(t, ",") && isIdentifier(get(tok, i+4)) && eqGet(tok, i+6, ">")) continue; // e.g. T3<L<S>, S, S> |
2229 | if (eq(s, ",") && isIdentifier(get(tok, i-4)) && eqGet(tok, i-6, "<")) continue; // e.g. T3<S, S, S> |
2230 | if (eq(s, ",") && eqOneOf(_get(tok, i-6), "implements", "throws")) continue; |
2231 | |
2232 | // check for cast |
2233 | if (eq(s, "(") && eq(t, ")") && i >= 5) { |
2234 | if (!eqOneOf(get(tok, i+4), "{", ";")) { |
2235 | S x = tok.get(i-4); |
2236 | if (!isIdentifier(x)) continue; |
2237 | if (eqOneOf(x, "ret", "return")) continue; |
2238 | } |
2239 | } |
2240 | if (eqOneOf(t, ",", ")", ";", ":")) { |
2241 | tok.set(i, tok.get(i) + ".class"); |
2242 | change = true; |
2243 | } |
2244 | } |
2245 | } |
2246 | ret change; |
2247 | } |
2248 | |
2249 | static void expandClassReferences(L<S> tok, Set<S> classNames) { |
2250 | if (expandClassReferences_lazy(tok, classNames)) |
2251 | reTok(tok); |
2252 | } |
2253 | |
2254 | // "<id>/<ClassName>" => "((ClassName) <id>)" |
2255 | static void slashCasts(L<S> tok, final Set<S> classNames) { |
2256 | /*jreplace(tok, "<id>/<id>", "(($3) $1)", tokcondition { |
2257 | ret classNames.contains(tok.get(i+5)); |
2258 | });*/ |
2259 | int n = l(tok)-4; |
2260 | for (int i = 1; i < n; i += 2) |
2261 | if (tok.get(i+2).equals("/") && isIdentifier(tok.get(i)) |
2262 | && classNames.contains(tok.get(i+4))) |
2263 | replaceTokens_reTok(tok, i, i+5, "((" + tok.get(i+4) + ") " + tok.get(i) + ")"); |
2264 | } |
2265 | |
2266 | // "<ClassName>(...)" => "new <ClassName>(...)" |
2267 | // doesn't work at beginning of statement as we can't easily |
2268 | // distinguish it from a constructor declaration. |
2269 | static void newWithoutNew(L<S> tok, final Set<S> classNames) { |
2270 | TokCondition cond = newWithoutNew_condition(classNames); |
2271 | jreplace(tok, "<id>(", "new $1(", cond); |
2272 | // just one case with type arg for now |
2273 | jreplace(tok, "<id><<id>>(", "new $1<$3>(", cond); |
2274 | } |
2275 | |
2276 | static TokCondition newWithoutNew_condition(final Set<S> classNames) { |
2277 | ret tokcondition { |
2278 | if (!classNames.contains(tok.get(i+1))) false; |
2279 | S prev = _get(tok, i-1); |
2280 | bool ok = eq(prev, ">") ? eqGet(tok, i-3, "-") |
2281 | : neqOneOf(prev, "new", ";", "}", "{", "public", "protected", "private", "."); |
2282 | //print("newWithoutNew: checking " + struct(subList(tok, i-1, i+2)) + " => " + ok); |
2283 | ret ok; |
2284 | }; |
2285 | } |
2286 | |
2287 | static bool processConceptsDot(L<S> tok) { |
2288 | bool anyChange = false, change; |
2289 | do { |
2290 | change = false; |
2291 | for (int i : jfindAll(tok, "concepts.")) |
2292 | if (contains(get(tok, i+3), "\n")) { |
2293 | replaceTokens(tok, i, i+3, "!" + "include once #1004863 // Dynamic Concepts"); |
2294 | reTok(tok, i, i+3); |
2295 | change = anyChange = true; |
2296 | break; |
2297 | } |
2298 | } while (change); |
2299 | ret anyChange; |
2300 | } |
2301 | |
2302 | static void caseAsVariableName(L<S> tok) { |
2303 | if (!tok.contains("case")) ret; |
2304 | for (int i = 1; i+2 < l(tok); i += 2) { |
2305 | S t = tok.get(i+2); |
2306 | if (tok.get(i).equals("case") |
2307 | && !(t.startsWith("'") || isInteger(t) || isIdentifier(t))) |
2308 | tok.set(i, "_case"); |
2309 | } |
2310 | } |
2311 | |
2312 | static void continueAsFunctionName(L<S> tok) { |
2313 | jreplace(tok, "continue(", "_continue("); |
2314 | } |
2315 | |
2316 | // f bla => "bla" - and "please include function bla." |
2317 | static void functionReferences(L<S> tok) { |
2318 | int i; |
2319 | |
2320 | jreplace_dyn(tok, "f-thread <id>", func(L<S> tok, int cIdx) { |
2321 | "dynamicCallableMC_thread(" + quote(tok.get(cIdx+6)) + ")" |
2322 | }); |
2323 | |
2324 | S keyword = "f"; |
2325 | while ((i = jfind(tok, keyword + " <id>", tokcondition { |
2326 | ret !eq(tok.get(i+3), "instanceof"); |
2327 | })) >= 0) { |
2328 | S f = tok.get(i+2); |
2329 | clearTokens(tok, i, i+2); |
2330 | tok.set(i+2, quote(f)); |
2331 | reTok(tok, i, i+2); |
2332 | tok.set(l(tok)-1, last(tok) + "\nplease include function " + f + "."); |
2333 | reTok(tok, l(tok)-1, l(tok)); |
2334 | } |
2335 | |
2336 | // r|rThread|rEnter|rThreadEnter fname => r|rThread|... { fname() } |
2337 | while ((i = jfindOneOf_cond(tok, tokcondition { |
2338 | ret !eq(tok.get(i+3), "instanceof"); |
2339 | }, "r <id>", "rThread <id>", "rEnter <id>", "rThreadEnter <id>")) >= 0) { |
2340 | S f = tok.get(i+2); |
2341 | replaceTokens(tok, i, i+3, tok.get(i) + " { " + f + "(); }"); |
2342 | reTok(tok, i, i+3); |
2343 | } |
2344 | |
2345 | // dm_q fname => r_dm_q(r fname) |
2346 | jreplace(tok, "dm_q <id>", "r_dm_q(r $2)"); |
2347 | |
2348 | // vf<S> fname => voidfunc(S s) { fname(s) } |
2349 | jreplace(tok, "vf<<id>> <id>", "voidfunc($3 a) { $5(a) }"); |
2350 | |
2351 | // vf<L<S>> fname => voidfunc(L<S> a) { fname(a) } |
2352 | jreplace(tok, "vf<<id><<id>>> <id>", "voidfunc($3<$5> a) { $8(a) }"); |
2353 | |
2354 | // construct<S> Entry => func(S s) -> Entry { new Entry(s) } |
2355 | jreplace(tok, "construct<<id>> <id>", "func($3 a) -> $5 { new $5(a) }"); |
2356 | |
2357 | // f<S> fname => func -> S { fname() } |
2358 | jreplace(tok, "f<<id>> <id>", "func -> $3 { $5() }"); |
2359 | |
2360 | // f<S, S> fname => func(S x) -> S { fname(x) } |
2361 | jreplace(tok, "f<<id>, <id>> <id>", "func($3 x) -> $5 { $7(x) }"); |
2362 | |
2363 | // f<S, L<S>> fname => func(S x) -> L<S> { fname(x) } |
2364 | jreplace(tok, "f<<id>, <id><<id>>> <id>", "func($3 x) -> $5 $6 $7 $8 { $10(x) }"); |
2365 | |
2366 | // f<S, L<S>, S> fname => func(S x, L<S> y) -> S { fname(x, y) } |
2367 | jreplace(tok, "f<<id>, <id><<id>>, <id>> <id>", "func($3 x, $5 $6 $7 $8 y) -> $10 { $12(x, y) }"); |
2368 | |
2369 | // if1 fname => a -> fname(a) |
2370 | jreplace_dyn(tok, "if1 <id>", func(LS tok, int i) -> S { |
2371 | S var = makeVar(); |
2372 | ret var + " -> " + tok.get(i+2) + "(" + var + ")"; |
2373 | }); |
2374 | } |
2375 | |
2376 | static void quicknu(L<S> tok) { |
2377 | jreplace(tok, "nu <id>(", "nu($2.class, "); // not needed anymore |
2378 | jreplace(tok, "nu <id>", "new $2"); |
2379 | } |
2380 | |
2381 | // fill variable innerClasses_list |
2382 | static void innerClassesVar(L<S> tok, Set<S> have) { |
2383 | int i = jfind_check myInnerClasses_list(tok, ">myInnerClasses_list;"); |
2384 | if (i < 0) ret; |
2385 | tok.set(i+4, "=litlist(\n" + joinQuoted(", ", have) + ");"); |
2386 | reTok(tok, i+4, i+5); |
2387 | } |
2388 | |
2389 | // fill variable innerClasses_list |
2390 | static void fillVar_transpilationDate(L<S> tok) { |
2391 | int i = jfind_check myTranspilationDate_value(tok, "long myTranspilationDate_value;"); |
2392 | if (i < 0) ret; |
2393 | tok.set(i+4, " = " + now() + "L;"); |
2394 | reTok(tok, i+4, i+5); |
2395 | } |
2396 | |
2397 | // process ifclass x ... endif blocks |
2398 | static void tok_ifclass(L<S> tok, Set<S> have) { |
2399 | if (!tok.contains("ifclass")) ret; |
2400 | |
2401 | int i = l(tok); |
2402 | while ((i = rjfind(tok, 1, i-1, "ifclass <id>")) >= 0) { |
2403 | int j = jfind(tok, i+4, "endif"); |
2404 | if (j < 0) j = l(tok)-1; |
2405 | S name = tok.get(i+2); |
2406 | bool has = have.contains(name); |
2407 | //print("ifclass " + name + " => " + has); |
2408 | if (has) { |
2409 | clearTokens_reTok(tok, j, j+1); |
2410 | clearTokens_reTok(tok, i, i+3); |
2411 | } else |
2412 | clearTokens(tok, i, j+1); |
2413 | } |
2414 | } |
2415 | |
2416 | // set flag *. |
2417 | static void tok_definitions(L<S> tok) { |
2418 | int i; |
2419 | while ((i = jfind_check flag(tok, "set flag <id>.")) >= 0) { |
2420 | S fname = tok.get(i+4); |
2421 | print("Setting flag " + fname); |
2422 | definitions.add(fname); |
2423 | clearAllTokens(tok.subList(i, i+8)); |
2424 | } |
2425 | |
2426 | while ((i = jfind_check flag(tok, "unset flag <id>.")) >= 0) { |
2427 | S fname = tok.get(i+4); |
2428 | print("Unsetting flag " + fname); |
2429 | definitions.remove(fname); |
2430 | clearAllTokens(tok.subList(i, i+8)); |
2431 | } |
2432 | } |
2433 | |
2434 | static void tok_findRewrites(L<S> tok) { |
2435 | int i; |
2436 | while ((i = jfind(tok, "rewrite <id> =")) >= 0) { |
2437 | S token = tok.get(i+2); |
2438 | int repStart = i+6; |
2439 | int repEnd = smartIndexOf(tok, repStart, "."); |
2440 | S replacement = joinSubList(tok, repStart, repEnd-1); |
2441 | clearTokens(tok, i, repEnd+1); |
2442 | rewrites.put(token, replacement); |
2443 | print("Have rewrite: " + token + " => " + replacement); |
2444 | } |
2445 | } |
2446 | |
2447 | static void tok_processRewrites(L<S> tok) { |
2448 | for (S token : keys(rewrites)) |
2449 | jreplace(tok, token, rewrites.get(token)); |
2450 | } |
2451 | |
2452 | // extend *. (set base class of main class) |
2453 | static void tok_extend(L<S> tok) { |
2454 | int i; |
2455 | while ((i = jfind(tok, "extend <id>.")) >= 0) { |
2456 | mainBaseClass = tok.get(i+2); |
2457 | clearAllTokens(tok, i, i+7); |
2458 | } |
2459 | } |
2460 | |
2461 | // process ifndef x ... endifndef blocks |
2462 | static void tok_ifndef(L<S> tok) { |
2463 | if (!tok.contains("ifndef")) ret; |
2464 | |
2465 | int i; |
2466 | while ((i = rjfind(tok, "ifndef <id>")) >= 0) { |
2467 | int j = jfind(tok, i+4, "endifndef"); |
2468 | if (j < 0) j = l(tok)-1; |
2469 | S fname = tok.get(i+2); |
2470 | bool has = !definitions.contains(fname); |
2471 | //print("ifndef " + fname + " => " + has); |
2472 | clearTokens(tok, i, i+3); |
2473 | clearTokens(tok, j, j+1); |
2474 | if (!has) clearTokens(tok, i+3, j); |
2475 | reTok(tok, i, j+1); |
2476 | } |
2477 | } |
2478 | |
2479 | // process ifdef x ... endifdef blocks |
2480 | static void tok_ifdef(L<S> tok) { |
2481 | if (!tok.contains("ifdef")) ret; |
2482 | |
2483 | int i; |
2484 | while ((i = rjfind(tok, "ifdef <id>")) >= 0) { |
2485 | int j = jfind(tok, i+4, "endifdef"); |
2486 | if (j < 0) j = l(tok)-1; |
2487 | S fname = tok.get(i+2); |
2488 | bool has = definitions.contains(fname); |
2489 | //print("ifdef " + fname + " => " + has); |
2490 | clearTokens(tok, i, i+3); |
2491 | clearTokens(tok, j, j+1); |
2492 | if (!has) clearTokens(tok, i+3, j); |
2493 | reTok(tok, i, j+1); |
2494 | } |
2495 | } |
2496 | |
2497 | svoid conceptDeclarations(L<S> tok) { |
2498 | for (S kw : ll("concept", "sconcept")) { |
2499 | O cond = tokcondition { tok_addFieldOrder(tok, i+1); true; }; |
2500 | bool re = false; |
2501 | if (jreplace(tok, kw + " <id> {", "static class $2 extends Concept {", cond)) re = true; |
2502 | if (jreplace(tok, kw + " <id> implements", "static class $2 extends Concept implements", cond)) re = true; |
2503 | if (jreplace(tok, kw + " <id>", "static class $2", cond)) re = true; |
2504 | if (re) reTok(tok); |
2505 | } |
2506 | } |
2507 | |
2508 | svoid shortenedSubconcepts(L<S> tok) { |
2509 | jreplace(tok, "<id> > <id> {", "concept $3 extends $1 {", tokcondition { |
2510 | bool b = (i == 0 || tok.get(i).contains("\n")) || eq(_get(tok, i-1), "abstract"); // only at beginning of line or after "abstract" |
2511 | //print("subconcept " + b + ": " + structure(subList(tok, i-1, i+5))); |
2512 | ret b; |
2513 | }); |
2514 | } |
2515 | |
2516 | // -slightly experimental |
2517 | // -do calculation in another thread, then return to AWT thread |
2518 | // -must be placed in a block |
2519 | // -transforms rest of block |
2520 | svoid unswing(L<S> tok) { |
2521 | int i; |
2522 | while ((i = jfind(tok, "unswing {")) >= 0) { |
2523 | int idx = i+2; |
2524 | int closingBracket = findEndOfBracketPart(tok, idx)-1; |
2525 | int endOfOuterBlock = findEndOfBracketPart(tok, closingBracket)-1; |
2526 | tok.set(i, "thread"); |
2527 | tok.set(closingBracket, " awt {"); |
2528 | tok.set(endOfOuterBlock, "}}}"); |
2529 | reTok(tok, closingBracket-1, endOfOuterBlock+1); |
2530 | } |
2531 | } |
2532 | |
2533 | // -Syntax: lock theLock; |
2534 | // -lock a lock, unlock at end of current block with finally |
2535 | svoid lockBlocks(L<S> tok) { |
2536 | int i; |
2537 | while ((i = jfind(tok, "lock <id>", tokcondition { ret neq(tok.get(i+3), "instanceof"); })) >= 0) { |
2538 | int semicolon = findEndOfStatement(tok, i)-1; |
2539 | S var = makeVar(); |
2540 | int endOfOuterBlock = findEndOfBracketPart(tok, semicolon)-1; |
2541 | replaceTokens(tok, i, semicolon+1, |
2542 | "Lock " + var + " = " + joinSubList(tok, i+2, semicolon-1) + "; lock(" + var + "); try {"); |
2543 | tok.set(endOfOuterBlock, "} finally { unlock(" + var + "); } }"); |
2544 | reTok(tok, i, endOfOuterBlock+1); |
2545 | } |
2546 | } |
2547 | |
2548 | // -Syntax: temp Bla bla = bla(); |
2549 | // -expands to try(Bla bla = bla()) { ... } with rest of block inside |
2550 | svoid tempBlocks(L<S> tok) { |
2551 | int i; |
2552 | while ((i = jfind(tok, "temp <id>")) >= 0) { |
2553 | int semicolon = findEndOfStatement(tok, i)-1; |
2554 | int endOfOuterBlock = findEndOfBracketPart(tok, semicolon)-1; |
2555 | L<S> sub = subList(tok, i-1, semicolon); |
2556 | int eq = subList(sub, 0, smartIndexOfOneOf(sub, "{", "(")).indexOf("="); |
2557 | S var; |
2558 | if (eq >= 0) |
2559 | var = sub.get(eq-2); |
2560 | else { |
2561 | // no var name, e.g. temp newThoughtSpace(); |
2562 | var = makeVar(); |
2563 | tok.set(i+2, "AutoCloseable " + var + " = " + tok.get(i+2)); |
2564 | } |
2565 | |
2566 | //tok.set(i, "try ("); |
2567 | //tok.set(semicolon, ") {"; |
2568 | //tok.set(endOfOuterBlock, "}}"); |
2569 | |
2570 | tok.set(i, ""); |
2571 | tok.set(semicolon, "; try {"); |
2572 | tok.set(endOfOuterBlock, "} finally { _close(" + var + "); }}"); |
2573 | |
2574 | reTok(tok, i, endOfOuterBlock+1); |
2575 | } |
2576 | } |
2577 | |
2578 | svoid forgetCachedIncludes { |
2579 | cachedIncludes.clear(); |
2580 | } |
2581 | |
2582 | // TODO: when to do this / merge contents if there are multiple transpilers? |
2583 | svoid cleanMeUp { |
2584 | for (CachedInclude ci : values(cachedIncludes)) |
2585 | ci.clean(); |
2586 | vmKeepWithProgramMD5_save('cachedIncludes); |
2587 | } |
2588 | |
2589 | svoid printSources(L<S> tok) { |
2590 | print("----"); |
2591 | print(join(tok)); |
2592 | print("----"); |
2593 | } |
2594 | |
2595 | svoid tok_quickInstanceOf(L<S> tok, final Set<S> haveClasses) { |
2596 | if (!quickInstanceOfEnabled) ret; |
2597 | // "x << X" or "x >> X" => "x instanceof X" |
2598 | for (S op : ll("<<", ">>")) |
2599 | jreplace(tok, "<id> " + op + " <id>", "$1 instanceof $4", tokcondition { |
2600 | ret haveClasses.contains(tok.get(i+7)) |
2601 | && !eqOneOf(tok.get(i-1), "<", "extends", "implements"); |
2602 | }); |
2603 | } |
2604 | |
2605 | sbool hasDef(S s) { |
2606 | ret definitions.contains(s); |
2607 | } |
2608 | |
2609 | svoid expandTriple(L<S> tok) { |
2610 | jreplace(tok, "T3< <id> >", "T3<$3, $3, $3>"); |
2611 | jreplace(tok, "T3< <id><<id>> >", "T3<$3<$5>, $3<$5>, $3<$5>>"); |
2612 | } |
2613 | |
2614 | svoid tok_shortFinals(L<S> tok) { |
2615 | jreplace(tok, "fS", "final S"); |
2616 | jreplace(tok, "fO", "final O"); |
2617 | jreplace(tok, "fL", "final L"); |
2618 | jreplace(tok, "fMap", "final Map"); |
2619 | jreplace(tok, "fRunnable", "final Runnable"); |
2620 | jreplace(tok, "f int", "final int"); |
2621 | } |
2622 | |
2623 | svoid tok_mainClassNameAndPackage(LS tok) { |
2624 | int i; |
2625 | if ((i = jfind(tok, "mainClassName <id>")) >= 0) { |
2626 | mainClassName = tok.get(i+2); |
2627 | if (eqGet(tok, i+4, ".") && isIdentifier(get(tok, i+6))) { |
2628 | mainPackage = mainClassName; |
2629 | mainClassName = tok.get(i+6); |
2630 | clearTokensAndReTok(tok, i, i+7); |
2631 | } else |
2632 | clearTokensAndReTok(tok, i, i+3); |
2633 | } |
2634 | |
2635 | if ((i = jfind(tok, "mainPackage <id>")) >= 0) { |
2636 | mainPackage = tok.get(i+2); |
2637 | clearTokens(tok, i, i+3); |
2638 | reTok(tok, i, i+3); |
2639 | } |
2640 | } |
2641 | |
2642 | svoid defineMapLikes(LS tok) { |
2643 | int i; |
2644 | while ((i = jfind(tok, "mapLike <id>")) >= 0) { |
2645 | mapLikeFunctions.add(tok.get(i+2)); |
2646 | clearTokens_reTok(tok, i, i+2); |
2647 | } |
2648 | } |
2649 | |
2650 | svoid defineMapMethodLikes(LS tok) { |
2651 | int i; |
2652 | while ((i = jfind(tok, "mapMethodLike <id>")) >= 0) { |
2653 | mapMethodLikeFunctions.add(tok.get(i+2)); |
2654 | clearTokens_reTok(tok, i, i+2); |
2655 | } |
2656 | } |
2657 | |
2658 | // functions that work like "nu" syntactically (accept a class as "super-first" parameter [left of the bracket]) |
2659 | svoid defineNuLikes(LS tok) { |
2660 | int i; |
2661 | while ((i = jfind(tok, "nuLike <id>")) >= 0) { |
2662 | nuLikeFunctions.add(tok.get(i+2)); |
2663 | clearTokens_reTok(tok, i, i+2); |
2664 | } |
2665 | } |
2666 | |
2667 | svoid defineExtraSF(LS tok) { |
2668 | int i; |
2669 | IntRange reTok = null; |
2670 | while ((i = jfind(tok, "function <id> is in *.")) >= 0) { |
2671 | extraStandardFunctions.put(tok.get(i+2), fsI(unquote(tok.get(i+8)))); |
2672 | clearTokens(tok, i, i+12); |
2673 | reTok = combineIntRanges(reTok, intRange(i, i+12)); |
2674 | } |
2675 | reTok(tok, reTok); |
2676 | } |
2677 | |
2678 | sbool tok_applyMapLikeFunctions(LS tok, final Set<S> mapLikeFunctions) { |
2679 | // map funcname(...) => map(f funcname, ...) |
2680 | // filter funcname(...) => filter(f funcname, ...) |
2681 | // ... |
2682 | ret jreplace(tok, "<id> <id>(", "$1(f $2,", func(L<S> tok, int i) -> bool { |
2683 | contains(mapLikeFunctions, tok.get(i+1)) |
2684 | }); |
2685 | } |
2686 | |
2687 | sbool tok_applyMapMethodLikeFunctions(LS tok, final Set<S> mapMethodLikeFunctions) { |
2688 | // mapMethod funcname(...) => mapMethod('funcname, ...) |
2689 | // collect funcname(...) => collect('funcname, ...) |
2690 | // ... |
2691 | ret jreplace_dyn(tok, "<id> <id>(", |
2692 | func(L<S> tok, int cIdx) -> S { tok.get(cIdx) + "(" + quote(tok.get(cIdx+2)) + "," }, |
2693 | func(L<S> tok, int i) -> bool { |
2694 | contains(mapMethodLikeFunctions, tok.get(i+1)) |
2695 | }); |
2696 | } |
2697 | |
2698 | svoid runMetaPostBlocks(LS tok) { |
2699 | for ping (S code : unnull(metaPostBlocks)) |
2700 | call(hotwireCached(standardFunctionSnippet(assertIdentifier(code))), code, tok); |
2701 | //callF(codeToFunctionOnArbitraryType(code, "LS", L, "tok"), tok); |
2702 | } |
2703 | |
2704 | svoid runMetaTransformers(LS tok) { |
2705 | for ping (S code : unnull(metaTransformers)) |
2706 | tok_runMetaTransformer(tok, code); |
2707 | } |
2708 | |
2709 | sbool tok_applyNuLikeFunctions(LS tok, final Set<S> nuLikeFunctions) { |
2710 | // nu ClassName(...) => nu(ClassName, ...) |
2711 | // ... |
2712 | ret jreplace_dyn(tok, "<id> <id>(", |
2713 | func(L<S> tok, int cIdx) -> S { tok.get(cIdx) + "(" + tok.get(cIdx+2) + ".class," }, |
2714 | func(L<S> tok, int i) -> bool { |
2715 | contains(nuLikeFunctions, tok.get(i+1)) |
2716 | }); |
2717 | } |
2718 | |
2719 | sbool metaCodeAllowed() { |
2720 | ret allowMetaCode || containsIC(definitions, "allowMetaCode"); |
2721 | } |
2722 | |
2723 | static LS indexTokenList(LS tok) { |
2724 | if (useIndexedList2) ret indexedList2(tok); |
2725 | if (useTokenIndexedList) ret tokenIndexedList3(tok); |
2726 | ret tok; |
2727 | } |
2728 | |
2729 | svoid tok_earlyGeneralStuff(LS tok) { |
2730 | tok_standardBot1(tok); |
2731 | tok_processSimplified(tok); |
2732 | tok_compactModules(tok); |
2733 | } |
Began life as a copy of #759
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1025804 |
Snippet name: | Transpiler with TokenIndexedList3 [dev.] |
Eternal ID of this version: | #1025804/5 |
Text MD5: | 17173d60b4fb926477587ddc5e6b9377 |
Transpilation MD5: | c93fde82cf662445e3975e9d9c741df4 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code (desktop) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2019-10-21 14:54:00 |
Source code size: | 91420 bytes / 2733 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 278 / 907 |
Version history: | 4 change(s) |
Referenced in: | [show references] |