Class: RVM::Interpreter::Loop

Inherits:
Element
  • Object
show all
Defined in:
lib/rvm/interpreter.rb

Overview

This is a loop. It is executed over and over again as long as the passed condition evaluates to a value that matches is_true?.


TODO: Add BreakException to stop execution of loops. TODO: Add NextExceeption to skip rest of a evaluation on loop code.

Instance Attribute Summary

Attributes inherited from Element

#pos

Instance Method Summary collapse

Methods inherited from Element

#optimize

Constructor Details

#initialize(condition, body, pos = nil) ⇒ Loop

Initializes a new loop.

condition

is executed before each run of the loop. If it evaluates to true the loop is executed another time otherwise the exection ends.

body

For each itteration of the loop this is executed once.

pos

The position within the source code of the definition - for deugging purpose.



279
280
281
282
283
# File 'lib/rvm/interpreter.rb', line 279

def initialize(condition, body, pos = nil)
  super(pos)
  @condition = condition
  @body = body
end

Instance Method Details

#execute(env) ⇒ Object

The loop will execute as long as the code passed as condition evaluates to is_true?. Once the loop stops executing the return value is the result of the last body execution.



297
298
299
300
301
302
303
# File 'lib/rvm/interpreter.rb', line 297

def execute env
  r = nil
  while @condition.execute(env).is_true?
    r = @body.execute(env)
  end
  r
end

#pretty_print(q) ⇒ Object



285
286
287
288
289
290
291
# File 'lib/rvm/interpreter.rb', line 285

def pretty_print(q)
  first = true
  q.text "while ("
  q.pp @condition
  q.text ")"
  q.pp @body
end