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.



613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'lib/voodoo/generators/common_code_generator.rb', line 613

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.



608
609
610
# File 'lib/voodoo/generators/common_code_generator.rb', line 608

def args
  @args
end

#bytesObject

Returns the value of attribute bytes.



607
608
609
# File 'lib/voodoo/generators/common_code_generator.rb', line 607

def bytes
  @bytes
end

#localsObject (readonly)

Returns the value of attribute locals.



608
609
610
# File 'lib/voodoo/generators/common_code_generator.rb', line 608

def locals
  @locals
end

#offsetObject

Returns the value of attribute offset.



607
608
609
# File 'lib/voodoo/generators/common_code_generator.rb', line 607

def offset
  @offset
end

#parentObject (readonly)

Returns the value of attribute parent.



608
609
610
# File 'lib/voodoo/generators/common_code_generator.rb', line 608

def parent
  @parent
end

#symbolsObject (readonly)

Returns the value of attribute symbols.



608
609
610
# File 'lib/voodoo/generators/common_code_generator.rb', line 608

def symbols
  @symbols
end

Class Method Details

.gensymObject

Generates a new, unique symbol.



683
684
685
686
# File 'lib/voodoo/generators/common_code_generator.rb', line 683

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

.initial_environmentObject

Returns an initial, top-level environment.



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

def self.initial_environment
  Environment.new
end

Instance Method Details

#[](symbol) ⇒ Object

Looks up symbol in this environment.



678
679
680
# File 'lib/voodoo/generators/common_code_generator.rb', line 678

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.



631
632
633
634
# File 'lib/voodoo/generators/common_code_generator.rb', line 631

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.



641
642
643
644
645
646
647
648
649
# File 'lib/voodoo/generators/common_code_generator.rb', line 641

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.



652
653
654
655
# File 'lib/voodoo/generators/common_code_generator.rb', line 652

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.



662
663
664
665
666
667
668
669
670
# File 'lib/voodoo/generators/common_code_generator.rb', line 662

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.



673
674
675
# File 'lib/voodoo/generators/common_code_generator.rb', line 673

def gensym
  Environment.gensym
end