rouge-lang 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,82 +6,49 @@ module Rouge::Printer
6
6
 
7
7
  def self.print(form, out)
8
8
  case form
9
- when Integer
9
+ when Numeric
10
+ # Handles Integer, Float, Rational, and Complex instances.
10
11
  out << form.to_s
11
- when Rational
12
- out << "#{form.numerator}/#{form.denominator}"
13
- when Rouge::Symbol
14
- if form.ns_s
15
- out << form.ns_s
16
- out << "/"
17
- end
18
- out << form.name_s
19
- when Symbol
20
- out << form.inspect
21
- when String
22
- out << form.inspect
23
12
  when Array
24
- out << "["
25
- form.each.with_index do |e, i|
26
- out << " " unless i.zero?
27
- print(e, out)
28
- end
29
- out << "]"
30
- when Rouge::Seq::Empty
31
- out << "()"
32
- when Rouge::Seq::Cons
33
- if form.length == 2 and form[0] == Rouge::Symbol[:quote]
34
- out << "'"
35
- print(form[1], out)
36
- elsif form.length == 2 and form[0] == Rouge::Symbol[:var]
37
- out << "#'"
38
- print(form[1], out)
39
- else
40
- out << "("
41
- form.each.with_index do |e, i|
42
- out << " " unless i.zero?
43
- print(e, out)
44
- end
45
- out << ")"
46
- end
47
- when Rouge::Var
48
- out << "#'#{form.ns}/#{form.name}"
13
+ out << "[#{print_collection(form)}]"
49
14
  when Hash
50
- out << "{"
51
- form.each.with_index do |kv,i|
52
- out << ", " unless i.zero?
53
- print(kv[0], out)
54
- out << " "
55
- print(kv[1], out)
56
- end
57
- out << "}"
15
+ out << "{#{print_collection(form)}}"
58
16
  when Set
59
- out << "\#{"
60
- form.each_with_index do |el, i|
61
- print el, out
62
- out << " " unless i == (form.size - 1)
17
+ out << "\#{#{print_collection(form)}}"
18
+ when Regexp
19
+ out << "#\"#{form.source}\""
20
+ when Symbol
21
+ # Symbols containing white space are printed with the results of #inspect,
22
+ # otherwise they are printed with the results #to_s. This maintains an
23
+ # experience consistent with Clojure whenever possible while providing
24
+ # clarity in cases where Symbols contain white space, although this is
25
+ # typically uncommon.
26
+ if /\s/.match(form)
27
+ out << form.inspect
28
+ else
29
+ out << ":#{form}"
63
30
  end
64
- out << "}"
65
- when NilClass
66
- out << "nil"
67
- when TrueClass
68
- out << "true"
69
- when FalseClass
70
- out << "false"
31
+ when Rouge::Builtin, Rouge::Symbol, Rouge::Var, Rouge::Seq::Empty, Rouge::Seq::Cons
32
+ out << form.to_s
71
33
  when Class, Module
72
34
  if form.name
73
35
  out << "ruby/#{form.name.split('::').join('.')}"
74
36
  else
75
37
  out << form.inspect
76
38
  end
77
- when Rouge::Builtin
78
- out << "rouge.builtin/#{form.inner.name}"
79
- when Regexp
80
- out << "#\"#{form.source}\""
81
39
  else
82
40
  out << form.inspect
83
41
  end
84
42
  end
43
+
44
+ # Prints a collection of elements using `print`.
45
+ def self.print_collection(collection)
46
+ if collection.is_a? Hash
47
+ collection.to_a.map {|pair| print_collection(pair) }.join(', ')
48
+ else
49
+ collection.map {|el| print(el, '') }.join(' ')
50
+ end
51
+ end
85
52
  end
86
53
 
87
54
  # vim: set sw=2 et cc=80:
@@ -16,7 +16,6 @@ module Rouge::Seq
16
16
  # A partial implementation of ISeq. You supply #first and #next, it gives:
17
17
  #
18
18
  # - #cons
19
- # - #inspect
20
19
  # - #to_s
21
20
  # - #seq
22
21
  # - #length (#count)
@@ -28,13 +27,13 @@ module Rouge::Seq
28
27
  module ASeq
29
28
  include ISeq
30
29
 
31
- def inspect
32
- "(#{to_a.map(&:inspect).join " "})"
30
+ def to_s
31
+ "(#{to_a.map(&:to_s).join " "})"
33
32
  end
34
33
 
35
- def to_s; inspect; end
36
-
37
- def seq; self; end
34
+ def seq
35
+ self
36
+ end
38
37
 
39
38
  def first; raise NotImplementedError; end
40
39
  def next; raise NotImplementedError; end
@@ -84,13 +83,13 @@ module Rouge::Seq
84
83
  cursor.first
85
84
  end
86
85
 
87
- def ==(seq)
88
- if seq.is_a?(ISeq)
89
- return self.to_a == seq.to_a
86
+ def ==(other)
87
+ if other.is_a?(ISeq)
88
+ return self.to_a == other.to_a
90
89
  end
91
90
 
92
- if seq.is_a?(::Array)
93
- return self.to_a == seq
91
+ if other.is_a?(::Array)
92
+ return self.to_a == other
94
93
  end
95
94
 
96
95
  false
@@ -128,8 +127,9 @@ module Rouge::Seq
128
127
  class << Empty
129
128
  include ASeq
130
129
 
131
- def inspect; "()"; end
132
- def to_s; inspect; end
130
+ def to_s
131
+ '()'
132
+ end
133
133
 
134
134
  def seq; nil; end
135
135
  def first; nil; end
@@ -145,8 +145,10 @@ module Rouge::Seq
145
145
  class Cons
146
146
  include ASeq
147
147
 
148
+ attr_reader :head, :tail
149
+
148
150
  def initialize(head, tail)
149
- if tail and !tail.is_a?(ISeq)
151
+ if tail && !tail.is_a?(ISeq)
150
152
  raise ArgumentError,
151
153
  "tail should be an ISeq, not #{tail.inspect} (#{tail.class})"
152
154
  end
@@ -154,11 +156,27 @@ module Rouge::Seq
154
156
  @head, @tail = head, tail
155
157
  end
156
158
 
157
- def first; @head; end
158
- def next; Rouge::Seq.seq @tail; end
159
+ def first
160
+ @head
161
+ end
162
+
163
+ def next
164
+ Rouge::Seq.seq(@tail)
165
+ end
166
+
167
+ def to_s
168
+ if self.length == 2 && self[0] == Rouge::Symbol[:quote]
169
+ "'#{self[1]}"
170
+ elsif self.length == 2 && self[0] == Rouge::Symbol[:var]
171
+ "#'#{self[1]}"
172
+ else
173
+ "(#{Rouge::Printer.print_collection(self)})"
174
+ end
175
+ end
159
176
 
160
177
  def self.[](*elements)
161
178
  length = elements.length
179
+
162
180
  return Empty if length.zero?
163
181
 
164
182
  head = nil
@@ -168,8 +186,6 @@ module Rouge::Seq
168
186
 
169
187
  head.freeze
170
188
  end
171
-
172
- attr_reader :head, :tail
173
189
  end
174
190
 
175
191
  # A seq over a Ruby Array.
@@ -240,8 +256,13 @@ module Rouge::Seq
240
256
  seq.send(sym, *args, &block)
241
257
  end
242
258
 
243
- def inspect; seq.inspect; end
244
- def to_s; seq.to_s; end
259
+ def inspect
260
+ seq.inspect
261
+ end
262
+
263
+ def to_s
264
+ seq.to_s
265
+ end
245
266
  end
246
267
 
247
268
  # An error thrown when we try to do a seq operation on something that's not
@@ -260,14 +281,10 @@ module Rouge::Seq
260
281
  else
261
282
  Rouge::Seq::Array.new(form, 0)
262
283
  end
263
- when Enumerator
284
+ when Hash, Set, Enumerator
264
285
  seq(form.to_a)
265
286
  when String
266
287
  seq(form.chars)
267
- when Set
268
- seq(form.to_a)
269
- when Hash
270
- seq(form.to_a)
271
288
  else
272
289
  raise UnknownSeqError, form.inspect
273
290
  end
@@ -4,13 +4,15 @@ require 'rouge/metadata'
4
4
  class Rouge::Symbol
5
5
  include Rouge::Metadata
6
6
 
7
+ attr_reader :ns, :name, :ns_s, :name_s, :name_parts, :new_sym
8
+
7
9
  # The symbols for t/f/n are the Ruby objects themselves.
8
10
  LOOKUP = {
9
11
  :true => true,
10
12
  :false => false,
11
13
  :nil => nil,
12
14
  }
13
-
15
+
14
16
  KNOWNS = {
15
17
  :/ => [nil, :/],
16
18
  :"./" => [nil, :"./"],
@@ -58,20 +60,20 @@ class Rouge::Symbol
58
60
  end
59
61
 
60
62
  def to_sym
61
- :"#{@ns ? "#@ns/" : ""}#@name"
63
+ :"#{to_s}"
62
64
  end
63
65
 
64
66
  def inspect
65
67
  "Rouge::Symbol[#{to_sym.inspect}]"
66
68
  end
67
69
 
68
- def to_s; inspect; end
70
+ def to_s
71
+ "#{@ns ? "#@ns/" : ""}#@name"
72
+ end
69
73
 
70
74
  def ==(right)
71
75
  right.is_a?(Rouge::Symbol) and right.ns == @ns and right.name == @name
72
76
  end
73
-
74
- attr_reader :ns, :name, :ns_s, :name_s, :name_parts, :new_sym
75
77
  end
76
78
 
77
79
  # vim: set sw=2 et cc=80:
@@ -1,29 +1,31 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  class Rouge::Var
4
+ attr_reader :ns, :name
5
+
4
6
  @@stack = []
5
7
 
6
8
  def initialize(ns, name, root=Rouge::Var::UnboundSentinel)
7
- @ns = ns
8
- @name = name
9
9
  raise ArgumentError, "bad var ns" unless ns.is_a? Symbol
10
10
  raise ArgumentError, "bad var name" unless name.is_a? Symbol
11
+
12
+ @ns = ns
13
+ @name = name
14
+
11
15
  if root == Rouge::Var::UnboundSentinel
12
- @root = Rouge::Var::Unbound.new self
16
+ @root = Rouge::Var::Unbound.new(self)
13
17
  else
14
18
  @root = root
15
19
  end
16
20
  end
17
21
 
18
- def ==(var)
19
- var.is_a?(Rouge::Var) and @ns == var.ns and @name == var.name
22
+ def ==(other)
23
+ other.is_a?(Rouge::Var) && @ns == other.ns && @name == other.name
20
24
  end
21
25
 
22
- attr_reader :ns, :name
23
-
24
26
  def deref
25
27
  @@stack.reverse_each do |map|
26
- if map.include? @name
28
+ if map.include?(@name)
27
29
  return map[@name]
28
30
  end
29
31
  end
@@ -32,11 +34,11 @@ class Rouge::Var
32
34
  end
33
35
 
34
36
  def inspect
35
- "Rouge::Var.new(#{@ns.inspect}, #{@name.inspect}, #{@root.inspect})"
37
+ "#<Rouge::Var: (#{@ns.inspect}, #{@name.inspect}, #{@root.inspect})>"
36
38
  end
37
39
 
38
40
  def to_s
39
- inspect
41
+ "#'#@ns/#@name"
40
42
  end
41
43
 
42
44
  def self.push(map)
@@ -48,22 +50,26 @@ class Rouge::Var
48
50
  end
49
51
  end
50
52
 
51
- Rouge::Var::UnboundSentinel = Object.new
52
- class << Rouge::Var::UnboundSentinel
53
- def inspect; "#<Rouge::Var::UnboundSentinel>"; end
54
- def to_s; inspect; end
53
+ class Rouge::Var::UnboundSentinel
54
+ def self.inspect
55
+ "#<Rouge::Var::UnboundSentinel>"
56
+ end
55
57
  end
56
58
 
57
59
  class Rouge::Var::Unbound
60
+ attr_reader :var
61
+
58
62
  def initialize(var)
59
63
  @var = var
60
64
  end
61
65
 
62
- def ==(ub)
63
- @var == ub.var
66
+ def ==(other)
67
+ @var == other.var
64
68
  end
65
69
 
66
- attr_reader :var
70
+ def inspect
71
+ "#<Rouge::Var::Unbound: #@var>"
72
+ end
67
73
  end
68
74
 
69
75
  # vim: set sw=2 et cc=80:
@@ -1,7 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module Rouge
4
- VERSION = "0.0.9"
4
+ VERSION = "0.0.10"
5
5
  end
6
6
 
7
7
  # vim: set sw=2 et cc=80:
@@ -2,6 +2,8 @@
2
2
 
3
3
  [:Macro, :Builtin, :Dequote, :Splice].each do |name|
4
4
  Rouge.const_set name, Class.new {
5
+ attr_reader :inner
6
+
5
7
  def initialize(inner)
6
8
  @inner = inner
7
9
  end
@@ -14,14 +16,16 @@
14
16
  "#{self.class.name}[#{@inner.inspect}]"
15
17
  end
16
18
 
17
- def to_s; inspect; end
18
-
19
19
  def ==(right)
20
20
  right.is_a?(self.class) and right.inner == @inner
21
21
  end
22
-
23
- attr_reader :inner
24
22
  }
25
23
  end
26
24
 
25
+ class Rouge::Builtin
26
+ def to_s
27
+ "rouge.builtin/#{inner.name}"
28
+ end
29
+ end
30
+
27
31
  # vim: set sw=2 et cc=80:
@@ -30,7 +30,9 @@ describe Rouge::Printer do
30
30
  end
31
31
 
32
32
  context "string-symbols" do
33
- it { Rouge.print(:"!ruby!", "").should eq ":\"!ruby!\"" }
33
+ it { Rouge.print(:"!ruby!", "").should eq ":!ruby!" }
34
+ it { Rouge.print(:"lady-bird", "").should eq ":lady-bird" }
35
+ it { Rouge.print(:"bean sprouts", "").should eq ":\"bean sprouts\"" }
34
36
  end
35
37
  end
36
38
 
@@ -110,12 +110,12 @@ describe Rouge::Seq::Cons do
110
110
  Rouge::Seq::Cons.new(3, Rouge::Seq::Empty))) }
111
111
  end
112
112
 
113
- describe "#inspect" do
114
- it { Rouge::Seq::Cons[].inspect.should eq "()" }
115
- it { Rouge::Seq::Cons[1].inspect.should eq "(1)" }
116
- it { Rouge::Seq::Cons[1, 2].inspect.should eq "(1 2)" }
117
- it { Rouge::Seq::Cons[1, 2, 3].inspect.should eq "(1 2 3)" }
118
- it { Rouge::Seq::Cons[1, 2, 3].tail.inspect.should eq "(2 3)" }
113
+ describe "#to_s" do
114
+ it { Rouge::Seq::Cons[].to_s.should eq "()" }
115
+ it { Rouge::Seq::Cons[1].to_s.should eq "(1)" }
116
+ it { Rouge::Seq::Cons[1, 2].to_s.should eq "(1 2)" }
117
+ it { Rouge::Seq::Cons[1, 2, 3].to_s.should eq "(1 2 3)" }
118
+ it { Rouge::Seq::Cons[1, 2, 3].tail.to_s.should eq "(2 3)" }
119
119
  end
120
120
 
121
121
  describe "the ASeq implementation" do
@@ -30,6 +30,11 @@ describe Rouge::Symbol do
30
30
  it { Rouge::Symbol[:boo].to_sym.should eq :boo }
31
31
  it { Rouge::Symbol[:"what/nice"].to_sym.should eq :"what/nice" }
32
32
  end
33
+
34
+ describe "#to_s" do
35
+ it { Rouge::Symbol[:"breakfast"].to_s.should eq "breakfast" }
36
+ it { Rouge::Symbol[:"breakfast/toast"].to_s.should eq "breakfast/toast" }
37
+ end
33
38
  end
34
39
 
35
40
  # vim: set sw=2 et cc=80:
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.9
4
+ version: 0.0.10
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-12-28 00:00:00.000000000 Z
12
+ date: 2013-01-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -188,7 +188,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
188
188
  version: '0'
189
189
  segments:
190
190
  - 0
191
- hash: -574578803
191
+ hash: -185805507
192
192
  required_rubygems_version: !ruby/object:Gem::Requirement
193
193
  none: false
194
194
  requirements:
@@ -197,7 +197,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
197
197
  version: '0'
198
198
  segments:
199
199
  - 0
200
- hash: -574578803
200
+ hash: -185805507
201
201
  requirements: []
202
202
  rubyforge_project:
203
203
  rubygems_version: 1.8.24