Class: NScript::WhileNode

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

Constant Summary

Constants inherited from Node

Node::TAB

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(condition, body) ⇒ WhileNode

Returns a new instance of WhileNode.



695
696
697
# File 'lib/nscript/parser/nodes.rb', line 695

def initialize(condition, body)
  @condition, @body = condition, body
end

Instance Method Details

#compile_node(o) ⇒ Object



699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
# File 'lib/nscript/parser/nodes.rb', line 699

def compile_node(o)
  returns     = o.delete(:return)
  top         = o.delete(:top) && !returns
  o[:indent]  = idt(1)
  o[:top]     = true
  cond        = @condition.compile(o)
  set         = ''
  if !top
    rvar      = o[:scope].free_variable
    set       = "#{idt}#{rvar} = [];\n"
    @body     = PushNode.wrap(rvar, @body)
  end
  post        = returns ? "\n#{idt}return #{rvar};" : ''
  return write("#{set}#{idt}while (#{cond}) null;#{post}") if @body.nil?
  write("#{set}#{idt}while (#{cond}) {\n#{@body.compile(o)}\n#{idt}}#{post}")
end