Class: RedParse::IfNode

Inherits:
ValueNode show all
Defined in:
lib/redparse/node.rb

Constant Summary

Constants included from FlattenedIvars

FlattenedIvars::EXCLUDED_IVARS

Instance Attribute Summary collapse

Attributes inherited from Node

#endline, #errors, #offset, #parent, #startline

Attributes included from Stackable::Meta

#boolean_identity_params, #identity_params

Instance Method Summary collapse

Methods inherited from ValueNode

#lvalue

Methods inherited from Node

#+, #+@, #==, [], #[]=, #add_parent_links!, #begin_parsetree, create, #data, #deep_copy, #delete_extraneous_ivars!, #delete_linenums!, #depthwalk, #depthwalk_nodes, #error?, #evalable_inspect, #fixup_multiple_assignments!, #fixup_rescue_assignments!, #initialize_ivars, inline_symbols, #inspect, #lhs_unparse, #linerange, #lvalue, #lvars_defined_in, #merge_replacement_session, namelist, #negate, #original_brackets_assign, param_names, #parsetrees, #pretty_print, #prohibit_fixup, #replace_ivars_and_self, #replace_value, #rescue_parsetree, #to_parsetree, #to_parsetree_and_warnings, #unary, #walk, #xform_tree!

Methods included from Stackable::Meta

#build_exemplars, #enumerate_exemplars, #identity_param

Methods included from FlattenedIvars

#flattened_ivars, #flattened_ivars_equal?

Methods included from Stackable

#identity_name

Constructor Details

#initialize(iftok, condition, thentok, consequent, elsifs, else_, endtok) ⇒ IfNode

Returns a new instance of IfNode.



4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
# File 'lib/redparse/node.rb', line 4034

def initialize(iftok,condition,thentok,consequent,elsifs,else_,endtok)
  @offset=iftok.offset
  if else_ 
    else_=else_.val or @empty_else=true
  end
  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

Instance Attribute Details

#empty_elseObject (readonly)

Returns the value of attribute empty_else.



4055
4056
4057
# File 'lib/redparse/node.rb', line 4055

def empty_else
  @empty_else
end

Instance Method Details

#else_Object



4051
# File 'lib/redparse/node.rb', line 4051

alias else otherwise

#if_Object



4052
# File 'lib/redparse/node.rb', line 4052

alias if condition

#imageObject



4068
# File 'lib/redparse/node.rb', line 4068

def image; "(if)" end

#parsetree(o) ⇒ Object



4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
# File 'lib/redparse/node.rb', line 4098

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_Object



4053
# File 'lib/redparse/node.rb', line 4053

alias then consequent

#to_lispObject



4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
# File 'lib/redparse/node.rb', line 4086

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) ⇒ Object



4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
# File 'lib/redparse/node.rb', line 4057

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
  result+=";end"
  return result
end