Class: Stone::Scope
- Inherits:
-
Object
- Object
- Stone::Scope
- Defined in:
- lib/stone/scope.rb
Instance Attribute Summary collapse
-
#definitions ⇒ Object
readonly
Returns the value of attribute definitions.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#type_declarations ⇒ Object
readonly
Returns the value of attribute type_declarations.
Class Method Summary collapse
Instance Method Summary collapse
- #child ⇒ Object
- #declare_type(name, type:, location: nil) ⇒ Object
- #declared_type(name) ⇒ Object
- #define(name, value:, location: nil) ⇒ Object
- #defined_locally?(name) ⇒ Boolean
- #depth ⇒ Object
-
#initialize(parent = nil) ⇒ Scope
constructor
A new instance of Scope.
- #lookup(name) ⇒ Object
- #lookup_local(name) ⇒ Object
-
#lookup_type(name) ⇒ Object
Resolve a type name to verify it exists in scope.
- #lookup_type_declaration(name) ⇒ Object
- #top_level? ⇒ Boolean
- #type_declaration_location(name) ⇒ Object
- #type_declared_locally?(name) ⇒ Boolean
Constructor Details
#initialize(parent = nil) ⇒ Scope
Returns a new instance of Scope.
5 6 7 8 9 |
# File 'lib/stone/scope.rb', line 5 def initialize(parent = nil) @parent = parent @definitions = {} @type_declarations = {} end |
Instance Attribute Details
#definitions ⇒ Object (readonly)
Returns the value of attribute definitions.
3 4 5 |
# File 'lib/stone/scope.rb', line 3 def definitions @definitions end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
3 4 5 |
# File 'lib/stone/scope.rb', line 3 def parent @parent end |
#type_declarations ⇒ Object (readonly)
Returns the value of attribute type_declarations.
3 4 5 |
# File 'lib/stone/scope.rb', line 3 def type_declarations @type_declarations end |
Class Method Details
.reset_top_level! ⇒ Object
81 82 83 |
# File 'lib/stone/scope.rb', line 81 def self.reset_top_level! @top_level = nil end |
.top_level ⇒ Object
77 78 79 |
# File 'lib/stone/scope.rb', line 77 def self.top_level @top_level ||= new end |
Instance Method Details
#declare_type(name, type:, location: nil) ⇒ Object
19 20 21 |
# File 'lib/stone/scope.rb', line 19 def declare_type(name, type:, location: nil) @type_declarations[name] = {type:, location:} end |
#declared_type(name) ⇒ Object
43 44 45 46 |
# File 'lib/stone/scope.rb', line 43 def declared_type(name) decl = lookup_type_declaration(name) decl&.dig(:type) end |
#define(name, value:, location: nil) ⇒ Object
15 16 17 |
# File 'lib/stone/scope.rb', line 15 def define(name, value:, location: nil) @definitions[name] = {value:, location:} end |
#defined_locally?(name) ⇒ Boolean
35 36 37 |
# File 'lib/stone/scope.rb', line 35 def defined_locally?(name) @definitions.key?(name) end |
#depth ⇒ Object
69 70 71 |
# File 'lib/stone/scope.rb', line 69 def depth @parent ? @parent.depth + 1 : 0 end |
#lookup(name) ⇒ Object
23 24 25 |
# File 'lib/stone/scope.rb', line 23 def lookup(name) @definitions[name] || @parent&.lookup(name) end |
#lookup_local(name) ⇒ Object
27 28 29 |
# File 'lib/stone/scope.rb', line 27 def lookup_local(name) @definitions[name] end |
#lookup_type(name) ⇒ Object
Resolve a type name to verify it exists in scope. Returns the type name if found, nil otherwise. Checks: built-in types, type declarations, definitions (constants). Note: lookup_type_declaration and lookup already traverse parent chain.
57 58 59 60 61 62 63 |
# File 'lib/stone/scope.rb', line 57 def lookup_type(name) return name if builtin_type?(name) return name if lookup_type_declaration(name) return name if lookup(name) nil end |
#lookup_type_declaration(name) ⇒ Object
31 32 33 |
# File 'lib/stone/scope.rb', line 31 def lookup_type_declaration(name) @type_declarations[name] || @parent&.lookup_type_declaration(name) end |
#top_level? ⇒ Boolean
73 74 75 |
# File 'lib/stone/scope.rb', line 73 def top_level? @parent.nil? end |
#type_declaration_location(name) ⇒ Object
48 49 50 51 |
# File 'lib/stone/scope.rb', line 48 def type_declaration_location(name) decl = lookup_type_declaration(name) decl&.dig(:location) end |
#type_declared_locally?(name) ⇒ Boolean
39 40 41 |
# File 'lib/stone/scope.rb', line 39 def type_declared_locally?(name) @type_declarations.key?(name) end |