class Basic101::Runtime

Attributes

for_stack[R]
input[R]
output[R]

Public Class Methods

new(args = {}) click to toggle source
# File lib/basic101/runtime.rb, line 11
def initialize(args = {})
  @output = Output.new(args.fetch(:output_file, $stdout))
  @input = Input.new(@output, args.fetch(:input_file, $stdin))
  @program = args.fetch(:program, Program.new)
  @functions = Functions.new
  @for_stack = ForStack.new
  @random = Random.new(0)
  @transcript = NullTranscript.new
end

Public Instance Methods

add_function(function) click to toggle source
# File lib/basic101/runtime.rb, line 96
def add_function(function)
  @functions.add_function(function)
end
call_function(identifier, argument_values) click to toggle source
# File lib/basic101/runtime.rb, line 92
def call_function(identifier, argument_values)
  @functions.call(self, identifier, argument_values)
end
end_program() click to toggle source
# File lib/basic101/runtime.rb, line 55
def end_program
  @program_counter.goto_end
end
function_exists?(identifier) click to toggle source
# File lib/basic101/runtime.rb, line 88
def function_exists?(identifier)
  @functions.has_function?(identifier)
end
get_array(identifier, num_dimensions) click to toggle source
# File lib/basic101/runtime.rb, line 108
def get_array(identifier, num_dimensions) 
  @arrays[identifier.to_s] ||=
    BasicArray.new(num_dimensions, identifier.default)
end
get_data_item() click to toggle source
# File lib/basic101/runtime.rb, line 113
def get_data_item
  data_item = @data_items.shift
  raise OutOfDataError unless data_item
  data_item
end
get_scalar(identifier) click to toggle source
# File lib/basic101/runtime.rb, line 100
def get_scalar(identifier)
  @scalars[identifier.to_s] ||= BasicInteger.new(0)
end
gosub_line(line_number) click to toggle source
# File lib/basic101/runtime.rb, line 39
def gosub_line(line_number)
  @program_counter.gosub_line(line_number)
end
goto_index(index) click to toggle source
# File lib/basic101/runtime.rb, line 47
def goto_index(index)
  @program_counter.goto_index(index)
end
goto_index_after(index) click to toggle source
# File lib/basic101/runtime.rb, line 51
def goto_index_after(index)
  @program_counter.goto_index_after(index)
end
goto_line(line_number) click to toggle source
# File lib/basic101/runtime.rb, line 35
def goto_line(line_number)
  @program_counter.goto_line(line_number)
end
rand() click to toggle source
# File lib/basic101/runtime.rb, line 31
def rand
  @random.rand
end
randomize() click to toggle source
# File lib/basic101/runtime.rb, line 27
def randomize
  @random = Random.new
end
restore(line_number = nil) click to toggle source
# File lib/basic101/runtime.rb, line 119
def restore(line_number = nil)
  @data_items = @program.data_items(line_number)
end
return() click to toggle source
# File lib/basic101/runtime.rb, line 43
def return
  @program_counter.return
end
run() click to toggle source
# File lib/basic101/runtime.rb, line 59
def run
  transcribe_errors do
    reset
    begin
      while !@program_counter.end?
        statement = @program_counter.current_statement
        begin
          @program_counter.goto_next_statement
          statement.execute(self)
        rescue StandardError => e
          statement.raise_error_with_line_number(e)
        end
      end
    end
  end
end
set_scalar(identifier, value) click to toggle source
# File lib/basic101/runtime.rb, line 104
def set_scalar(identifier, value)
  @scalars[identifier.to_s] = value
end
transcribe_errors() { || ... } click to toggle source
# File lib/basic101/runtime.rb, line 76
def transcribe_errors
  begin
    yield
  rescue Parslet::ParseFailed => e
    @transcript.save_output_lines e
    raise
  rescue Basic101::Error => e
    @transcript.save_output_lines e
    raise
  end
end
transcript=(transcript) click to toggle source
# File lib/basic101/runtime.rb, line 21
def transcript=(transcript)
  @transcript = transcript
  @output.transcript = @transcript
  @input.transcript = @transcript
end

Private Instance Methods

reset() click to toggle source
# File lib/basic101/runtime.rb, line 125
def reset
  @scalars = {}
  @arrays = {}
  @program_counter = ProgramCounter.new(@program)
  restore
end