repr_cpp_token_operator.cpp 8.48 KB
Newer Older
Pierre Aubert's avatar
Pierre Aubert committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/***************************************
	Auteur : Pierre Aubert
	Mail : aubertp7@gmail.com
	Licence : CeCILL-C
****************************************/

#include "string_utils.h"
#include "repr_cpp_operator.h"
#include "repr_cpp_token_operator.h"

///Save the headers of the PToken functions for Operator
/**	@param tabGraphToken : PTabGraphToken of all Operator
 * 	@param graphToken : graph of the current token
 * 	@return C++ header
*/
std::string repr_cpp_tokenOperatorHeader(const PTabGraphToken & tabGraphToken, const PGraphToken & graphToken){
	std::string body(""), name(graphToken.getName()), varName(graphToken.getVarName()), varType(graphToken.getVarType());
	PVecOperator vecOp;
	search_operatorByGraphUse(vecOp, tabGraphToken.getVecOperator(), graphToken.getName());
	if(vecOp.size() != 0lu){
		for(PVecOperator::iterator it(vecOp.begin()); it != vecOp.end(); ++it){
			std::string baseFctName("Operator" + repr_cpp_operator_getEnumValue(it->getValueEnum()));
			body += "\t\tbool " + name + baseFctName + "Do(" + varType + " & " + varName + ");\n";
			body += "\t\tvoid " + name + baseFctName + "Undo(" + varType + " & " + varName + ");\n\n";
		}
	}
	return body;
}

///Save the implementation of a single token Operator parsing function for the given graphToken
/**	@param op : operator to be used
 * 	@param graphToken : graph token related to the current operator
 * 	@return C++
*/
std::string repr_cpp_singleTokenOperator(const POperator & op, const PGraphToken & graphToken){
	std::string body(""), name(graphToken.getName()), varName(graphToken.getVarName()), varType(graphToken.getVarType());
37
	std::string opToken(op.getVecToken().front());
Pierre Aubert's avatar
Pierre Aubert committed
38 39 40 41 42
	body += "\t//repr_cpp_token_operator : Let's parse an Operator with 1 token\n";
	
	body += "\t//repr_cpp_token_operator : the first operand is already parsed and given as parameter\n";
	body += "\t//repr_cpp_token_operator : it would be great to add a while\n\n";
	
43
	body += "\tif(!isMatch(\""+opToken+"\")){return false;}\n";
Pierre Aubert's avatar
Pierre Aubert committed
44 45 46 47 48 49 50 51 52 53 54
	
	body += "\t//repr_cpp_token_operator : Let's call the graph token of the second operand\n";
	
	body += "\tPStmt rightOp = "+op.getExprClass()+"_default();\n";
	std::string rightOperandGraph(op.getVecExprGraph().back());
	body += "\tif(!" + rightOperandGraph + "(rightOp)){return false;}\n";
	
	body += "\t//repr_cpp_token_operator : here the operator is well parsed\n";
	
	body += "\tPStmt op = "+op.getExprClass()+"_default();\n";
	body += "\t"+op.getExprClass()+"_set"+firstToUpper(op.getAttrEnum())+"(op, "+op.getValueEnum()+");\n";
55
	body += "\tPStmt & vecOperand = "+op.getExprClass()+"_get"+firstToUpper(op.getVecOp())+"(op);\n";
56 57
	body += "\t\n";
	body += "\tbool isStandardPushBack(true);\n";
58 59 60 61 62 63
	body += "\tif(isAnOperator(rightOp)){\n";
	body += "\t\tlong priorityOp(get_priority_operator(op)), priorityRight(get_priority_operator(rightOp));\n";
	body += "\t\tstd::cout << \"op '"+opToken+"' : priorityOp = \" << priorityOp << \", priorityRight = \" << priorityRight << std::endl;\n";
	body += "\t\tif(priorityOp > priorityRight){\n";
	body += "\t\t\t//repr_cpp_token_operator : Let's get the first operand of the right operator rightOp\n";
	body += "\t\t\tPStmt * firstOp = get_first_operand_of_operator(rightOp);\n";
Pierre Aubert's avatar
Pierre Aubert committed
64
	body += "\t\t\tif(firstOp == NULL){return false;}\n";
65 66 67 68
	body += "\t\t\tvecOperand.push_back("+varName+");\t//This is a copy, so it works\n";
	body += "\t\t\tvecOperand.push_back(*firstOp);\n";
	body += "\t\t\t*firstOp = op;\n";
	body += "\t\t\t"+varName+" = rightOp;\n";
69 70 71 72
	body += "\t\t\tisStandardPushBack = false;\n";
	body += "\t\t}\n";
	body += "\t}\n";
	body += "\tif(isStandardPushBack){\n";
73
	body += "\t\tstd::cout << \"op '"+opToken+"' : Do a standard push_back\" << std::endl;\n";
74 75 76 77 78
	body += "\t\tvecOperand.push_back("+varName+");\n";
	body += "\t\tvecOperand.push_back(rightOp);\n";
	body += "\t\t//repr_cpp_token_operator : Finally, put the operator in the in/out parameter\n";
	body += "\t\t"+varName+" = op;\n";
	body += "\t}\n";
Pierre Aubert's avatar
Pierre Aubert committed
79 80 81 82 83 84 85 86 87 88
	return body;
}

///Save the implementation of a double tokens Operator parsing function for the given graphToken
/**	@param op : operator to be used
 * 	@param graphToken : graph token related to the current operator
 * 	@return C++
*/
std::string repr_cpp_doubleTokenOperator(const POperator & op, const PGraphToken & graphToken){
	std::string body(""), name(graphToken.getName()), varName(graphToken.getVarName()), varType(graphToken.getVarType());
Pierre Aubert's avatar
Pierre Aubert committed
89
	body += "\t//repr_cpp_token_operator : Let's parse an Operator with 2 tokens\n";
Pierre Aubert's avatar
Pierre Aubert committed
90 91 92 93 94 95 96 97 98 99 100
	
	return body;
}

///Save the implementation of a triple tokens Operator parsing function for the given graphToken
/**	@param op : operator to be used
 * 	@param graphToken : graph token related to the current operator
 * 	@return C++
*/
std::string repr_cpp_tripleTokenOperator(const POperator & op, const PGraphToken & graphToken){
	std::string body(""), name(graphToken.getName()), varName(graphToken.getVarName()), varType(graphToken.getVarType());
Pierre Aubert's avatar
Pierre Aubert committed
101
	body += "\t//repr_cpp_token_operator : Let's parse an Operator with 3 tokens\n";
Pierre Aubert's avatar
Pierre Aubert committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
	
	return body;
}

///Save the implementation of the Operator parsing function for the given graphToken
/**	@param parserName : name of the generated parser class
 * 	@param op : operator to be used
 * 	@param graphToken : graph token related to the current operator
 * 	@return C++
*/
std::string repr_cpp_tokenOperatorSourceOperatorDo(const std::string & parserName, const POperator & op, const PGraphToken & graphToken){
	std::string body(""), name(graphToken.getName()), varName(graphToken.getVarName()), varType(graphToken.getVarType());
	std::string opValue(repr_cpp_operator_getEnumValue(op.getValueEnum()));
	std::string baseFctName("Operator" + opValue);
	size_t nbToken(op.getVecToken().size());
	body += "///Parse the Operator " + opValue + " related to the GraphToken "+varName+"\n";
	body += "/**\t@param " + varName + " : " + varType + " to be parsed by the operator\n";
	body += " * \t@return true on success, false otherwise\n";
	body += "*/\n";
	body += "bool " + parserName + "::" + name + baseFctName + "Do(" + varType + " & " + varName + "){\n";
	body += "\tp_parser->pushPosition();\n";
	if(nbToken == 1lu){
		body += repr_cpp_singleTokenOperator(op, graphToken);
	}else if(nbToken == 2lu){
		body += repr_cpp_doubleTokenOperator(op, graphToken);
	}else if(nbToken == 3lu){
		body += repr_cpp_tripleTokenOperator(op, graphToken);
	}else{
		body += "\t#error \"repr_cpp_tokenOperatorSourceOperatorDo : no rules to parse operator with "+convertToString(nbToken)+" token\"\n";
	}
	
	body += "\tPStmt & _vecOp = " + varType + "_get"+firstToUpper(op.getVecOp())+"(" + varName + ");\n";
Pierre Aubert's avatar
Pierre Aubert committed
134
	body += "\t//repr_cpp_token_operator : If there is no operand\n";
Pierre Aubert's avatar
Pierre Aubert committed
135 136
	body += "\tif(_vecOp.size() == 0lu){return false;}\n";
	
137
	body +="\treturn true;\n";
Pierre Aubert's avatar
Pierre Aubert committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
	body +="}\n\n";
	return body;
}


///Save the implementation of the Operator parsing function for the given graphToken
/**	@param parserName : name of the generated parser class
 * 	@param op : operator to be used
 * 	@param graphToken : graph token related to the current operator
*/
std::string repr_cpp_tokenOperatorSourceOperatorUndo(const std::string & parserName, const POperator & op, const PGraphToken & graphToken){
	std::string body(""), name(graphToken.getName()), varName(graphToken.getVarName()), varType(graphToken.getVarType());
	std::string opValue(repr_cpp_operator_getEnumValue(op.getValueEnum()));
	std::string baseFctName("Operator" + repr_cpp_operator_getEnumValue(op.getValueEnum()));
	body += "///Undo the Parsing of the Operator " + opValue + " related to the GraphToken "+varName+"\n";
	body += "/**\t@param " + varName + " : " + varType + " to be parsed by the operator\n";
	body += "*/\n";
	body += "void " + parserName + "::" + name + baseFctName + "Undo(" + varType + " & " + varName + "){\n";
	body += "\tp_parser->popPosition();\n";
	body +="}\n\n";
	return body;
}


///Save the source of the PToken functions
/**	@param parserName : name of the generated parser class
 * 	@param tabGraphToken : contains all the graph and internal representation
 * 	@param graph : graph of the current token
 * 	@return C++ source
*/
std::string repr_cpp_tokenOperatorSource(const std::string & parserName, const PTabGraphToken & tabGraphToken, const PGraphToken & graph)
{
	std::string body("");
	PVecOperator vecOp;
	search_operatorByGraphUse(vecOp, tabGraphToken.getVecOperator(), graph.getName());
	if(vecOp.size() != 0lu){
		for(PVecOperator::iterator it(vecOp.begin()); it != vecOp.end(); ++it){
			body += repr_cpp_tokenOperatorSourceOperatorDo(parserName, *it, graph);
			body += repr_cpp_tokenOperatorSourceOperatorUndo(parserName, *it, graph);
		}
	}
	return body;
}