Sha256: 1e0905535a340f0d02bce81a1e2c02fb8eada6e6a3ffd01d301e52f4f8a54cfe

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

#!/usr/bin/env ruby

require "bundler/setup"

require_relative "../lib/whitespace"

def main
  require "optparse"
  options = {}
  opts = OptionParser.new do |opts|
    executable_name = File.basename($PROGRAM_NAME)
    opts.banner = <<-BANNER
A Whitespace interpreter written in Ruby.

Usage: #{executable_name} [options] filename

    BANNER

    opts.on("-v", "--version", "Show version information, then exit") do
      puts Whitespace::VERSION
      exit
    end

    opts.on("-h", "--help", "Show this help, then exit") do
      puts opts
      exit
    end
  end
  opts.parse!

  nargs = ARGV.length
  if nargs == 0 || nargs > 1
    puts opts
    exit 1
  end

  abs_filename = File.expand_path(ARGV[0])
  src = File.new(abs_filename).read

  vm = Whitespace::VM.new
  console = Whitespace::Console.new
  parser = Whitespace::Parser.new(vm, console)

  begin
    vm.load(parser.parse(src))
    vm.run
  rescue => e
    raise Whitespace::Error
  end
end

begin
  main
rescue Whitespace::Error => e
  STDERR.puts "A problem occurred while executing your Whitespace program"
  STDERR.puts "Error: #{e.cause.message}"
  exit 1
rescue ArgumentError => e
  STDERR.puts "Please check your options/arguments"
  STDERR.puts "Error: #{e.message}"
  exit 1
rescue => e
  STDERR.puts "Sorry, we encountered an unrecoverable error"
  STDERR.puts e.message
  STDERR.puts e.backtrace.join("\n")
  exit 1
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
whitespace-ruby-1.0.0 bin/whitespace