Class: NScript::OpNode
- Inherits:
-
Node
- Object
- Node
- NScript::OpNode
show all
- Defined in:
- lib/nscript/parser/nodes.rb
Constant Summary
collapse
- CONVERSIONS =
{
:== => "===",
:'!=' => "!==",
:and => '&&',
:or => '||',
:is => '===',
:isnt => "!==",
:not => '!'
}
- CHAINABLE =
[:<, :>, :>=, :<=, :===, :'!===']
- ASSIGNMENT =
[:'||=', :'&&=', :'?=']
- PREFIX_OPERATORS =
[:typeof, :delete]
Constants inherited
from Node
Node::TAB
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Node
#children, children, #compile, #compile_closure, #contains?, #idt, statement, #statement?, statement_only, #statement_only?, top_sensitive, #top_sensitive?, #unwrap, #write
Constructor Details
#initialize(operator, first, second = nil, flip = false) ⇒ OpNode
Returns a new instance of OpNode.
517
518
519
520
|
# File 'lib/nscript/parser/nodes.rb', line 517
def initialize(operator, first, second=nil, flip=false)
@first, @second, @flip = first, second, flip
@operator = CONVERSIONS[operator.to_sym] || operator
end
|
Instance Attribute Details
#operator ⇒ Object
Returns the value of attribute operator.
501
502
503
|
# File 'lib/nscript/parser/nodes.rb', line 501
def operator
@operator
end
|
#second ⇒ Object
Returns the value of attribute second.
502
503
504
|
# File 'lib/nscript/parser/nodes.rb', line 502
def second
@second
end
|
Instance Method Details
#chainable? ⇒ Boolean
526
527
528
|
# File 'lib/nscript/parser/nodes.rb', line 526
def chainable?
CHAINABLE.include?(operator.to_sym)
end
|
#compile_assignment(o) ⇒ Object
544
545
546
547
548
549
550
|
# File 'lib/nscript/parser/nodes.rb', line 544
def compile_assignment(o)
first, second = @first.compile(o), @second.compile(o)
o[:scope].find(first) if @first.unwrap.is_a?(Value)
sym = @operator[0..1]
return "#{first} = #{ExistenceNode.compile_test(o, @first)} ? #{first} : #{second}" if @operator == '?='
"#{first} = #{first} #{sym} #{second}"
end
|
#compile_chain(o) ⇒ Object
538
539
540
541
542
|
# File 'lib/nscript/parser/nodes.rb', line 538
def compile_chain(o)
shared = @first.unwrap.second
@first.second, shared = *shared.compile_reference(o) if shared.is_a?(CallNode)
"(#{@first.compile(o)}) && (#{shared.compile(o)} #{@operator} #{@second.compile(o)})"
end
|
#compile_existence(o) ⇒ Object
552
553
554
555
|
# File 'lib/nscript/parser/nodes.rb', line 552
def compile_existence(o)
first, second = @first.compile(o), @second.compile(o)
"#{ExistenceNode.compile_test(o, @first)} ? #{first} : #{second}"
end
|
#compile_node(o) ⇒ Object
530
531
532
533
534
535
536
|
# File 'lib/nscript/parser/nodes.rb', line 530
def compile_node(o)
return write(compile_chain(o)) if chainable? && @first.unwrap.is_a?(OpNode) && @first.unwrap.chainable?
return write(compile_assignment(o)) if ASSIGNMENT.include?(@operator.to_sym)
return write(compile_unary(o)) if unary?
return write(compile_existence(o)) if @operator == '?'
write("#{@first.compile(o)} #{@operator} #{@second.compile(o)}")
end
|
#compile_unary(o) ⇒ Object
557
558
559
560
561
562
|
# File 'lib/nscript/parser/nodes.rb', line 557
def compile_unary(o)
space = PREFIX_OPERATORS.include?(@operator.to_sym) ? ' ' : ''
parts = [@operator.to_s, space, @first.compile(o)]
parts.reverse! if @flip
parts.join('')
end
|
#unary? ⇒ Boolean
522
523
524
|
# File 'lib/nscript/parser/nodes.rb', line 522
def unary?
@second.nil?
end
|