Class: NScript::CodeNode

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

Constant Summary collapse

UPPERCASE =
/[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



572
573
574
575
576
# File 'lib/nscript/parser/nodes.rb', line 572

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.



567
568
569
# File 'lib/nscript/parser/nodes.rb', line 567

def body
  @body
end

#boundObject (readonly)

Returns the value of attribute bound.



567
568
569
# File 'lib/nscript/parser/nodes.rb', line 567

def bound
  @bound
end

#nameObject

Returns the value of attribute name.



568
569
570
# File 'lib/nscript/parser/nodes.rb', line 568

def name
  @name
end

#paramsObject (readonly)

Returns the value of attribute params.



567
568
569
# File 'lib/nscript/parser/nodes.rb', line 567

def params
  @params
end

#protoObject

Returns the value of attribute proto.



568
569
570
# File 'lib/nscript/parser/nodes.rb', line 568

def proto
  @proto
end

Instance Method Details

#compile_node(o) ⇒ Object



582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
# File 'lib/nscript/parser/nodes.rb', line 582

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 = "(function#{name_part}() {\n#{idt(2)}return __func.apply(__this, arguments);\n#{idt(1)}});"
  write("(function(__this) {\n#{idt(1)}var __func = #{func};\n#{idt(1)}return #{inner}\n#{idt}})(this)")
end

#constructor?Boolean



578
579
580
# File 'lib/nscript/parser/nodes.rb', line 578

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