class NScript::OpNode

Constants

ASSIGNMENT
CHAINABLE
CONVERSIONS
PREFIX_OPERATORS

Attributes

operator[R]
second[RW]

Public Class Methods

new(operator, first, second=nil, flip=false) click to toggle source
# 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

Public Instance Methods

chainable?() click to toggle source
# File lib/nscript/parser/nodes.rb, line 526
def chainable?
  CHAINABLE.include?(operator.to_sym)
end
compile_assignment(o) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/nscript/parser/nodes.rb, line 522
def unary?
  @second.nil?
end