Class: StyleScript::AssignNode
Overview
Setting the value of a local variable, or the value of an object property.
Constant Summary collapse
- PROTO_ASSIGN =
/\A(\S+)\.prototype/
- LEADING_DOT =
/\A\.(prototype\.)?/
Constants inherited from Node
Instance Method Summary collapse
- #compile_node(o) ⇒ Object
-
#compile_pattern_match(o) ⇒ Object
Implementation of recursive pattern matching, when assigning array or object literals to a value.
- #compile_splice(o) ⇒ Object
-
#initialize(variable, value, context = nil) ⇒ AssignNode
constructor
A new instance of AssignNode.
- #statement? ⇒ Boolean
- #value? ⇒ Boolean
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.
512 513 514 |
# File 'lib/style_script/nodes.rb', line 512 def initialize(variable, value, context=nil) @variable, @value, @context = variable, value, context end |
Instance Method Details
#compile_node(o) ⇒ Object
516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
# File 'lib/style_script/nodes.rb', line 516 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
Implementation of recursive pattern matching, when assigning array or object literals to a value. Peeks at their properties to assign inner names. See: wiki.ecmascript.org/doku.php?id=harmony:destructuring
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
# File 'lib/style_script/nodes.rb', line 548 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
565 566 567 568 569 570 571 572 |
# File 'lib/style_script/nodes.rb', line 565 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
541 542 543 |
# File 'lib/style_script/nodes.rb', line 541 def statement? value? && (@variable.array? || @variable.object?) end |
#value? ⇒ Boolean
537 538 539 |
# File 'lib/style_script/nodes.rb', line 537 def value? @variable.is_a?(ValueNode) end |