Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add basic reading, printing, and context handling for sets

  • Loading branch information...
commit 3ed9eab71f8bbf3085013216f92ed991127f6209 1 parent 3b110ea
@noprompt noprompt authored
View
1  lib/rouge.rb
@@ -18,6 +18,7 @@ module Rouge
require 'rouge/printer'
require 'rouge/context'
require 'rouge/repl'
+ require 'set'
def self.print(form, out)
Rouge::Printer.print form, out
View
12 lib/rouge/context.rb
@@ -210,6 +210,18 @@ def eval_call(fun, args, block)
raise ArgumentError,
"Wrong number of args (#{num_args}) passed to ruby/Hash"
end
+ when Set
+ if num_args == 1
+ el = args[0]
+ if fun.include? el
+ el
+ else
+ nil
+ end
+ else
+ raise ArgumentError,
+ "Wrong number of args (#{num_args}) passed to ruby/Set"
+ end
else
fun.call(*args, &block)
end
View
7 lib/rouge/printer.rb
@@ -53,6 +53,13 @@ def self.print(form, out)
print(kv[1], out)
end
out << "}"
+ when Set
+ out << "\#{"
+ form.each_with_index do |el, i|
+ print el, out
+ out << " " unless i == (form.size - 1)
+ end
+ out << "}"
when NilClass
out << "nil"
when TrueClass
View
43 lib/rouge/reader.rb
@@ -16,7 +16,6 @@ def initialize(ns, input)
@gensyms = []
end
-
def lex
r =
case peek
@@ -247,32 +246,47 @@ def dequote form
end
def regexp
- s = ""
- t = '"'
+ expression = ""
+ terminator = '"'
+
while true
- c = @src[@n]
+ char = @src[@n]
- if c.nil?
- reader_raise EndOfDataError, "in regexp, got: #{s}"
+ if char.nil?
+ reader_raise EndOfDataError, "in regexp, got: #{expression}"
end
@n += 1
- if c == t
+ if char == terminator
break
end
- if c == ?\\
- c = "\\"
- if peek == ?"
- c << consume
+ if char == ?\\
+ char = "\\"
+
+ # Prevent breaking early.
+ if peek == terminator
+ char << consume
end
end
- s << c
+ expression << char
end
- Regexp.new(s).freeze
+ Regexp.new(expression).freeze
+ end
+
+ def set
+ s = Set.new
+
+ until peek == '}'
+ el = lex
+ s.add el
+ end
+
+ consume
+ s.freeze
end
def dispatch
@@ -284,6 +298,9 @@ def dispatch
Rouge::Symbol[:fn],
(1..count).map {|n| Rouge::Symbol[:"%#{n}"]}.freeze,
body]
+ when "{"
+ consume
+ set
when "'"
consume
Rouge::Seq::Cons[Rouge::Symbol[:var], lex]
Please sign in to comment.
Something went wrong with that request. Please try again.