ContRap-Core
Public Types | Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes

crp::Interpreter Class Reference

Class Interpreter implements the command interpreter of ContRap. More...

#include <interpreter.h>

Public Types

enum  DebugLevel {
  NO_DEBUG = 0, ENTRY_DEBUG = 1, SEARCH_DEBUG = 2, ARGUMENT_DEBUG = 4,
  DEEP_DEBUG = 8, USER_DEBUG = 16
}
 

Debug levels.

More...

Public Member Functions

 Interpreter (int debugger, Engine *engine)
 Creates a new interpreter.
 ~Interpreter ()
 Releases the memory.
void clear ()
 Clears the interpreter.
DPtr evaluate (const DPtr &data, const SPtr< Scope > &scope=0)
 Evaluates one statement.
DPtr type_cast (const DPtr &object, const std::string &type, const SPtr< Scope > &scope=0)
 Makes a full type cast of the object to the given type.
DPtr call (const DPtr &function, const SPtr< List > &arguments, SPtr< Scope > scope=0, const std::map< std::string, DPtr > &options=empty_options)
 Calls a function with given arguments if possible.
DPtr call (const std::string &function, const SPtr< List > &arguments, SPtr< Scope > scope=0, const std::map< std::string, DPtr > &options=empty_options)
 Calls a function by name with given arguments if possible.
DPtr check_call (const DPtr &function, const SPtr< List > &arguments, SPtr< Scope > scope=0)
 Calls a function with given arguments if possible.
DPtr check_call (const std::string &function, const SPtr< List > &arguments, const std::map< std::string, DPtr > &options, SPtr< Scope > scope=0)
 Calls a function with given arguments if possible.
DPtr load_library (const std::string &library, Library::OpenMode mode=Library::NO_CHANGE)
 Tells the interpreter to load a library.
const SPtr< Scope > & get_scope ()
 Returns the global scope.
const SPtr< List > & get_history ()
 Returns the output history.
void set_recursion_depth (int depth)
 Sets the maximal depth of the recursion.
bool halt (bool status)
 Aborts the current evaluation.
void set_debug_level (int status=NO_DEBUG)
 Sets the debug level for intermediate computations.
int get_debug_level ()
 Returns the current debug level.
void set_strict (bool status=false)
 Sets the strict mode.
int get_strict ()
 Returns the current strict status.
void set_trace (const std::string &function, bool status)
 Adds a function to the list of traced functions.
void clear_trace ()
 Clears the list of traced functions.

Static Public Attributes

static std::map< std::string,
DPtr
empty_options
 Empty options struct.

Protected Member Functions

void init (int debugger=0)
 Initializes the interpreter.
void init_interface ()
 Initializes the libraries and the variables.
void finalize ()
 Prepares the interpreter for deletion.
DPtr evaluate (const DPtr &object, SPtr< Scope > scope, const std::string &exception, int depth, int casts)
 Evaluates one statement.
DPtr type_cast (const DPtr &object, const std::string &type, const SPtr< Scope > &scope, int depth, int casts)
 Makes a full type cast of the object to the given type.
DPtr light_cast (const DPtr &object, const std::string &type)
 Makes a light type cast of the object to the given type.
DPtr call_eval (const std::string &name, const DPtr &object, const SPtr< List > &arguments, const std::map< std::string, DPtr > &options, const SPtr< Scope > &scope, const std::string &exception, int depth, int casts)
 Returns the result of the function, which is suitable for being called with the given arguments.
DPtr check_call_eval (const std::string &name, const DPtr &object, const SPtr< List > &arguments, const std::map< std::string, DPtr > &options, const SPtr< Scope > &scope, const std::string &exception, int depth, int casts)
 Returns the result of the function, which is suitable for being called with the given arguments.
DPtr check_call (const std::string &name, const DPtr &object, const SPtr< List > &arguments, const std::map< std::string, DPtr > &options, const SPtr< Scope > &scope, const std::string &exception, int depth, int casts)
 Returns the result of the function, which is suitable for being called with the given arguments.
DPtr check_call_atomic (const std::string &name, const DPtr &object, const SPtr< List > &arguments, const std::map< std::string, DPtr > &options, const SPtr< Scope > &scope, const std::string &exception, int depth, int casts)
 Returns the result of the atomic function, which is suitable for being called with the given arguments.
int prepare_function_call (const std::string &name, const SPtr< AtomicFunction > &function, const SPtr< List > &arguments, SPtr< List > &casted, const SPtr< Scope > &scope, int &n, int depth, int casts)
 Prepares the execution of a function.
bool check_condition (const std::string &name, const SPtr< AtomicFunction > &function, const SPtr< List > &arguments, const std::map< std::string, DPtr > &options, const SPtr< Scope > &scope, int depth, int casts)
 Checks the function condition.
SPtr< Listevaluate_arguments (const SPtr< List > &list, List::iterator start, List::iterator stop, const SPtr< Scope > &scope)
 Evaluates a list of arguments.
DPtr create_class (const SPtr< Block > &block, const SPtr< Scope > &scope)
 Creates a class instance.
DPtr new_object (const std::string &type, const SPtr< Class > &object, const SPtr< List > &arguments, const std::map< std::string, DPtr > &options, const SPtr< Scope > &scope, int depth)
 Creates a new object.
bool evaluate_boolean (const DPtr &statement, const SPtr< Scope > &scope, int depth)
 Evaluation of boolean expressions.
DPtr evaluate_selector (const SPtr< Selector > &selector, SPtr< Scope > scope, const std::string &exception, int depth, int casts)
 Evaluates a selector.
DPtr assign_lvalue (DPtr &lvalue, const DPtr &rvalue, const SPtr< Scope > &scope, int depth, int casts)
 Assignes an lvalue.
DPtr collect_arguments (DPtr function, SPtr< List > arguments)
 Collects the evaluated arguments to a function.
void populate_scope (const SPtr< List > &parameters, const SPtr< List > &arguments, const std::list< Option > &defaults, const std::map< std::string, DPtr > &options, SPtr< Scope > &scope, SPtr< List > real_options, int depth)
 Populates the scope with values.
DPtr call_function (const std::string &name, const SPtr< AtomicFunction > &function, SPtr< List > arguments, const std::map< std::string, DPtr > &options, SPtr< Scope > scope, int depth, int casts)
 Function call.
bool check_debug (const std::string &name, const SPtr< Scope > &scope)
 Checks if the function is set to be traced.
DPtr load_libraries (const SPtr< List > &libraries, const SPtr< Scope > &scope, Library::OpenMode mode)
 Loads methods from a library.
void unload_libraries (const SPtr< List > &libraries)
 Unloads a library.
SPtr< Listevaluate_list (const SPtr< List > &list, const SPtr< Scope > &scope, const std::string &exception, int depth, int casts)
 List evaluation.

Protected Attributes

SPtr< Scopeglobal_scope
 An interpreter has a global name scope.
PluginManagerplugin_manager
 Plugin manager.
ObjectFactoryobject_factory
 Object factory.
bool stop
 If abort flag is set the interpreter exits in the next evaluate call.
int debug_level
 Debugging level.
std::map< std::string, bool > trace
 Trace flag.
bool strict
 Strict mode is the non symbolic mode of contrap.
SPtr< Listhistory
 Outputs.
Engineengine
 Corresponding parser.
Loggerlogger
 Logger.
int stack_depth
 Stack depth.
SPtr< bool > True
 Global boolean variables.
SPtr< bool > False

Detailed Description

Class Interpreter implements the command interpreter of ContRap.


Member Enumeration Documentation

Debug levels.

Enumerator:
NO_DEBUG 

No debugging messages reported.

ENTRY_DEBUG 

Debug procedure entries and returns.

SEARCH_DEBUG 

Debug procedure search mechanisms.

ARGUMENT_DEBUG 

Debug procedure argument evaluation.

Notice that the arguments of the final function are shown anyway if ENTRY_DEBUG is on.

DEEP_DEBUG 

Low level debug for developing functions.

In this mode low level features, like assignments are shown.

USER_DEBUG 

User debug flag.


Constructor & Destructor Documentation

crp::Interpreter::Interpreter ( int  debugger,
Engine engine 
)

Creates a new interpreter.

Parameters:
debuggerIf set to a value different to zero the debugging mode is on from the beginning
engineEngine is coupled with the corresponding text parser
crp::Interpreter::~Interpreter ( )

Releases the memory.


Member Function Documentation

DPtr crp::Interpreter::assign_lvalue ( DPtr lvalue,
const DPtr rvalue,
const SPtr< Scope > &  scope,
int  depth,
int  casts 
) [protected]

Assignes an lvalue.

DPtr crp::Interpreter::call ( const DPtr function,
const SPtr< List > &  arguments,
SPtr< Scope scope = 0,
const std::map< std::string, DPtr > &  options = empty_options 
)

Calls a function with given arguments if possible.

If there is no corresponding function a call statement is returned. Use check_call() if you want to decide if a function can be called.

NOTICE: The function does NOT evaluate its arguments

Parameters:
functionStatement pointing to a function sequence
argumentsArguments of the function
scopeScope of the evaluation
Returns:
Resulting value
DPtr crp::Interpreter::call ( const std::string &  function,
const SPtr< List > &  arguments,
SPtr< Scope scope = 0,
const std::map< std::string, DPtr > &  options = empty_options 
)

Calls a function by name with given arguments if possible.

If there is no corresponding function a call statement is returned. Use check_call() if you want to decide if a function can be called.

NOTICE: The function does NOT evaluate its arguments

Parameters:
functionFunction name
argumentsArguments of the function
scopeScope of the evaluation
Returns:
Resulting value
DPtr crp::Interpreter::call_eval ( const std::string &  name,
const DPtr object,
const SPtr< List > &  arguments,
const std::map< std::string, DPtr > &  options,
const SPtr< Scope > &  scope,
const std::string &  exception,
int  depth,
int  casts 
) [protected]

Returns the result of the function, which is suitable for being called with the given arguments.

If there is no such function a symbolic call description is returned.

Parameters:
nameName of the function
objectStatement pointing to a function sequence
argumentsArguments of the function
scopeScope of the evaluation
exceptionExceptional symbol
depthRecursion depth
nameName of the function
castsNumber of recursive casts
Returns:
Resulting value
DPtr crp::Interpreter::call_function ( const std::string &  name,
const SPtr< AtomicFunction > &  function,
SPtr< List arguments,
const std::map< std::string, DPtr > &  options,
SPtr< Scope scope,
int  depth,
int  casts 
) [protected]

Function call.

Parameters:
nameName of the function in the local context
functionFunction statement
argumentsList with the arguments of the function
scopeExecution scope
depthRecursion depth
castsNumber of recursive casts
DPtr crp::Interpreter::check_call ( const DPtr function,
const SPtr< List > &  arguments,
SPtr< Scope scope = 0 
)

Calls a function with given arguments if possible.

If there is no corresponding function an invalid pointer is returned.

NOTICE: The function does NOT evaluate its arguments

Parameters:
functionStatement pointing to a function sequence
argumentsArguments of the function
scopeScope of the evaluation
Returns:
Resulting value
DPtr crp::Interpreter::check_call ( const std::string &  name,
const DPtr object,
const SPtr< List > &  arguments,
const std::map< std::string, DPtr > &  options,
const SPtr< Scope > &  scope,
const std::string &  exception,
int  depth,
int  casts 
) [protected]

Returns the result of the function, which is suitable for being called with the given arguments.

If there is no such function an invalid pointer is returned.

Parameters:
nameName of the function
objectStatement pointing to a function sequence
argumentsArguments of the function
scopeScope of the evaluation
depthRecursion depth
castsNuber of recursive casts
Returns:
Object of the function to execute or zero pointer
DPtr crp::Interpreter::check_call ( const std::string &  function,
const SPtr< List > &  arguments,
const std::map< std::string, DPtr > &  options,
SPtr< Scope scope = 0 
)

Calls a function with given arguments if possible.

If there is no corresponding function an invalid pointer is returned.

NOTICE: The function does NOT evaluate its arguments

Parameters:
functionStatement pointing to a function sequence
argumentsArguments of the function
scopeScope of the evaluation
Returns:
Resulting value
DPtr crp::Interpreter::check_call_atomic ( const std::string &  name,
const DPtr object,
const SPtr< List > &  arguments,
const std::map< std::string, DPtr > &  options,
const SPtr< Scope > &  scope,
const std::string &  exception,
int  depth,
int  casts 
) [protected]

Returns the result of the atomic function, which is suitable for being called with the given arguments.

If there is no such function an invalid pointer is returned.

Parameters:
nameName of the function
objectStatement pointing to a function sequence
argumentsArguments of the function
scopeScope of the evaluation
depthRecursion depth
castsNuber of recursive casts
Returns:
Object of the function to execute or zero pointer
DPtr crp::Interpreter::check_call_eval ( const std::string &  name,
const DPtr object,
const SPtr< List > &  arguments,
const std::map< std::string, DPtr > &  options,
const SPtr< Scope > &  scope,
const std::string &  exception,
int  depth,
int  casts 
) [protected]

Returns the result of the function, which is suitable for being called with the given arguments.

If there is no such function an invalid pointer is returned.

Parameters:
nameName of the function
objectStatement pointing to a function sequence
argumentsArguments of the function
scopeScope of the evaluation
exceptionExceptional symbol
depthRecursion depth
castsNuber of recursive casts
Returns:
Object of the function to execute or zero pointer
bool crp::Interpreter::check_condition ( const std::string &  name,
const SPtr< AtomicFunction > &  function,
const SPtr< List > &  arguments,
const std::map< std::string, DPtr > &  options,
const SPtr< Scope > &  scope,
int  depth,
int  casts 
) [protected]

Checks the function condition.

bool crp::Interpreter::check_debug ( const std::string &  name,
const SPtr< Scope > &  scope 
) [protected]

Checks if the function is set to be traced.

void crp::Interpreter::clear ( )

Clears the interpreter.

void crp::Interpreter::clear_trace ( )

Clears the list of traced functions.

Notice that if at least one function is traced, the interpreter might become inefficient.

DPtr crp::Interpreter::collect_arguments ( DPtr  function,
SPtr< List arguments 
) [protected]

Collects the evaluated arguments to a function.

DPtr crp::Interpreter::create_class ( const SPtr< Block > &  block,
const SPtr< Scope > &  scope 
) [protected]

Creates a class instance.

DPtr crp::Interpreter::evaluate ( const DPtr data,
const SPtr< Scope > &  scope = 0 
)

Evaluates one statement.

Parameters:
objectStatement to execute
scopeScope of the evaluation
Returns:
Result of the execution
DPtr crp::Interpreter::evaluate ( const DPtr object,
SPtr< Scope scope,
const std::string &  exception,
int  depth,
int  casts 
) [protected]

Evaluates one statement.

Parameters:
objectStatement to execute
scopeScope to evaluate the expression
objectObject which is used for evaluation
exceptionException symbol for detection of recursive definitions
depthRecursion depth
castsNumber of recursive casts
Returns:
Result of the execution
SPtr<List> crp::Interpreter::evaluate_arguments ( const SPtr< List > &  list,
List::iterator  start,
List::iterator  stop,
const SPtr< Scope > &  scope 
) [protected]

Evaluates a list of arguments.

Parameters:
listObeject to evaluate
startStarting iterator
stopStop iterator
Returns:
List with evaluated arguments
bool crp::Interpreter::evaluate_boolean ( const DPtr statement,
const SPtr< Scope > &  scope,
int  depth 
) [protected]

Evaluation of boolean expressions.

Parameters:
statementStatement which should evaluate to boolean
scopeScope of the evaluation
Returns:
Truth value
SPtr<List> crp::Interpreter::evaluate_list ( const SPtr< List > &  list,
const SPtr< Scope > &  scope,
const std::string &  exception,
int  depth,
int  casts 
) [protected]

List evaluation.

DPtr crp::Interpreter::evaluate_selector ( const SPtr< Selector > &  selector,
SPtr< Scope scope,
const std::string &  exception,
int  depth,
int  casts 
) [protected]

Evaluates a selector.

void crp::Interpreter::finalize ( ) [protected]

Prepares the interpreter for deletion.

int crp::Interpreter::get_debug_level ( ) [inline]

Returns the current debug level.

Returns:
Current debug level.
const SPtr<List>& crp::Interpreter::get_history ( ) [inline]

Returns the output history.

Returns:
Output history list
const SPtr<Scope>& crp::Interpreter::get_scope ( )

Returns the global scope.

Returns:
Global scope
int crp::Interpreter::get_strict ( ) [inline]

Returns the current strict status.

Returns:
True if the interpreter is in the strict mode.
bool crp::Interpreter::halt ( bool  status)

Aborts the current evaluation.

Parameters:
statusInterpreter status
Returns:
The previous status
void crp::Interpreter::init ( int  debugger = 0) [protected]

Initializes the interpreter.

void crp::Interpreter::init_interface ( ) [protected]

Initializes the libraries and the variables.

DPtr crp::Interpreter::light_cast ( const DPtr object,
const std::string &  type 
) [protected]

Makes a light type cast of the object to the given type.

A light cast only checks for synatcatica equality of types.

Parameters:
objectObject to cast
typeName of the type to cast to
scopeScope of the evaluation
depthRecursion depth
castsNumber of recursive casts
Returns:
Casted object or null pointer if no cast is possible
DPtr crp::Interpreter::load_libraries ( const SPtr< List > &  libraries,
const SPtr< Scope > &  scope,
Library::OpenMode  mode 
) [protected]

Loads methods from a library.

Parameters:
librariesObjects to load
scopeEvaluation scope
modeOpen mode for this and dependent libraries
Returns:
Loaded object
DPtr crp::Interpreter::load_library ( const std::string &  library,
Library::OpenMode  mode = Library::NO_CHANGE 
)

Tells the interpreter to load a library.

Parameters:
libraryName of the library
modeOpen mode of the library
Returns:
Result of the loading process
DPtr crp::Interpreter::new_object ( const std::string &  type,
const SPtr< Class > &  object,
const SPtr< List > &  arguments,
const std::map< std::string, DPtr > &  options,
const SPtr< Scope > &  scope,
int  depth 
) [protected]

Creates a new object.

Parameters:
typeName of the type of the object
objectObject pointing to an object description
argumentsArguments of the constructor
scopeScope of the evaluation
exceptionExceptional symbol
depthRecursion depth
Returns:
Resulting value
void crp::Interpreter::populate_scope ( const SPtr< List > &  parameters,
const SPtr< List > &  arguments,
const std::list< Option > &  defaults,
const std::map< std::string, DPtr > &  options,
SPtr< Scope > &  scope,
SPtr< List real_options,
int  depth 
) [protected]

Populates the scope with values.

int crp::Interpreter::prepare_function_call ( const std::string &  name,
const SPtr< AtomicFunction > &  function,
const SPtr< List > &  arguments,
SPtr< List > &  casted,
const SPtr< Scope > &  scope,
int &  n,
int  depth,
int  casts 
) [protected]

Prepares the execution of a function.

void crp::Interpreter::set_debug_level ( int  status = NO_DEBUG) [inline]

Sets the debug level for intermediate computations.

Debug levels are encoded by integers, the greater the number the more debugging messages appear.

Parameters:
statusNew debug level, an or combination of DebugLevel flags.
void crp::Interpreter::set_recursion_depth ( int  depth) [inline]

Sets the maximal depth of the recursion.

Parameters:
depthRecursion depth
void crp::Interpreter::set_strict ( bool  status = false) [inline]

Sets the strict mode.

Parameters:
statusTrue if the interpreter should work in the strict mode.
void crp::Interpreter::set_trace ( const std::string &  function,
bool  status 
) [inline]

Adds a function to the list of traced functions.

Parameters:
functionExported name of the function or native function name.
statusIf set to true the function is traced.
DPtr crp::Interpreter::type_cast ( const DPtr object,
const std::string &  type,
const SPtr< Scope > &  scope = 0 
)

Makes a full type cast of the object to the given type.

A full type cast is performed with all available tools. This includes the syntactic type equality, a meta cast using the object factory and implicit conversion.

Parameters:
objectObject to cast
typeName of the type to cast to
scopeScope of the evaluation
Returns:
Casted object an invalid pointer if no cast is possible
DPtr crp::Interpreter::type_cast ( const DPtr object,
const std::string &  type,
const SPtr< Scope > &  scope,
int  depth,
int  casts 
) [protected]

Makes a full type cast of the object to the given type.

Parameters:
objectObject to cast
typeName of the type to cast to
scopeScope of the evaluation
depthRecursion depth
castsNumber of recursive casts
Returns:
Casted object or null pointer if no cast is possible
void crp::Interpreter::unload_libraries ( const SPtr< List > &  libraries) [protected]

Unloads a library.

Parameters:
librariesObjects to unload

Field Documentation

Debugging level.

A boolean or combination of DebugLevel flags.

std::map<std::string, DPtr> crp::Interpreter::empty_options [static]

Empty options struct.

Corresponding parser.

SPtr<bool> crp::Interpreter::False [protected]

An interpreter has a global name scope.

Outputs.

Object factory.

Plugin manager.

Stack depth.

bool crp::Interpreter::stop [protected]

If abort flag is set the interpreter exits in the next evaluate call.

bool crp::Interpreter::strict [protected]

Strict mode is the non symbolic mode of contrap.

std::map<std::string, bool> crp::Interpreter::trace [protected]

Trace flag.

SPtr<bool> crp::Interpreter::True [protected]

Global boolean variables.


The documentation for this class was generated from the following file: