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

25
LINES

< > BotCompany Repo | #1025425 // tok_ifRecordMatch

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

Libraryless. Click here for Pure Java version (6335L/37K).

// Note: the variable names must match the record field names
// as we still need a way to find the field names from here
static void tok_ifRecordMatch(LS tok) {
  int i;
  while ((i = jfind_check is(tok, "if <id> is <id>(")) >= 0) {
    int iOpening = indexOf(tok, i, "(");  
    int iClosing = indexOf(tok, iOpening+2, ")");
    S var = tok.get(i+2), className = tok.get(i+6);
    S cast = "((" + className + ") " + var + ")";
    int start = iClosing+2;
    tok_statementToBlock(tok, start);
    int end = findEndOfStatement(tok, start);
    StringBuilder statement = new("if (\*var*/ instanceof \*className*/");
    new StringBuilder decls;
    LPairS vars = tok_typesAndNamesOfParams(subList(tok, iOpening+1, iClosing));
    for (PairS v : vars) {
      S type = v.a, name = v.b;
      S nonPrim = tok_toNonPrimitiveTypes(type);
      if (!tok_typeIsObject(type))
        statement.append(" && \*cast*/.\*name*/ instanceof \*nonPrim*/");
      decls.append("\n\*type*/ \*name*/ = cast \*cast*/.\*name*/;");
    }
    replaceTokens_reTok(tok, i, start+1, statement + ") {" + decls);
  }
}

Author comment

Began life as a copy of #1017552

download  show line numbers  debug dex  old transpilations   

Travelled to 6 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1025425
Snippet name: tok_ifRecordMatch
Eternal ID of this version: #1025425/9
Text MD5: 7d5bc6e6902f8c7aa0c6e202e3ae3128
Transpilation MD5: 5d50f2d61aa26d2083d75bac3fef818a
Author: stefan
Category: javax / transpiling
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-09-29 00:17:03
Source code size: 1116 bytes / 25 lines
Pitched / IR pitched: No / No
Views / Downloads: 204 / 328
Version history: 8 change(s)
Referenced in: [show references]