Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

1416
LINES

< > BotCompany Repo | #1002427 // Accellerating 629 (SPIKE)

JavaX translator [tags: use-pretranspiled]

Transpiled version (1417L) is out of date.

1  
import java.util.*;
2  
import java.io.*;
3  
import java.util.regex.*;
4  
import java.net.*;
5  
import java.math.*;
6  
7  
public class main {
8  
  static boolean debug = true;
9  
  
10  
  static String[] standardFunctions = {
11  
    "#1002425/runTranslatorQuick",
12  
    "#1002423/tag",
13  
    "#1002421/highest",
14  
    "#1002419/newParser",
15  
    "#1002416/isDigit",
16  
    "#1002415/structureLines",
17  
    "#1002411/rand",
18  
    "#1002407/getTuringScore",
19  
    "#1002406/run_overBot",
20  
    "#1002403/lastTiming",
21  
    "#1002401/getDispatcher",
22  
    "#1002400/evalWith",
23  
    "#1002399/jparse",
24  
    "#1002397/unidiff",
25  
    "#1002395/eval",
26  
    "#1002393/logStructure",
27  
    "#1002392/makeEleuSet",
28  
    "#1002389/selfLink",
29  
    "#1002388/hiddenFields",
30  
    "#1002377/a",
31  
    "#1002376/askSelf",
32  
    "#1002374/loadSnippetThroughBot",
33  
    "#1002359/getSlackTS",
34  
    "#1002354/unixDay",
35  
    "#1002348/nempty",
36  
    "#1002346/secretProgramFile",
37  
    "#1002345/serveHttps",
38  
    "#1002334/editSnippet",
39  
    "#1002336/createSnippet",
40  
    "#1002335/eleuSnippetPassword",
41  
    "#1002333/hquery",
42  
    "#1002322/compileBot_clearCache",
43  
    "#1002320/getMemoryInfo",
44  
    "#1002319/filterByField",
45  
    "#1002318/collectField",
46  
    "#1002316/findByField",
47  
    "#1002315/synchroHashSet",
48  
    "#1002309/findContainerTag",
49  
    "#1002305/removeLast",
50  
    "#1002303/reverseLookup",
51  
    "#1002300/setAdd",
52  
    "#1002292/mergeBracketThingies",
53  
    "#1002291/unbracket",
54  
    "#1002290/isBracketedID",
55  
    "#1002288/printStackTrace",
56  
    "#1002287/lithashset",
57  
    "#1002285/assertIdentifier",
58  
    "#1002284/splitAtJavaToken",
59  
    "#1002283/printF",
60  
    "#1002276/callOtherBot",
61  
    "#1002275/getBot",
62  
    "#1002274/getFromOtherBot",
63  
    "#1002273/getActualURI",
64  
    "#1002271/splitByJavaToken",
65  
    "#1002270/getMainBot",
66  
    "#1002269/getUserName",
67  
    "#1002266/getLastTiming",
68  
    "#1002263/saveTiming",
69  
    "#1002248/getCacheProgramDir",
70  
    "#1002246/hotwire_overBot",
71  
    "#1002244/touchFile",
72  
    "#1002226/logOutput",
73  
    "#1002224/hotwireCached",
74  
    "#1002218/lastN",
75  
    "#1002214/htag",
76  
    "#1002209/getClassName",
77  
    "#1002208/asRuntimeException",
78  
    "#1002207/softFail",
79  
    "#1002206/assertIsIdentifier",
80  
    "#1002204/dir2zip_recurse",
81  
    "#1002202/getNameOfPublicClass",
82  
    "#1002201/hasModifier",
83  
    "#1002200/getJavaModifiers",
84  
    "#1002199/exceptionToUser",
85  
    "#1002198/throwableToUser",
86  
    "#1002197/formatExceptionForUser",
87  
    "#1002196/formatChannelName",
88  
    "#1002195/getHashSet",
89  
    "#1002194/countDistinct",
90  
    "#1002193/devChannelToken",
91  
    "#1002189/restructure",
92  
    "#1002188/mapBasedReplace",
93  
    "#1002186/tokRegionMatchIC",
94  
    "#1002183/multiSlurp",
95  
    "#1002180/hasMethodNamed",
96  
    "#1002179/objectToMap",
97  
    "#1002178/copyFields",
98  
    "#1002177/getAverage",
99  
    "#1002176/round",
100  
    "#1002175/toDouble",
101  
    "#1002172/loadRelpToken",
102  
    "#1002170/slackGetChannelIDs",
103  
    "#1002169/slackSlurp",
104  
    "#1002162/isLinux",
105  
    "#1002151/unixHour",
106  
    "#1002149/removeIgnoreCase",
107  
    "#1002148/containsIgnoreCase",
108  
    "#1002147/splitListBy",
109  
    "#1002146/printFormat",
110  
    "#1002145/randomOne",
111  
    "#1002144/unq",
112  
    "#1002141/htmlQuery",
113  
    "#1002137/snippetImageURL",
114  
    "#1002136/replaceSublist",
115  
    "#1002135/indexOfSubList",
116  
    "#1002133/jreplace",
117  
    "#1002127/quoteAll",
118  
    "#1002126/scanToEndOfInitializer",
119  
    "#1002125/findInitializers",
120  
    "#1002118/getBracketMap",
121  
    "#1002116/liftLast",
122  
    "#1002113/getPlural",
123  
    "#1002112/n",
124  
    "#1002111/getSortedSet",
125  
    "#1002110/dropPrefix",
126  
    "#1002109/isNonNegativeInteger",
127  
    "#1002108/empty",
128  
    "#1002107/lookupPossiblyIgnoreCase",
129  
    "#1002106/endsWithOneOf",
130  
    "#1002104/loadTextFileFromZip",
131  
    "#1002103/listZip",
132  
    "#1002102/loadBinary",
133  
    "#1002099/cppCopy",
134  
    "#1002098/resizeCollection",
135  
    "#1002097/replaceCollection",
136  
    "#1002092/list",
137  
    "#1002091/nullIfEmpty",
138  
    "#1002090/loadSnippetVersion",
139  
    "#1002088/listFilesWithSuffix",
140  
    "#1002086/endsWithIgnoreCase",
141  
    "#1002085/deleteAllFilesWithSuffix",
142  
    "#1002082/isLowerHexString",
143  
    "#1002081/isMD5",
144  
    "#1002080/getSnippetMD5",
145  
    "#1002075/rat",
146  
    "#1002074/filterByMethod",
147  
    "#1002073/joinQuoted",
148  
    "#1002071/trim",
149  
    "#1002066/licensed",
150  
    "#1002062/unixTime",
151  
    "#1002058/emptyToNull",
152  
    "#1002055/replaceToken",
153  
    "#1002051/stdEq",
154  
    "#1002050/allToString",
155  
    "#1002048/dropSuffix",
156  
    "#1002047/bigint",
157  
    "#1002042/keys",
158  
    "#1002041/cmp",
159  
    "#1002040/keyWithBiggestValue",
160  
    "#1002038/dataSize",
161  
    "#1002037/printMemoryInfo",
162  
    "#1002035/toM_double",
163  
    "#1002034/gcAndPrintMemoryInfo",
164  
    "#1002033/random",
165  
    "#1002032/fromUTF8",
166  
    "#1002031/loadTextFileFromZipFile",
167  
    "#1002029/listZipFile",
168  
    "#1002026/toLower",
169  
    "#1002025/load",
170  
    "#1002024/save",
171  
    "#1002022/isEmpty",
172  
    "#1002021/booleanValue",
173  
    "#1002019/allMethodNames",
174  
    "#1002014/mainClass",
175  
    "#1002013/toObjectArray",
176  
    "#1002012/assertNotNull",
177  
    "#1002010/totalLengthOfCodeTokens",
178  
    "#1001998/pythonToJavaTok",
179  
    "#1001996/loadVariableDefinition",
180  
    "#1001995/lookupIgnoreCase",
181  
    "#1001992/str",
182  
    "#1001990/allInjectionsPrettyList",
183  
    "#1001988/padList",
184  
    "#1001986/htmlTable",
185  
    "#1001985/instanceOf",
186  
    "#1001984/dataToTable_makeRow",
187  
    "#1001978/slackGetUserName",
188  
    "#1001975/simpleSpaces",
189  
    "#1001974/smartSet",
190  
    "#1001970/loadLog",
191  
    "#1001967/assertFalse",
192  
    "#1001965/sendToLocalBot_cached",
193  
    "#1001964/newFindBot",
194  
    "#1001963/matchMetaNoStar",
195  
    "#1001960/synchroTreeMap",
196  
    "#1001959/callHtmlMethod",
197  
    "#1001954/getString",
198  
    "#1001952/toM",
199  
    "#1001949/getProgramFile",
200  
    "#1001948/scanLog",
201  
    "#1001946/mathOp",
202  
    "#1001944/matchMeta",
203  
    "#1001941/getNumberOfComputers",
204  
    "#1001940/slackReactionScore",
205  
    "#1001939/printStructure",
206  
    "#1001936/slackReact",
207  
    "#1001935/slackSmile",
208  
    "#1001933/makePostData",
209  
    "#1001931/cleanUp",
210  
    "#1001930/feedbackScore",
211  
    "#1001929/collapseWord",
212  
    "#1001927/diff",
213  
    "#1001926/slackReadChannel",
214  
    "#1001924/count",
215  
    "#1001922/numberLines",
216  
    "#1001921/replaceWords",
217  
    "#1001918/jsonDecodeMap",
218  
    "#1001917/slackGetUserInfo",
219  
    "#1001914/emptyList",
220  
    "#1002310/subList",
221  
    "#1001909/printList",
222  
    "#1001907/slackReadTalkingBots",
223  
    "#1001906/reversedList",
224  
    "#1001902/loadTextFileMandatory",
225  
    "#1001901/loadSecretTextFileMandatory",
226  
    "#1001900/loadSecretTextFile",
227  
    "#1001899/asSet",
228  
    "#1001898/dropWords",
229  
    "#1001896/toLowerCase",
230  
    "#1001895/hasTransitive",
231  
    "#1001894/makeHull",
232  
    "#1001892/isIdentifier",
233  
    "#1001891/format",
234  
    "#1001885/replaceKeywordBlock",
235  
    "#1001880/fill",
236  
    "#1001879/statements",
237  
    "#1001878/indexOf",
238  
    "#1001876/jmatch",
239  
    "#1001875/mousePosition",
240  
    "#1001871/group",
241  
    "#1001870/newObject",
242  
    "#1001866/tagGet",
243  
    "#1001865/neq",
244  
    "#1001863/findTopLevelClass",
245  
    "#1001860/htmlEncode",
246  
    "#1001858/getAuthorOfSignedJava",
247  
    "#1001856/signJavaWithComputerID",
248  
    "#1001855/htmlQuote",
249  
    "#1001854/containsSnippetID",
250  
    "#1001853/substr",
251  
    "#1001852/serve404",
252  
    "#1001851/isString",
253  
    "#1001850/sortFilesByName",
254  
    "#1001843/string",
255  
    "#1001842/match",
256  
    "#1001840/assertEq",
257  
    "#1001838/make",
258  
    "#1001832/magicIndexOfSubList",
259  
    "#1001828/defaultTranslate",
260  
    "#1001827/containsToken",
261  
    "#1001825/onFocusLeave",
262  
    "#1001823/rightAlign",
263  
    "#1001822/isChecked",
264  
    "#1001821/extractMainComponent",
265  
    "#1001820/run",
266  
    "#1001818/includeInMain",
267  
    "#1001817/includesToken",
268  
    "#1001815/tokMainJava",
269  
    "#1001809/findBlock",
270  
    "#1001808/runTranslator",
271  
    "#1001801/askMyself",
272  
    "#1001800/callStaticAnswerMethod",
273  
    "#1001799/safeGet",
274  
    "#1001798/getLine",
275  
    "#1001797/dropFirstLine",
276  
    "#1001796/min",
277  
    "#1001795/arraycopy",
278  
    "#1001793/matchStart",
279  
    "#1001792/makeBot",
280  
    "#1001790/showBigText",
281  
    "#1001788/onEnter",
282  
    "#1001786/htmlBot",
283  
    "#1001783/hgrid",
284  
    "#1001781/sendToBotQuietly",
285  
    "#1001779/runInAllVMs",  
286  
    "#1001780/getOutputOfInjections",
287  
    "#1001777/allSwingComponents",
288  
    "#1001774/vgrid",
289  
    "#1001773/smartAdd",
290  
    "#1001771/fullBotScan",
291  
    "#1001767/getBotAddress",
292  
    "#1001765/defineBot",
293  
    "#1001763/search",
294  
    "#1001762/getBotNameFromSnippet",
295  
    "#1001761/stack",
296  
    "#1001757/isMap",
297  
    "#1001755/getSelectedLineAsMap",
298  
    "#1001754/getSelectedLine",
299  
    "#1001753/getFocusOwner",
300  
    "#1001751/containsBot",
301  
    "#1001750/startBotSeparateVM",
302  
    "#1001748/startBotVM",
303  
    "#1001746/updateLoop",
304  
    "#1001742/getInjectionID",
305  
    "#1001741/or",
306  
    "#1001740/talkToAllOtherVMs",
307  
    "#1001739/getVMPort",
308  
    "#1001737/makeSilentAndroid",
309  
    "#1001736/addToMultiPortSilent",
310  
    "#1001733/setFrameTitle",
311  
    "#1001730/currentThread",
312  
    "#1001727/directorySize",
313  
    "#1001726/numFiles",
314  
    "#1001725/findAllFiles",
315  
    "#1001723/allDataDirectories",
316  
    "#1001721/safeUnstructure",
317  
    "#1001719/injectToBotVM",
318  
    "#1001717/newTypeWriterTextArea",
319  
    "#1001715/shallowClone",
320  
    "#1001713/hasBot",
321  
    "#1001712/javaxBot",
322  
    "#1001711/injectTo",
323  
    "#1001707/getSelectedColumn",
324  
    "#1001701/makeResponder",
325  
    "#1001696/parseDouble",
326  
    "#1001695/litmap",
327  
    "#1001693/removeToken",
328  
    "#1001686/equalsIgnoreCase",
329  
    "#1001681/sendTestResult",
330  
    "#1001678/talkToSubBot",
331  
    "#1001677/startMultiPort",
332  
    "#1001676/addToMultiPort",
333  
    "#1001674/getMultiPorts",
334  
    "#1001673/getVMID",
335  
    "#1001670/getFileSize",
336  
    "#1001666/toK",
337  
    "#1001664/max",
338  
    "#1001663/rsubstring",
339  
    "#1001660/convertToDefaultListModel",
340  
    "#1001659/allSwingComponentsOfType",
341  
    "#1001658/sendToCentralBotWithTimeout",
342  
    "#1001657/loadPageWithTimeout",
343  
    "#1001656/centralServerUp",
344  
    "#1001654/sendToCentralBot",
345  
    "#1001653/remoteBotsUp",
346  
    "#1001650/serveHTML",
347  
    "#1001648/serveHttp",
348  
    "#1001645/addToConsole",
349  
    "#1001644/createJavaProgram",
350  
    "#1001643/findSnippet",
351  
    "#1001642/findTranslators",
352  
    "#1001641/hasCodeTokens",
353  
    "#1001629/weakref",
354  
    "#1001637/findAll",
355  
    "#1001636/scanForComponents",
356  
    "#1001635/allFrames",
357  
    "#1001634/cloneList",
358  
    "#1001633/waitUntilInjectionsDone",
359  
    "#1001632/injectToAll",
360  
    "#1001631/createTempProgram",
361  
    "#1001627/makeList",
362  
    "#1001626/showSideBySide",
363  
    "#1001625/wrap",
364  
    "#1001624/swingUpdate",
365  
    "#1001623/showFrame",
366  
    "#1001621/getClassBody",
367  
    "#1001619/innerClasses",
368  
    "#1001617/showTable",
369  
    "#1001613/dataToTable",
370  
    "#1001606/include",
371  
    "#1001598/assertTrue",
372  
    "#1001593/tunnelToRemoteBot",
373  
    "#1001586/renameVM",
374  
    "#1001584/unsign",
375  
    "#1001583/isSignedWithKey",
376  
    "#1001580/nlTok",
377  
    "#1001577/suckHumans",
378  
    "#1001576/suckWebChat",
379  
    "#1001574/iterateOverBlockingQueue",
380  
    "#1001573/uploadProgramOutput",
381  
    "#1001571/getProgramOutput",
382  
    "#1001568/getPublicKeyOfComputer",
383  
    "#1001567/getAuthorOfSignedText",
384  
    "#1001566/signWithComputerID",
385  
    "#1001565/getMySignatureForText",
386  
    "#1001564/getMyPublicKey",
387  
    "#1001563/isRandomID",
388  
    "#1001562/isComputerID",
389  
    "#1001560/fullTrim",
390  
    "#1001557/fromUtf8",
391  
    "#1001555/getTimeToHEAD",
392  
    "#1001553/dropPrefixMandatory",
393  
    "#1001552/base64decode",
394  
    "#1001549/getSecretProgramDir",
395  
    "#1001544/sayInWebChat",
396  
    "#1001543/ltrim",
397  
    "#1001540/stdcompare",
398  
    "#1001539/sortByIndex",
399  
    "#1001538/sortByFirstColumn",
400  
    "#1001537/setTableModel",
401  
    "#1001536/firstPartOfHelloString",
402  
    "#1001530/structureOrText",
403  
    "#1001529/addSaveButton",
404  
    "#1001528/addToWindow",
405  
    "#1001527/addCommentToWindow",
406  
    "#1001523/showColoredText",
407  
    "#1001520/eqic",
408  
    "#1001504/makeExecutable",
409  
    "#1001499/startsWithIgnoreCase",
410  
    "#1001498/isType",
411  
    "#1001497/parseBoolean",
412  
    "#1001491/getOS",
413  
    "#1001488/tokenLineNr",
414  
    "#1001484/autoFrameTitle",
415  
    "#1001483/sendToAllVMs",
416  
    "#1001481/programsRunningInAllVMs",
417  
    "#1001480/programsRunningInVM",
418  
    "#1001479/canHyperMove",
419  
    "#1001478/fastNohupJavax",
420  
    "#1001477/runInNewThread",
421  
    "#1001473/isVMRunningProgram",
422  
    "#1001470/programIsRunning",
423  
    "#1001466/hyperMoveAnother",
424  
    "#1001463/isMac",
425  
    "#1001462/userDir",
426  
    "#1001461/listDirs",
427  
    "#1001459/listFiles",
428  
    "#1001458/showList",
429  
    "#1001456/logQuoted",
430  
    "#1001454/tempDir",
431  
    "#1001450/moveDown",
432  
    "#1001445/toGrayScale",
433  
    "#1001439/getFrameTitle",
434  
    "#1001438/fillTableWithStrings",
435  
    "#1001436/talkToAllVMs",
436  
    "#1001433/getScreenSize",
437  
    "#1001432/moveToTopRightCorner",
438  
    "#1001431/fillListWithStrings",
439  
    "#1001429/getNumberOfJavaXVMs",
440  
    "#1001428/getFrame",
441  
    "#1001422/getBotPort",
442  
    "#1001419/verifyMD5",
443  
    "#1001518/l",
444  
    "#1001516/len",
445  
    "#1001418/length",
446  
    "#1001411/isURL",
447  
    "#1001407/getInstance",
448  
    "#1001400/sendToAll",
449  
    "#1001399/talkToAll",
450  
    "#1001396/saveProgramTextFile",
451  
    "#1001395/getSupportedQuestions",
452  
    "#1001393/getSnippetFromBossBot",
453  
    "#1001394/boss",
454  
    "#1001381/startPreSpunVM",
455  
    "#1001380/countPreSpunVMs",
456  
    "#1001378/countBotsNamed",
457  
    "#1001375/injectable",
458  
    "#1001373/listAllFrames",
459  
    "#1001372/nohupJavax",
460  
    "#1001368/hyperMove",
461  
    "#1001366/waitForBotStartUp",
462  
    "#1001365/startBot",
463  
    "#1001352/restartProgramID",
464  
    "#1001347/shootScreen",
465  
    "#1001346/showImage",
466  
    "#1001340/zipBot",
467  
    "#1001338/backupAndCleanProgramDir",
468  
    "#1001336/stream2file",
469  
    "#1001335/zip2dir",
470  
    "#1001333/sameSnippetID",
471  
    "#1001332/killPortAndWait",
472  
    "#1001331/killProgramID",
473  
    "#1001330/getListOfRunningProgramIDs",
474  
    "#1001329/sendToLocalBot",
475  
    "#1001326/programID",
476  
    "#1001323/parseBool",
477  
    "#1001322/sendToLocalBotOpt",
478  
    "#1001320/myVMPort",
479  
    "#1001315/grep",
480  
    "#1001314/processIDExists",
481  
    "#1001313/swingLater",
482  
    "#1001308/centerFrame",
483  
    "#1001301/androidSayInGerman",
484  
    "#1001299/standardQuery",
485  
    "#1001299/additionalQuery",
486  
    "#1001295/oneOf",
487  
    "#1001292/androidLater",
488  
    "#1001291/androidShowFullScreenColor",
489  
    "#1001282/androidShow",
490  
    "#1001281/androidShowFullScreenImage",
491  
    "#1001277/androidUnmuteAudio",
492  
    "#1001275/androidMuteAudio",
493  
    "#1001273/makeSpeechCereproc",
494  
    "#1001271/androidPlayMp3",
495  
    "#1001267/playMp3",
496  
    "#1001266/getProgramDir",
497  
    "#1001263/htmlencode",
498  
    "#1001262/isTag",
499  
    "#1001261/gunzipBinaryData",
500  
    "#1001256/makeAndroid3",
501  
    "#1001255/findStaticMethod",
502  
    "#1001251/sendToRemoteBot",
503  
    "#1001250/format3",
504  
    "#1001246/parseLong",
505  
    "#1001243/joinRest",
506  
    "#1001241/concatLists",
507  
    "#1001239/quote",
508  
    "#1001238/getSnippetMD5",
509  
    "#1001235/toStringArray",
510  
    "#1001234/find2",
511  
    "#1001232/find3",
512  
    "#1001226/contactMyself",
513  
    "#1001219/getAutoReportToChat",
514  
    "#1001217/findMandatoryBot",
515  
    "#1001208/makeTempDir",
516  
    "#1001204/findMainClass",
517  
    "#1001303/findBot",
518  
    "#1001199/callOpt",
519  
    "#1001198/sentenceEq",
520  
    "#1001194/quoteIfNotIdentifier",
521  
    "#1001193/findCodeTokens",
522  
    "#1001190/synchroTreeSet",
523  
    "#1001189/readLineHidden",
524  
    "#1001185/getChatHistory",
525  
    "#1001184/getMyComputerID",
526  
    "#1001183/getComputerID",
527  
    "#1001181/getMyIPs",
528  
    "#1001180/pcall",
529  
    "#1001179/detectGateways",
530  
    "#1001174/dir2zip",
531  
    "#1001173/zip2zip",
532  
    "#1001172/renameToken",
533  
    "#1001166/toUtf8",
534  
    "#1001165/findEndOfBracketPart",
535  
    "#1001162/getClassDeclarationName",
536  
    "#1001161/getVarDeclarationName",
537  
    "#1001160/findEndOfTypeArgs",
538  
    "#1001159/allVariableDeclarations",
539  
    "#1001158/clearAllTokens",
540  
    "#1001157/allClasses",
541  
    "#1001156/leftScanModifiers",
542  
    "#1001154/getServerTranspiled",
543  
    "#1001152/getMainClass",
544  
    "#1001151/getInnerException",
545  
    "#1001147/telnetFromConsoleTo",
546  
    "#1001144/latestInstalledJavaX",
547  
    "#1001142/showText_fontSize",
548  
    "#1001141/firstToUpper",
549  
    "#1001139/quickBotScan",
550  
    "#1001138/isJavaIdentifier",
551  
    "#1001137/findClass",
552  
    "#1001136/nuObject",
553  
    "#1001135/isQuoted",
554  
    "#1001127/publicCommOn",
555  
    "#1001124/record",
556  
    "#1001123/listFields",
557  
    "#1001122/getPID",
558  
    "#1001116/sleepSeconds",
559  
    "#1001114/checkIfHttpServerIsOnline",
560  
    "#1001112/getProgramID",
561  
    "#1001111/codeTokensOnly",
562  
    "#1001106/showText",
563  
    "#1001066/startDialogServerOnPortAbove",
564  
    "#1001233/parse3",
565  
    "#1001104/match3",
566  
    "#1001103/makeAndroid",
567  
    "#1001103/makeAndroid2",
568  
    "#1001103/makeAndroidNoConsole",
569  
    "#1001101/getOpt",
570  
    "#1001087/answerQuestionsOnPort",
571  
    "#1001087/answerQuestionsOnPortAndConsole",
572  
    "#1001084/reverseString",
573  
    "#1001076/talkTo",
574  
    "#1001074/readLine",
575  
    "#1001070/sendKillSwitch",
576  
    "#1001068/synchroList",
577  
    "#1001067/appendToFile",
578  
    "#1001066/startDialogServerIfPortAvailable",
579  
    "#1001066/startDialogServer",
580  
    "#1001355/unstructure",
581  
    "#1001058/deleteDirectory",
582  
    "#1001056/interpret",
583  
    "#1001055/assertEquals",
584  
    "#1001052/programDir",
585  
    "#1001051/listProgramDir",
586  
    "#1001044/saveLocally",
587  
    "#1001043/readLocally",
588  
    "#1001047/litlist",
589  
    "#1001046/findEndOfBlock",
590  
    "#1001039/quickProgramScan",
591  
    "#1001038/last",
592  
    "#1001031/onFrameClose",
593  
    "#1001030/handleEscapeKey",
594  
    "#1001029/printVMSize",
595  
    "#1001026/eq",
596  
    "#1001024/internAll",
597  
    "#1001017/matcher",
598  
    "#1001012/getSnippetTitle",
599  
    "#1001008/roundUpTo",
600  
    "#1001007/safeSubstring",
601  
    "#1001032/clone",
602  
    "#1000999/asList",
603  
    "#1000998/md5",
604  
    "#1000994/loadFont",
605  
    "#1000984/makeServerSocket",
606  
    "#1000968/loadLibrary",
607  
    "#1000962/suckFromChat",
608  
    "#1000962/suckFromChatNoHistory",
609  
    "#1000961/isInstanceX",
610  
    "#1000960/listUserThreadsWithStackTraces",
611  
    "#1000959/listThreads",
612  
    "#1000955/autoReportToChatOff",
613  
    "#1000980/isAutoReportToChatOn",
614  
    "#1000952/die",
615  
    "#1000950/autoReportToChat",
616  
    "#1000946/getJavaX",
617  
    "#1000942/urldecode",
618  
    "#1000940/exit",
619  
    "#1000938/synchronizedList",
620  
    "#1000937/chatSend",
621  
    "#1000935/reportToChat",
622  
    "#1000932/waitForChatServer",
623  
    "#1000931/portIsBound",
624  
    "#1000929/startChatServerIfNotUp",
625  
    "#1000921/makeFrame",
626  
    "#1000918/smartJoin",
627  
    "#1000916/slte",
628  
    "#1000909/killMyself",
629  
    "#1000908/runCmdWithTerminal",
630  
    "#1000906/htmlgetparams",
631  
    "#1000905/error",
632  
    "#1000892/dropAllTags",
633  
    "#1000891/setOpt",
634  
    "#1000877/installTimer",
635  
    "#1000869/randomID",
636  
    "#1000862/tagIs",
637  
    "#1000864/isAndroid",
638  
    "#1000861/isUpperCase",
639  
    "#1000860/equals",
640  
    "#1000856/htmlunquote",
641  
    "#1000855/backtick",
642  
    "#1000853/backtickJavax",
643  
    "#1000851/linkFrom",
644  
    "#1000848/splitScript",
645  
    "#1000844/copyStream",
646  
    "#1000843/copyFile",
647  
    "#1000841/printMyIPs",
648  
    "#1000838/classicNohupJavax",
649  
    "#1000835/topToBottomPanel",
650  
    "#1000832/bashQuote",
651  
    "#1000830/winQuote",
652  
    "#1000829/isWindows",
653  
    "#1000828/nohup",
654  
    "#1000827/mkdirsForFile",
655  
    "#1000826/withLabel",
656  
    "#1000814/dropPunctuation",
657  
    "#1000813/tokensToLowerCase",
658  
    "#1000812/match2",
659  
    "#1000811/parseInt",
660  
    "#1000808/isPortOpen",
661  
    "#1000983/installHelloMessage",
662  
    "#1000801/now",
663  
    "#1000800/startTiming",
664  
    "#1000800/stopTiming",
665  
    "#1000799/findField",
666  
    "#1000798/dropFirst",
667  
    "#1000793/htmldecode",
668  
    "#1000784/sort",
669  
    "#1000779/wakeUp",
670  
    "#1000769/javaTokPlusPeriod",
671  
    "#1000754/uploadData",
672  
    "#1000754/uploadDataSuperHigh",
673  
    "#1000750/base64encode",
674  
    "#1000744/loadBinaryFile",
675  
    "#1000741/uploadImage",
676  
    "#2000554/binaryUrlencode",
677  
    "#1000737/kevin", // hmm, this has so many library dependencies
678  
    "#1000736/sleep",
679  
    "#1000734/find",
680  
    "#1000735/getBattery",
681  
    "#1000729/copyStream",
682  
    "#1000705/drop",
683  
    "#1000719/dropLast",
684  
    "#1000692/randomLetters",
685  
    "#1000689/processSilent",
686  
    "#1000688/javaTok",
687  
    "#1000685/matchTokensList",
688  
    "#1000679/gunzipToText",
689  
    "#1000677/loadBinarySnippet",
690  
    "#1000676/getGlobalCache",
691  
    "#1000675/loadDataSnippetImpl",
692  
    "#1000672/indent",
693  
    "#1000670/htmlcoarsetok",
694  
    "#1000668/callMain",
695  
    "#1000664/mysqlUnquote",
696  
    "#1000663/processDebug",
697  
    "#1000661/process",
698  
    "#1000658/print",
699  
    "#1001042/saveTextFile",
700  
    "#1001049/loadTextFile",
701  
    "#1001108/readTextFile",
702  
    "#1002380/loadMainJava",
703  
    "#1001807/saveMainJava",
704  
    "#1001950/toLines",
705  
    "#2000478/toLinesTrim",
706  
    "#1000641/toLinesFullTrim",
707  
    "#1002398/fromLines",
708  
    "#1000997/loadSnippet",
709  
    "#1000535/loadSnippetAndTitle",
710  
    "#1000520/isSnippetID",
711  
    "#2000331/makeJavaStringLiteral",
712  
    "#1000893/popup",
713  
    "#2000367/popupError",
714  
    "#2000368/exitOnFrameClose",
715  
    "#2000378/commonPrefix",
716  
    "#2000379/commonSuffix",
717  
    "#1001735/unquote", // formerly, 471
718  
    "#2000444/cncToLines",
719  
    "#2000460/bytesToHex",
720  
    "#1000837/loadBinaryPage",
721  
    "#1001113/saveBinaryFile",
722  
    "#1001561/rtrim",
723  
    "#1000879/loadPage",
724  
    "#1000879/loadPageSilently",
725  
    "#2000489/matchAll",
726  
    "#2000490/debugOn",
727  
    "#2000491/getClass",
728  
    "#1000630/call",
729  
    "#1000963/hotwire",
730  
    "#1000415/set", // we should really improve the function invocation detection, this is probably included too often...
731  
    "#1000441/setWidgetColor",
732  
    "#1000442/goToHomeScreen",
733  
    "#2000502/patternQuote",
734  
    "#1000810/join",
735  
    "#1002222/repeat",
736  
    "#1001913/unnull",
737  
    "#2000510/formatDouble",
738  
    "#1001958/loadImage", // needs the image classes
739  
    "#1000659/fail",
740  
    "#1000524/structureSize",
741  
    "#1000565/doPost",
742  
    "#1000566/ntUpload",
743  
    "#1000567/isInteger",
744  
    "#1000568/computerID",
745  
    "#1000569/userHome", // TODO: adapt for Android!
746  
    "#1000570/makeRandomID",
747  
    "#1000580/urlencode",
748  
    "#1000581/findSnippetsNamed",
749  
    "#1000582/findSnippetNamed",
750  
    "#1000588/javaQuote",
751  
    "#1000593/jsonTok",
752  
    "#1000620/jsonDecode",
753  
    "#1000595/compileAndLoadMainClass",
754  
    "#1000598/isSafeCompilable",
755  
    "#1000601/updateMyCommentOnSnippet",
756  
    "#1000608/moveImportsUp",
757  
    "#1000609/updateWidgetWithText",
758  
    "#1000619/get",
759  
    "#1000619/get_raw",
760  
    "#1000621/getSnippetComments",
761  
    "#1000628/first",
762  
    "#1001386/structure", // v2
763  
    "#1000644/shorten",
764  
    "#1000646/isJavaxSafe",
765  
    "#1000888/isJavaCompilable",
766  
    "#1000647/mysqlTok",
767  
    "#1000657/indexOfIgnoreCase",
768  
    "#1000708/parseSnippetID",
769  
    "#1000709/formatSnippetID",
770  
    "#1000711/shortenSnippetID",
771  
    "#1000716/confused",
772  
    "#1000717/todo",
773  
    "#1000889/getStackTrace"
774  
  };
775  
  
776  
  public static void main(String[] args) throws IOException {
777  
    long startTime = now();
778  
    String s = loadMainJava();
779  
    
780  
    Map<String, String> sf = new HashMap();
781  
    for (String x : standardFunctions) {
782  
      String[] f = x.split("/");
783  
      sf.put(f[1], f[0]);
784  
    }
785  
    
786  
    for (int i = 0; ; i++) {
787  
      Set<String> defd = new HashSet(findFunctions(s));
788  
      List<String> tok = javaTok(s);
789  
      
790  
      // changes tok
791  
      List<String> invocations = findInvocations(tok, sf);
792  
      s = join(tok);
793  
      
794  
      List<String> needed = diff(invocations, defd);
795  
      print("Functions needed: " + needed);
796  
      if (needed.isEmpty())
797  
        break;
798  
    
799  
      for (String x : needed) {
800  
        String id = sf.get(x);
801  
        System.out.println("Adding function: " + x + " (" + id + ")");
802  
        s = addFunction(s, id);
803  
      }
804  
      
805  
      defd = new HashSet(findFunctions(s));
806  
      for (String x : needed)
807  
        if (!defd.contains(x))
808  
          fail("Function not defined properly: " + x);
809  
      System.out.println("Iteration " + (i+2));
810  
      if (i >= 1000) fail("Too many iterations");
811  
    }
812  
    saveMainJava(s);
813  
    print("629: " + (now()-startTime) + " ms");
814  
  }
815  
  
816  
  static List<String> findInvocations(List<String> tok, Map<String, String> sf) {
817  
    int i;
818  
    List<String> l = new ArrayList();
819  
    while ((i = jfind(tok, "please include function *.")) >= 0) {
820  
      String fname = tok.get(i+6);
821  
      l.add(fname);
822  
      clearAllTokens(tok.subList(i, i+10));
823  
    }
824  
825  
    boolean result = false;
826  
    for (i = 1; i+2 < tok.size(); i += 2) {
827  
      String f = tok.get(i);
828  
      if (!isIdentifier(f)) continue;
829  
      if ((i == 1 || !tok.get(i-2).equals(".")) && tok.get(i+2).equals("(")) {
830  
        boolean inSF = sf.containsKey(f);
831  
        if (debug)
832  
          print("Possible invocation: " + f + ", inSF: " + inSF);
833  
        if (inSF)
834  
          l.add(f);
835  
      }
836  
    }
837  
    
838  
    return l;
839  
  }
840  
  
841  
  static boolean substringIs(String s, int i, String pat) {
842  
    return i >= 0 && i+pat.length() < s.length() && s.substring(i, i+pat.length()).equals(pat);
843  
  }
844  
  
845  
  // OK, this should be fast enough.
846  
  static List<String> findFunctions(String src) {
847  
    int idx = src.indexOf("main {"); // yes it's a hack...
848  
    if (idx >= 0) src = src.substring(idx);
849  
    
850  
    Pattern pattern = Pattern.compile("static[^={]*\\s+(\\w+)\\(");
851  
    
852  
    //System.out.println("Scanning for functions");
853  
    List<String> functions = new ArrayList<String>();
854  
    for (String line : toLines(src)) {
855  
      Matcher matcher = pattern.matcher(line);
856  
      if (matcher.find()) {
857  
        String f = matcher.group(1);
858  
        functions.add(f);
859  
        //System.out.println("Function found: " + f);
860  
      }
861  
    }
862  
    return functions;
863  
  }
864  
  
865  
  public static String addFunction(String s, String fID) throws IOException {
866  
    int i = s.lastIndexOf('}');
867  
    String function = loadSnippet(fID);
868  
    return s.substring(0, i) + "\n" + function +"\n" + s.substring(i);
869  
  }
870  
  
871  
  public static List<String> toLines(String s) {
872  
    List<String> lines = new ArrayList<String>();
873  
    int start = 0;
874  
    while (true) {
875  
      int i = toLines_nextLineBreak(s, start);
876  
      if (i < 0) {
877  
        if (s.length() > start) lines.add(s.substring(start));
878  
        break;
879  
      }
880  
881  
      lines.add(s.substring(start, i));
882  
      if (s.charAt(i) == '\r' && i+1 < s.length() && s.charAt(i+1) == '\n')
883  
        i += 2;
884  
      else
885  
        ++i;
886  
887  
      start = i;
888  
    }
889  
    return lines;
890  
  }
891  
892  
  private static int toLines_nextLineBreak(String s, int start) {
893  
    for (int i = start; i < s.length(); i++) {
894  
      char c = s.charAt(i);
895  
      if (c == '\r' || c == '\n')
896  
        return i;
897  
    }
898  
    return -1;
899  
  }
900  
  
901  
  static String mainJava;
902  
  
903  
  static String loadMainJava() throws IOException {
904  
    if (mainJava != null) return mainJava;
905  
    return loadTextFile("input/main.java", "");
906  
  }
907  
908  
  static void saveMainJava(String s) throws IOException {
909  
    if (mainJava != null)
910  
      mainJava = s;
911  
    else
912  
      saveTextFile("output/main.java", s);
913  
  }
914  
  
915  
  static void saveMainJava(List<String> tok) throws IOException {
916  
    saveMainJava(join(tok));
917  
  }
918  
  
919  
  static String charsetForTextFiles = "UTF-8";
920  
  
921  
  public static String loadTextFile(String fileName, String defaultContents) throws IOException {
922  
    if (!new File(fileName).exists())
923  
      return defaultContents;
924  
925  
    FileInputStream fileInputStream = new FileInputStream(fileName);
926  
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles);
927  
    return loadTextFile(inputStreamReader);
928  
  }
929  
930  
  public static String loadTextFile(Reader reader) throws IOException {
931  
    StringBuilder builder = new StringBuilder();
932  
    try {
933  
      BufferedReader bufferedReader = new BufferedReader(reader);
934  
      String line;
935  
      while ((line = bufferedReader.readLine()) != null)
936  
        builder.append(line).append('\n');
937  
    } finally {
938  
      reader.close();
939  
    }
940  
    return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1);
941  
  }
942  
  
943  
  /** writes safely (to temp file, then rename) */
944  
  public static void saveTextFile(String fileName, String contents) throws IOException {
945  
    File file = new File(fileName);
946  
    File parentFile = file.getParentFile();
947  
    if (parentFile != null)
948  
      parentFile.mkdirs();
949  
    String tempFileName = fileName + "_temp";
950  
    FileOutputStream fileOutputStream = new FileOutputStream(tempFileName);
951  
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charsetForTextFiles);
952  
    PrintWriter printWriter = new PrintWriter(outputStreamWriter);
953  
    printWriter.print(contents);
954  
    printWriter.close();
955  
    if (file.exists() && !file.delete())
956  
      throw new IOException("Can't delete " + fileName);
957  
958  
    if (!new File(tempFileName).renameTo(file))
959  
      throw new IOException("Can't rename " + tempFileName + " to " + fileName);
960  
  }  
961  
  
962  
  public static String loadSnippet(String snippetID) throws IOException {
963  
    return loadSnippet(snippetID, preferCached);
964  
  }
965  
  
966  
 public static String loadSnippet(String snippetID, boolean preferCached) throws IOException {
967  
    return loadSnippet(parseSnippetID(snippetID), preferCached);
968  
  }
969  
970  
  public static long parseSnippetID(String snippetID) {
971  
    return Long.parseLong(shortenSnippetID(snippetID));
972  
  }
973  
974  
  private static String shortenSnippetID(String snippetID) {
975  
    if (snippetID.startsWith("#"))
976  
      snippetID = snippetID.substring(1);
977  
    String httpBlaBla = "http://tinybrain.de/";
978  
    if (snippetID.startsWith(httpBlaBla))
979  
      snippetID = snippetID.substring(httpBlaBla.length());
980  
    return snippetID;
981  
  }
982  
983  
  public static boolean isSnippetID(String snippetID) {
984  
    snippetID = shortenSnippetID(snippetID);
985  
    return isInteger(snippetID) && Long.parseLong(snippetID) != 0;
986  
  }
987  
988  
  public static boolean isInteger(String s) {
989  
    return Pattern.matches("\\-?\\d+", s);
990  
  }
991  
  
992  
  static boolean preferCached = false;
993  
  
994  
  public static String loadSnippet(long snippetID) throws IOException {
995  
    return loadSnippet(snippetID, preferCached);
996  
  }
997  
998  
  public static String loadSnippet(long snippetID, boolean preferCached) throws IOException {
999  
    if (preferCached) {
1000  
      initSnippetCache();
1001  
      String text = DiskSnippetCache_get(snippetID);
1002  
      if (text != null)
1003  
        return text;
1004  
    }
1005  
1006  
    String text;
1007  
    try {
1008  
      URL url = new URL("http://tinybrain.de:8080/getraw.php?id=" + snippetID);
1009  
      text = loadPage(url);
1010  
    } catch (FileNotFoundException e) {
1011  
      throw new IOException("Snippet #" + snippetID + " not found or not public");
1012  
    }
1013  
1014  
    try {
1015  
      initSnippetCache();
1016  
      DiskSnippetCache_put(snippetID, text);
1017  
    } catch (IOException e) {
1018  
      System.err.println("Minor warning: Couldn't save snippet to cache ("  + DiskSnippetCache_getDir() + ")");
1019  
    }
1020  
1021  
    return text;
1022  
  }
1023  
1024  
  private static String loadPage(URL url) throws IOException {
1025  
    System.out.println("Loading: " + url.toExternalForm());
1026  
    URLConnection con = url.openConnection();
1027  
    return loadPage(con, url);
1028  
  }
1029  
1030  
  public static String loadPage(URLConnection con, URL url) throws IOException {
1031  
    String contentType = con.getContentType();
1032  
    if (contentType == null)
1033  
      throw new IOException("Page could not be read: " + url);
1034  
    //Log.info("Content-Type: " + contentType);
1035  
    String charset = guessCharset(contentType);
1036  
    Reader r = new InputStreamReader(con.getInputStream(), charset);
1037  
    StringBuilder buf = new StringBuilder();
1038  
    while (true) {
1039  
      int ch = r.read();
1040  
      if (ch < 0)
1041  
        break;
1042  
      //Log.info("Chars read: " + buf.length());
1043  
      buf.append((char) ch);
1044  
    }
1045  
    return buf.toString();
1046  
  }
1047  
1048  
  public static String guessCharset(String contentType) {
1049  
    Pattern p = Pattern.compile("text/html;\\s+charset=([^\\s]+)\\s*");
1050  
    Matcher m = p.matcher(contentType);
1051  
    /* If Content-Type doesn't match this pre-conception, choose default and hope for the best. */
1052  
    return m.matches() ? m.group(1) : "ISO-8859-1";
1053  
  }
1054  
  
1055  
  static File DiskSnippetCache_dir;
1056  
1057  
  public static void initDiskSnippetCache(File dir) {
1058  
    DiskSnippetCache_dir = dir;
1059  
    dir.mkdirs();
1060  
  }
1061  
1062  
  public static synchronized String DiskSnippetCache_get(long snippetID) throws IOException {
1063  
    return loadTextFile(DiskSnippetCache_getFile(snippetID).getPath(), null);
1064  
  }
1065  
1066  
  private static File DiskSnippetCache_getFile(long snippetID) {
1067  
    return new File(DiskSnippetCache_dir, "" + snippetID);
1068  
  }
1069  
1070  
  public static synchronized void DiskSnippetCache_put(long snippetID, String snippet) throws IOException {
1071  
    saveTextFile(DiskSnippetCache_getFile(snippetID).getPath(), snippet);
1072  
  }
1073  
1074  
  public static File DiskSnippetCache_getDir() {
1075  
    return DiskSnippetCache_dir;
1076  
  }
1077  
1078  
  public static void initSnippetCache() {
1079  
    if (DiskSnippetCache_dir == null)
1080  
      initDiskSnippetCache(new File(System.getProperty("user.home"), ".tinybrain/snippet-cache"));
1081  
  }
1082  
  
1083  
  static RuntimeException fail() {
1084  
    throw new RuntimeException("fail");
1085  
  }
1086  
  
1087  
  static RuntimeException fail(Object msg) {
1088  
    throw new RuntimeException(String.valueOf(msg));
1089  
  }
1090  
  
1091  
// replacement for class JavaTok
1092  
// maybe incomplete, might want to add floating point numbers
1093  
// todo also: extended multi-line strings
1094  
1095  
static List<String> javaTok(String s) {
1096  
  List<String> tok = new ArrayList<String>();
1097  
  int l = s.length();
1098  
  
1099  
  int i = 0;
1100  
  while (i < l) {
1101  
    int j = i;
1102  
    char c; String cc;
1103  
    
1104  
    // scan for whitespace
1105  
    while (j < l) {
1106  
      c = s.charAt(j);
1107  
      cc = s.substring(j, Math.min(j+2, l));
1108  
      if (c == ' ' || c == '\t' || c == '\r' || c == '\n')
1109  
        ++j;
1110  
      else if (cc.equals("/*")) {
1111  
        do ++j; while (j < l && !s.substring(j, Math.min(j+2, l)).equals("*/"));
1112  
        j = Math.min(j+2, l);
1113  
      } else if (cc.equals("//")) {
1114  
        do ++j; while (j < l && "\r\n".indexOf(s.charAt(j)) < 0);
1115  
      } else
1116  
        break;
1117  
    }
1118  
    
1119  
    tok.add(s.substring(i, j));
1120  
    i = j;
1121  
    if (i >= l) break;
1122  
    c = s.charAt(i); // cc is not needed in rest of loop body
1123  
    cc = s.substring(i, Math.min(i+2, l));
1124  
1125  
    // scan for non-whitespace
1126  
    if (c == '\'' || c == '"') {
1127  
      char opener = c;
1128  
      ++j;
1129  
      while (j < l) {
1130  
        if (s.charAt(j) == opener) {
1131  
          ++j;
1132  
          break;
1133  
        } else if (s.charAt(j) == '\\' && j+1 < l)
1134  
          j += 2;
1135  
        else
1136  
          ++j;
1137  
      }
1138  
    } else if (Character.isJavaIdentifierStart(c))
1139  
      do ++j; while (j < l && Character.isJavaIdentifierPart(s.charAt(j)));
1140  
    else if (Character.isDigit(c)) {
1141  
      do ++j; while (j < l && Character.isDigit(s.charAt(j)));
1142  
      if (j < l && s.charAt(j) == 'L') ++j; // Long constants like 1L
1143  
    } else if (cc.equals("[[")) {
1144  
      do ++j; while (j+1 < l && !s.substring(j, j+2).equals("]]"));
1145  
      j = Math.min(j+2, l);
1146  
    } else
1147  
      ++j;
1148  
1149  
    tok.add(s.substring(i, j));
1150  
    i = j;
1151  
  }
1152  
  
1153  
  if ((tok.size() % 2) == 0) tok.add("");
1154  
  return tok;
1155  
}
1156  
1157  
static List<String> javaTok(List<String> tok) {
1158  
  return javaTok(join(tok));
1159  
}
1160  
1161  
  public static String join(String glue, Iterable<String> strings) {
1162  
    StringBuilder buf = new StringBuilder();
1163  
    Iterator<String> i = strings.iterator();
1164  
    if (i.hasNext()) {
1165  
      buf.append(i.next());
1166  
      while (i.hasNext())
1167  
        buf.append(glue).append(i.next());
1168  
    }
1169  
    return buf.toString();
1170  
  }
1171  
  
1172  
  public static String join(String glue, String[] strings) {
1173  
    return join(glue, Arrays.asList(strings));
1174  
  }
1175  
  
1176  
  public static String join(Iterable<String> strings) {
1177  
    return join("", strings);
1178  
  }
1179  
  
1180  
  public static String join(String[] strings) {
1181  
    return join("", strings);
1182  
  }  
1183  
  
1184  
// leaves tok properly tokenized
1185  
// returns true iff anything was replaced
1186  
static boolean jreplace(List<String> tok, String in, String out) {
1187  
  return jreplace(tok, in, out, false, true);
1188  
}
1189  
1190  
static boolean jreplace(List<String> tok, String in, String out, boolean ignoreCase, boolean reTok) {
1191  
  List<String> tokin = javaTok(in);
1192  
  replaceSublist(tokin, litlist("<", "", "quoted", "", ">"), litlist("<quoted>"));
1193  
  replaceSublist(tokin, litlist("<", "", "id", "", ">"), litlist("<id>"));
1194  
  
1195  
  boolean anyChange = false;
1196  
  for (int n = 0; n < 10000; n++) {
1197  
    int i = findCodeTokens(tok, ignoreCase, toStringArray(codeTokensOnly(tokin)));
1198  
    if (i < 0) {
1199  
      if (anyChange && reTok)
1200  
        replaceCollection(tok, javaTok(tok));
1201  
      return anyChange;
1202  
    }
1203  
    List<String> subList = tok.subList(i-1, i+l(tokin)-1); // N to N
1204  
    String expansion = jreplace_expandRefs(out, subList);
1205  
    clearAllTokens(tok.subList(i, i+l(tokin)-2)); // code to code
1206  
    tok.set(i, expansion);
1207  
    anyChange = true;
1208  
  }
1209  
  throw fail("woot? 10000!");
1210  
}
1211  
1212  
// "$1" is first code token, "$2" second code token etc.
1213  
static String jreplace_expandRefs(String s, List<String> tokref) {
1214  
  List<String> tok = javaTok(s);
1215  
  for (int i = 1; i < l(tok)-2; i += 2) {
1216  
    if (tok.get(i).startsWith("$") && isInteger(tok.get(i).substring(1))) {
1217  
      String x = tokref.get(-1+parseInt(tok.get(i).substring(1))*2);
1218  
      tok.set(i, x);
1219  
    }
1220  
  }
1221  
  return join(tok);
1222  
}
1223  
1224  
  static void replaceToken(List<String> tok, String in, String out) {
1225  
    renameToken(tok, in, out);
1226  
  }
1227  
1228  
static <A> void replaceCollection(Collection<A> dest, Collection<A> src) {
1229  
  dest.clear();
1230  
  dest.addAll(src);
1231  
}
1232  
1233  
static <A> ArrayList<A> litlist(A... a) {
1234  
  return new ArrayList<A>(Arrays.asList(a));
1235  
}
1236  
 static List<String> codeTokensOnly(List<String> tok) {
1237  
    List<String> l = new ArrayList<String>();
1238  
    for (int i = 1; i < tok.size(); i += 2)
1239  
      l.add(tok.get(i));
1240  
    return l;
1241  
  }
1242  
  
1243  
static void replaceSublist(List<String> l, List<String> x, List<String> y) {
1244  
  int i = 0;
1245  
  while (true) {
1246  
    i = indexOfSubList(l, x, i);
1247  
    if (i < 0) return;
1248  
    
1249  
    // It's inefficient :D
1250  
    for (int j = 0; j < l(x); j++) l.remove(i);
1251  
    l.addAll(i, y);
1252  
    i += l(y);
1253  
  }
1254  
}
1255  
1256  
static int l(Object[] array) {
1257  
  return array == null ? 0 : array.length;
1258  
}
1259  
1260  
static int l(Collection c) {
1261  
  return c == null ? 0 : c.size();
1262  
}
1263  
1264  
static int l(Map m) {
1265  
  return m == null ? 0 : m.size();
1266  
}
1267  
1268  
static int l(String s) {
1269  
  return s == null ? 0 : s.length();
1270  
} 
1271  
1272  
1273  
static int parseInt(String s) {
1274  
  return Integer.parseInt(s);
1275  
}
1276  
1277  
  static void renameToken(List<String> tok, String in, String out) {
1278  
    int renames = 0;
1279  
    for (int i = 1; i < tok.size(); i += 2) {
1280  
      if (tok.get(i).equals(in)) {
1281  
        tok.set(i, out);
1282  
        ++renames;
1283  
      }
1284  
    }
1285  
  }
1286  
  
1287  
1288  
static String[] toStringArray(List<String> list) {
1289  
  return list.toArray(new String[list.size()]);
1290  
}
1291  
1292  
static String[] toStringArray(Object o) {
1293  
  if (o instanceof String[])
1294  
    return (String[]) o;
1295  
  else if (o instanceof List)
1296  
    return toStringArray((List<String>) o);
1297  
  else
1298  
    throw fail("Not a list or array: " + o);
1299  
}
1300  
1301  
  static int findCodeTokens(List<String> tok, String... tokens) {
1302  
    return findCodeTokens(tok, 1, false, tokens);
1303  
  }
1304  
  
1305  
  static int findCodeTokens(List<String> tok, boolean ignoreCase, String... tokens) {
1306  
    return findCodeTokens(tok, 1, ignoreCase, tokens);
1307  
  }
1308  
  
1309  
  static int findCodeTokens(List<String> tok, int startIdx, boolean ignoreCase, String... tokens) {
1310  
    outer: for (int i = startIdx | 1; i+tokens.length*2-2 < tok.size(); i += 2) {
1311  
      for (int j = 0; j < tokens.length; j++) {
1312  
        String p = tokens[j], t = tok.get(i+j*2);
1313  
        boolean match;
1314  
        if (eq(p, "*")) match = true;
1315  
        else if (eq(p, "<quoted>")) match = isQuoted(t);
1316  
        else if (eq(p, "<id>")) match = isIdentifier(t);
1317  
        else match = ignoreCase ? eqic(p, t) : eq(p, t);
1318  
        
1319  
        if (!match)
1320  
          continue outer;
1321  
      }
1322  
      return i;
1323  
    }
1324  
    return -1;
1325  
  }
1326  
  
1327  
  
1328  
  static void clearAllTokens(List<String> tok) {
1329  
    for (int i = 0; i < tok.size(); i++)
1330  
      tok.set(i, "");
1331  
  }
1332  
  
1333  
  static void clearAllTokens(List<String> tok, int i, int j) {
1334  
    for (; i < j; i++)
1335  
      tok.set(i, "");
1336  
  }
1337  
1338  
static boolean isIdentifier(String s) {
1339  
  return isJavaIdentifier(s);
1340  
}
1341  
1342  
static boolean eqic(String a, String b) {
1343  
  if ((a == null) != (b == null)) return false;
1344  
  if (a == null) return true;
1345  
  return a.equalsIgnoreCase(b);
1346  
}
1347  
1348  
1349  
// supports the usual quotings (', ", variable length double brackets)
1350  
static boolean isQuoted(String s) {
1351  
  if (s.startsWith("'") || s.startsWith("\"")) return true;
1352  
  if (!s.startsWith("[")) return false;
1353  
  int i = 1;
1354  
  while (i < s.length() && s.charAt(i) == '=') ++i;
1355  
  return i < s.length() && s.charAt(i) == '[';
1356  
  //return Pattern.compile("^\\[=*\\[").matcher(s).find();
1357  
}
1358  
1359  
1360  
static boolean eq(Object a, Object b) {
1361  
  if (a == null) return b == null;
1362  
  if (a.equals(b)) return true;
1363  
  if (a instanceof BigInteger) {
1364  
    if (b instanceof Integer) return a.equals(BigInteger.valueOf((Integer) b));
1365  
    if (b instanceof Long) return a.equals(BigInteger.valueOf((Long) b));
1366  
  }
1367  
  return false;
1368  
}
1369  
1370  
static boolean isJavaIdentifier(String s) {
1371  
  if (s.length() == 0 || !Character.isJavaIdentifierStart(s.charAt(0)))
1372  
    return false;
1373  
  for (int i = 1; i < s.length(); i++)
1374  
    if (!Character.isJavaIdentifierPart(s.charAt(i)))
1375  
      return false;
1376  
  return true;
1377  
}
1378  
1379  
static <A> int indexOfSubList(List<A> x, List<A> y, int i) {
1380  
  outer: for (; i+l(y) <= l(x); i++) {
1381  
    for (int j = 0; j < l(y); j++)
1382  
      if (neq(x.get(i+j), y.get(j)))
1383  
        continue outer;
1384  
    return i;
1385  
  }
1386  
  return -1;
1387  
}
1388  
1389  
static boolean neq(Object a, Object b) {
1390  
  return !eq(a, b);
1391  
}
1392  
1393  
static long now() {
1394  
  return System.currentTimeMillis();
1395  
}
1396  
1397  
static void print(String s) {
1398  
  System.out.println(s);
1399  
}
1400  
1401  
static List<String> diff(List<String> a, Set<String> b) {
1402  
  List<String> l = new ArrayList();
1403  
  for (String s : a)
1404  
    if (!b.contains(s))
1405  
      l.add(s);
1406  
  return l;
1407  
}
1408  
1409  
static int jfind(List<String> tok, String in) {
1410  
  List<String> tokin = javaTok(in);
1411  
  replaceSublist(tokin, litlist("<", "", "quoted", "", ">"), litlist("<quoted>"));
1412  
  replaceSublist(tokin, litlist("<", "", "id", "", ">"), litlist("<id>"));
1413  
  
1414  
  return findCodeTokens(tok, false, toStringArray(codeTokensOnly(tokin)));
1415  
}
1416  
}

Author comment

Began life as a copy of #629

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1002427
Snippet name: Accellerating 629 (SPIKE)
Eternal ID of this version: #1002427/1
Text MD5: e14b88403c748a9c8ae978c79cb82e54
Author: stefan
Category: javax
Type: JavaX translator
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-02-22 02:47:29
Source code size: 42748 bytes / 1416 lines
Pitched / IR pitched: No / No
Views / Downloads: 857 / 1054
Referenced in: [show references]