Class: Stone::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/stone/scope.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#definitionsObject (readonly)

Returns the value of attribute definitions.



3
4
5
# File 'lib/stone/scope.rb', line 3

def definitions
  @definitions
end

#parentObject (readonly)

Returns the value of attribute parent.



3
4
5
# File 'lib/stone/scope.rb', line 3

def parent
  @parent
end

#type_declarationsObject (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_levelObject



77
78
79
# File 'lib/stone/scope.rb', line 77

def self.top_level
  @top_level ||= new
end

Instance Method Details

#childObject



11
12
13
# File 'lib/stone/scope.rb', line 11

def child
  Scope.new(self)
end

#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

Returns:

  • (Boolean)


35
36
37
# File 'lib/stone/scope.rb', line 35

def defined_locally?(name)
  @definitions.key?(name)
end

#depthObject



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

Returns:

  • (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

Returns:

  • (Boolean)


39
40
41
# File 'lib/stone/scope.rb', line 39

def type_declared_locally?(name)
  @type_declarations.key?(name)
end