Permalink
Browse files

Merge pull request #20 from noprompt/repl

Major refactoring of Rouge binary and Rouge::REPL
  • Loading branch information...
2 parents c344af9 + d177d50 commit 9e435e50bc4334e06236d96ba38d0fc223229e71 Arlen Christian Mart Cuss committed Dec 12, 2012
Showing with 80 additions and 31 deletions.
  1. +59 −2 bin/rouge
  2. +4 −4 lib/rouge.rb
  3. +17 −25 lib/rouge/repl.rb
View
@@ -1,6 +1,63 @@
#!/usr/bin/env ruby
# The Rouge REPL/interpreter.
-
$: << "#{File.dirname(__FILE__)}/../lib"
require 'rouge'
-Rouge.repl(ARGV)
+require 'optparse'
+
+options = {:backtrace => true}
+
+option_parser = OptionParser.new do |opts|
+ opts.banner = "rouge [switch] [filename]"
+
+ opts.on('-v', '--version', 'Print version number') do
+ puts "Rouge #{Rouge::VERSION}"
+ end
+
+ opts.on('-e STR', '--eval STR', 'Evaluate the contents of STR') do |str|
+ Rouge.boot!
+ Rouge::Context.new(Rouge[:user]).readeval(str)
+ exit 0
+ end
+
+ opts.on('--time-startup', 'Report boot up time') do
+ Rouge.boot!
+ puts Time.now - Rouge.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
+
+if ARGV.length == 1
+ file = ARGV[0]
+
+ 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
+
+ Rouge.boot!
+ Rouge::Context.new(Rouge[:user]).readeval(code)
+
+ exit 0
+elsif ARGV.length > 1
+ STDERR.puts option_parser.help
+ exit 1
+end
+
+Rouge.repl(options)
View
@@ -41,13 +41,13 @@ def self.boot!
user.refer core
user.refer Rouge[:ruby]
- Rouge::Context.new(user).readeval(
- File.read(Rouge.relative_to_lib('boot.rg')))
+ boot_rg = File.read(Rouge.relative_to_lib('boot.rg'))
+ Rouge::Context.new(user).readeval(boot_rg)
end
- def self.repl(argv)
+ def self.repl(options = {})
boot!
- Rouge::REPL.repl(argv)
+ Rouge::REPL.run!(options)
end
def self.relative_to_lib name
View
@@ -1,35 +1,23 @@
# encoding: utf-8
require 'readline'
-module Rouge::REPL; end
+module Rouge::REPL
-class << Rouge::REPL
- def repl_error(e)
- STDOUT.puts "!! #{e.class}: #{e.message}"
- STDOUT.puts "#{e.backtrace.join "\n"}"
- end
-
- def repl(argv)
- context = Rouge::Context.new Rouge[:user]
+ def self.run!(options = {:backtrace => true})
+ puts "Rouge #{Rouge::VERSION}"
- if ARGV == ["--time-startup"]
- STDOUT.puts Time.now - Rouge.start
- exit(0)
- elsif argv.length == 1
- f = File.read(argv[0])
- if f[0] == ?#
- f = f[f.index("\n") + 1..-1]
- end
+ repl_error = lambda do |e|
+ STDOUT.puts "!! #{e.class}: #{e.message}"
- context.readeval(f)
- exit(0)
- elsif argv.length > 1
- STDERR.puts "!! usage: #$0 [FILE]"
- exit(1)
+ if options[:backtrace]
+ STDOUT.puts "#{e.backtrace.join "\n"}"
+ end
end
+ context = Rouge::Context.new(Rouge[:user])
count = 0
chaining = false
+
while true
if not chaining
prompt = "#{context.ns.name}=> "
@@ -50,15 +38,18 @@ def repl(argv)
chaining = true
next
rescue Rouge::Reader::UnexpectedCharacterError => reader_err
- repl_error reader_err
+ repl_error.call reader_err
end
chaining = false
+
begin
form = Rouge::Compiler.compile(
context.ns,
Set[*context.lexical_keys],
- form)
+ form
+ )
+
result = context.eval(form)
Rouge.print(result, STDOUT)
@@ -73,10 +64,11 @@ def repl(argv)
context = cce.context
count = 0
rescue => e
- repl_error e
+ repl_error.call e
end
end
end
+
end
# vim: set sw=2 et cc=80:

0 comments on commit 9e435e5

Please sign in to comment.