Class: Nydp::LexicalContext

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ LexicalContext

Returns a new instance of LexicalContext.



4
5
6
7
# File 'lib/nydp/lexical_context.rb', line 4

def initialize parent
  @parent = parent
  @values = { }
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



2
3
4
# File 'lib/nydp/lexical_context.rb', line 2

def parent
  @parent
end

#valuesObject (readonly)

Returns the value of attribute values.



2
3
4
# File 'lib/nydp/lexical_context.rb', line 2

def values
  @values
end

Instance Method Details

#at(name) ⇒ Object



20
21
22
# File 'lib/nydp/lexical_context.rb', line 20

def at name
  values[name]
end

#nth(n) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'lib/nydp/lexical_context.rb', line 9

def nth n
  case n
  when 0
    self
  when -1
    raise "wrong nesting level"
  else
    parent.nth(n - 1)
  end
end

#set(name, value) ⇒ Object



24
25
26
# File 'lib/nydp/lexical_context.rb', line 24

def set name, value
  values[name] = value
end

#to_sObject



35
36
37
# File 'lib/nydp/lexical_context.rb', line 35

def to_s
  to_s_with_indent ''
end

#to_s_with_indent(str) ⇒ Object



28
29
30
31
32
33
# File 'lib/nydp/lexical_context.rb', line 28

def to_s_with_indent str
  me = @values.map { |k, v|
    [str, k, "=>", v].join ' '
  }.join "\n"
  me + (parent ? parent.to_s_with_indent("  #{str}") : '')
end