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

43
LINES

< > BotCompany Repo | #1015726 // Infer variable types in snippet [handles some cases]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Uses 3874K of libraries. Click here for Pure Java version (8051L/56K).

!7

p-experiment {
  S snippetID = #1015725;
  S orig = loadSnippet(snippetID);
  L<S> tok = javaTok(orig);
  Map<Int> bracketMap = getBracketMap(tok);
  L<Int> l = jfindAll(tok, "? <id> =");
  for (int i : l) {
    S type = or2(tok_inferTypeAt(tok, bracketMap, i+6), "?");
    //print(tok.get(i+2) + " => " + type);
    if (nempty(type)) tok.set(i, type);
  }
  S text = join(tok);
  print();
  printIndent(text);
  if (neq(text, orig))
    editSnippet(snippetID, text);
}

sS tok_inferTypeAt(L<S> tok, Map<Int> bracketMap, int i) {
  int j = scanToEndOfInitializer(tok, bracketMap, i);
  L<S> tok2 = subList(tok, i-1, j+1);
  //printStruct(tok2);
  fS fname = pairA(tok_parseFunctionCall(tok2));
  if (fname == null) null;
  //print("fname: " + fname);
  S snippetID = stdFunctions_cached().get(fname);
  if (snippetID == null) null;
  
  LL<S> defs = findFullFunctionDefs(javaTokSnippet(snippetID), true);
  //print("Have " + l(defs) + " defs");
  defs = filter(func(L<S> def) -> bool { eq(tok_functionName(def), fname) }, defs);
  //print("Have " + l(defs) + " defs");
  new HashSet<S> types;
  for (L<S> def : defs) {
    int k = tok_skipModifiers(def, 1);
    // TODO: skip type args
    S type = joinSubList(def, k, indexOf(def, k, fname)-1);
    types.add(type);
  }
  ret singletonOpt(types);
}

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: #1015726
Snippet name: Infer variable types in snippet [handles some cases]
Eternal ID of this version: #1015726/14
Text MD5: e64e8e6630fb879a3c3f1e829baab35b
Transpilation MD5: 88b80f79b1c9aac39e5b77b245ef89a8
Author: stefan
Category: javax / code analysis
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2018-05-26 13:49:43
Source code size: 1347 bytes / 43 lines
Pitched / IR pitched: No / No
Views / Downloads: 510 / 1289
Version history: 13 change(s)
Referenced in: [show references]