Click here to Skip to main content
15,896,201 members
Articles / Containers / Virtual Machine

TOOL

Rate me:
Please Sign up or sign in to vote.
4.98/5 (52 votes)
23 Oct 200676 min read 231.3K   5.4K   147  
TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language interpreter. The purpose of this article is to introduce the TOOL interpreter and language from the perspective of a person who has a desire to include a scripting solution as part of his project.
/*****************************************************************************/
/*                              SOURCE FILE                                  */
/*****************************************************************************/
/*
       $Archive:   $

      $Revision:   $
          $Date:   $
        $Author:   $

    Description:   The VMCalculator Object aggregates an expression analyzer and
                   a variables map to provide a single class wrapper for the
                   system.

                      TOOL And XML FORMS License
                      ==========================

                      Except where otherwise noted, all of the documentation 
                      and software included in the TOOL package is 
                      copyrighted by Michael Swartzendruber.

                      Copyright (C) 2005 Michael John Swartzendruber. 
                      All rights reserved.

                      Access to this code, whether intentional or accidental,
                      does NOT IMPLY any transfer of rights.

                      This software is provided "as-is," without any express 
                      or implied warranty. In no event shall the author be held
                      liable for any damages arising from the use of this software.

                      Permission is granted to anyone to use this software for 
                      any purpose, including commercial applications, and to 
                      alter and redistribute it, provided that the following 
                      conditions are met:

                      1. All redistributions of source code files must retain 
                         all copyright notices that are currently in place, 
                         and this list of conditions without modification.

                      2. The origin of this software must not be misrepresented;
                         you must not claim that you wrote the original software.

                      3. If you use this software in another product, an acknowledgment
                         in the product documentation would be appreciated but is
                         not required.

                      4. Modified versions in source or binary form must be plainly 
                         marked as such, and must not be misrepresented as being 
                         the original software.
*/
static char OBJECT_ID[] = "$Revision:   $ : $Date:   $";
/*****************************************************************************/

#include "VMCalcVariable.h"
#include "VMCalcSubExpression.h"
#include "VMCalculator.h"


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCalculator::VMCalculator

       DESCRIPTION:  ctor.

             INPUT:  void 
            OUTPUT:  none

           RETURNS:  none
*/
VMCalculator::VMCalculator( void )
{
  m_oAnalyzer.AtachVariables( &m_oVariables );
}
/* End of function "VMCalculator::VMCalculator"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCalculator::~VMCalculator

       DESCRIPTION:  dtor. erases variables map

             INPUT:  void 
            OUTPUT:  none

           RETURNS:  none
*/
VMCalculator::~VMCalculator( void )
{
  IterCMapVariable oIter = m_oVariables.begin();

  while ( m_oVariables.end() != oIter )
  {
    VMCalcValueBase* pVal = (*oIter).second;
    delete pVal;
    ++oIter;
  }
  m_oVariables.erase( m_oVariables.begin(), m_oVariables.end() );	
}
/* End of function "VMCalculator::~VMCalculator"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCalculator::SetEquation

       DESCRIPTION:  Assigns an equation to the analyzer

             INPUT:  pchEquation - pointer to string containing the equation 
                     to be solved
            OUTPUT:  none

           RETURNS:  results of equation analysis
*/
int VMCalculator::SetEquation( const char* pchEquation )
{
  return( m_oAnalyzer.ChangeExpression( pchEquation ) );
}
/* End of function "VMCalculator::SetEquation"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCalculator::GetResult

       DESCRIPTION:  used to extract the results of processing the equation

             INPUT:  rdOutput - the results will go here
            OUTPUT:  none

           RETURNS:  result code for the equation processing
*/
int VMCalculator::GetResult( double& rdOutput )
{
  return( m_oAnalyzer.Value( rdOutput ) );
}
/* End of function "VMCalculator::GetResult"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCalculator::AddSubExpression

       DESCRIPTION:  used to add a sub-equation to the processing tree

             INPUT:  pchVarName - pointer to string containing the name for 
                                  the sub equation
                     pchVarStrValue - pointer to string containg the actual
                                      equation
            OUTPUT:  

           RETURNS:  void
*/
void VMCalculator::AddSubExpression( const char* pchVarName, const char* pchVarStrValue )
{
  VMCalcExpressionAnalyzer* poExpression = new VMCalcExpressionAnalyzer( &m_oVariables );
  poExpression->ChangeExpression( pchVarStrValue );

  VMCalcSubExpression* poVariable = new VMCalcSubExpression;
  poVariable->SetValue( poExpression );

  std::string oVarName     = pchVarName;
  m_oVariables[ oVarName ] = poVariable;
}
/* End of function "VMCalculator::AddSubExpression"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCalculator::AddVariable

       DESCRIPTION:  Adds a new variable to the collection

             INPUT:  pchVarName - the variable name
                     pchVarStrValue - the value for the variable
            OUTPUT:  

           RETURNS:  void
*/
void VMCalculator::AddVariable( const char* pchVarName, const char* pchVarStrValue )
{
  RemoveVariable( pchVarName );

  VMCalcVariable* poVariable = new VMCalcVariable;
  poVariable->SetValue( atof( pchVarStrValue ) );

  std::string oVarName   = pchVarName;
  m_oVariables[oVarName] = poVariable;
}
/* End of function "VMCalculator::AddVariable"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCalculator::RemoveVariable

       DESCRIPTION:  removes a variable from the collection

             INPUT:  pchName - pointer to string containing the name of the 
                     variable to remove
            OUTPUT:  none

           RETURNS:  void
*/
void VMCalculator::RemoveVariable( const char* pchName )
{
  std::string oKey = pchName;

  IterCMapVariable oIter = m_oVariables.find( oKey );

  if ( m_oVariables.end() != oIter )
  {
    VMCalcValueBase* pValue = (*oIter).second;
    delete pValue;
    m_oVariables.erase( oIter );
  }
}
/* End of function "VMCalculator::RemoveVariable"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMCalculator::GetVariables

       DESCRIPTION:  returns the variables collection to the caller

             INPUT:  void
            OUTPUT:  none

           RETURNS:  pointer to the internal variables map
*/
CMapVariable* VMCalculator::GetVariables( void )
{
  return( &m_oVariables );
}
/* End of function "VMCalculator::GetVariables"
/*****************************************************************************/



/*****************************************************************************/
/* Check-in history */
/*
 *$Log:  $
*/
/*****************************************************************************/

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions