Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-27 09:18:07

0001 //
0002 // ********************************************************************
0003 // * License and Disclaimer                                           *
0004 // *                                                                  *
0005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
0006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
0007 // * conditions of the Geant4 Software License,  included in the file *
0008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
0009 // * include a list of copyright holders.                             *
0010 // *                                                                  *
0011 // * Neither the authors of this software system, nor their employing *
0012 // * institutes,nor the agencies providing financial support for this *
0013 // * work  make  any representation or  warranty, express or implied, *
0014 // * regarding  this  software system or assume any liability for its *
0015 // * use.  Please see the license in the file  LICENSE  and URL above *
0016 // * for the full disclaimer and the limitation of liability.         *
0017 // *                                                                  *
0018 // * This  code  implementation is the result of  the  scientific and *
0019 // * technical work of the GEANT4 collaboration.                      *
0020 // * By using,  copying,  modifying or  distributing the software (or *
0021 // * any work based  on the software)  you  agree  to acknowledge its *
0022 // * use  in  resulting  scientific  publications,  and indicate your *
0023 // * acceptance of all terms of the Geant4 Software license.          *
0024 // ********************************************************************
0025 //
0026 /*
0027  * =============================================================================
0028  *
0029  *       Filename:  CexmcCustomFilter.cc
0030  *
0031  *    Description:  custom filter grammar and compiler
0032  *
0033  *        Version:  1.0
0034  *        Created:  17.07.2010 15:37:38
0035  *       Revision:  none
0036  *       Compiler:  gcc
0037  *
0038  *         Author:  Alexey Radkov (), 
0039  *        Company:  PNPI
0040  *
0041  * =============================================================================
0042  */
0043 
0044 #ifdef CEXMC_USE_CUSTOM_FILTER
0045 
0046 #include "CexmcCustomFilter.hh"
0047 
0048 
0049 namespace  CexmcCustomFilter
0050 {
0051     void  Compiler::operator()( ParseResult &  parseResult, Action  value )
0052                                                                         const
0053     {
0054         parseResult.action = value;
0055     }
0056 
0057 
0058     void  Compiler::operator()( ParseResult &  parseResult, Subtree &  value )
0059                                                                         const
0060     {
0061         parseResult.expression = value;
0062     }
0063 
0064 
0065     void  Compiler::operator()( Subtree &  ast, Node &  node ) const
0066     {
0067         try
0068         {
0069             ast = boost::get< Subtree >( node );
0070         }
0071         catch ( const boost::bad_get & )
0072         {
0073             ast.type = Operator( Top );
0074             ast.children.push_back( node );
0075         }
0076     }
0077 
0078 
0079     void  Compiler::operator()( Node &  self, Node &  left, Node &  right,
0080                                 Operator  value ) const
0081     {
0082         Subtree &  ast( boost::get< Subtree >( self ) );
0083 
0084         ast.children.push_back( left );
0085         ast.type = value;
0086 
0087         Subtree *  astRight( boost::get< Subtree >( &right ) );
0088 
0089         if ( ! astRight )
0090         {
0091             ast.children.push_back( right );
0092             return;
0093         }
0094 
0095         bool        haveSamePriorities( false );
0096         Operator *  rightOp( boost::get< Operator >( &astRight->type ) );
0097 
0098         if ( rightOp )
0099             haveSamePriorities = value.priority == rightOp->priority;
0100 
0101         if ( value.hasRLAssoc || ! haveSamePriorities )
0102         {
0103             ast.children.push_back( right );
0104             return;
0105         }
0106 
0107         Subtree *  astDeepestRight( astRight );
0108 
0109         /* propagate left binary operators with LR associativity (i.e. all in
0110          * our grammar) deep into the AST until any operator with a different
0111          * priority (which includes operators in parentheses that have priority
0112          * 0) or a unary operator or a function occured */
0113         while ( true )
0114         {
0115             Subtree *   candidate = boost::get< Subtree >(
0116                                             &astDeepestRight->children[ 0 ] );
0117             if ( ! candidate )
0118                 break;
0119 
0120             if ( candidate->children.size() < 2 )
0121                 break;
0122 
0123             bool        haveSamePriorities_( false );
0124             Operator *  candidateOp( boost::get< Operator >(
0125                                                         &candidate->type ) );
0126 
0127             if ( candidateOp )
0128                 haveSamePriorities_ = value.priority == candidateOp->priority;
0129 
0130             /* FIXME: what to do if candidate has RL association? Our grammar is
0131              * not a subject of this issue; probably no grammar is a subject */
0132             if ( ! haveSamePriorities_ )
0133                 break;
0134 
0135             astDeepestRight = candidate;
0136         }
0137 
0138         Subtree    astResult;
0139         astResult.children.push_back( ast.children[ 0 ] );
0140         astResult.children.push_back( astDeepestRight->children[ 0 ] );
0141         astResult.type = value;
0142         astDeepestRight->children[ 0 ] = astResult;
0143         self = right;
0144     }
0145 
0146 
0147     void  Compiler::operator()( Node &  self, Node &  child, Operator  value )
0148                                                                         const
0149     {
0150         Subtree &  ast( boost::get< Subtree >( self ) );
0151         ast.children.push_back( child );
0152         ast.type = value;
0153     }
0154 
0155 
0156     void  Compiler::operator()( Node &  self, Node &  primary ) const
0157     {
0158         self = primary;
0159 
0160         Subtree *  ast( boost::get< Subtree >( &self ) );
0161 
0162         if ( ! ast )
0163             return;
0164 
0165         Operator *  op( boost::get< Operator >( &ast->type ) );
0166 
0167         if ( op )
0168             op->priority = 0;
0169     }
0170 
0171 
0172     void  Compiler::operator()( Node &  self, Node &  child,
0173                                 std::string &  value ) const
0174     {
0175         Subtree &  ast( boost::get< Subtree >( self ) );
0176 
0177         ast.children.push_back( child );
0178         ast.type = value;
0179     }
0180 
0181 
0182     void  Compiler::operator()( Leaf &  self, std::string &  name ) const
0183     {
0184         Variable &  variable( boost::get< Variable >( self ) );
0185         variable.name = name;
0186     }
0187 
0188 
0189     void  Compiler::operator()( Leaf &  self, int  value, size_t  index ) const
0190     {
0191         Variable &  variable( boost::get< Variable >( self ) );
0192         switch ( index )
0193         {
0194         case 0 :
0195             variable.index1 = value;
0196             break;
0197         case 1 :
0198             variable.index2 = value;
0199             break;
0200         default :
0201             break;
0202         }
0203     }
0204 }
0205 
0206 #endif
0207