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

31
LINES

< > BotCompany Repo | #1005446 - evalJava_prep - prepare JavaX code for evaluation

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (5850L/39K).

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<S> evalJava_prep_args;
static new ThreadLocal<Bool> evalJava_prep_voidMagic; // set to false to avoid looking up standard function return types

static S evalJava_prep(S code, S mainName, S preCode) {
  L<S> tok = javaTok(trim(code));
  
  // =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, ";");
    
  code = tok_addReturn(tok);
  S returnType = containsReturnWithArgument(code) ? "O" : "void";
  S main =
    "static " + returnType + " " + mainName + "(" + unnull(evalJava_prep_args!) + ") throws Exception {\n" 
      + preCode + code + "\n" + "}";
  ret main;
}

Author comment

Began life as a copy of #1005407

download  show line numbers  debug dex   

Travelled to 15 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, xrpafgyirdlv

No comments. add comment

Snippet ID: #1005446
Snippet name: evalJava_prep - prepare JavaX code for evaluation
Eternal ID of this version: #1005446/15
Text MD5: a90651affc81f2d396c15e724982acc1
Transpilation MD5: 4af0bddd85177b2f9203f94993c6b35e
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2020-11-11 12:50:45
Source code size: 1107 bytes / 31 lines
Pitched / IR pitched: No / No
Views / Downloads: 393 / 441
Version history: 14 change(s)
Referenced in: [show references]

Formerly at http://tinybrain.de/1005446 & http://1005446.tinybrain.de