Class: Nydp::ContextSymbol

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

Class Method Summary collapse

Class Method Details

.build(effective_depth, name, binding_index, lexical_depth) ⇒ Object



64
65
66
67
68
# File 'lib/nydp/context_symbol.rb', line 64

def self.build effective_depth, name, binding_index, lexical_depth
  const_get(:"ContextSymbol_#{effective_depth}_#{binding_index}").new(name, lexical_depth)
rescue
  raise "building ContextSymbol #{[effective_depth, name, binding_index, lexical_depth]._nydp_inspect}"
end

.const_missing(const) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/nydp/context_symbol.rb', line 3

def self.const_missing const
  if const.to_s =~ /^ContextSymbol_\d+_\d+$/
    name          = const.to_s.split(/_/)
    define_klass(const, name[1].to_i, name[2].to_i)
    const_get const
  else
    super(const)
  end
end

.define_klass(name, depth, binding_index) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nydp/context_symbol.rb', line 13

def self.define_klass name, depth, binding_index
  getctx = ([".parent"] * depth).join
  at_index = if binding_index < 10
               "at_#{binding_index}"
             else
               "at_index(#{binding_index})"
             end

  set_index = if binding_index < 10
                "at_#{binding_index}= value"
              else
                "set_index(#{binding_index}, value)"
              end

  line = __LINE__ + 2
  code = <<-KLASS
    def initialize name, lexical_depth
      @name, @lexical_depth = name, lexical_depth
    end

    def lexical_reach n
      @lexical_depth + n
    end

    def compile_to_ruby indent, srcs, opts=nil
      "\#{indent}_arg_\#{@name.to_s._nydp_name_to_rb_name}"
    end

    def value ctx
      ctx#{getctx}.#{at_index} || nil
    rescue
      raise "failed looking up \#{@name._nydp_inspect} (\#{@name.class.name}) : lookup expression was ctx#{getctx}.#{at_index}"
    end

    def assign value, ctx
      ctx#{getctx}.#{set_index}
    rescue StandardError => e
      raise "problem in \#{self.class.name}#assign, name is \#{@name}, depth is \#{depth}, index is #{binding_index}"
    end

    def depth   ; #{depth}                               ; end
    def inspect ; to_s                                   ; end
    # def to_s    ; "[#{depth}##{binding_index}#\#{@lexical_depth}]\#{@name}" ; end
    def to_s    ; @name ; end
  KLASS

  const_set name, Class.new(Nydp::ContextSymbol) {
    eval code, binding, "#{name.to_s} : #{__FILE__}", line
  }
end