Class: Halunke::Interpreter::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/halunke/interpreter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent = nil) ⇒ Context

Returns a new instance of Context.



53
54
55
56
# File 'lib/halunke/interpreter.rb', line 53

def initialize(parent = nil)
  @parent = parent
  @context = {}
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



51
52
53
# File 'lib/halunke/interpreter.rb', line 51

def parent
  @parent
end

Instance Method Details

#[](name) ⇒ Object



63
64
65
66
67
68
# File 'lib/halunke/interpreter.rb', line 63

def [](name)
  @context.fetch(name)
rescue KeyError
  raise KeyError if @parent.nil?
  @parent[name]
end

#[]=(name, value) ⇒ Object

Raises:

  • (FrozenError)


58
59
60
61
# File 'lib/halunke/interpreter.rb', line 58

def []=(name, value)
  raise FrozenError if key? name
  @context[name] = value
end

#create_childObject



74
75
76
# File 'lib/halunke/interpreter.rb', line 74

def create_child
  Context.new(self)
end

#key?(name) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/halunke/interpreter.rb', line 70

def key?(name)
  @context.key?(name)
end