rouge-lang 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/bin/rouge CHANGED
@@ -1,6 +1,63 @@
1
1
  #!/usr/bin/env ruby
2
2
  # The Rouge REPL/interpreter.
3
-
4
3
  $: << "#{File.dirname(__FILE__)}/../lib"
5
4
  require 'rouge'
6
- Rouge.repl(ARGV)
5
+ require 'optparse'
6
+
7
+ options = {:backtrace => true}
8
+
9
+ option_parser = OptionParser.new do |opts|
10
+ opts.banner = "rouge [switch] [filename]"
11
+
12
+ opts.on('-v', '--version', 'Print version number') do
13
+ puts "Rouge #{Rouge::VERSION}"
14
+ end
15
+
16
+ opts.on('-e STR', '--eval STR', 'Evaluate the contents of STR') do |str|
17
+ Rouge.boot!
18
+ Rouge::Context.new(Rouge[:user]).readeval(str)
19
+ exit 0
20
+ end
21
+
22
+ opts.on('--time-startup', 'Report boot up time') do
23
+ Rouge.boot!
24
+ puts Time.now - Rouge.start
25
+ exit 0
26
+ end
27
+
28
+ opts.on('--[no-]backtrace', 'Enable/disable backtracing in REPL') do |bool|
29
+ options[:backtrace] = bool
30
+ end
31
+ end
32
+
33
+ begin
34
+ option_parser.parse!
35
+ rescue OptionParser::MissingArgument => e
36
+ puts "rouge: #{e}"
37
+ end
38
+
39
+ if ARGV.length == 1
40
+ file = ARGV[0]
41
+
42
+ if File.file?(file)
43
+ code = File.read(file)
44
+ else
45
+ STDERR.puts "rouge: No such file -- #{file}"
46
+ exit 1
47
+ end
48
+
49
+ # Permit shebangs at the top of the document.
50
+ if code[0..1] == "#!"
51
+ code = code[code.index("\n") + 1..-1]
52
+ end
53
+
54
+ Rouge.boot!
55
+ Rouge::Context.new(Rouge[:user]).readeval(code)
56
+
57
+ exit 0
58
+ elsif ARGV.length > 1
59
+ STDERR.puts option_parser.help
60
+ exit 1
61
+ end
62
+
63
+ Rouge.repl(options)
data/lib/rouge/repl.rb CHANGED
@@ -1,35 +1,23 @@
1
1
  # encoding: utf-8
2
2
  require 'readline'
3
3
 
4
- module Rouge::REPL; end
4
+ module Rouge::REPL
5
5
 
6
- class << Rouge::REPL
7
- def repl_error(e)
8
- STDOUT.puts "!! #{e.class}: #{e.message}"
9
- STDOUT.puts "#{e.backtrace.join "\n"}"
10
- end
11
-
12
- def repl(argv)
13
- context = Rouge::Context.new Rouge[:user]
6
+ def self.run!(options = {:backtrace => true})
7
+ puts "Rouge #{Rouge::VERSION}"
14
8
 
15
- if ARGV == ["--time-startup"]
16
- STDOUT.puts Time.now - Rouge.start
17
- exit(0)
18
- elsif argv.length == 1
19
- f = File.read(argv[0])
20
- if f[0] == ?#
21
- f = f[f.index("\n") + 1..-1]
22
- end
9
+ repl_error = lambda do |e|
10
+ STDOUT.puts "!! #{e.class}: #{e.message}"
23
11
 
24
- context.readeval(f)
25
- exit(0)
26
- elsif argv.length > 1
27
- STDERR.puts "!! usage: #$0 [FILE]"
28
- exit(1)
12
+ if options[:backtrace]
13
+ STDOUT.puts "#{e.backtrace.join "\n"}"
14
+ end
29
15
  end
30
16
 
17
+ context = Rouge::Context.new(Rouge[:user])
31
18
  count = 0
32
19
  chaining = false
20
+
33
21
  while true
34
22
  if not chaining
35
23
  prompt = "#{context.ns.name}=> "
@@ -50,15 +38,18 @@ class << Rouge::REPL
50
38
  chaining = true
51
39
  next
52
40
  rescue Rouge::Reader::UnexpectedCharacterError => reader_err
53
- repl_error reader_err
41
+ repl_error.call reader_err
54
42
  end
55
43
 
56
44
  chaining = false
45
+
57
46
  begin
58
47
  form = Rouge::Compiler.compile(
59
48
  context.ns,
60
49
  Set[*context.lexical_keys],
61
- form)
50
+ form
51
+ )
52
+
62
53
  result = context.eval(form)
63
54
 
64
55
  Rouge.print(result, STDOUT)
@@ -73,10 +64,11 @@ class << Rouge::REPL
73
64
  context = cce.context
74
65
  count = 0
75
66
  rescue => e
76
- repl_error e
67
+ repl_error.call e
77
68
  end
78
69
  end
79
70
  end
71
+
80
72
  end
81
73
 
82
74
  # vim: set sw=2 et cc=80:
data/lib/rouge/seq.rb CHANGED
@@ -64,7 +64,9 @@ module Rouge::Seq
64
64
  len
65
65
  end
66
66
 
67
- alias count length
67
+ def count
68
+ length
69
+ end
68
70
 
69
71
  def [](idx)
70
72
  return to_a[idx] if idx.is_a? Range
@@ -187,6 +189,26 @@ module Rouge::Seq
187
189
  Array.new(@array, @idx + 1)
188
190
  end
189
191
  end
192
+
193
+ def length
194
+ @array.length - @idx
195
+ end
196
+
197
+ def [](idx)
198
+ @array[@idx + idx]
199
+ end
200
+
201
+ def to_a
202
+ @array[@idx..-1]
203
+ end
204
+
205
+ def each(&block)
206
+ to_a.each(&block)
207
+ end
208
+
209
+ def map(&block)
210
+ to_a.map(&block)
211
+ end
190
212
  end
191
213
 
192
214
  # A lazy seq; contains the body (thunk) which is a lambda to get the "real"
data/lib/rouge/version.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Rouge
4
- VERSION = "0.0.5"
4
+ VERSION = "0.0.6"
5
5
  end
6
6
 
7
7
  # vim: set sw=2 et cc=80:
data/lib/rouge.rb CHANGED
@@ -41,13 +41,13 @@ module Rouge
41
41
  user.refer core
42
42
  user.refer Rouge[:ruby]
43
43
 
44
- Rouge::Context.new(user).readeval(
45
- File.read(Rouge.relative_to_lib('boot.rg')))
44
+ boot_rg = File.read(Rouge.relative_to_lib('boot.rg'))
45
+ Rouge::Context.new(user).readeval(boot_rg)
46
46
  end
47
47
 
48
- def self.repl(argv)
48
+ def self.repl(options = {})
49
49
  boot!
50
- Rouge::REPL.repl(argv)
50
+ Rouge::REPL.run!(options)
51
51
  end
52
52
 
53
53
  def self.relative_to_lib name
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rouge-lang
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: