Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Arlen Christian Mart Cuss committed Sep 24, 2012
1 parent 2fdbde8 commit 837d2ca
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 24 deletions.
4 changes: 2 additions & 2 deletions lib/rouge.rb
Expand Up @@ -9,8 +9,8 @@ class << Rouge
require 'rouge/context'
require 'rouge/repl'

def print(form)
Rouge::Printer.print form
def print(form, out)
Rouge::Printer.print form, out
end

def [](ns)
Expand Down
60 changes: 40 additions & 20 deletions lib/rouge/printer.rb
Expand Up @@ -4,48 +4,68 @@
module Rouge::Printer
class UnknownFormError < StandardError; end

def self.print(form)
def self.print(form, out)
case form
when Integer
form.to_s
out << form.to_s
when Rouge::Symbol
form.inner.to_s
out << form.inner.to_s
when Symbol
form.inspect
out << form.inspect
when String
form.inspect
when Array
"[#{form.map {|e| print e}.join " "}]"
out << form.inspect
when Rouge::Cons::Empty
"()"
out << "()"
when Rouge::Cons
if form.length == 2 and form[0] == Rouge::Symbol[:quote]
"'#{print form[1]}"
out << "'"
print(form[1], out)
elsif form.length == 2 and form[0] == Rouge::Symbol[:var]
"#'#{print form[1]}"
out << "#'"
print(form[1], out)
else
"(#{form.map {|e| print e}.join " "})"
out << "("
len = form.length
form.each.with_index do |e, i|
out << " " unless i.zero?
print(e, out)
end
out << ")"
end
when Array
out << "["
form.each.with_index do |e, i|
out << " " unless i.zero?
print(e, out)
end
out << "]"
when Rouge::Var
"#'#{form.name}"
out << "#'#{form.name}"
when Hash
"{#{form.map {|k,v| print(k) + " " + print(v)}.join ", "}}"
out << "{"
form.each.with_index do |kv,i|
out << ", " unless i.zero?
print(kv[0], out)
out << " "
print(kv[1], out)
end
out << "}"
when NilClass
"nil"
out << "nil"
when TrueClass
"true"
out << "true"
when FalseClass
"false"
out << "false"
when Class, Module
if form.name
"ruby/#{form.name.split('::').join('.')}"
out << "ruby/#{form.name.split('::').join('.')}"
else
form.inspect
out << form.inspect
end
when Rouge::Builtin
"rouge.builtin/#{form.inner.name}"
out << "rouge.builtin/#{form.inner.name}"
else
form.inspect
out << form.inspect
end
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/rouge/repl.rb
Expand Up @@ -41,7 +41,8 @@ def repl(argv)
chaining = false
begin
result = context.eval(form)
STDOUT.puts Rouge.print(result)
Rouge.print(result, STDOUT)
STDOUT.puts

count += 1 if count < 10
count.downto(2) do |i|
Expand Down
16 changes: 15 additions & 1 deletion spec/rouge_spec.rb
Expand Up @@ -2,6 +2,7 @@
require 'spec_helper'
require 'rouge'
require 'term/ansicolor'
require 'ruby-prof'

describe Rouge do
before do
Expand Down Expand Up @@ -35,7 +36,14 @@
describe "the Rouge specs" do
Dir[relative_to_spec("*.rg")].each do |file|
it "should pass #{File.basename file}" do
r = Rouge::Context.new(Rouge[:user]).readeval(File.read(file))
RubyProf.start

begin
r = Rouge::Context.new(Rouge[:user]).readeval(File.read(file))
rescue => e
r = {:passed => 0, :failed => []}
end

total = r[:passed] + r[:failed].length

message =
Expand All @@ -51,6 +59,12 @@
else
STDOUT.puts Term::ANSIColor.green(message)
end

result = RubyProf.stop

File.open("prof-#{file}.html", "w") do |f|
RubyProf::GraphHtmlPrinter.new(result).print(f)
end
end
end
end
Expand Down

0 comments on commit 837d2ca

Please sign in to comment.