Class: Dentaku::PrintVisitor
- Inherits:
-
Object
- Object
- Dentaku::PrintVisitor
- Defined in:
- lib/dentaku/print_visitor.rb
Instance Method Summary collapse
-
#initialize(node) ⇒ PrintVisitor
constructor
A new instance of PrintVisitor.
- #should_output?(node, precedence, output_on_equal) ⇒ Boolean
- #to_s ⇒ Object
- #visit_access(node) ⇒ Object
- #visit_array(node) ⇒ Object
- #visit_case(node) ⇒ Object
- #visit_case_conditional(node) ⇒ Object
- #visit_else(node) ⇒ Object
- #visit_function(node) ⇒ Object
- #visit_identifier(node) ⇒ Object
- #visit_literal(node) ⇒ Object
- #visit_negation(node) ⇒ Object
- #visit_nil(node) ⇒ Object
- #visit_operand(node, precedence, prefix: "", suffix: "", dir: :none) ⇒ Object
- #visit_operation(node) ⇒ Object
- #visit_switch(node) ⇒ Object
- #visit_then(node) ⇒ Object
- #visit_when(node) ⇒ Object
Constructor Details
#initialize(node) ⇒ PrintVisitor
Returns a new instance of PrintVisitor.
3 4 5 6 |
# File 'lib/dentaku/print_visitor.rb', line 3 def initialize(node) @output = '' node.accept(self) end |
Instance Method Details
#should_output?(node, precedence, output_on_equal) ⇒ Boolean
28 29 30 31 32 33 |
# File 'lib/dentaku/print_visitor.rb', line 28 def should_output?(node, precedence, output_on_equal) return false unless node.is_a?(Dentaku::AST::Operation) target_precedence = node.class.precedence target_precedence < precedence || (output_on_equal && target_precedence == precedence) end |
#to_s ⇒ Object
108 109 110 |
# File 'lib/dentaku/print_visitor.rb', line 108 def to_s @output end |
#visit_access(node) ⇒ Object
85 86 87 88 89 90 |
# File 'lib/dentaku/print_visitor.rb', line 85 def visit_access(node) node.structure.accept(self) @output << "[" node.index.accept(self) @output << "]" end |
#visit_array(node) ⇒ Object
104 105 106 |
# File 'lib/dentaku/print_visitor.rb', line 104 def visit_array(node) @output << node.value.to_s end |
#visit_case(node) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/dentaku/print_visitor.rb', line 46 def visit_case(node) @output << "CASE " node.switch.accept(self) node.conditions.each { |c| c.accept(self) } node.else && node.else.accept(self) @output << " END" end |
#visit_case_conditional(node) ⇒ Object
58 59 60 61 |
# File 'lib/dentaku/print_visitor.rb', line 58 def visit_case_conditional(node) node.when.accept(self) node.then.accept(self) end |
#visit_else(node) ⇒ Object
73 74 75 76 |
# File 'lib/dentaku/print_visitor.rb', line 73 def visit_else(node) @output << " ELSE " node.node.accept(self) end |
#visit_function(node) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/dentaku/print_visitor.rb', line 35 def visit_function(node) @output << node.name @output << "(" arg_count = node.args.length node.args.each_with_index do |a, index| a.accept(self) @output << ", " unless index >= arg_count - 1 end @output << ")" end |
#visit_identifier(node) ⇒ Object
96 97 98 |
# File 'lib/dentaku/print_visitor.rb', line 96 def visit_identifier(node) @output << node.identifier end |
#visit_literal(node) ⇒ Object
92 93 94 |
# File 'lib/dentaku/print_visitor.rb', line 92 def visit_literal(node) @output << node.quoted end |
#visit_negation(node) ⇒ Object
78 79 80 81 82 83 |
# File 'lib/dentaku/print_visitor.rb', line 78 def visit_negation(node) @output << "-" @output << "(" unless node.node.is_a? Dentaku::AST::Literal node.node.accept(self) @output << ")" unless node.node.is_a? Dentaku::AST::Literal end |
#visit_nil(node) ⇒ Object
100 101 102 |
# File 'lib/dentaku/print_visitor.rb', line 100 def visit_nil(node) @output << "NULL" end |
#visit_operand(node, precedence, prefix: "", suffix: "", dir: :none) ⇒ Object
20 21 22 23 24 25 26 |
# File 'lib/dentaku/print_visitor.rb', line 20 def visit_operand(node, precedence, prefix: "", suffix: "", dir: :none) @output << prefix @output << "(" if should_output?(node, precedence, dir == :right) node.accept(self) @output << ")" if should_output?(node, precedence, dir == :right) @output << suffix end |
#visit_operation(node) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/dentaku/print_visitor.rb', line 8 def visit_operation(node) if node.left visit_operand(node.left, node.class.precedence, suffix: " ", dir: :left) end @output << node.display_operator if node.right visit_operand(node.right, node.class.precedence, prefix: " ", dir: :right) end end |
#visit_switch(node) ⇒ Object
54 55 56 |
# File 'lib/dentaku/print_visitor.rb', line 54 def visit_switch(node) node.node.accept(self) end |
#visit_then(node) ⇒ Object
68 69 70 71 |
# File 'lib/dentaku/print_visitor.rb', line 68 def visit_then(node) @output << " THEN " node.node.accept(self) end |
#visit_when(node) ⇒ Object
63 64 65 66 |
# File 'lib/dentaku/print_visitor.rb', line 63 def visit_when(node) @output << " WHEN " node.node.accept(self) end |