Class: Peruby::Op::While
- Includes:
- LoopExecution
- Defined in:
- lib/peruby/op/control.rb
Overview
While or until loop operation.
Instance Method Summary collapse
-
#initialize(condition, body, until_loop, continuation = nil, label = nil) ⇒ While
constructor
A new instance of While.
- #run(env, _context) ⇒ Object
- #to_callable ⇒ Object
Methods inherited from Base
#argument_cells, #local_slot, #lvalue, #run_subroutine, #warning_directive?
Constructor Details
#initialize(condition, body, until_loop, continuation = nil, label = nil) ⇒ While
Returns a new instance of While.
173 174 175 176 177 178 179 |
# File 'lib/peruby/op/control.rb', line 173 def initialize(condition, body, until_loop, continuation = nil, label = nil) @condition = condition @body = body @until_loop = until_loop @continue = continuation @label = label end |
Instance Method Details
#run(env, _context) ⇒ Object
181 182 183 184 185 186 187 188 189 |
# File 'lib/peruby/op/control.rb', line 181 def run(env, _context) loop do truth = Conv.truthy?(@condition.run(env, :scalar)) break if @until_loop ? truth : !truth break unless run_iteration?(env) end nil end |
#to_callable ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 |
# File 'lib/peruby/op/control.rb', line 191 def to_callable condition = @condition.to_callable scalar_condition = @condition.scalar_callable if @condition.respond_to?(:scalar_callable) body = @body.to_callable void_body = @body.to_void_callable if @body.respond_to?(:to_void_callable) continuation = @continue&.to_callable lambda do |env, _context| loop do value = scalar_condition ? scalar_condition.call(env) : condition.call(env, :scalar) truth = Conv.truthy?(value) break if @until_loop ? truth : !truth keep_going = compiled_iteration?(env, body, void_body, continuation) break unless keep_going end nil end end |