Class: Treetop::Compiler::RubyBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/treetop/compiler/ruby_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRubyBuilder

Returns a new instance of RubyBuilder.



7
8
9
10
11
# File 'lib/treetop/compiler/ruby_builder.rb', line 7

def initialize
  @level = 0
  @address_space = LexicalAddressSpace.new
  @ruby = ""
end

Instance Attribute Details

#address_spaceObject (readonly)

Returns the value of attribute address_space.



5
6
7
# File 'lib/treetop/compiler/ruby_builder.rb', line 5

def address_space
  @address_space
end

#levelObject (readonly)

Returns the value of attribute level.



5
6
7
# File 'lib/treetop/compiler/ruby_builder.rb', line 5

def level
  @level
end

#rubyObject (readonly)

Returns the value of attribute ruby.



5
6
7
# File 'lib/treetop/compiler/ruby_builder.rb', line 5

def ruby
  @ruby
end

Instance Method Details

#<<(ruby_line) ⇒ Object



13
14
15
16
# File 'lib/treetop/compiler/ruby_builder.rb', line 13

def <<(ruby_line)              
  return if ruby_line.blank?
  ruby << ruby_line.tabto(level) << "\n"
end

#accumulate(left, right) ⇒ Object



58
59
60
# File 'lib/treetop/compiler/ruby_builder.rb', line 58

def accumulate(left, right)
  self << "#{left} << #{right}"
end

#assign(left, right) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/treetop/compiler/ruby_builder.rb', line 46

def assign(left, right)
  if left.instance_of? Array
    self << "#{left.join(', ')} = #{right.join(', ')}"
  else
    self << "#{left} = #{right}"
  end
end

#breakObject



84
85
86
# File 'lib/treetop/compiler/ruby_builder.rb', line 84

def break
  self << 'break'
end

#class_declaration(name, &block) ⇒ Object



28
29
30
31
32
# File 'lib/treetop/compiler/ruby_builder.rb', line 28

def class_declaration(name, &block)
  self << "class #{name}"
  indented(&block)
  self << "end"
end

#else_(&block) ⇒ Object



72
73
74
75
76
# File 'lib/treetop/compiler/ruby_builder.rb', line 72

def else_(&block)
  self << 'else'
  indented(&block)
  self << 'end'
end

#extend(var, module_name) ⇒ Object



54
55
56
# File 'lib/treetop/compiler/ruby_builder.rb', line 54

def extend(var, module_name)
  self << "#{var}.extend(#{module_name})"
end

#if_(condition, &block) ⇒ Object



67
68
69
70
# File 'lib/treetop/compiler/ruby_builder.rb', line 67

def if_(condition, &block)
  if__(condition, &block)
  self << 'end'
end

#if__(condition, &block) ⇒ Object



62
63
64
65
# File 'lib/treetop/compiler/ruby_builder.rb', line 62

def if__(condition, &block)
  self << "if #{condition}"
  indented(&block)
end

#in(depth = 2) ⇒ Object



88
89
90
91
# File 'lib/treetop/compiler/ruby_builder.rb', line 88

def in(depth = 2)
  @level += depth
  self
end

#indented(depth = 2) ⇒ Object



22
23
24
25
26
# File 'lib/treetop/compiler/ruby_builder.rb', line 22

def indented(depth = 2)
  self.in(depth)
  yield
  self.out(depth)
end

#loop(&block) ⇒ Object



78
79
80
81
82
# File 'lib/treetop/compiler/ruby_builder.rb', line 78

def loop(&block)
  self << 'loop do'
  indented(&block)
  self << 'end'
end

#method_declaration(name, &block) ⇒ Object



40
41
42
43
44
# File 'lib/treetop/compiler/ruby_builder.rb', line 40

def method_declaration(name, &block)
  self << "def #{name}"
  indented(&block)
  self << "end"
end

#module_declaration(name, &block) ⇒ Object



34
35
36
37
38
# File 'lib/treetop/compiler/ruby_builder.rb', line 34

def module_declaration(name, &block)
  self << "module #{name}"
  indented(&block)
  self << "end"
end

#newlineObject



18
19
20
# File 'lib/treetop/compiler/ruby_builder.rb', line 18

def newline
  ruby << "\n"
end

#next_addressObject



98
99
100
# File 'lib/treetop/compiler/ruby_builder.rb', line 98

def next_address
  address_space.next_address
end

#out(depth = 2) ⇒ Object



93
94
95
96
# File 'lib/treetop/compiler/ruby_builder.rb', line 93

def out(depth = 2)
  @level -= depth
  self
end

#reset_addressesObject



102
103
104
# File 'lib/treetop/compiler/ruby_builder.rb', line 102

def reset_addresses
  address_space.reset_addresses
end