Class: Decode::Symbol

Inherits:
Object
  • Object
show all
Defined in:
lib/decode/symbol.rb

Overview

A named element which represents some significant element of some kind in a computer program.

Direct Known Subclasses

Definition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(kind, name, parent: nil, language: parent.language) ⇒ Symbol

Initialize the symbol.

Parameters:

  • kind (Symbol)

    The kind of symbol.

  • name (Symbol)

    The name of the symbol.

  • parent (Symbol) (defaults to: nil)

    The parent lexical scope.

  • language (Language) (defaults to: parent.language)

    The language in which the symbol is defined in.



34
35
36
37
38
39
40
41
42
# File 'lib/decode/symbol.rb', line 34

def initialize(kind, name, parent: nil, language: parent.language)
	@kind = kind
	@name = name
	@parent = parent
	@language = language
	
	@path = nil
	@qualified_name = nil
end

Instance Attribute Details

#kindObject (readonly)

The kind of symbol. e.g. ‘:module`.



56
57
58
# File 'lib/decode/symbol.rb', line 56

def kind
  @kind
end

#languageObject (readonly)

The language the symbol is defined within.



66
67
68
# File 'lib/decode/symbol.rb', line 66

def language
  @language
end

#nameObject (readonly)

The symbol name. e.g. ‘:Decode`.



60
61
62
# File 'lib/decode/symbol.rb', line 60

def name
  @name
end

#parentObject (readonly)

The parent symbol, defining lexical scope.



63
64
65
# File 'lib/decode/symbol.rb', line 63

def parent
  @parent
end

Instance Method Details

#inspectObject



50
51
52
# File 'lib/decode/symbol.rb', line 50

def inspect
	"\#<#{self.class} #{@kind} #{qualified_name}>"
end

#keyKey

A symbol-specific key which represents the lexical scope.

Returns:



46
47
48
# File 'lib/decode/symbol.rb', line 46

def key
	Key.new(@kind, @name)
end

#lexical_pathObject

The lexical path, only taking into account the symbol names.



86
87
88
# File 'lib/decode/symbol.rb', line 86

def lexical_path
	self.path.map(&:name)
end

#pathArray

The lexical scope which is an array of lexical Key instances as generated by #key.

Returns:

  • (Array)


75
76
77
78
79
80
81
82
83
# File 'lib/decode/symbol.rb', line 75

def path
	if @path
		@path
	elsif @parent
		@path = [*@parent.path, self.key]
	else
		@path = [self.key]
	end
end

#qualified_nameObject

The qualified name is an absolute name which includes any and all namespacing.



69
70
71
# File 'lib/decode/symbol.rb', line 69

def qualified_name
	@qualified_name ||= @language.join(self.path).freeze
end