Class: Expressive::Scope

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

Direct Known Subclasses

TopLevel

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = {}) ⇒ Scope

Returns a new instance of Scope.



7
8
9
10
11
# File 'lib/scope.rb', line 7

def initialize(parent = {})
  @parent = parent
  @symbols = {}
  @lookups = {}
end

Instance Attribute Details

#lookupsObject (readonly)

Returns the value of attribute lookups.



6
7
8
# File 'lib/scope.rb', line 6

def lookups
  @lookups
end

Instance Method Details

#[](name) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/scope.rb', line 30

def [](name)
  if @symbols[name].nil?
    @parent[name]
  else
    @symbols[name]
  end
end

#[]=(name, value) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/scope.rb', line 38

def []=(name, value)
  if value
    if value.is_a?(ExtendedValue)
      @symbols[name] = value unless @symbols.include?(name)
    else
      current_value = @symbols[name]
      if current_value and current_value.is_a?(ExtendedValue)
        current_value.set(value)
      else
        @symbols[name] = value
      end
    end
  else
    @symbols[name] = value
  end
end

#add_lookup_function(lookup_function_name, passed_in_options = {}, &func_proc) ⇒ Object



18
19
20
# File 'lib/scope.rb', line 18

def add_lookup_function(lookup_function_name, passed_in_options = {}, &func_proc)
  @lookups[lookup_function_name] = [passed_in_options, func_proc]
end

#add_lookup_table(lookup_table_name, lookups) ⇒ Object



13
14
15
16
# File 'lib/scope.rb', line 13

def add_lookup_table(lookup_table_name, lookups)
  @lookups[lookup_table_name] = {} unless @lookups.include?(lookup_table_name)
  @lookups[lookup_table_name].merge!(lookups)
end

#clear_lookup_tablesObject



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

def clear_lookup_tables
  @lookups.clear
end

#define(name, &block) ⇒ Object



68
69
70
# File 'lib/scope.rb', line 68

def define(name, &block)
  self[name] =  Function.new(&block)
end

#include?(name) ⇒ Boolean

Returns:



26
27
28
# File 'lib/scope.rb', line 26

def include?(name)
  @symbols.include?(name) or @lookups.include?(name)
end

#merge(scope) ⇒ Object



55
56
57
# File 'lib/scope.rb', line 55

def merge(scope)
  @symbols.merge!(scope)
end

#override_scope(scope) ⇒ Object



59
60
61
62
# File 'lib/scope.rb', line 59

def override_scope(scope)
  scope.merge!(@symbols)
  @symbols = scope
end

#retrieve_scopeObject



64
65
66
# File 'lib/scope.rb', line 64

def retrieve_scope
  @symbols
end

#syntax(name, &block) ⇒ Object



72
73
74
# File 'lib/scope.rb', line 72

def syntax(name, &block)
  self[name] =  Syntax.new(&block)
end