Class: SyntaxTree::Binary
- Inherits:
-
Object
- Object
- SyntaxTree::Binary
- Defined in:
- lib/syntax_tree.rb
Overview
Binary represents any expression that involves two sub-expressions with an operator in between. This can be something that looks like a mathematical operation:
1 + 1
but can also be something like pushing a value onto an array:
array << value
Instance Attribute Summary collapse
-
#comments ⇒ Object
readonly
- Array[ Comment | EmbDoc ]
-
the comments attached to this node.
-
#left ⇒ Object
readonly
- untyped
-
the left-hand side of the expression.
-
#location ⇒ Object
readonly
- Location
-
the location of this node.
-
#operator ⇒ Object
readonly
- Symbol
-
the operator used between the two expressions.
-
#right ⇒ Object
readonly
- untyped
-
the right-hand side of the expression.
Instance Method Summary collapse
- #child_nodes ⇒ Object
- #format(q) ⇒ Object
-
#initialize(left:, operator:, right:, location:, comments: []) ⇒ Binary
constructor
A new instance of Binary.
- #pretty_print(q) ⇒ Object
- #to_json(*opts) ⇒ Object
Constructor Details
#initialize(left:, operator:, right:, location:, comments: []) ⇒ Binary
Returns a new instance of Binary.
2364 2365 2366 2367 2368 2369 2370 |
# File 'lib/syntax_tree.rb', line 2364 def initialize(left:, operator:, right:, location:, comments: []) @left = left @operator = operator @right = right @location = location @comments = comments end |
Instance Attribute Details
#comments ⇒ Object (readonly)
- Array[ Comment | EmbDoc ]
-
the comments attached to this node
2362 2363 2364 |
# File 'lib/syntax_tree.rb', line 2362 def comments @comments end |
#left ⇒ Object (readonly)
- untyped
-
the left-hand side of the expression
2350 2351 2352 |
# File 'lib/syntax_tree.rb', line 2350 def left @left end |
#location ⇒ Object (readonly)
- Location
-
the location of this node
2359 2360 2361 |
# File 'lib/syntax_tree.rb', line 2359 def location @location end |
#operator ⇒ Object (readonly)
- Symbol
-
the operator used between the two expressions
2353 2354 2355 |
# File 'lib/syntax_tree.rb', line 2353 def operator @operator end |
#right ⇒ Object (readonly)
- untyped
-
the right-hand side of the expression
2356 2357 2358 |
# File 'lib/syntax_tree.rb', line 2356 def right @right end |
Instance Method Details
#child_nodes ⇒ Object
2372 2373 2374 |
# File 'lib/syntax_tree.rb', line 2372 def child_nodes [left, right] end |
#format(q) ⇒ Object
2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 |
# File 'lib/syntax_tree.rb', line 2376 def format(q) power = operator == :** q.group do q.group { q.format(left) } q.text(" ") unless power q.group do q.text(operator) q.indent do q.breakable(power ? "" : " ") q.format(right) end end end end |
#pretty_print(q) ⇒ Object
2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 |
# File 'lib/syntax_tree.rb', line 2394 def pretty_print(q) q.group(2, "(", ")") do q.text("binary") q.breakable q.pp(left) q.breakable q.text(operator) q.breakable q.pp(right) q.pp(Comment::List.new(comments)) end end |
#to_json(*opts) ⇒ Object
2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 |
# File 'lib/syntax_tree.rb', line 2411 def to_json(*opts) { type: :binary, left: left, op: operator, right: right, loc: location, cmts: comments }.to_json(*opts) end |