NAME

Data::Typed::Expression::Env - Evalutation environment for typed expressions

VERSION

Version 0.005

SYNOPSIS

See Data::Typed::Expression.

DESCRIPTION

An environment is an object describing known types and variables to be used by expressions of type Data::Typed::Expression. Types can be marked as simple ones or defined as compounds (structures) with fields of other types.

METHODS

new

Creates a new enviroment in which expressions can be evaluated.

Arguments are two hashrefs, the first one containing types declarations, the second one containing variables definitions.

Each type declaration is a single hash entry, with type name being the key and type definition being the value.

Type definition is either undef, when there is nothing "internal" about the type or a hashref, for compound types. Each entry in compound type definition is a mapping from a compound element name to its type name.

For example, if we define the car type as having "color" property, which is represented as RGB triple, "price" as double and "name" as a string, the corresponding type definitions can look like:

{
  car => {
    color => 'color',
    price => 'double',
    name => 'string'
  },
  color => {
    r => 'double',
    g => 'double',
    b => 'double'
  },
  double => undef,
  string => undef
}

Variables definition is a mapping from variable name to its type name.

new_with

Creates a new environment based on the current one.

The created environment contains all the types and variables from the current environment, as well as the new types and variables passed as the arguments, in the same way as to new() method.

If types or variables which are defined in the current object and also passed as parameters, definitions given in parameters override the current ones in the created object.

get_type_def

Returns type definition for a given type name, as passed to new().

get_var_type

Returns variable type name, as passed to new().

validate

Checks if the given expression represents a valid one, in the context of the current environent.

For expression to be valid, all used variables, types and components must exist in the environment, and appropriate operators arguments must be of special and coherent types (e.g. int for array indexing or int or double for mathematical operations).

Returns name of the type of passed expression.