Class: Basic101::ProgramCounter

Inherits:
Object
  • Object
show all
Defined in:
lib/basic101/program_counter.rb

Instance Method Summary collapse

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_statementObject



46
47
48
# File 'lib/basic101/program_counter.rb', line 46

def current_statement
  @program[@index]
end

#end?Boolean

Returns:

  • (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_endObject



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_statementObject



11
12
13
# File 'lib/basic101/program_counter.rb', line 11

def goto_next_statement
  @index += 1
end

#returnObject

Raises:



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