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.
5 6 7 8 9 |
# File 'lib/basic101/program_counter.rb', line 5 def initialize(program) @program = program @index = 0 @stack = [] end |
Instance Method Details
#current_statement ⇒ Object
46 47 48 |
# File 'lib/basic101/program_counter.rb', line 46 def current_statement @program[@index] end |
#end? ⇒ Boolean
15 16 17 |
# File 'lib/basic101/program_counter.rb', line 15 def end? @index >= @program.statement_count end |
#gosub_line(line_number) ⇒ Object
31 32 33 34 |
# File 'lib/basic101/program_counter.rb', line 31 def gosub_line(line_number) @stack.push @index goto_line line_number end |
#goto_end ⇒ Object
36 37 38 |
# File 'lib/basic101/program_counter.rb', line 36 def goto_end @index = @program.statement_count + 1 end |
#goto_index(index) ⇒ Object
19 20 21 |
# File 'lib/basic101/program_counter.rb', line 19 def goto_index(index) @index = index end |
#goto_index_after(index) ⇒ Object
23 24 25 |
# File 'lib/basic101/program_counter.rb', line 23 def goto_index_after(index) goto_index(index + 1) end |
#goto_line(line_number) ⇒ Object
27 28 29 |
# File 'lib/basic101/program_counter.rb', line 27 def goto_line(line_number) goto_index @program.index_of_line(line_number) end |
#goto_next_statement ⇒ Object
11 12 13 |
# File 'lib/basic101/program_counter.rb', line 11 def goto_next_statement @index += 1 end |
#return ⇒ Object
40 41 42 43 44 |
# File 'lib/basic101/program_counter.rb', line 40 def return index = @stack.pop raise ReturnWithoutGosub unless index @index = index end |