Class: StyleScript::CodeNode

Inherits:
Node
  • Object
show all
Defined in:
lib/style_script/nodes.rb

Overview

A function definition. The only node that creates a new Scope. A CodeNode does not have any children – they’re within the new scope.

Constant Summary collapse

UPPERCASE =

Constructor functions start with an uppercase letter, by convention.

/[A-Z]/

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(params, body, tag = nil) ⇒ CodeNode

Returns a new instance of CodeNode.



655
656
657
658
659
# File 'lib/style_script/nodes.rb', line 655

def initialize(params, body, tag=nil)
  @params = params
  @body   = body
  @bound  = tag == :boundfunc
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



649
650
651
# File 'lib/style_script/nodes.rb', line 649

def body
  @body
end

#boundObject (readonly)

Returns the value of attribute bound.



649
650
651
# File 'lib/style_script/nodes.rb', line 649

def bound
  @bound
end

#nameObject

Returns the value of attribute name.



650
651
652
# File 'lib/style_script/nodes.rb', line 650

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



649
650
651
# File 'lib/style_script/nodes.rb', line 649

def params
  @params
end

#protoObject

Returns the value of attribute proto.



650
651
652
# File 'lib/style_script/nodes.rb', line 650

def proto
  @proto
end

Instance Method Details

#compile_node(o) ⇒ Object



665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# File 'lib/style_script/nodes.rb', line 665

def compile_node(o)
  shared_scope = o.delete(:shared_scope)
  top          = o.delete(:top)
  o[:scope]    = shared_scope || Scope.new(o[:scope], @body, self)
  o[:return]   = true
  o[:top]      = true
  o[:indent]   = idt(@bound ? 2 : 1)
  o.delete(:no_wrap)
  o.delete(:globals)
  if @params.last.is_a?(SplatNode)
    splat = @params.pop
    splat.index = @params.length
    @body.unshift(splat)
  end
  @params.each {|id| o[:scope].parameter(id.to_s) }
  code  = @body.empty? ? "" : "\n#{@body.compile_with_declarations(o)}\n"
  name_part = @name ? " #{@name}" : ''
  func  = "function#{@bound ? '' : name_part}(#{@params.join(', ')}) {#{code}#{idt(@bound ? 1 : 0)}}"
  func  = "(#{func})" if top && !@bound
  return write(func) unless @bound
  inner = "// Generated by StyleScript v1.0.0\n(function#{name_part}() {\n#{idt(2)}return __func.apply(__this, arguments);\n#{idt(1)}});"
  write("// Generated by StyleScript v1.0.0\n(function(__this) {\n#{idt(1)}var __func = #{func};\n#{idt(1)}return #{inner}\n#{idt}})(this)")
end

#constructor?Boolean

Returns:

  • (Boolean)


661
662
663
# File 'lib/style_script/nodes.rb', line 661

def constructor?
  @name && @name[0..0][UPPERCASE]
end