Class: NScript::AssignNode
- Inherits:
-
Node
- Object
- Node
- NScript::AssignNode
show all
- Defined in:
- lib/nscript/parser/nodes.rb
Constant Summary
collapse
- PROTO_ASSIGN =
/\A(\S+)\.prototype/
- LEADING_DOT =
/\A\.(prototype\.)?/
Constants inherited
from Node
Node::TAB
Instance Method Summary
collapse
Methods inherited from Node
#children, children, #compile, #compile_closure, #contains?, #idt, statement, statement_only, #statement_only?, top_sensitive, #top_sensitive?, #unwrap, #write
Constructor Details
#initialize(variable, value, context = nil) ⇒ AssignNode
Returns a new instance of AssignNode.
439
440
441
|
# File 'lib/nscript/parser/nodes.rb', line 439
def initialize(variable, value, context=nil)
@variable, @value, @context = variable, value, context
end
|
Instance Method Details
#compile_node(o) ⇒ Object
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
|
# 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) ⇒ Object
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
|
# 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) ⇒ Object
489
490
491
492
493
494
495
496
|
# 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? ⇒ Boolean
468
469
470
|
# File 'lib/nscript/parser/nodes.rb', line 468
def statement?
value? && (@variable.array? || @variable.object?)
end
|
#value? ⇒ Boolean
464
465
466
|
# File 'lib/nscript/parser/nodes.rb', line 464
def value?
@variable.is_a?(ValueNode)
end
|