#!/usr/bin/env ruby # The Rouge REPL/interpreter. $: << "#{File.dirname(__FILE__)}/../lib" require 'rouge' require 'optparse' # Boots Rouge and evaluates code in a new context. Prints non-nil results if # print is true. def evaluate(code, print = false) Rouge.boot! res = Rouge::Context.new(Rouge[:user]).readeval(code) if print && res puts Rouge.print(res, '') end end options = {:backtrace => true} option_parser = OptionParser.new do |opts| opts.banner = "Usage: rouge [switch] [filename]" opts.on('-v', '--version', 'Print version number') do puts "Rouge #{Rouge::VERSION}" exit 0 end opts.on('-e STR', '--eval STR', 'Evaluate expressions in STR; print non-nil results') do |str| evaluate(str, true) exit 0 end opts.on('--time-startup', 'Report boot up time') do start = Time.now Rouge.boot! puts Time.now - start exit 0 end opts.on('--[no-]backtrace', 'Enable/disable backtracing in REPL') do |bool| options[:backtrace] = bool end end begin option_parser.parse! rescue OptionParser::MissingArgument => e puts "rouge: #{e}" end # Using a standalone '-', which traditionally is the switch for indicating # acceptance of standard input, causes some subtle issues with the # OptionParser. if ARGV.include?('-') code = '' while line = STDIN.gets code << line end evaluate(code, true) exit 0 end if ARGV.length >= 1 file = ARGV.shift if File.file?(file) code = File.read(file) else STDERR.puts "rouge: No such file -- #{file}" exit 1 end # Permit shebangs at the top of the document. if code[0..1] == "#!" code = code[code.index("\n") + 1..-1] end evaluate(code) exit 0 end Rouge.repl(options)