Class: Nydp::LexicalContext

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#cons, #list, #literal?, #pair?, #sig, #sym, #sym?

Methods included from Converter

#n2r, #r2n

Constructor Details

#initialize(parent) ⇒ LexicalContext



7
8
9
# File 'lib/nydp/lexical_context.rb', line 7

def initialize parent
  @parent = parent
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mname, *args) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/nydp/lexical_context.rb', line 30

def method_missing mname, *args
  if mname.to_s =~ /at_\d+=/
    instance_variable_set :"@#{mname.to_s.sub(/=/, '')}", args[0]
  elsif mname.to_s =~ /at_\d+/
    instance_variable_get :"@#{mname}"
  else
    super
  end
end

Instance Attribute Details

#at_0Object

Returns the value of attribute at_0.



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

def at_0
  @at_0
end

#at_1Object

Returns the value of attribute at_1.



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

def at_1
  @at_1
end

#at_2Object

Returns the value of attribute at_2.



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

def at_2
  @at_2
end

#at_3Object

Returns the value of attribute at_3.



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

def at_3
  @at_3
end

#at_4Object

Returns the value of attribute at_4.



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

def at_4
  @at_4
end

#at_5Object

Returns the value of attribute at_5.



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

def at_5
  @at_5
end

#at_6Object

Returns the value of attribute at_6.



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

def at_6
  @at_6
end

#at_7Object

Returns the value of attribute at_7.



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

def at_7
  @at_7
end

#at_8Object

Returns the value of attribute at_8.



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

def at_8
  @at_8
end

#at_9Object

Returns the value of attribute at_9.



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

def at_9
  @at_9
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

Instance Method Details

#at_index(index) ⇒ Object



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

def at_index index
  instance_variable_get :"@at_#{index}"
end

#nth(n) ⇒ Object



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

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

#prettyObject



47
48
49
# File 'lib/nydp/lexical_context.rb', line 47

def pretty
  to_s_with_indent ''
end

#set_index(index, value) ⇒ Object



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

def set_index index, value
  instance_variable_set :"@at_#{index}", value
end

#to_sObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/nydp/lexical_context.rb', line 51

def to_s
  values = []
  n = 0
  while at_index n
    values << at_index(n).inspect
    n += 1
  end
  parent_s = parent ? " parent #{parent.to_s}" : ""
  "(context (#{values.join ' '})#{parent_s})"
end

#to_s_with_indent(str) ⇒ Object



40
41
42
43
44
45
# File 'lib/nydp/lexical_context.rb', line 40

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