static S evalJava_prep(S code) { ret evalJava_prep(code, "calc"); } static S evalJava_prep(S code, S mainName) { ret evalJava_prep(code, mainName, ""); } static new ThreadLocal evalJava_prep_args; static new ThreadLocal evalJava_prep_voidMagic; // set to false to avoid looking up standard function return types static S evalJava_prep(S code, S mainName, S preCode) { LS tok = javaTok(trim(code)); S global = ""; int idx; while ((idx = jfind(tok, "global {")) >= 0) { int j = findEndOfBracketPart(tok, idx+2); global = joinSubList(tok, idx+3, j-1) + "\n"; clearTokens_reTok(tok, idx, j+1); } // =1*2 as in Lua if (eqGet(tok, 1, "=")) tok = subList(tok, 2); // if code is something like "sclass Bla {}", just return as is if (tok_isStaticLevelCode(tok)) ret code; if (!isFalse(evalJava_prep_voidMagic!) && tok_shouldAddReturn(tok) && eqGet(tok, 3, "(") && isIdentifier(get(tok, 1)) && isKnownVoidFunction(get(tok, 1))) tokAppend_reTok(tok, l(tok)-2, ";"); // just a single identifier: interpret as function call if (l(tok) == 3 && isIdentifier(firstToken(tok))) tokAppend_reTok(tok, 1, "()"); code = tok_addReturn(tok); S returnType = containsReturnWithArgument(code) ? "O" : "void"; S main = global + "static " + returnType + " " + mainName + "(" + unnull(evalJava_prep_args!) + ") throws Exception {\n" + preCode + code + "\n" + "}"; ret main; }