Sha256: fad1d8619e4ce45a1dadff869d40c9756bfec24ff4ede8cf7f09def7f067b4ee

Contents?: true

Size: 852 Bytes

Versions: 3

Compression:

Stored size: 852 Bytes

Contents

# frozen_string_literal: true

module Basic101

  class ProgramCounter

    def initialize(program)
      @program = program
      @index = 0
      @stack = []
    end

    def goto_next_statement
      @index += 1
    end

    def end?
      @index >= @program.statement_count
    end

    def goto_index(index)
      @index = index
    end

    def goto_index_after(index)
      goto_index(index + 1)
    end

    def goto_line(line_number)
      goto_index @program.index_of_line(line_number)
    end

    def gosub_line(line_number)
      @stack.push @index
      goto_line line_number
    end

    def goto_end
      @index = @program.statement_count + 1
    end

    def return
      index = @stack.pop
      raise ReturnWithoutGosub unless index
      @index = index
    end

    def current_statement
      @program[@index]
    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
basic101-1.0.2 lib/basic101/program_counter.rb
basic101-1.0.1 lib/basic101/program_counter.rb
basic101-1.0.0 lib/basic101/program_counter.rb