Class: XASH::ContextStack

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/xash/context_stack.rb

Instance Method Summary collapse

Constructor Details

#initialize(evaluator) ⇒ ContextStack

Returns a new instance of ContextStack.



7
8
9
10
11
12
13
14
15
16
# File 'lib/xash/context_stack.rb', line 7

def initialize(evaluator)
    @evaluator = evaluator

    @context_stack = []

    #root context
    root = Context.new({ 'do' => [] }, nil)
    root.name = '<root>'
    @context_stack.push(root)
end

Instance Method Details

#[](index) ⇒ Object



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

def [](index)
    @context_stack[index]
end

#currentObject



30
31
32
# File 'lib/xash/context_stack.rb', line 30

def current
    @context_stack.last
end

#eachObject



18
19
20
21
22
23
24
# File 'lib/xash/context_stack.rb', line 18

def each
    if block_given?
        @context_stack.each{ |c| yield c }
    else
        @context_stack.each
    end
end

#exist_variable?(name) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/xash/context_stack.rb', line 65

def exist_variable?(name)
    get_variable(name)
rescue
    false
else
    true
end

#get_variable(name) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/xash/context_stack.rb', line 45

def get_variable(name)
    #search order
    #parents -> context_stack(attached -> local)
    @context_stack.reverse_each do |context|
        if context.exist_variable?(name)
            return context.variable(name)
        end
    end
    
    cur = current
    while cur
        if cur.exist_variable?(name)
            return cur.variable(name)
        end
        cur = cur.parent
    end

    raise UndefinedLocalVariableError, "undefined local variable `#{name}`"
end

#metaObject



34
35
36
# File 'lib/xash/context_stack.rb', line 34

def meta
    @context_stack[-2]
end

#meta_contextObject



79
80
81
82
83
84
# File 'lib/xash/context_stack.rb', line 79

def meta_context
    current = @context_stack.pop
    ret = yield
    @context_stack.push(current)
    ret
end

#push(context) ⇒ Object



38
39
40
41
42
43
# File 'lib/xash/context_stack.rb', line 38

def push(context)
    @context_stack.push context
    ret = yield
    @context_stack.pop
    ret
end

#variable(name) ⇒ Object



73
74
75
76
77
# File 'lib/xash/context_stack.rb', line 73

def variable(name)
    get_variable(name)
rescue
    @evaluator.error UndefinedLocalVariableError, "undefined local variable `#{name}`"
end