Class: Voodoo::CommonCodeGenerator::Environment

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

Overview

Used for scoping. Maintains a symbol table and keeps track of the number of bytes that have been allocated on the stack.

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.



651
652
653
654
655
656
657
658
659
660
661
662
663
664
# File 'lib/voodoo/generators/common_code_generator.rb', line 651

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
  ## Offset between base pointer and end of frame.
  @offset = parent ? parent.offset : 0
  ## Number of bytes allocated in this environment.
  @bytes = 0
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



646
647
648
# File 'lib/voodoo/generators/common_code_generator.rb', line 646

def args
  @args
end

#bytesObject

Returns the value of attribute bytes.



645
646
647
# File 'lib/voodoo/generators/common_code_generator.rb', line 645

def bytes
  @bytes
end

#localsObject (readonly)

Returns the value of attribute locals.



646
647
648
# File 'lib/voodoo/generators/common_code_generator.rb', line 646

def locals
  @locals
end

#offsetObject

Returns the value of attribute offset.



645
646
647
# File 'lib/voodoo/generators/common_code_generator.rb', line 645

def offset
  @offset
end

#parentObject (readonly)

Returns the value of attribute parent.



646
647
648
# File 'lib/voodoo/generators/common_code_generator.rb', line 646

def parent
  @parent
end

#symbolsObject (readonly)

Returns the value of attribute symbols.



646
647
648
# File 'lib/voodoo/generators/common_code_generator.rb', line 646

def symbols
  @symbols
end

Class Method Details

.gensymObject

Generates a new, unique symbol.



721
722
723
724
# File 'lib/voodoo/generators/common_code_generator.rb', line 721

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

.initial_environmentObject

Returns an initial, top-level environment.



727
728
729
# File 'lib/voodoo/generators/common_code_generator.rb', line 727

def self.initial_environment
  Environment.new
end

Instance Method Details

#[](symbol) ⇒ Object

Looks up symbol in this environment.



716
717
718
# File 'lib/voodoo/generators/common_code_generator.rb', line 716

def [] symbol
  @symbols[symbol]
end

#add_arg(symbol, info = nil) ⇒ Object

Adds symbol as an argument in this environment. info can be used by the code generator to store information it needs about the symbol.



669
670
671
672
# File 'lib/voodoo/generators/common_code_generator.rb', line 669

def add_arg symbol, info = nil
  @symbols[symbol] = info
  @args = @args + 1
end

#add_args(symbols) ⇒ Object

Adds each of symbols to the arguments in this environment. Each entry in symbols can be either a symbol, or an array where the first element is the symbol and the second element is extra information to be stored with the symbol.



679
680
681
682
683
684
685
686
687
# File 'lib/voodoo/generators/common_code_generator.rb', line 679

def add_args symbols
  symbols.each do |sym|
    if sym.respond_to? :[]
      add_arg *sym
    else
      add_arg sym
    end
  end
end

#add_local(symbol, info = nil) ⇒ Object

Adds symbol as a local variable in this environment.



690
691
692
693
# File 'lib/voodoo/generators/common_code_generator.rb', line 690

def add_local symbol, info = nil
  @symbols[symbol] = info
  @locals = @locals + 1
end

#add_locals(symbols) ⇒ Object

Adds each of symbols to the local variables in this environment. Each entry in symbols can be either a symbol, or an array where the first element is the symbol and the second element is extra information to be stored with the symbol.



700
701
702
703
704
705
706
707
708
# File 'lib/voodoo/generators/common_code_generator.rb', line 700

def add_locals symbols
  symbols.each do |sym|
    if sym.respond_to? :[]
      add_local *sym
    else
      add_local sym
    end
  end
end

#gensymObject

Generates a new, unique symbol.



711
712
713
# File 'lib/voodoo/generators/common_code_generator.rb', line 711

def gensym
  Environment.gensym
end