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.



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_statementObject



48
49
50
# File 'lib/basic101/program_counter.rb', line 48

def current_statement
  @program[@index]
end

#end?Boolean

Returns:

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



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_statementObject



13
14
15
# File 'lib/basic101/program_counter.rb', line 13

def goto_next_statement
  @index += 1
end

#returnObject

Raises:



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