class Basic101::ForStatement

Attributes

reference[R]

attr_writer :next_statement

Public Class Methods

new(reference, from, to, step) click to toggle source
# File lib/basic101/for_statement.rb, line 12
def initialize(reference, from, to, step)
  @reference = reference
  @from = from
  @to = to
  @step = step || BasicInteger.new(1)
end

Public Instance Methods

delete_from_stack(runtime) click to toggle source
# File lib/basic101/for_statement.rb, line 47
def delete_from_stack(runtime)
  runtime.for_stack.delete_reference(@reference)
end
done?(runtime) click to toggle source
# File lib/basic101/for_statement.rb, line 32
def done?(runtime)
  step = @step.eval(runtime)
  counter = @reference.eval(runtime)
  to = @to.eval(runtime)
  if step.value < 0
    counter.lt(to)
  else
    counter.gt(to)
  end.to_b
end
execute(runtime) click to toggle source
# File lib/basic101/for_statement.rb, line 19
def execute(runtime)
  delete_from_stack(runtime)
  runtime.for_stack.push self
  from = @from.eval(runtime)
  @reference.assign(runtime, from)
end
goto_following_statement(runtime) click to toggle source
# File lib/basic101/for_statement.rb, line 43
def goto_following_statement(runtime)
  runtime.goto_index_after @index
end
increment(runtime) click to toggle source
# File lib/basic101/for_statement.rb, line 26
def increment(runtime)
  step = @step.eval(runtime)
  counter = @reference.eval(runtime)
  @reference.assign(runtime, counter + step)
end

Protected Instance Methods

state() click to toggle source
# File lib/basic101/for_statement.rb, line 53
def state
  [@reference, @from, @to, @step]
end