class Gobstones::CLI::Runner

Public Class Methods

new(code) click to toggle source
# File lib/gobstones/cli/runner.rb, line 14
def initialize(code)
  @code = code
  @parser = Gobstones::Parser::TreetopParser.new
end
run(file_name) click to toggle source
# File lib/gobstones/cli/runner.rb, line 10
def self.run(file_name)
  new(File.read(file_name)).run
end

Public Instance Methods

run() click to toggle source
# File lib/gobstones/cli/runner.rb, line 19
def run
  print_program_result parse_program.evaluate
rescue Gobstones::Parser::ParseError => e
  handle_parse_error e
rescue Gobstones::Runner::GobstonesTypeError => e
  handle_type_error e
rescue Gobstones::Runner::BoomError => e
  handle_boom_error e
rescue Gobstones::Runner::UndefinedVariableError => e
  handle_undefined_variable_error e
rescue Gobstones::Runner::EmptyCellError => e
  handle_empty_cell_error e
rescue Gobstones::Runner::OutOfBoardError => e
  handle_out_of_board_error e
rescue Gobstones::Runner::DefinitionNotFound => e
  handle_definition_not_found_error e
rescue Gobstones::Runner::WrongArgumentsError => e
  handle_wrong_arguments_error e
rescue Gobstones::Runner::GobstonesRuntimeError => e
  handle_runtime_error e
rescue StandardError => e
  raise e
end

Private Instance Methods

handle_boom_error(boom_error) click to toggle source
# File lib/gobstones/cli/runner.rb, line 64
def handle_boom_error(boom_error)
  puts "BOOM!! #{boom_error.message}"
end
handle_definition_not_found_error(definition_not_found_error) click to toggle source
# File lib/gobstones/cli/runner.rb, line 80
def handle_definition_not_found_error(definition_not_found_error)
  puts definition_not_found_error.message
end
handle_empty_cell_error(_empty_cell_error) click to toggle source
# File lib/gobstones/cli/runner.rb, line 72
def handle_empty_cell_error(_empty_cell_error)
  puts 'There are no balls to take out!'
end
handle_out_of_board_error(_out_of_board_error) click to toggle source
# File lib/gobstones/cli/runner.rb, line 76
def handle_out_of_board_error(_out_of_board_error)
  puts 'You fell from the board!'
end
handle_parse_error(parse_error) click to toggle source
# File lib/gobstones/cli/runner.rb, line 53
def handle_parse_error(parse_error)
  parse_error.parser.failure_reason =~ /^(Expected .+) after/m
  puts "#{Regexp.last_match(1).gsub("\n", '$NEWLINE')}:"
  puts parse_error.code.lines.to_a[parse_error.parser.failure_line - 1]
  puts "#{'~' * (parse_error.parser.failure_column - 1)}^"
end
handle_runtime_error(runtime_error) click to toggle source
# File lib/gobstones/cli/runner.rb, line 88
def handle_runtime_error(runtime_error)
  puts runtime_error.message
end
handle_type_error(type_error) click to toggle source
# File lib/gobstones/cli/runner.rb, line 60
def handle_type_error(type_error)
  puts "Type Error: #{type_error.message}"
end
handle_undefined_variable_error(undefined_variable_error) click to toggle source
# File lib/gobstones/cli/runner.rb, line 68
def handle_undefined_variable_error(undefined_variable_error)
  puts undefined_variable_error.message
end
handle_wrong_arguments_error(wrong_arguments_error) click to toggle source
# File lib/gobstones/cli/runner.rb, line 84
def handle_wrong_arguments_error(wrong_arguments_error)
  puts wrong_arguments_error.message
end
parse_program() click to toggle source
# File lib/gobstones/cli/runner.rb, line 49
def parse_program
  @parser.parse @code
end
print_program_result(result) click to toggle source