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).

1  
!7
2  
3  
p-experiment {
4  
  S snippetID = #1015725;
5  
  S orig = loadSnippet(snippetID);
6  
  L<S> tok = javaTok(orig);
7  
  Map<Int> bracketMap = getBracketMap(tok);
8  
  L<Int> l = jfindAll(tok, "? <id> =");
9  
  for (int i : l) {
10  
    S type = or2(tok_inferTypeAt(tok, bracketMap, i+6), "?");
11  
    //print(tok.get(i+2) + " => " + type);
12  
    if (nempty(type)) tok.set(i, type);
13  
  }
14  
  S text = join(tok);
15  
  print();
16  
  printIndent(text);
17  
  if (neq(text, orig))
18  
    editSnippet(snippetID, text);
19  
}
20  
21  
sS tok_inferTypeAt(L<S> tok, Map<Int> bracketMap, int i) {
22  
  int j = scanToEndOfInitializer(tok, bracketMap, i);
23  
  L<S> tok2 = subList(tok, i-1, j+1);
24  
  //printStruct(tok2);
25  
  fS fname = pairA(tok_parseFunctionCall(tok2));
26  
  if (fname == null) null;
27  
  //print("fname: " + fname);
28  
  S snippetID = stdFunctions_cached().get(fname);
29  
  if (snippetID == null) null;
30  
  
31  
  LL<S> defs = findFullFunctionDefs(javaTokSnippet(snippetID), true);
32  
  //print("Have " + l(defs) + " defs");
33  
  defs = filter(func(L<S> def) -> bool { eq(tok_functionName(def), fname) }, defs);
34  
  //print("Have " + l(defs) + " defs");
35  
  new HashSet<S> types;
36  
  for (L<S> def : defs) {
37  
    int k = tok_skipModifiers(def, 1);
38  
    // TODO: skip type args
39  
    S type = joinSubList(def, k, indexOf(def, k, fname)-1);
40  
    types.add(type);
41  
  }
42  
  ret singletonOpt(types);
43  
}

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: 512 / 1293
Version history: 13 change(s)
Referenced in: [show references]