@Override
  protected void visitItemAccessNode(ItemAccessNode node) {
    // simplify children first
    visitChildren(node);
    ExprNode baseExpr = node.getChild(0);
    ExprNode keyExpr = node.getChild(1);
    if (baseExpr instanceof ListLiteralNode && keyExpr instanceof IntegerNode) {
      ListLiteralNode listLiteral = (ListLiteralNode) baseExpr;
      long index = ((IntegerNode) keyExpr).getValue();
      if (index > 0 && index < listLiteral.numChildren()) {
        node.getParent().replaceChild(node, listLiteral.getChild((int) index));
      } else {
        // out of range
        node.getParent().replaceChild(node, new NullNode(node.getSourceLocation()));
      }
    } else if (baseExpr instanceof MapLiteralNode) {
      MapLiteralNode mapLiteral = (MapLiteralNode) baseExpr;
      for (int i = 0; i < mapLiteral.numChildren(); i += 2) {
        if (ExprEquivalence.get().equivalent(keyExpr, mapLiteral.getChild(i))) {
          node.getParent().replaceChild(node, mapLiteral.getChild(i + 1));
          return;
        }
      }
      // no matching key
      node.getParent().replaceChild(node, new NullNode(node.getSourceLocation()));
    }
  }
@ Override protected void visitItemAccessNode ( ItemAccessNode node ) { visitChildren ( node ) ; ExprNode baseExpr = node . getChild ( 0 ) ; ExprNode keyExpr = node . getChild ( 1 ) ; if ( baseExpr instanceof ListLiteralNode && keyExpr instanceof IntegerNode ) { ListLiteralNode listLiteral = ( ListLiteralNode ) baseExpr ; long index = ( ( IntegerNode ) keyExpr ) . getValue ( ) ; if ( index > 0 && index < listLiteral . numChildren ( ) ) { node . getParent ( ) . replaceChild ( node , listLiteral . getChild ( ( int ) index ) ) ; } else { node . getParent ( ) . replaceChild ( node , new NullNode ( node . getSourceLocation ( ) ) ) ; } } else if ( baseExpr instanceof MapLiteralNode ) { MapLiteralNode mapLiteral = ( MapLiteralNode ) baseExpr ; for ( int i = 0 ; i < mapLiteral . numChildren ( ) ; i += 2 ) { if ( ExprEquivalence . get ( ) . equivalent ( keyExpr , mapLiteral . getChild ( i ) ) ) { node . getParent ( ) . replaceChild ( node , mapLiteral . getChild ( i + 1 ) ) ; return ; } } node . getParent ( ) . replaceChild ( node , new NullNode ( node . getSourceLocation ( ) ) ) ; } }
collections