Class: Voodoo::CommonCodeGenerator::Environment
- Inherits:
-
Object
- Object
- Voodoo::CommonCodeGenerator::Environment
- 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
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#bytes ⇒ Object
Returns the value of attribute bytes.
-
#locals ⇒ Object
readonly
Returns the value of attribute locals.
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#symbols ⇒ Object
readonly
Returns the value of attribute symbols.
Class Method Summary collapse
-
.gensym ⇒ Object
Generates a new, unique symbol.
-
.initial_environment ⇒ Object
Returns an initial, top-level environment.
Instance Method Summary collapse
-
#[](symbol) ⇒ Object
Looks up symbol in this environment.
-
#add_arg(symbol, info = nil) ⇒ Object
Adds symbol as an argument in this environment.
-
#add_args(symbols) ⇒ Object
Adds each of symbols to the arguments in this environment.
-
#add_local(symbol, info = nil) ⇒ Object
Adds symbol as a local variable in this environment.
-
#add_locals(symbols) ⇒ Object
Adds each of symbols to the local variables in this environment.
-
#gensym ⇒ Object
Generates a new, unique symbol.
-
#initialize(parent = nil) ⇒ Environment
constructor
Creates a new environment.
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.
623 624 625 626 627 628 629 630 631 632 633 634 635 636 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 623 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
#args ⇒ Object (readonly)
Returns the value of attribute args.
618 619 620 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 618 def args @args end |
#bytes ⇒ Object
Returns the value of attribute bytes.
617 618 619 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 617 def bytes @bytes end |
#locals ⇒ Object (readonly)
Returns the value of attribute locals.
618 619 620 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 618 def locals @locals end |
#offset ⇒ Object
Returns the value of attribute offset.
617 618 619 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 617 def offset @offset end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
618 619 620 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 618 def parent @parent end |
#symbols ⇒ Object (readonly)
Returns the value of attribute symbols.
618 619 620 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 618 def symbols @symbols end |
Class Method Details
.gensym ⇒ Object
Generates a new, unique symbol.
693 694 695 696 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 693 def self.gensym @@gensym_counter = @@gensym_counter + 1 "_G#{@@gensym_counter}".to_sym end |
.initial_environment ⇒ Object
Returns an initial, top-level environment.
699 700 701 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 699 def self.initial_environment Environment.new end |
Instance Method Details
#[](symbol) ⇒ Object
Looks up symbol in this environment.
688 689 690 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 688 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.
641 642 643 644 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 641 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.
651 652 653 654 655 656 657 658 659 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 651 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.
662 663 664 665 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 662 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.
672 673 674 675 676 677 678 679 680 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 672 def add_locals symbols symbols.each do |sym| if sym.respond_to? :[] add_local *sym else add_local sym end end end |
#gensym ⇒ Object
Generates a new, unique symbol.
683 684 685 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 683 def gensym Environment.gensym end |