class NScript::AssignNode

Constants

LEADING_DOT
PROTO_ASSIGN

Public Class Methods

new(variable, value, context=nil) click to toggle source
# File lib/nscript/parser/nodes.rb, line 439
def initialize(variable, value, context=nil)
  @variable, @value, @context = variable, value, context
end

Public Instance Methods

compile_node(o) click to toggle source
# File lib/nscript/parser/nodes.rb, line 443
def compile_node(o)
  top = o.delete(:top)
  return compile_pattern_match(o) if statement?
  return compile_splice(o) if value? && @variable.splice?
  stmt        = o.delete(:as_statement)
  name        = @variable.compile(o)
  last        = value? ? @variable.last.to_s.sub(LEADING_DOT, '') : name
  proto       = name[PROTO_ASSIGN, 1]
  if @value.is_a?(CodeNode)
    @value.name  = last  if last.match(Lexer::IDENTIFIER)
    @value.proto = proto if proto
  end
  return write("#{name}: #{@value.compile(o)}") if @context == :object
  o[:scope].find(name) unless value? && @variable.properties?
  val = "#{name} = #{@value.compile(o)}"
  return write("#{idt}#{val};") if stmt
  val = "(#{val})" if !top || o[:return]
  val = "#{idt}return #{val}" if o[:return]
  write(val)
end
compile_pattern_match(o) click to toggle source
# File lib/nscript/parser/nodes.rb, line 472
def compile_pattern_match(o)
  val_var = o[:scope].free_variable
  assigns = ["#{idt}#{val_var} = #{@value.compile(o)};"]
  o.merge!(:top => true, :as_statement => true)
  @variable.base.objects.each_with_index do |obj, i|
    obj, i = obj.value, obj.variable.base if @variable.object?
    access_class = @variable.array? ? IndexNode : AccessorNode
    if obj.is_a?(SplatNode)
      val = LiteralNode.wrap(obj.compile_value(o, val_var, @variable.base.objects.index(obj)))
    else
      val = ValueNode.new(val_var, [access_class.new(Value.new(i.to_s))])
    end
    assigns << AssignNode.new(obj, val).compile(o)
  end
  write(assigns.join("\n"))
end
compile_splice(o) click to toggle source
# File lib/nscript/parser/nodes.rb, line 489
def compile_splice(o)
  var   = @variable.compile(o.merge(:only_first => true))
  range = @variable.properties.last.range
  plus  = range.exclusive? ? '' : ' + 1'
  from  = range.from.compile(o)
  to    = "#{range.to.compile(o)} - #{from}#{plus}"
  write("#{var}.splice.apply(#{var}, [#{from}, #{to}].concat(#{@value.compile(o)}))")
end
statement?() click to toggle source
# File lib/nscript/parser/nodes.rb, line 468
def statement?
  value? && (@variable.array? || @variable.object?)
end
value?() click to toggle source
# File lib/nscript/parser/nodes.rb, line 464
def value?
  @variable.is_a?(ValueNode)
end