Class: Puppet::Pops::Evaluator::EvaluatorImpl

Inherits:
Object
  • Object
show all
Includes:
ExternalSyntaxSupport, Runtime3Support, Utils
Defined in:
lib/puppet/pops/evaluator/evaluator_impl.rb

Overview

This implementation of Puppet::Pops::Evaluator performs evaluation using the puppet 3.x runtime system in a manner largely compatible with Puppet 3.x, but adds new features and introduces constraints.

The evaluation uses _polymorphic dispatch_ which works by dispatching to the first found method named after the class or one of its super-classes. The EvaluatorImpl itself mainly deals with evaluation (it currently also handles assignment), and it uses a delegation pattern to more specialized handlers of some operators that in turn use polymorphic dispatch; this to not clutter EvaluatorImpl with too much responsibility).

Since a pattern is used, only the main entry points are fully documented. The parameters o and scope are the same in all the polymorphic methods, (the type of the parameter o is reflected in the method’s name; either the actual class, or one of its super classes). The scope parameter is always the scope in which the evaluation takes place. If nothing else is mentioned, the return is always the result of evaluation.

See Visitable and Visitor for more information about polymorphic calling.

Constant Summary collapse

INFINITY =

This constant is not defined as Float::INFINITY in Ruby 1.8.7 (but is available in later version Refactor when support is dropped for Ruby 1.8.7.

1.0 / 0.0
EMPTY_STRING =
''.freeze
COMMA_SEPARATOR =
', '.freeze
Issues =

Reference to Issues name space makes it easier to refer to issues (Issues are shared with the validator).

Puppet::Pops::Issues

Constants included from ExternalSyntaxSupport

Puppet::Pops::Evaluator::ExternalSyntaxSupport::TYPES

Constants included from Runtime3Support

Runtime3Support::CLASS_STRING, Runtime3Support::NAME_SPACE_SEPARATOR

Instance Method Summary collapse

Methods included from ExternalSyntaxSupport

#assert_external_syntax, #checker_for_syntax, #lookup_keys_for_syntax

Methods included from Runtime3Support

#add_relationship, #call_function, #capitalize_qualified_name, #coerce_numeric, #convert, #convert2, #convert2_NilClass, #convert2_Symbol, #convert_Array, #convert_Hash, #convert_NilClass, #convert_Object, #convert_PAnyType, #convert_PCatalogEntryType, #convert_Regexp, #convert_String, #convert_Symbol, #create_local_scope_from, #create_match_scope_from, #create_resource_defaults, #create_resource_overrides, #create_resource_parameter, #create_resources, #fail, #find_resource, #get_resource_parameter_value, #get_scope_nesting_level, #get_variable_value, #is_boolean?, #is_parameter_of_resource?, #is_true?, #optionally_fail, #resource_to_ptype, #set_match_data, #set_scope_nesting_level, #set_variable, #variable_bound?, #variable_exists?

Methods included from Utils

find_adapter, find_closest_positioned, is_absolute?, is_numeric?, match_to_fp, name_to_segments, relativize_name, to_n, to_n_with_radix

Constructor Details

#initializeEvaluatorImpl

Returns a new instance of EvaluatorImpl.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/puppet/pops/evaluator/evaluator_impl.rb', line 45

def initialize
  @@eval_visitor     ||= Puppet::Pops::Visitor.new(self, "eval", 1, 1)
  @@lvalue_visitor   ||= Puppet::Pops::Visitor.new(self, "lvalue", 1, 1)
  @@assign_visitor   ||= Puppet::Pops::Visitor.new(self, "assign", 3, 3)
  @@string_visitor   ||= Puppet::Pops::Visitor.new(self, "string", 1, 1)

  @@type_calculator  ||= Puppet::Pops::Types::TypeCalculator.new()
  @@type_parser      ||= Puppet::Pops::Types::TypeParser.new()

  @@compare_operator     ||= Puppet::Pops::Evaluator::CompareOperator.new()
  @@relationship_operator ||= Puppet::Pops::Evaluator::RelationshipOperator.new()

  # Initialize the runtime module
  Puppet::Pops::Evaluator::Runtime3Support.instance_method(:initialize).bind(self).call()
end

Instance Method Details

#assign(target, value, o, scope) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Assigns the given value to the given target. The additional argument o is the instruction that produced the target/value tuple and it is used to set the origin of the result.

Parameters:

  • target (Object)

    assignment target - see methods on the pattern assign_TYPE for actual supported types.

  • value (Object)

    the value to assign to ‘target`

  • o (Puppet::Pops::Model::PopsObject)

    originating instruction

  • scope (Object)

    the runtime specific scope where evaluation should take place



102
103
104
# File 'lib/puppet/pops/evaluator/evaluator_impl.rb', line 102

def assign(target, value, o, scope)
  @@assign_visitor.visit_this_3(self, target, value, o, scope)
end

#evaluate(target, scope) ⇒ Object

Evaluates the given target object in the given scope.

Parameters:

  • target (Object)

    evaluation target - see methods on the pattern assign_TYPE for actual supported types.

  • scope (Object)

    the runtime specific scope class where evaluation should take place

Returns:

  • (Object)

    the result of the evaluation



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/puppet/pops/evaluator/evaluator_impl.rb', line 75

def evaluate(target, scope)
  begin
    @@eval_visitor.visit_this_1(self, target, scope)

  rescue Puppet::Pops::SemanticError => e
    # a raised issue may not know the semantic target
    fail(e.issue, e.semantic || target, e.options, e)

  rescue StandardError => e
    if e.is_a? Puppet::ParseError
      # ParseError's are supposed to be fully configured with location information
      raise e
    end
    fail(Issues::RUNTIME_ERROR, target, {:detail => e.message}, e)
  end
end

#evaluate_block_with_bindings(scope, variable_bindings, block_expr) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Evaluate a BlockExpression in a new scope with variables bound to the given values.

Parameters:



135
136
137
138
139
140
141
142
# File 'lib/puppet/pops/evaluator/evaluator_impl.rb', line 135

def evaluate_block_with_bindings(scope, variable_bindings, block_expr)
  with_guarded_scope(scope) do
    # change to create local scope_from - cannot give it file and line -
    # that is the place of the call, not "here"
    create_local_scope_from(variable_bindings, scope)
    evaluate(block_expr, scope)
  end
end

#lvalue(o, scope) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Computes a value that can be used as the LHS in an assignment.

Parameters:

  • o (Object)

    the expression to evaluate as a left (assignable) entity

  • scope (Object)

    the runtime specific scope where evaluation should take place



112
113
114
# File 'lib/puppet/pops/evaluator/evaluator_impl.rb', line 112

def lvalue(o, scope)
  @@lvalue_visitor.visit_this_1(self, o, scope)
end

#string(o, scope) ⇒ Object

Produces a String representation of the given object o as used in interpolation.

Parameters:

  • o (Object)

    the expression of which a string representation is wanted

  • scope (Object)

    the runtime specific scope where evaluation should take place



122
123
124
# File 'lib/puppet/pops/evaluator/evaluator_impl.rb', line 122

def string(o, scope)
  @@string_visitor.visit_this_1(self, o, scope)
end

#type_calculatorObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



62
63
64
# File 'lib/puppet/pops/evaluator/evaluator_impl.rb', line 62

def type_calculator
  @@type_calculator
end