Class: Carbon::Compiler::Visitor::Generation::Context
- Inherits:
-
Object
- Object
- Carbon::Compiler::Visitor::Generation::Context
- Defined in:
- lib/carbon/compiler/visitor/generation/context.rb
Overview
Context for building function bodies.
Instance Attribute Summary collapse
-
#function ⇒ Object
readonly
Returns the value of attribute function.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
Instance Method Summary collapse
- #[](name) ⇒ Object
- #[]=(name, value) ⇒ Object
- #block ⇒ Object
- #build(*p) ⇒ Object
- #current ⇒ Object
- #find(name) ⇒ Object
-
#initialize(definition, index, function) ⇒ Context
constructor
A new instance of Context.
- #key?(name) ⇒ Boolean
- #new(name = "") ⇒ Object
- #pop ⇒ Object
- #push ⇒ Object
- #swap(other) ⇒ Object
- #top ⇒ Object
Constructor Details
#initialize(definition, index, function) ⇒ Context
Returns a new instance of Context.
13 14 15 16 17 18 |
# File 'lib/carbon/compiler/visitor/generation/context.rb', line 13 def initialize(definition, index, function) @locals = [] @definition = definition @index = index @function = function end |
Instance Attribute Details
#function ⇒ Object (readonly)
Returns the value of attribute function.
11 12 13 |
# File 'lib/carbon/compiler/visitor/generation/context.rb', line 11 def function @function end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
10 11 12 |
# File 'lib/carbon/compiler/visitor/generation/context.rb', line 10 def index @index end |
Instance Method Details
#[](name) ⇒ Object
48 49 50 |
# File 'lib/carbon/compiler/visitor/generation/context.rb', line 48 def [](name) current[name] end |
#[]=(name, value) ⇒ Object
52 53 54 |
# File 'lib/carbon/compiler/visitor/generation/context.rb', line 52 def []=(name, value) top[name] = value end |
#block ⇒ Object
20 21 22 |
# File 'lib/carbon/compiler/visitor/generation/context.rb', line 20 def block @block ||= @definition.add("entry") end |
#build(*p) ⇒ Object
28 29 30 31 32 33 |
# File 'lib/carbon/compiler/visitor/generation/context.rb', line 28 def build(*p) return block.build(*p) unless block_given? result = nil block.build(*p) { |*a| result = yield(*a) } result end |
#current ⇒ Object
56 57 58 |
# File 'lib/carbon/compiler/visitor/generation/context.rb', line 56 def current @locals.inject({}, :merge) end |
#find(name) ⇒ Object
40 41 42 |
# File 'lib/carbon/compiler/visitor/generation/context.rb', line 40 def find(name) index.fetch(name) end |
#key?(name) ⇒ Boolean
44 45 46 |
# File 'lib/carbon/compiler/visitor/generation/context.rb', line 44 def key?(name) current.key?(name) end |
#new(name = "") ⇒ Object
24 25 26 |
# File 'lib/carbon/compiler/visitor/generation/context.rb', line 24 def new(name = "") @definition.add(name) end |
#pop ⇒ Object
64 65 66 |
# File 'lib/carbon/compiler/visitor/generation/context.rb', line 64 def pop @locals.pop end |
#push ⇒ Object
60 61 62 |
# File 'lib/carbon/compiler/visitor/generation/context.rb', line 60 def push @locals << {} end |
#swap(other) ⇒ Object
35 36 37 38 |
# File 'lib/carbon/compiler/visitor/generation/context.rb', line 35 def swap(other) old, @block = @block, other old end |
#top ⇒ Object
68 69 70 |
# File 'lib/carbon/compiler/visitor/generation/context.rb', line 68 def top @locals.last end |