Esempio n. 1
0
  @Override
  public RubyNode visitArgumentNode(org.jruby.ast.ArgumentNode node) {
    final SourceSection sourceSection = translate(node.getPosition());

    final RubyNode readNode;

    if (useArray()) {
      readNode = readArgument(sourceSection, index);
    } else {
      if (state == State.PRE) {
        readNode = readArgument(sourceSection, index);
      } else if (state == State.POST) {
        readNode =
            readArgument(
                sourceSection,
                (argsNode.getPreCount() + argsNode.getOptionalArgsCount() + argsNode.getPostCount())
                    - index
                    - 1);
      } else {
        throw new IllegalStateException();
      }
    }

    final FrameSlot slot =
        methodBodyTranslator.getEnvironment().getFrameDescriptor().findFrameSlot(node.getName());
    return WriteLocalVariableNodeFactory.create(context, sourceSection, slot, readNode);
  }
Esempio n. 2
0
  private RubyNode translateLocalAssignment(
      ISourcePosition sourcePosition, String name, org.jruby.ast.Node valueNode) {
    final SourceSection sourceSection = translate(sourcePosition);

    final RubyNode readNode;

    if (valueNode instanceof org.jruby.ast.NilImplicitNode) {
      // Multiple assignment

      if (useArray()) {
        readNode =
            ArrayIndexNodeFactory.create(context, sourceSection, index, loadArray(sourceSection));
      } else {
        readNode = readArgument(sourceSection, index);
      }
    } else {
      // Optional argument
      final RubyNode defaultValue = valueNode.accept(this);
      readNode =
          new ReadOptionalArgumentNode(
              context, sourceSection, index, index + argsNode.getPostCount() + 1, defaultValue);
    }

    final FrameSlot slot =
        methodBodyTranslator.getEnvironment().getFrameDescriptor().findFrameSlot(name);
    return WriteLocalVariableNodeFactory.create(context, sourceSection, slot, readNode);
  }
Esempio n. 3
0
  @Override
  public RubyNode visitRestArgNode(org.jruby.ast.RestArgNode node) {
    final SourceSection sourceSection = translate(node.getPosition());

    final RubyNode readNode;

    if (useArray()) {
      readNode =
          ArrayGetTailNodeFactory.create(
              context, sourceSection, argsNode.getPreCount(), loadArray(sourceSection));
    } else {
      readNode = new ReadRestArgumentNode(context, sourceSection, argsNode.getPreCount());
    }

    final FrameSlot slot =
        methodBodyTranslator.getEnvironment().getFrameDescriptor().findFrameSlot(node.getName());
    return WriteLocalVariableNodeFactory.create(context, sourceSection, slot, readNode);
  }
Esempio n. 4
0
 private RubyNode readArgument(SourceSection sourceSection, int index) {
   if (useArray()) {
     return ArrayIndexNodeFactory.create(context, sourceSection, index, loadArray(sourceSection));
   } else {
     if (state == State.PRE) {
       return new ReadPreArgumentNode(
           context,
           sourceSection,
           index,
           isBlock ? MissingArgumentBehaviour.NIL : MissingArgumentBehaviour.RUNTIME_ERROR);
     } else if (state == State.POST) {
       return new ReadPostArgumentNode(
           context,
           sourceSection,
           (argsNode.getPreCount() + argsNode.getOptionalArgsCount() + argsNode.getPostCount())
               - index
               - 1);
     } else {
       throw new IllegalStateException();
     }
   }
 }
Esempio n. 5
0
  @Override
  public RubyNode visitArgsNode(org.jruby.ast.ArgsNode node) {
    argsNode = node;

    final SourceSection sourceSection = translate(node.getPosition());

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

    int localIndex = 0;

    if (node.getPre() != null) {
      state = State.PRE;
      for (org.jruby.ast.Node arg : node.getPre().childNodes()) {
        sequence.add(arg.accept(this));
        index = ++localIndex;
      }
    }

    if (node.getOptArgs() != null) {
      // (BlockNode 0, (OptArgNode:a 0, (LocalAsgnNode:a 0, (FixnumNode 0))), ...)
      state = State.OPT;
      for (org.jruby.ast.Node arg : node.getOptArgs().childNodes()) {
        sequence.add(arg.accept(this));
        index = ++localIndex;
      }
    }

    if (node.getPost() != null) {
      state = State.POST;
      for (org.jruby.ast.Node arg : node.getPost().childNodes()) {
        sequence.add(arg.accept(this));
        index = ++localIndex;
      }
    }

    if (node.getRestArgNode() != null) {
      methodBodyTranslator.getEnvironment().hasRestParameter = true;
      sequence.add(node.getRestArgNode().accept(this));
    }

    if (node.getBlock() != null) {
      sequence.add(node.getBlock().accept(this));
    }

    return SequenceNode.sequence(context, sourceSection, sequence);
  }