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.
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
#args ⇒ Object (readonly)
Returns the value of attribute args.
646 647 648 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 646 def args @args end |
#bytes ⇒ Object
Returns the value of attribute bytes.
645 646 647 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 645 def bytes @bytes end |
#locals ⇒ Object (readonly)
Returns the value of attribute locals.
646 647 648 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 646 def locals @locals end |
#offset ⇒ Object
Returns the value of attribute offset.
645 646 647 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 645 def offset @offset end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
646 647 648 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 646 def parent @parent end |
#symbols ⇒ Object (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
.gensym ⇒ Object
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_environment ⇒ Object
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 |
#gensym ⇒ Object
Generates a new, unique symbol.
711 712 713 |
# File 'lib/voodoo/generators/common_code_generator.rb', line 711 def gensym Environment.gensym end |