Class: Sexp2Ruby::Node::Call

Inherits:
Base
  • Object
show all
Defined in:
lib/sexp2ruby/node/call.rb

Overview

A method call.

Examples:

“‘ a s(:call, nil, :a)

A.b s(:call, s(:const, :A), :b)

a(b) s(:call, nil, :a, s(:call, nil, :b))

a(b, c: d, &e) s(

:call,
nil,
:a,
s(:call, nil, :b),
s(:hash, s(:lit, :c), s(:call, nil, :d)),
s(:block_pass, s(:call, nil, :e))

)

a = :a; a.b s(

:block,
s(:lasgn, :a, s(:lit, :a)),
s(:call, s(:lvar, :a), :b)

) “‘

Constant Summary collapse

BINARY =

binary operation messages

[:<=>, :==, :<, :>, :<=, :>=, :-, :+, :*, :/, :%, :<<, :>>, :**, :'!=']

Constants inherited from Base

Base::ASSIGN_NODES, Base::LF, Base::LINE_LENGTH

Instance Attribute Summary

Attributes inherited from Base

#processor

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Sexp2Ruby::Node::Base

Instance Method Details

#arguments?(exp) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/sexp2ruby/node/call.rb', line 38

def arguments?(exp)
  exp.length > 2 # 1. receiver, 2. method name, 3+ arguments
end

#to_s(exp) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/sexp2ruby/node/call.rb', line 42

def to_s(exp)
  receiver_node_type = exp.first.nil? ? nil : exp.first.first
  receiver = process exp.shift
  receiver = "(#{receiver})" if ASSIGN_NODES.include? receiver_node_type

  name = exp.shift
  args = []

  # this allows us to do both old and new sexp forms:
  exp.push(*exp.pop[1..-1]) if exp.size == 1 && exp.first.first == :arglist

  call_push(name)

  in_context :arglist do
    until exp.empty? do
      arg_type = exp.first.sexp_type
      is_empty_hash = (exp.first == s(:hash))
      arg = process exp.shift

      next if arg.empty?

      strip_hash = (arg_type == :hash and
        not BINARY.include? name and
        not is_empty_hash and
        (exp.empty? or exp.first.sexp_type == :splat))
      wrap_arg = ASSIGN_NODES.include? arg_type

      arg = arg[2..-3] if strip_hash
      arg = "(#{arg})" if wrap_arg

      args << arg
    end
  end

  case name
  when *BINARY then
    "(#{receiver} #{name} #{args.join(', ')})"
  when :[] then
    receiver ||= "self"
    "#{receiver}[#{args.join(', ')}]"
  when :[]= then
    receiver ||= "self"
    rhs = args.pop
    "#{receiver}[#{args.join(', ')}] = #{rhs}"
  when :"!" then
    "(not #{receiver})"
  when :"-@" then
    "-#{receiver}"
  when :"+@" then
    "+#{receiver}"
  else
    args = arguments(args, name)
    receiver = "#{receiver}." if receiver
    "#{receiver}#{name}#{args}"
  end
ensure
  call_pop
end