/*************************************************************** * Function: CodeParser::parseCellIndexExpr() * Purpose : Parse the XML code of a cell indexing expression * Initial : Maxime Chevalier-Boisvert on February 15, 2009 **************************************************************** Revisions and bug fixes: */ Expression* CodeParser::parseCellIndexExpr(const XML::Element* pElement) { // Parse the symbol expression Expression* pSymExpr = parseExpression(pElement->getChildElement(0)); // If the symbol expression is not a symbol, throw an exception if (pSymExpr->getExprType() != Expression::SYMBOL) throw XML::ParseError("Expected symbol expression", pElement->getTextPos()); // Compute the number of arguments size_t numArgs = pElement->getNumChildren() - 1; // Declare a vector for the arguments ParamExpr::ExprVector arguments; // For each child element for (size_t i = 0; i < numArgs; ++i) { // Parse this argument arguments.push_back(parseExpression(pElement->getChildElement(i + 1))); } // Create and return the new cell indexing expression return new CellIndexExpr( (SymbolExpr*)pSymExpr, arguments ); }
/*************************************************************** * Function: CodeParser::parseParamExpr() * Purpose : Parse the XML code of a parameterized expression * Initial : Maxime Chevalier-Boisvert on November 7, 2008 **************************************************************** Revisions and bug fixes: */ Expression* CodeParser::parseParamExpr(const XML::Element* pElement) { // Parse the symbol expression Expression* pExpr = parseExpression(pElement->getChildElement(0)); // Compute the number of arguments size_t numArgs = pElement->getNumChildren() - 1; // Declare a vector for the arguments ParamExpr::ExprVector arguments; // For each child element for (size_t i = 0; i < numArgs; ++i) { // Parse this argument arguments.push_back(parseExpression(pElement->getChildElement(i + 1))); } // Create and return the new parameterized expression return new ParamExpr( pExpr, arguments ); }
/*************************************************************** * Function: processEndExpr(Expression*) * Purpose : Pre-process range end sub-expressions in expressions * Initial : Maxime Chevalier-Boisvert on March 1, 2009 **************************************************************** Revisions and bug fixes: */ Expression* processEndExpr(Expression* pExpr, ProgFunction* pFunction, const EndExpr::AssocVector& assocs) { // Switch on the expression type switch (pExpr->getExprType()) { // Parameterized expression case Expression::PARAM: { // Get a typed pointer to the expression ParamExpr* pParamExpr = (ParamExpr*)pExpr; // Get the symbol expression SymbolExpr* pSymbol = pParamExpr->getSymExpr(); // Get the argument vector const ParamExpr::ExprVector& args = pParamExpr->getArguments(); // Create a vector for the transformed arguments ParamExpr::ExprVector newArgs; // For each argument for (size_t i = 0; i < args.size(); ++i) { // Create a new association vector EndExpr::AssocVector newAssocs; // Create an association for this argument and add it to the vector newAssocs.push_back( EndExpr::Assoc( pSymbol, i, i == args.size() - 1 ) ); // Add the higher-level associations to the end of the vector newAssocs.insert(newAssocs.end(), assocs.begin(), assocs.end()); // Transform this argument expression newArgs.push_back( processEndExpr( args[i], pFunction, newAssocs ) ); } // Return the transformed parameterized expression return new ParamExpr(pSymbol->copy(), newArgs); } break; // Cell indexing expression case Expression::CELL_INDEX: { // Get a typed pointer to the expression CellIndexExpr* pCellExpr = (CellIndexExpr*)pExpr; // Get the symbol expression SymbolExpr* pSymbol = pCellExpr->getSymExpr(); // Get the argument vector const CellIndexExpr::ExprVector& args = pCellExpr->getArguments(); // Create a vector for the transformed arguments CellIndexExpr::ExprVector newArgs; // For each argument for (size_t i = 0; i < args.size(); ++i) { // Create a new association vector EndExpr::AssocVector newAssocs; // Create an association for this argument and add it to the vector newAssocs.push_back( EndExpr::Assoc( pSymbol, i, i == args.size() - 1 ) ); // Add the higher-level associations to the end of the vector newAssocs.insert(newAssocs.end(), assocs.begin(), assocs.end()); // Transform this argument expression newArgs.push_back( processEndExpr( args[i], pFunction, newAssocs ) ); } // Return the transformed parameterized expression return new CellIndexExpr(pSymbol->copy(), newArgs); } break; // Range end expression case Expression::END: { // Ensure associations were found assert (assocs.empty() == false); // Create and return a new range end expression with the matrix associations return new EndExpr(assocs); } break; // All other expression types default: { // Copy the expression pExpr = pExpr->copy(); // Get the list of sub-expressions for this expression Expression::ExprVector subExprs = pExpr->getSubExprs(); // For each sub-expression for (size_t i = 0; i < subExprs.size(); ++i) { // Get a pointer to the sub-expression Expression* pSubExpr = subExprs[i]; // If this sub-expression is NULL, skip it if (pSubExpr == NULL) continue; // Transform this sub-expression Expression* pNewExpr = processEndExpr(pSubExpr, pFunction, assocs); // Replace the sub-expression by the transformed version pExpr->replaceSubExpr(i, pNewExpr); } // Return the modified expression return pExpr; } } }