Class: Voodoo::CommonCodeGenerator::Environment

Inherits:
Object
  • Object
show all
Defined in:
lib/voodoo/generators/common_code_generator.rb

Constant Summary collapse

@@gensym_counter =
0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ Environment

Creates a new environment. If parent is specified, it will become the new environment’s parent. The new environment inherits all symbols from the parent environment.



255
256
257
258
259
260
261
262
263
264
# File 'lib/voodoo/generators/common_code_generator.rb', line 255

def initialize parent = nil
  ## Parent environment
  @parent = parent
  ## Symbol lookup table
  @symbols = parent ? parent.symbols.dup : {}
  ## Number of arguments
  @args = parent ? parent.args : 0
  ## Number of local variables
  @locals = parent ? parent.locals : 0
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



250
251
252
# File 'lib/voodoo/generators/common_code_generator.rb', line 250

def args
  @args
end

#localsObject (readonly)

Returns the value of attribute locals.



250
251
252
# File 'lib/voodoo/generators/common_code_generator.rb', line 250

def locals
  @locals
end

#parentObject (readonly)

Returns the value of attribute parent.



250
251
252
# File 'lib/voodoo/generators/common_code_generator.rb', line 250

def parent
  @parent
end

#symbolsObject (readonly)

Returns the value of attribute symbols.



250
251
252
# File 'lib/voodoo/generators/common_code_generator.rb', line 250

def symbols
  @symbols
end

Class Method Details

.gensymObject

Generates a new, unique symbol.



301
302
303
304
# File 'lib/voodoo/generators/common_code_generator.rb', line 301

def self.gensym
  @@gensym_counter = @@gensym_counter + 1
  "_G#{@@gensym_counter}".to_sym
end

.initial_environmentObject

Returns an initial, top-level environment.



307
308
309
# File 'lib/voodoo/generators/common_code_generator.rb', line 307

def self.initial_environment
  Environment.new
end

Instance Method Details

#[](symbol) ⇒ Object

Looks up symbol in this environment.



296
297
298
# File 'lib/voodoo/generators/common_code_generator.rb', line 296

def [] symbol
  @symbols[symbol]
end

#add_arg(symbol) ⇒ Object

Adds symbol as an argument in this environment.



267
268
269
270
# File 'lib/voodoo/generators/common_code_generator.rb', line 267

def add_arg symbol
  @symbols[symbol] = [:arg, @args]
  @args = @args + 1
end

#add_args(symbols) ⇒ Object

Adds each of symbols to the arguments in this environment.



274
275
276
# File 'lib/voodoo/generators/common_code_generator.rb', line 274

def add_args symbols
  symbols.each { |sym| add_arg sym }
end

#add_local(symbol) ⇒ Object

Adds symbol as a local variable in this environment.



279
280
281
282
# File 'lib/voodoo/generators/common_code_generator.rb', line 279

def add_local symbol
  @symbols[symbol] = [:local, @locals]
  @locals = @locals + 1
end

#add_locals(symbols) ⇒ Object

Adds each of symbols to the local variables in this environment.



286
287
288
# File 'lib/voodoo/generators/common_code_generator.rb', line 286

def add_locals symbols
  symbols.each { |sym| add_local sym }
end

#gensymObject

Generates a new, unique symbol.



291
292
293
# File 'lib/voodoo/generators/common_code_generator.rb', line 291

def gensym
  Environment.gensym
end