Class: Basic101::ProgramCounter
- Inherits:
-
Object
- Object
- Basic101::ProgramCounter
- Defined in:
- lib/basic101/program_counter.rb
Instance Method Summary collapse
- #current_statement ⇒ Object
- #end? ⇒ Boolean
- #gosub_line(line_number) ⇒ Object
- #goto_end ⇒ Object
- #goto_index(index) ⇒ Object
- #goto_index_after(index) ⇒ Object
- #goto_line(line_number) ⇒ Object
- #goto_next_statement ⇒ Object
-
#initialize(program) ⇒ ProgramCounter
constructor
A new instance of ProgramCounter.
- #return ⇒ Object
Constructor Details
#initialize(program) ⇒ ProgramCounter
Returns a new instance of ProgramCounter.
7 8 9 10 11 |
# File 'lib/basic101/program_counter.rb', line 7 def initialize(program) @program = program @index = 0 @stack = [] end |
Instance Method Details
#current_statement ⇒ Object
48 49 50 |
# File 'lib/basic101/program_counter.rb', line 48 def current_statement @program[@index] end |
#end? ⇒ Boolean
17 18 19 |
# File 'lib/basic101/program_counter.rb', line 17 def end? @index >= @program.statement_count end |
#gosub_line(line_number) ⇒ Object
33 34 35 36 |
# File 'lib/basic101/program_counter.rb', line 33 def gosub_line(line_number) @stack.push @index goto_line line_number end |
#goto_end ⇒ Object
38 39 40 |
# File 'lib/basic101/program_counter.rb', line 38 def goto_end @index = @program.statement_count + 1 end |
#goto_index(index) ⇒ Object
21 22 23 |
# File 'lib/basic101/program_counter.rb', line 21 def goto_index(index) @index = index end |
#goto_index_after(index) ⇒ Object
25 26 27 |
# File 'lib/basic101/program_counter.rb', line 25 def goto_index_after(index) goto_index(index + 1) end |
#goto_line(line_number) ⇒ Object
29 30 31 |
# File 'lib/basic101/program_counter.rb', line 29 def goto_line(line_number) goto_index @program.index_of_line(line_number) end |
#goto_next_statement ⇒ Object
13 14 15 |
# File 'lib/basic101/program_counter.rb', line 13 def goto_next_statement @index += 1 end |
#return ⇒ Object
42 43 44 45 46 |
# File 'lib/basic101/program_counter.rb', line 42 def return index = @stack.pop raise ReturnWithoutGosub unless index @index = index end |