Class: Dictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/rpl/dictionary.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDictionary

Returns a new instance of Dictionary.



8
9
10
11
12
# File 'lib/rpl/dictionary.rb', line 8

def initialize
  @words = {}
  @vars = {}
  @local_vars_layers = []
end

Instance Attribute Details

#local_vars_layersObject (readonly)

Returns the value of attribute local_vars_layers.



4
5
6
# File 'lib/rpl/dictionary.rb', line 4

def local_vars_layers
  @local_vars_layers
end

#varsObject (readonly)

Returns the value of attribute vars.



4
5
6
# File 'lib/rpl/dictionary.rb', line 4

def vars
  @vars
end

#wordsObject (readonly)

Returns the value of attribute words.



4
5
6
# File 'lib/rpl/dictionary.rb', line 4

def words
  @words
end

Instance Method Details

#add_local_var(name, implementation) ⇒ Object



44
45
46
# File 'lib/rpl/dictionary.rb', line 44

def add_local_var( name, implementation )
  @local_vars_layers.last[ name ] = implementation
end

#add_local_vars_layerObject



40
41
42
# File 'lib/rpl/dictionary.rb', line 40

def add_local_vars_layer
  @local_vars_layers << {}
end

#add_var(name, implementation) ⇒ Object



22
23
24
# File 'lib/rpl/dictionary.rb', line 22

def add_var( name, implementation )
  @vars[ name ] = implementation
end

#add_word(names, category, help, implementation) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/rpl/dictionary.rb', line 14

def add_word( names, category, help, implementation )
  names.each do |name|
    @words[ name ] = { category: category,
                       help: help,
                       implementation: implementation }
  end
end

#lookup(name) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rpl/dictionary.rb', line 62

def lookup( name )
  # look in local variables from the deepest layer up
  local_vars_layer = @local_vars_layers.reverse.find { |layer| layer[ name ] }
  word = local_vars_layer.nil? ? nil : local_vars_layer[ name ]

  # otherwise look in (global) variables
  word ||= @vars[ name ]

  # or is it a core word
  word ||= @words[ name ].nil? ? nil : @words[ name ][:implementation]

  word
end

#remove_all_varsObject



36
37
38
# File 'lib/rpl/dictionary.rb', line 36

def remove_all_vars
  @vars = {}
end

#remove_local_var(name) ⇒ Object



54
55
56
# File 'lib/rpl/dictionary.rb', line 54

def remove_local_var( name )
  remove_local_vars( [name] )
end

#remove_local_vars(names) ⇒ Object



48
49
50
51
52
# File 'lib/rpl/dictionary.rb', line 48

def remove_local_vars( names )
  names.each do |name|
    @local_vars_layers.last.delete( name )
  end
end

#remove_local_vars_layerObject



58
59
60
# File 'lib/rpl/dictionary.rb', line 58

def remove_local_vars_layer
  @local_vars_layers.pop
end

#remove_var(name) ⇒ Object



32
33
34
# File 'lib/rpl/dictionary.rb', line 32

def remove_var( name )
  remove_vars( [name] )
end

#remove_vars(names) ⇒ Object



26
27
28
29
30
# File 'lib/rpl/dictionary.rb', line 26

def remove_vars( names )
  names.each do |name|
    @vars.delete( name )
  end
end