Class: XASH::ContextStack
- Inherits:
-
Object
- Object
- XASH::ContextStack
- Includes:
- Enumerable
- Defined in:
- lib/xash/context_stack.rb
Instance Method Summary collapse
- #[](index) ⇒ Object
- #current ⇒ Object
- #each ⇒ Object
- #exist_variable?(name) ⇒ Boolean
- #get_variable(name) ⇒ Object
-
#initialize(evaluator) ⇒ ContextStack
constructor
A new instance of ContextStack.
- #meta ⇒ Object
- #meta_context ⇒ Object
- #push(context) ⇒ Object
- #variable(name) ⇒ Object
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 |
#current ⇒ Object
30 31 32 |
# File 'lib/xash/context_stack.rb', line 30 def current @context_stack.last end |
#each ⇒ Object
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
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 |
#meta ⇒ Object
34 35 36 |
# File 'lib/xash/context_stack.rb', line 34 def @context_stack[-2] end |
#meta_context ⇒ Object
79 80 81 82 83 84 |
# File 'lib/xash/context_stack.rb', line 79 def 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 |