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



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

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

Instance Attribute Details

#lookupsObject (readonly)

Returns the value of attribute lookups.



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

def lookups
  @lookups
end

Instance Method Details

#[](name) ⇒ Object



29
30
31
# File 'lib/scope.rb', line 29

def [](name)
  @symbols[name] || @parent[name]
end

#[]=(name, value) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/scope.rb', line 33

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



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

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



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

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



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

def clear_lookup_tables
  @lookups.clear
end

#define(name, &block) ⇒ Object



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

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

#include?(name) ⇒ Boolean



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

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

#merge(scope) ⇒ Object



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

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

#override_scope(scope) ⇒ Object



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

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

#retrieve_scopeObject



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

def retrieve_scope
  @symbols
end

#syntax(name, &block) ⇒ Object



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

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