Class: Piglet::Interpreter

Inherits:
Object
  • Object
show all
Defined in:
lib/piglet/interpreter.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Interpreter

Returns a new instance of Interpreter.



8
9
10
11
12
# File 'lib/piglet/interpreter.rb', line 8

def initialize(&block)
  @top_level_statements = [ ]
  
  interpret(&block) if block_given?
end

Instance Method Details

#interpret(&block) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/piglet/interpreter.rb', line 14

def interpret(&block)
  if block_given?
    instance_eval(&block)
  end

  self
end

#next_relation_aliasObject



42
43
44
45
46
# File 'lib/piglet/interpreter.rb', line 42

def next_relation_alias
  @counter ||= 0
  @counter += 1
  "relation_#{@counter}"
end

#to_pig_latin(&block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/piglet/interpreter.rb', line 22

def to_pig_latin(&block)
  interpret(&block) if block_given?
  
  return '' if @top_level_statements.empty?
  
  handled_relations = Set.new
  statements = [ ]
  
  @top_level_statements.each do |top_level_statement|
    if top_level_statement.respond_to?(:relation) && ! top_level_statement.relation.nil?
      assignments(top_level_statement.relation, handled_relations).each do |assignment|
        statements << assignment
      end
    end
    statements << top_level_statement
  end
  
  statements.flatten.map { |s| s.to_s }.join(";\n") + ";\n"
end