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.



9
10
11
12
13
# File 'lib/treetop/compiler/ruby_builder.rb', line 9

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

Instance Attribute Details

#address_spaceObject (readonly)

Returns the value of attribute address_space.



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

def address_space
  @address_space
end

#levelObject (readonly)

Returns the value of attribute level.



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

def level
  @level
end

#rubyObject (readonly)

Returns the value of attribute ruby.



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

def ruby
  @ruby
end

Instance Method Details

#<<(ruby_line) ⇒ Object



15
16
17
18
# File 'lib/treetop/compiler/ruby_builder.rb', line 15

def <<(ruby_line)              
  return if ruby_line == ''
  ruby << ruby_line.tabto(level) << "\n"
end

#accumulate(left, right) ⇒ Object



60
61
62
# File 'lib/treetop/compiler/ruby_builder.rb', line 60

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

#assign(left, right) ⇒ Object



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

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

#breakObject



86
87
88
# File 'lib/treetop/compiler/ruby_builder.rb', line 86

def break
  self << 'break'
end

#class_declaration(name, &block) ⇒ Object



30
31
32
33
34
# File 'lib/treetop/compiler/ruby_builder.rb', line 30

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

#else_(&block) ⇒ Object



74
75
76
77
78
# File 'lib/treetop/compiler/ruby_builder.rb', line 74

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

#extend(var, module_name) ⇒ Object



56
57
58
# File 'lib/treetop/compiler/ruby_builder.rb', line 56

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

#if_(condition, &block) ⇒ Object



69
70
71
72
# File 'lib/treetop/compiler/ruby_builder.rb', line 69

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

#if__(condition, &block) ⇒ Object



64
65
66
67
# File 'lib/treetop/compiler/ruby_builder.rb', line 64

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

#in(depth = 2) ⇒ Object



90
91
92
93
# File 'lib/treetop/compiler/ruby_builder.rb', line 90

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

#indented(depth = 2) ⇒ Object



24
25
26
27
28
# File 'lib/treetop/compiler/ruby_builder.rb', line 24

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

#loop(&block) ⇒ Object



80
81
82
83
84
# File 'lib/treetop/compiler/ruby_builder.rb', line 80

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

#method_declaration(name, &block) ⇒ Object



42
43
44
45
46
# File 'lib/treetop/compiler/ruby_builder.rb', line 42

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

#module_declaration(name, &block) ⇒ Object



36
37
38
39
40
# File 'lib/treetop/compiler/ruby_builder.rb', line 36

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

#newlineObject



20
21
22
# File 'lib/treetop/compiler/ruby_builder.rb', line 20

def newline
  ruby << "\n"
end

#next_addressObject



100
101
102
# File 'lib/treetop/compiler/ruby_builder.rb', line 100

def next_address
  address_space.next_address
end

#out(depth = 2) ⇒ Object



95
96
97
98
# File 'lib/treetop/compiler/ruby_builder.rb', line 95

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

#reset_addressesObject



104
105
106
# File 'lib/treetop/compiler/ruby_builder.rb', line 104

def reset_addresses
  address_space.reset_addresses
end