Class: RedParse::WhileOpNode

Inherits:
RawOpNode show all
Includes:
KeywordOpNode
Defined in:
lib/redparse/node.rb,
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 included from KeywordOpNode

#unparse

Methods inherited from RawOpNode

create, #image, #raw_unparse

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, #image, #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(val1, op, val2 = nil) ⇒ WhileOpNode

Returns a new instance of WhileOpNode.



2919
2920
2921
2922
2923
2924
2925
2926
2927
# File 'lib/redparse/node.rb', line 2919

def initialize(val1,op,val2=nil)
  op,val2=nil,op unless val2
  op=op.ident if op.respond_to? :ident
  @reverse=false
  @loop=true
  @test_first= !( BeginNode===val1 )
  replace [val1,val2]
  condition.special_conditions! if condition.respond_to? :special_conditions!
end

Instance Attribute Details

#test_firstObject (readonly)

Returns the value of attribute test_first.



2934
2935
2936
# File 'lib/redparse/node.rb', line 2934

def test_first
  @test_first
end

Instance Method Details

#conditionObject



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

def condition; right end

#consequentObject



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

def consequent; left end

#doObject Also known as: body



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

def do; consequent end

#opObject



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

def op; "while" end

#parsetree(o) ⇒ Object



2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
# File 'lib/redparse/node.rb', line 2936

def parsetree(o)
  cond=condition.rescue_parsetree(o)
  body=consequent.parsetree(o)
  !@test_first and
    body.size == 2 and
      body.first == :begin and
        body=body.last
  if cond.first==:not
    kw=:until
    cond=cond.last
  else
    kw=:while
  end
  [kw, cond, body, (@test_first or body==[:nil])]
end

#reducer_identObject



17495
17496
17497
# File 'lib/redparse/ReduceWithsFor_RedParse_1_9.rb', line 17495

def reducer_ident
  :WhileOpNode
end

#reversedObject



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

def reversed; false end

#rip(p) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/redparse/ripper.rb', line 137

def rip p
  if BeginNode===consequent
    if @quirks
      #this is WRONG!!!, but it's how ripper works... urk
      #if expression modified by a while operator is a begin node,
      #ruby always executes the loop at least once, 
      #and doesn't check the condition til after the first execution.
      #Ripper reverses the order of condition and consequent to signal
      #an execute-at-least-once loop. But, that's not good enough.
      #because 'begin a end while b' now parses the same as 'b while begin a end'
      p.on_while_mod( condition.rip(p), consequent.rip(p) ) 
    else
      p.on_while_mod( consequent.rip(p), condition.rip(p), :loop_first )
    end
  else
    p.on_while_mod( consequent.rip(p), condition.rip(p) )
  end
end

#whileObject



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

def while; condition end