class RedParse::ListInNode::IfNode

Attributes

empty_else[R]

Public Class Methods

new(iftok,condition,thentok,consequent,elsifs,else_,endtok) click to toggle source
Calls superclass method RedParse::ListInNode::Node::new
# File lib/redparse/node.rb, line 4300
def initialize(iftok,condition,thentok,consequent,elsifs,else_,endtok)
  @offset=iftok.offset
  @empty_else= else_ && !(else_=else_.val)
  condition.special_conditions! if condition.respond_to? :special_conditions!
  elsifs.extend ListInNode if elsifs
  super(condition,consequent,elsifs,else_)
  @reverse=  iftok.ident=="unless"
  if @reverse
    @iftok_offset=iftok.offset
    fail "elsif not allowed with unless" unless elsifs.empty?
  end
end

Public Instance Methods

else() click to toggle source
# File lib/redparse/node.rb, line 4346
def else
  @reverse ? consequent : otherwise
end
Also aliased as: else_
else_()
Alias for: else
if() click to toggle source
# File lib/redparse/node.rb, line 4334
def if
  if @reverse
    negate condition, @iftok_offset
  else
    condition
  end
end
Also aliased as: if_
if_()
Alias for: if
image() click to toggle source
# File lib/redparse/node.rb, line 4332
def image; "(if)" end
parsetree(o) click to toggle source
# File lib/redparse/node.rb, line 4362
def parsetree(o)
  elsepart=otherwise.parsetree(o) if otherwise
  elsifs.reverse_each{|elsifnode|
    elsepart=elsifnode.parsetree(o) << elsepart
  }
  cond=condition.rescue_parsetree(o)
  actions=[
        consequent&&consequent.parsetree(o), 
        elsepart
  ]
  if cond.first==:not
    cond=cond.last
    reverse=!@reverse
  else
    reverse=@reverse
  end
  actions.reverse! if reverse
  result=[:if, cond, *actions]
  return result
end
then() click to toggle source
# File lib/redparse/node.rb, line 4342
def then
  @reverse ? otherwise : consequent
end
Also aliased as: then_
then_()
Alias for: then
to_lisp() click to toggle source
# File lib/redparse/node.rb, line 4350
def to_lisp
  if elsifs.empty? 
    "(#{@reverse ? :unless : :if} #{condition.to_lisp}\n"+
    "(then #{consequent.to_lisp})\n(else #{otherwise.to_lisp}))"
  else
    "(cond (#{condition.to_lisp} #{consequent.to_lisp})\n"+
          elsifs.map{|x| x.to_lisp}.join("\n")+
          "\n(else #{otherwise.to_lisp})"+
    "\n)"
  end
end
unparse(o=default_unparse_options) click to toggle source
# File lib/redparse/node.rb, line 4321
def unparse o=default_unparse_options
  result=@reverse ? "unless " : "if "
  result+="#{condition.unparse o}"
  result+=unparse_nl(consequent,o)+"#{consequent.unparse(o)}" if consequent
  result+=unparse_nl(elsifs.first,o)+elsifs.map{|n| n.unparse(o)}.join if elsifs
  result+=unparse_nl(else_,o)+"else "+else_.unparse(o) if else_
  result+=";else " if defined? @empty_else and @empty_else
  result+=";end"
  return result
end