Permalink
Browse files

Rewrite library in a better style

* And specs still pass!
  • Loading branch information...
Markus Schirp
Markus Schirp committed Jan 7, 2013
1 parent c010754 commit 38ed345f3456149cd2d4b4f9181aae0c68fa1728
Showing with 2,381 additions and 1,902 deletions.
  1. +68 −3 lib/to_source.rb
  2. +41 −0 lib/to_source/buffer.rb
  3. +44 −0 lib/to_source/command.rb
  4. +76 −0 lib/to_source/emitter.rb
  5. +246 −0 lib/to_source/emitter.rb~
  6. +21 −0 lib/to_source/emitter/access.rb
  7. +45 −0 lib/to_source/emitter/actual_arguments.rb
  8. +17 −0 lib/to_source/emitter/alias.rb
  9. +81 −0 lib/to_source/emitter/assignment.rb
  10. +18 −0 lib/to_source/emitter/attribute_assignment.rb
  11. +51 −0 lib/to_source/emitter/begin.rb
  12. +36 −0 lib/to_source/emitter/binary_operator.rb
  13. +28 −0 lib/to_source/emitter/binary_operator_method.rb
  14. +24 −0 lib/to_source/emitter/block.rb
  15. +16 −0 lib/to_source/emitter/block_argument.rb
  16. +15 −0 lib/to_source/emitter/block_pass.rb
  17. +28 −0 lib/to_source/emitter/class.rb
  18. +20 −0 lib/to_source/emitter/concat_arguments.rb
  19. +14 −0 lib/to_source/emitter/default_arguments.rb
  20. +62 −0 lib/to_source/emitter/define.rb
  21. +16 −0 lib/to_source/emitter/defined.rb
  22. +19 −0 lib/to_source/emitter/element_assignment.rb
  23. +15 −0 lib/to_source/emitter/element_reference.rb
  24. +13 −0 lib/to_source/emitter/empty_body.rb
  25. +22 −0 lib/to_source/emitter/ensure.rb
  26. +17 −0 lib/to_source/emitter/ensure_body.rb
  27. +14 −0 lib/to_source/emitter/execute_string.rb
  28. +111 −0 lib/to_source/emitter/formal_arguments.rb
  29. +50 −0 lib/to_source/emitter/if.rb
  30. +19 −0 lib/to_source/emitter/iter.rb
  31. +41 −0 lib/to_source/emitter/keyword_value.rb
  32. +240 −0 lib/to_source/emitter/literal.rb
  33. +16 −0 lib/to_source/emitter/match3.rb
  34. +21 −0 lib/to_source/emitter/module.rb
  35. +22 −0 lib/to_source/emitter/multiple_assignment.rb
  36. +14 −0 lib/to_source/emitter/nth_ref.rb
  37. +18 −0 lib/to_source/emitter/op_assign1.rb
  38. +18 −0 lib/to_source/emitter/op_assign2.rb
  39. +16 −0 lib/to_source/emitter/pattern_arguments.rb
  40. +35 −0 lib/to_source/emitter/receiver_case.rb
  41. +22 −0 lib/to_source/emitter/rescue.rb
  42. +58 −0 lib/to_source/emitter/rescue_condition.rb
  43. +18 −0 lib/to_source/emitter/scope.rb
  44. +16 −0 lib/to_source/emitter/scope_name.rb
  45. +17 −0 lib/to_source/emitter/scoped_name.rb
  46. +47 −0 lib/to_source/emitter/send.rb
  47. +61 −0 lib/to_source/emitter/send_with_arguments.rb
  48. +23 −0 lib/to_source/emitter/singleton_class.rb
  49. +18 −0 lib/to_source/emitter/splat.rb
  50. +15 −0 lib/to_source/emitter/splat_when.rb
  51. +32 −0 lib/to_source/emitter/static.rb
  52. +46 −0 lib/to_source/emitter/super.rb
  53. +14 −0 lib/to_source/emitter/to_array.rb
  54. +16 −0 lib/to_source/emitter/to_string.rb
  55. +16 −0 lib/to_source/emitter/toplevel.rb
  56. +20 −0 lib/to_source/emitter/unary_operator_method.rb
  57. +15 −0 lib/to_source/emitter/unless.rb
  58. +20 −0 lib/to_source/emitter/until.rb
  59. +20 −0 lib/to_source/emitter/util.rb
  60. +38 −0 lib/to_source/emitter/when.rb
  61. +20 −0 lib/to_source/emitter/while.rb
  62. +18 −0 lib/to_source/emitter/yield.rb
  63. +16 −0 lib/to_source/emitter/z_super.rb
  64. +59 −0 lib/to_source/state.rb
  65. +0 −1,884 lib/to_source/visitor.rb
  66. +46 −15 spec/unit/to_source/{visitor → }/class_methods/run_spec.rb
  67. +2 −0 to_source.gemspec
View
@@ -1,9 +1,73 @@
require 'set'
require 'melbourne'
-require 'to_source/visitor'
+require 'adamantium'
+require 'abstract_type'
+require 'equalizer'
+require 'to_source/command'
+require 'to_source/state'
+require 'to_source/emitter'
+require 'to_source/emitter/literal'
+require 'to_source/emitter/access'
+require 'to_source/emitter/formal_arguments'
+require 'to_source/emitter/actual_arguments'
+require 'to_source/emitter/scope'
+require 'to_source/emitter/define'
+require 'to_source/emitter/assignment'
+require 'to_source/emitter/static'
+require 'to_source/emitter/block'
+require 'to_source/emitter/toplevel'
+require 'to_source/emitter/keyword_value'
+require 'to_source/emitter/execute_string'
+require 'to_source/emitter/singleton_class'
+require 'to_source/emitter/empty_body'
+require 'to_source/emitter/rescue_condition'
+require 'to_source/emitter/rescue'
+require 'to_source/emitter/ensure_body'
+require 'to_source/emitter/scope_name'
+require 'to_source/emitter/nth_ref'
+require 'to_source/emitter/scoped_name'
+require 'to_source/emitter/send'
+require 'to_source/emitter/send_with_arguments'
+require 'to_source/emitter/block_pass'
+require 'to_source/emitter/iter'
+require 'to_source/emitter/pattern_arguments'
+require 'to_source/emitter/block_argument'
+require 'to_source/emitter/unary_operator_method'
+require 'to_source/emitter/binary_operator_method'
+require 'to_source/emitter/binary_operator'
+require 'to_source/emitter/element_reference'
+require 'to_source/emitter/to_array'
+require 'to_source/emitter/to_string'
+require 'to_source/emitter/defined'
+require 'to_source/emitter/attribute_assignment'
+require 'to_source/emitter/element_assignment'
+require 'to_source/emitter/if'
+require 'to_source/emitter/while'
+require 'to_source/emitter/ensure'
+require 'to_source/emitter/receiver_case'
+require 'to_source/emitter/when'
+require 'to_source/emitter/splat_when'
+require 'to_source/emitter/unless'
+require 'to_source/emitter/until'
+require 'to_source/emitter/class'
+require 'to_source/emitter/module'
+require 'to_source/emitter/op_assign2'
+require 'to_source/emitter/op_assign1'
+require 'to_source/emitter/z_super'
+require 'to_source/emitter/default_arguments'
+require 'to_source/emitter/multiple_assignment'
+require 'to_source/emitter/concat_arguments'
+require 'to_source/emitter/super'
+require 'to_source/emitter/match3'
+require 'to_source/emitter/yield'
+require 'to_source/emitter/alias'
+require 'to_source/emitter/splat'
+require 'to_source/emitter/begin'
+require 'to_source/emitter/util'
-# Namespace of library
+# Library namespace
module ToSource
+
# Convert node to string
#
# @param [Rubinius::AST::Node] node
@@ -13,6 +77,7 @@ module ToSource
# @api private
#
def self.to_source(node)
- Visitor.run(node)
+ Emitter.run(node)
end
+
end
View
@@ -0,0 +1,41 @@
+module ToSource
+
+ class Buffer
+ include Adamantium::Flat
+
+ attr_reader :lines
+
+ def initialize(lines = [])
+ @lines = lines
+ end
+
+ def to_s
+ @lines.join("\n")
+ end
+
+ def indent
+ new(lines.map { |line| " #{line}" })
+ end
+
+ def body(body)
+ self
+ end
+
+ def class_open(name, superclass)
+ push('class')
+ end
+
+ def push(token)
+ new(lines.dup << token)
+ end
+
+ def end
+ push('end')
+ end
+
+ def new(*args)
+ self.class.new(*args)
+ end
+ end
+
+end
View
@@ -0,0 +1,44 @@
+module ToSource
+
+ class Command
+ include Adamantium::Flat
+
+ NULL = Class.new(self) do
+ def run(_state)
+ end
+ end.new.freeze
+
+ class Token < self
+ attr_reader :content
+
+ def run(state)
+ state.push(self)
+ end
+
+ private
+
+ def initialize(content)
+ @content = content
+ end
+ end
+
+ class Shift < self
+ include Equalizer.new(:width)
+
+ attr_reader :width
+
+ def run(state)
+ state.shift(width)
+ end
+
+ private
+
+ def initialize(width)
+ @width = width
+ end
+
+ INDENT = Shift.new( 2)
+ UNINDENT = Shift.new(-2)
+ end
+ end
+end
View
@@ -0,0 +1,76 @@
+module ToSource
+
+ class Emitter
+ include Adamantium::Flat, Equalizer.new(:node)
+
+ REGISTRY = {}
+
+ def self.handle(node_class)
+ REGISTRY[node_class]=self
+ end
+
+ def self.build(node, buffer = [])
+ REGISTRY.fetch(node.class).new(node, buffer)
+ end
+
+ def self.run(node)
+ build(node).source
+ end
+
+
+ def source
+ state = State.new
+ buffer.each do |command|
+ state.execute(command)
+ end
+ state.source
+ end
+
+ attr_reader :node
+
+ private
+
+ def push(command)
+ buffer.push(command)
+ end
+
+ def new_line
+ emit("\n")
+ end
+
+ def space
+ emit(' ')
+ end
+
+ def emit_end
+ emit(:end)
+ end
+
+ def indent
+ push(Command::Shift::INDENT)
+ end
+
+ def unindent
+ push(Command::Shift::UNINDENT)
+ end
+
+ def emit(name)
+ push(Command::Token.new(name))
+ end
+
+ def visit(node)
+ self.class.build(node, buffer)
+ end
+
+ def run(emitter, node = self.node)
+ emitter.new(node, buffer)
+ end
+
+ attr_reader :buffer
+
+ def initialize(node, buffer)
+ @node, @buffer = node, buffer
+ dispatch
+ end
+ end
+end
Oops, something went wrong.

0 comments on commit 38ed345

Please sign in to comment.