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

26
LINES

< > BotCompany Repo | #1016767 // tok_listComprehensions

JavaX fragment (include)

1  
static void tok_listComprehensions(L<S> tok) {
2  
  if (!tok.contains("[")) ret;
3  
  for (int i = 1; i < l(tok); i += 2) try {
4  
    continue unless eq(tok.get(i), "[");
5  
    int iOp = indexOfAny(tok, i+2, "?", ":", "in", "[", "]");
6  
    if (iOp < 0) ret;
7  
    if (!eqOneOf(tok.get(iOp), ":", "in")) continue; // not a list comprehension
8  
    if (eqGet(tok, iOp+2, ".")) continue; // "in." (in is a variable name)
9  
    
10  
    Map<Int> bracketMap = getBracketMap(tok); // XXX - optimize
11  
    
12  
    S type = joinSubList(tok, i+2, iOp-3), id = tok.get(iOp-2);
13  
    int j = scanOverExpression(tok, bracketMap, iOp+2, "|");
14  
    S exp = join(subList(tok, iOp+2, j));
15  
    j += 2;
16  
    int k = scanOverExpression(tok, bracketMap, j, "]");
17  
    S where = join(subList(tok, j, k));
18  
    ++k;
19  
      
20  
    S code = "filter(" + exp + ", func(" + type + " " + id + ") -> Bool { " + where + " })";
21  
    replaceTokens(tok, i, k, code);
22  
    reTok(tok, i, k);
23  
  } on fail {
24  
    print("Was processing list comprehension: " + joinSubList(tok, i, i+30) + "...");
25  
  }
26  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1016767
Snippet name: tok_listComprehensions
Eternal ID of this version: #1016767/6
Text MD5: b9be9113c399ac181e57082071f19b97
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-07-24 03:03:59
Source code size: 1054 bytes / 26 lines
Pitched / IR pitched: No / No
Views / Downloads: 416 / 450
Version history: 5 change(s)
Referenced in: [show references]