Class: RedParse::IfNode

Inherits:
ValueNode show all
Defined in:
lib/redparse/node.rb,
lib/redparse/ripper.rb,
lib/redparse/ReduceWithsFor_RedParse_1_9.rb,
lib/redparse/ReduceWithsFor_RedParse_1_8.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, #reducer_method

Methods inherited from Node

#+, #+@, #==, [], #[]=, #add_parent_links!, #args_rip, #begin_parsetree, #classic_inspect, create, #data, #deep_copy, #delete_extraneous_ivars!, #delete_linenums!, #depthwalk, #depthwalk_nodes, #error?, #evalable_inspect, #fixup_multiple_assignments!, #fixup_rescue_assignments!, #force_stmt_list_rip, #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, #rfind, #rfind_all, #rgrep, #rip_and_rescues, #rip_explode!, #short_inspect, #stmts_rip, #to_parsetree, #to_parsetree_and_warnings, #to_ruby, #to_s, #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.



4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
# 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

Instance Attribute Details

#empty_elseObject (readonly)

Returns the value of attribute empty_else.



4319
4320
4321
# File 'lib/redparse/node.rb', line 4319

def empty_else
  @empty_else
end

Instance Method Details

#else_Object



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

alias else otherwise

#if_Object



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

alias if condition

#imageObject



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

def image; "(if)" end

#parsetree(o) ⇒ Object



4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
# 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

#reducer_identObject



17570
17571
17572
# File 'lib/redparse/ReduceWithsFor_RedParse_1_9.rb', line 17570

def reducer_ident
  :IfNode
end

#rip(p) ⇒ Object



334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/redparse/ripper.rb', line 334

def rip p
  elses=p.on_else(force_stmt_list_rip(otherwise,p)) if otherwise
  elsifs.reverse_each{|ei| 
   elses=p.on_elsif(
      ei.condition.rip(p),
      force_stmt_list_rip(ei.consequent,p),
      elses
    )
  }
  p.on_if(
    condition.rip(p),
    force_stmt_list_rip(consequent,p),
    elses
  )
end

#then_Object



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

alias then consequent

#to_lispObject



4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
# 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) ⇒ Object



4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
# 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