Class: Platform::IEL::EvaluationContext

Inherits:
Object
  • Object
show all
Defined in:
lib/introhive_expression_language/iel/evaluation_context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enclosing_context = nil) ⇒ EvaluationContext

Returns a new instance of EvaluationContext.



6
7
8
9
# File 'lib/introhive_expression_language/iel/evaluation_context.rb', line 6

def initialize(enclosing_context = nil)
  @enclosing_context = enclosing_context
  @symbols = {}
end

Instance Attribute Details

#enclosing_contextObject (readonly)

Returns the value of attribute enclosing_context.



4
5
6
# File 'lib/introhive_expression_language/iel/evaluation_context.rb', line 4

def enclosing_context
  @enclosing_context
end

Instance Method Details

#[](symbol) ⇒ Object



11
12
13
14
15
16
# File 'lib/introhive_expression_language/iel/evaluation_context.rb', line 11

def [](symbol)
  out = @symbols[symbol]
  return out if out.present?
  return @enclosing_context[symbol] if @enclosing_context
  nil
end

#[]=(symbol, symbol_detail) ⇒ Object

Raises:

  • (StandardError)


18
19
20
21
22
# File 'lib/introhive_expression_language/iel/evaluation_context.rb', line 18

def []=(symbol, symbol_detail)
  raise StandardError, "Cannot write symbol '#{symbol}' value '#{symbol_detail.inspect}' as it is not of type Platform::IEL::SexpParser::Node" unless symbol_detail.is_a?(SexpParser::Node)
  raise StandardError, "Cannot redefine constant symbol '#{symbol}' value '#{symbol_detail.inspect}'" if constant_name?(symbol) && self[symbol]
  @symbols[symbol] = symbol_detail
end

#constant_name?(symbol) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/introhive_expression_language/iel/evaluation_context.rb', line 24

def constant_name?(symbol)
  symbol.start_with?('C_')
end

#root_contextObject

Find and return the root context by following enclosing contexts.



29
30
31
32
# File 'lib/introhive_expression_language/iel/evaluation_context.rb', line 29

def root_context
  return enclosing_context.root_context if enclosing_context
  self
end