Class: NScript::IfNode

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_only, #statement_only?, top_sensitive, #top_sensitive?, #unwrap, #write

Constructor Details

#initialize(condition, body, else_body = nil, tags = {}) ⇒ IfNode

Returns a new instance of IfNode.



854
855
856
857
858
859
860
861
# File 'lib/nscript/parser/nodes.rb', line 854

def initialize(condition, body, else_body=nil, tags={})
  @condition = condition
  @body      = body && body.unwrap
  @else_body = else_body && else_body.unwrap
  @tags      = tags
  @multiple  = true if @condition.is_a?(Array)
  @condition = OpNode.new("!", ParentheticalNode.new(@condition)) if @tags[:invert]
end

Instance Method Details

#<<(else_body) ⇒ Object



863
864
865
866
867
# File 'lib/nscript/parser/nodes.rb', line 863

def <<(else_body)
  eb = else_body.unwrap
  @else_body ? @else_body << eb : @else_body = eb
  self
end

#add_comment(comment) ⇒ Object



869
870
871
872
# File 'lib/nscript/parser/nodes.rb', line 869

def add_comment(comment)
  @comment = comment
  self
end

#add_else(exprs) ⇒ Object



886
887
888
889
# File 'lib/nscript/parser/nodes.rb', line 886

def add_else(exprs)
  chain? ? @else_body.add_else(exprs) : @else_body = (exprs && exprs.unwrap)
  self
end

#chain?Boolean

Returns:

  • (Boolean)


891
892
893
# File 'lib/nscript/parser/nodes.rb', line 891

def chain?
  @chain ||= @else_body && @else_body.is_a?(IfNode)
end

#compile_condition(o) ⇒ Object



899
900
901
# File 'lib/nscript/parser/nodes.rb', line 899

def compile_condition(o)
  [@condition].flatten.map {|c| c.compile(o) }.join(' || ')
end

#compile_node(o) ⇒ Object



903
904
905
# File 'lib/nscript/parser/nodes.rb', line 903

def compile_node(o)
  write(statement? ? compile_statement(o) : compile_ternary(o))
end

#compile_statement(o) ⇒ Object



907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
# File 'lib/nscript/parser/nodes.rb', line 907

def compile_statement(o)
  child       = o.delete(:chain_child)
  cond_o      = o.dup
  cond_o.delete(:return)
  o[:indent]  = idt(1)
  o[:top]     = true
  if_dent     = child ? '' : idt
  com_dent    = child ? idt : ''
  prefix      = @comment ? @comment.compile(cond_o) + "\n#{com_dent}" : ''
  body        = Expressions.wrap(@body).compile(o)
  if_part     = "#{prefix}#{if_dent}if (#{compile_condition(cond_o)}) {\n#{body}\n#{idt}}"
  return if_part unless @else_body
  else_part = chain? ?
    " else #{@else_body.compile(o.merge(:indent => idt, :chain_child => true))}" :
    " else {\n#{Expressions.wrap(@else_body).compile(o)}\n#{idt}}"
  if_part + else_part
end

#compile_ternary(o) ⇒ Object



925
926
927
928
929
# File 'lib/nscript/parser/nodes.rb', line 925

def compile_ternary(o)
  if_part   = "#{@condition.compile(o)} ? #{@body.compile(o)}"
  else_part = @else_body ? "#{@else_body.compile(o)}" : 'null'
  "#{if_part} : #{else_part}"
end

#force_statementObject



874
875
876
877
# File 'lib/nscript/parser/nodes.rb', line 874

def force_statement
  @tags[:statement] = true
  self
end

#rewrite_condition(expression) ⇒ Object



879
880
881
882
883
884
# File 'lib/nscript/parser/nodes.rb', line 879

def rewrite_condition(expression)
  @condition = @multiple ? @condition.map {|c| OpNode.new("is", expression, c) } :
                           OpNode.new("is", expression, @condition)
  @else_body.rewrite_condition(expression) if chain?
  self
end

#statement?Boolean

Returns:

  • (Boolean)


895
896
897
# File 'lib/nscript/parser/nodes.rb', line 895

def statement?
  @is_statement ||= !!(@comment || @tags[:statement] || @body.statement? || (@else_body && @else_body.statement?))
end