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.
4 5 6 7 |
# File 'lib/dentaku/print_visitor.rb', line 4 def initialize(node) @output = '' node.accept(self) end |
Instance Method Details
#should_output?(node, precedence, output_on_equal) ⇒ Boolean
29 30 31 32 33 34 |
# File 'lib/dentaku/print_visitor.rb', line 29 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
109 110 111 |
# File 'lib/dentaku/print_visitor.rb', line 109 def to_s @output end |
#visit_access(node) ⇒ Object
86 87 88 89 90 91 |
# File 'lib/dentaku/print_visitor.rb', line 86 def visit_access(node) node.structure.accept(self) @output << "[" node.index.accept(self) @output << "]" end |
#visit_array(node) ⇒ Object
105 106 107 |
# File 'lib/dentaku/print_visitor.rb', line 105 def visit_array(node) @output << node.value.to_s end |
#visit_case(node) ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/dentaku/print_visitor.rb', line 47 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
59 60 61 62 |
# File 'lib/dentaku/print_visitor.rb', line 59 def visit_case_conditional(node) node.when.accept(self) node.then.accept(self) end |
#visit_else(node) ⇒ Object
74 75 76 77 |
# File 'lib/dentaku/print_visitor.rb', line 74 def visit_else(node) @output << " ELSE " node.node.accept(self) end |
#visit_function(node) ⇒ Object
36 37 38 39 40 41 42 43 44 45 |
# File 'lib/dentaku/print_visitor.rb', line 36 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
97 98 99 |
# File 'lib/dentaku/print_visitor.rb', line 97 def visit_identifier(node) @output << node.identifier end |
#visit_literal(node) ⇒ Object
93 94 95 |
# File 'lib/dentaku/print_visitor.rb', line 93 def visit_literal(node) @output << node.quoted end |
#visit_negation(node) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/dentaku/print_visitor.rb', line 79 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
101 102 103 |
# File 'lib/dentaku/print_visitor.rb', line 101 def visit_nil(node) @output << "NULL" end |
#visit_operand(node, precedence, prefix: "", suffix: "", dir: :none) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/dentaku/print_visitor.rb', line 21 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
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/dentaku/print_visitor.rb', line 9 def visit_operation(node) if node.left visit_operand(node.left, node.class.precedence, suffix: node.operator_spacing, dir: :left) end @output << node.display_operator if node.right visit_operand(node.right, node.class.precedence, prefix: node.operator_spacing, dir: :right) end end |
#visit_switch(node) ⇒ Object
55 56 57 |
# File 'lib/dentaku/print_visitor.rb', line 55 def visit_switch(node) node.node.accept(self) end |
#visit_then(node) ⇒ Object
69 70 71 72 |
# File 'lib/dentaku/print_visitor.rb', line 69 def visit_then(node) @output << " THEN " node.node.accept(self) end |
#visit_when(node) ⇒ Object
64 65 66 67 |
# File 'lib/dentaku/print_visitor.rb', line 64 def visit_when(node) @output << " WHEN " node.node.accept(self) end |