Class: Dentaku::PrintVisitor

Inherits:
Object
  • Object
show all
Defined in:
lib/dentaku/print_visitor.rb

Instance Method Summary collapse

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

#to_sObject



97
98
99
# File 'lib/dentaku/print_visitor.rb', line 97

def to_s
  @output
end

#visit_access(node) ⇒ Object



78
79
80
81
82
83
# File 'lib/dentaku/print_visitor.rb', line 78

def visit_access(node)
  node.structure.accept(self)
  @output << "["
  node.index.accept(self)
  @output << "]"
end

#visit_case(node) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/dentaku/print_visitor.rb', line 39

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



51
52
53
54
# File 'lib/dentaku/print_visitor.rb', line 51

def visit_case_conditional(node)
  node.when.accept(self)
  node.then.accept(self)
end

#visit_else(node) ⇒ Object



66
67
68
69
# File 'lib/dentaku/print_visitor.rb', line 66

def visit_else(node)
  @output << " ELSE "
  node.node.accept(self)
end

#visit_function(node) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/dentaku/print_visitor.rb', line 28

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



89
90
91
# File 'lib/dentaku/print_visitor.rb', line 89

def visit_identifier(node)
  @output << node.identifier
end

#visit_literal(node) ⇒ Object



85
86
87
# File 'lib/dentaku/print_visitor.rb', line 85

def visit_literal(node)
  @output << node.quoted
end

#visit_negation(node) ⇒ Object



71
72
73
74
75
76
# File 'lib/dentaku/print_visitor.rb', line 71

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



93
94
95
# File 'lib/dentaku/print_visitor.rb', line 93

def visit_nil(node)
  @output << "NULL"
end

#visit_operand(node, precedence, prefix: "", suffix: "") ⇒ Object



20
21
22
23
24
25
26
# File 'lib/dentaku/print_visitor.rb', line 20

def visit_operand(node, precedence, prefix: "", suffix: "")
  @output << prefix
  @output << "(" if node.is_a?(Dentaku::AST::Operation) && node.class.precedence < precedence
  node.accept(self)
  @output << ")" if node.is_a?(Dentaku::AST::Operation) && node.class.precedence < precedence
  @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: " ")
  end

  @output << node.display_operator

  if node.right
    visit_operand(node.right, node.class.precedence, prefix: " ")
  end
end

#visit_switch(node) ⇒ Object



47
48
49
# File 'lib/dentaku/print_visitor.rb', line 47

def visit_switch(node)
  node.node.accept(self)
end

#visit_then(node) ⇒ Object



61
62
63
64
# File 'lib/dentaku/print_visitor.rb', line 61

def visit_then(node)
  @output << " THEN "
  node.node.accept(self)
end

#visit_when(node) ⇒ Object



56
57
58
59
# File 'lib/dentaku/print_visitor.rb', line 56

def visit_when(node)
  @output << " WHEN "
  node.node.accept(self)
end