Sha256: b95578810a537990e3bf9dd7ca133e01c944c3bd36d69b50fa9303327474e0b9
Contents?: true
Size: 1.14 KB
Versions: 3
Compression:
Stored size: 1.14 KB
Contents
# frozen_string_literal: true require_relative 'statement' module Basic101 class ForStatement < Statement # attr_writer :next_statement attr_reader :reference def initialize(reference, from, to, step) @reference = reference @from = from @to = to @step = step || BasicInteger.new(1) end def execute(runtime) delete_from_stack(runtime) runtime.for_stack.push self from = @from.eval(runtime) @reference.assign(runtime, from) end def increment(runtime) step = @step.eval(runtime) counter = @reference.eval(runtime) @reference.assign(runtime, counter + step) end 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 def goto_following_statement(runtime) runtime.goto_index_after @index end def delete_from_stack(runtime) runtime.for_stack.delete_reference(@reference) end protected def state [@reference, @from, @to, @step] end end end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
basic101-1.0.2 | lib/basic101/for_statement.rb |
basic101-1.0.1 | lib/basic101/for_statement.rb |
basic101-1.0.0 | lib/basic101/for_statement.rb |