Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 0 additions & 21 deletions lib/prism/translation/parser/compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,6 @@ def visit_call_node(node)

if node.call_operator_loc.nil?
case name
when :-@
case (receiver = node.receiver).type
when :integer_node, :float_node, :rational_node, :imaginary_node
return visit(numeric_negate(node.message_loc, receiver))
end
when :!
return visit_block(builder.not_op(token(node.message_loc), token(node.opening_loc), visit(node.receiver), token(node.closing_loc)), block)
when :=~
Expand Down Expand Up @@ -1973,22 +1968,6 @@ def multi_target_elements(node)
elements
end

# Negate the value of a numeric node. This is a special case where you
# have a negative sign on one line and then a number on the next line.
# In normal Ruby, this will always be a method call. The parser gem,
# however, marks this as a numeric literal. We have to massage the tree
# here to get it into the correct form.
def numeric_negate(message_loc, receiver)
case receiver.type
when :integer_node, :float_node
receiver.copy(value: -receiver.value, location: message_loc.join(receiver.location))
when :rational_node
receiver.copy(numerator: -receiver.numerator, location: message_loc.join(receiver.location))
when :imaginary_node
receiver.copy(numeric: numeric_negate(message_loc, receiver.numeric), location: message_loc.join(receiver.location))
end
end

# Blocks can have a special set of parameters that automatically expand
# when given arrays if they have a single required parameter and no
# other parameters.
Expand Down
44 changes: 36 additions & 8 deletions snapshots/unary_method_calls.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
@ ProgramNode (location: (1,0)-(2,5))
@ ProgramNode (location: (1,0)-(8,2))
├── flags: ∅
├── locals: []
└── statements:
@ StatementsNode (location: (1,0)-(2,5))
@ StatementsNode (location: (1,0)-(8,2))
├── flags: ∅
└── body: (length: 2)
└── body: (length: 4)
├── @ CallNode (location: (1,0)-(1,5))
│ ├── flags: newline
│ ├── receiver:
Expand All @@ -19,15 +19,43 @@
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block: ∅
└── @ CallNode (location: (2,0)-(2,5))
├── @ CallNode (location: (2,0)-(2,5))
│ ├── flags: newline
│ ├── receiver:
│ │ @ IntegerNode (location: (2,0)-(2,2))
│ │ ├── flags: static_literal, decimal
│ │ └── value: 42
│ ├── call_operator_loc: (2,2)-(2,3) = "."
│ ├── name: :!
│ ├── message_loc: (2,3)-(2,5) = "!@"
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block: ∅
├── @ CallNode (location: (4,0)-(5,2))
│ ├── flags: newline
│ ├── receiver:
│ │ @ IntegerNode (location: (5,0)-(5,2))
│ │ ├── flags: static_literal, decimal
│ │ └── value: 42
│ ├── call_operator_loc: ∅
│ ├── name: :-@
│ ├── message_loc: (4,0)-(4,1) = "-"
│ ├── opening_loc: ∅
│ ├── arguments: ∅
│ ├── closing_loc: ∅
│ ├── equal_loc: ∅
│ └── block: ∅
└── @ CallNode (location: (7,0)-(8,2))
├── flags: newline
├── receiver:
│ @ IntegerNode (location: (2,0)-(2,2))
│ @ IntegerNode (location: (8,0)-(8,2))
│ ├── flags: static_literal, decimal
│ └── value: 42
├── call_operator_loc: (2,2)-(2,3) = "."
├── name: :!
├── message_loc: (2,3)-(2,5) = "!@"
├── call_operator_loc:
├── name: :+@
├── message_loc: (7,0)-(7,1) = "+"
├── opening_loc: ∅
├── arguments: ∅
├── closing_loc: ∅
Expand Down
6 changes: 6 additions & 0 deletions test/prism/fixtures/unary_method_calls.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
42.~@
42.!@

-
42

+
42
Loading