Example #1
0
  @Override
  public RubyNode visitMultipleAsgnNode(org.jruby.ast.MultipleAsgn19Node node) {
    final SourceSection sourceSection = translate(node.getPosition());

    // (MultipleAsgn19Node 0, (ArrayNode 0, (LocalAsgnNode:a 0, ), (LocalAsgnNode:b 0, )), null,
    // null))

    final int arrayIndex = index;

    final String arrayName = methodBodyTranslator.getEnvironment().allocateLocalTemp("destructure");
    final FrameSlot arraySlot = methodBodyTranslator.getEnvironment().declareVar(arrayName);

    pushArraySlot(arraySlot);

    final List<org.jruby.ast.Node> childNodes = node.childNodes().get(0).childNodes();

    final List<RubyNode> notNilSequence = new ArrayList<>();

    for (int n = 0; n < childNodes.size(); n++) {
      index = n;
      notNilSequence.add(childNodes.get(n).accept(this));
    }

    final RubyNode notNil = SequenceNode.sequence(context, sourceSection, notNilSequence);

    popArraySlot(arraySlot);

    final List<RubyNode> nilSequence = new ArrayList<>();

    if (!childNodes.isEmpty()) {
      // We haven't pushed a new array slot, so this will read the value which we couldn't convert
      // to an array into the first destructured argument
      index = arrayIndex;
      nilSequence.add(childNodes.get(0).accept(this));
    }

    final RubyNode nil = SequenceNode.sequence(context, sourceSection, nilSequence);

    return SequenceNode.sequence(
        context,
        sourceSection,
        WriteLocalVariableNodeFactory.create(
            context,
            sourceSection,
            arraySlot,
            ArrayCastNodeFactory.create(
                context, sourceSection, readArgument(sourceSection, arrayIndex))),
        new IfNode(
            context,
            sourceSection,
            BooleanCastNodeFactory.create(
                context,
                sourceSection,
                new IsNilNode(
                    context,
                    sourceSection,
                    ReadLocalVariableNodeFactory.create(context, sourceSection, arraySlot))),
            nil,
            notNil));
  }
Example #2
0
 protected RubyNode loadArray(SourceSection sourceSection) {
   return ReadLocalVariableNodeFactory.create(context, sourceSection, arraySlotStack.peek());
 }