Permalink
Browse files

Refactor arguments code generation using GenerateArgumentsList (#335)

  • Loading branch information...
1 parent dea86d7 commit 883129f379312ef06934cbb2c91fca8f223fd22c @4ian committed on GitHub Jan 4, 2017
@@ -854,14 +854,7 @@ gd::String EventsCodeGenerator::GenerateFreeCondition(const std::vector<gd::Stri
}
else
{
- gd::String argumentsStr;
- for (std::size_t i = 0;i<arguments.size();++i)
- {
- if ( i != 0 ) argumentsStr += ", ";
- argumentsStr += arguments[i];
- }
-
- predicat = instrInfos.codeExtraInformation.functionCallName+"("+argumentsStr+")";
+ predicat = instrInfos.codeExtraInformation.functionCallName+"("+GenerateArgumentsList(arguments, 0)+")";
}
//Add logical not if needed
@@ -900,14 +893,7 @@ gd::String EventsCodeGenerator::GenerateObjectCondition(const gd::String & objec
}
else
{
- gd::String argumentsStr;
- for (std::size_t i = 1;i<arguments.size();++i)
- {
- if ( i != 1 ) argumentsStr += ", ";
- argumentsStr += arguments[i];
- }
-
- predicat = objectFunctionCallNamePart+"("+argumentsStr+")";
+ predicat = objectFunctionCallNamePart+"("+GenerateArgumentsList(arguments, 1)+")";
}
if ( conditionInverted ) predicat = GenerateNegatedPredicat(predicat);
@@ -931,14 +917,7 @@ gd::String EventsCodeGenerator::GenerateBehaviorCondition(const gd::String & obj
}
else
{
- gd::String argumentsStr;
- for (std::size_t i = 2;i<arguments.size();++i)
- {
- if ( i != 2 ) argumentsStr += ", ";
- argumentsStr += arguments[i];
- }
-
- predicat = "("+argumentsStr+")";
+ predicat = "("+GenerateArgumentsList(arguments, 2)+")";
}
if ( conditionInverted ) predicat = GenerateNegatedPredicat(predicat);
@@ -961,14 +940,7 @@ gd::String EventsCodeGenerator::GenerateFreeAction(const std::vector<gd::String>
}
else
{
- gd::String argumentsStr;
- for (std::size_t i = 0;i<arguments.size();++i)
- {
- if ( i != 0 ) argumentsStr += ", ";
- argumentsStr += arguments[i];
- }
-
- call = instrInfos.codeExtraInformation.functionCallName+"("+argumentsStr+")";
+ call = instrInfos.codeExtraInformation.functionCallName+"("+GenerateArgumentsList(arguments)+")";
}
return call+";\n";
}
@@ -992,12 +964,7 @@ gd::String EventsCodeGenerator::GenerateObjectAction(const gd::String & objectNa
}
else
{
- gd::String argumentsStr;
- for (std::size_t i = 2;i<arguments.size();++i)
- {
- if ( i != 2 ) argumentsStr += ", ";
- argumentsStr += arguments[i];
- }
+ gd::String argumentsStr = GenerateArgumentsList(arguments, 1);
call = instrInfos.codeExtraInformation.functionCallName+"("+argumentsStr+")";
return "For each picked object \""+objectName+"\", call "+call+"("+argumentsStr+").\n";
@@ -1025,12 +992,7 @@ gd::String EventsCodeGenerator::GenerateBehaviorAction(const gd::String & object
}
else
{
- gd::String argumentsStr;
- for (std::size_t i = 2;i<arguments.size();++i)
- {
- if ( i != 2 ) argumentsStr += ", ";
- argumentsStr += arguments[i];
- }
+ gd::String argumentsStr = GenerateArgumentsList(arguments, 2);
call = instrInfos.codeExtraInformation.functionCallName+"("+argumentsStr+")";
return "For each picked object \""+objectName+"\", call "+call+"("+argumentsStr+")"
@@ -1044,6 +1006,18 @@ gd::String EventsCodeGenerator::GetObjectListName(const gd::String & name, const
return ManObjListName(name);
}
+gd::String EventsCodeGenerator::GenerateArgumentsList(const std::vector<gd::String> & arguments, size_t startFrom)
+{
+ gd::String argumentsStr;
+ for (std::size_t i = startFrom;i<arguments.size();++i)
+ {
+ if (!argumentsStr.empty()) argumentsStr += ", ";
+ argumentsStr += arguments[i];
+ }
+
+ return argumentsStr;
+}
+
EventsCodeGenerator::EventsCodeGenerator(gd::Project & project_, const gd::Layout & layout, const gd::Platform & platform_) :
project(project_),
scene(layout),
@@ -455,6 +455,13 @@ class GD_CORE_API EventsCodeGenerator
*/
gd::String GenerateFalse() const { return "false"; };
+ /**
+ * \brief Generate the list of comma-separated arguments to be used to call a function.
+ * \param arguments The code already generated for the arguments
+ * \param startFrom Index of the first argument, the previous will be ignored.
+ */
+ virtual gd::String GenerateArgumentsList(const std::vector<gd::String> & arguments, size_t startFrom = 0);
+
gd::Project & project; ///< The project being used.
const gd::Layout & scene; ///< The scene being generated.
const gd::Platform & platform; ///< The platform being used.
@@ -115,14 +115,7 @@ gd::String EventsCodeGenerator::GenerateObjectCondition(const gd::String & objec
}
else
{
- gd::String argumentsStr;
- for (std::size_t i = 1;i<arguments.size();++i)
- {
- if ( i != 1 ) argumentsStr += ", ";
- argumentsStr += arguments[i];
- }
-
- predicat = objectFunctionCallNamePart+"("+argumentsStr+")";
+ predicat = objectFunctionCallNamePart+"("+GenerateArgumentsList(arguments, 1)+")";
}
if ( conditionInverted ) predicat = GenerateNegatedPredicat(predicat);
@@ -169,14 +162,7 @@ gd::String EventsCodeGenerator::GenerateBehaviorCondition(const gd::String & obj
}
else
{
- gd::String argumentsStr;
- for (std::size_t i = 2;i<arguments.size();++i)
- {
- if ( i != 2 ) argumentsStr += ", ";
- argumentsStr += arguments[i];
- }
-
- predicat = objectFunctionCallNamePart+"("+argumentsStr+")";
+ predicat = objectFunctionCallNamePart+"("+GenerateArgumentsList(arguments, 2)+")";
}
if ( conditionInverted ) predicat = GenerateNegatedPredicat(predicat);
@@ -231,14 +217,7 @@ gd::String EventsCodeGenerator::GenerateObjectAction(const gd::String & objectNa
}
else
{
- gd::String argumentsStr;
- for (std::size_t i = 1;i<arguments.size();++i)
- {
- if ( i != 1 ) argumentsStr += ", ";
- argumentsStr += arguments[i];
- }
-
- call = objectPart+instrInfos.codeExtraInformation.functionCallName+"("+argumentsStr+")";
+ call = objectPart+instrInfos.codeExtraInformation.functionCallName+"("+GenerateArgumentsList(arguments, 1)+")";
}
actionCode += "for(std::size_t i = 0;i < "+ManObjListName(objectName)+".size();++i)\n";
@@ -279,14 +258,7 @@ gd::String EventsCodeGenerator::GenerateBehaviorAction(const gd::String & object
}
else
{
- gd::String argumentsStr;
- for (std::size_t i = 2;i<arguments.size();++i)
- {
- if ( i != 2 ) argumentsStr += ", ";
- argumentsStr += arguments[i];
- }
-
- call = objectPart+instrInfos.codeExtraInformation.functionCallName+"("+argumentsStr+")";
+ call = objectPart+instrInfos.codeExtraInformation.functionCallName+"("+GenerateArgumentsList(arguments, 2)+")";
}
//Verify that object has behavior.
@@ -164,14 +164,7 @@ gd::String EventsCodeGenerator::GenerateFreeCondition(const std::vector<gd::Stri
}
else
{
- gd::String argumentsStr;
- for (std::size_t i = 0;i<arguments.size();++i)
- {
- if ( i != 0 ) argumentsStr += ", ";
- argumentsStr += arguments[i];
- }
-
- predicat = instrInfos.codeExtraInformation.functionCallName+"("+argumentsStr+")";
+ predicat = instrInfos.codeExtraInformation.functionCallName+"("+GenerateArgumentsList(arguments)+")";
}
//Add logical not if needed
@@ -208,14 +201,7 @@ gd::String EventsCodeGenerator::GenerateObjectCondition(const gd::String & objec
}
else
{
- gd::String argumentsStr;
- for (std::size_t i = 1;i<arguments.size();++i)
- {
- if ( i != 1 ) argumentsStr += ", ";
- argumentsStr += arguments[i];
- }
-
- predicat = objectFunctionCallNamePart+"("+argumentsStr+")";
+ predicat = objectFunctionCallNamePart+"("+GenerateArgumentsList(arguments, 1)+")";
}
if ( conditionInverted ) predicat = GenerateNegatedPredicat(predicat);
@@ -255,14 +241,7 @@ gd::String EventsCodeGenerator::GenerateBehaviorCondition(const gd::String & obj
}
else
{
- gd::String argumentsStr;
- for (std::size_t i = 2;i<arguments.size();++i)
- {
- if ( i != 2 ) argumentsStr += ", ";
- argumentsStr += arguments[i];
- }
-
- predicat = objectFunctionCallNamePart+"("+argumentsStr+")";
+ predicat = objectFunctionCallNamePart+"("+GenerateArgumentsList(arguments, 2)+")";
}
if ( conditionInverted ) predicat = GenerateNegatedPredicat(predicat);
@@ -312,14 +291,7 @@ gd::String EventsCodeGenerator::GenerateObjectAction(const gd::String & objectNa
}
else
{
- gd::String argumentsStr;
- for (std::size_t i = 1;i<arguments.size();++i)
- {
- if ( i != 1 ) argumentsStr += ", ";
- argumentsStr += arguments[i];
- }
-
- call = objectPart+instrInfos.codeExtraInformation.functionCallName+"("+argumentsStr+")";
+ call = objectPart+instrInfos.codeExtraInformation.functionCallName+"("+GenerateArgumentsList(arguments, 1)+")";
}
actionCode += "for(var i = 0, len = "+GetObjectListName(objectName, context)+".length ;i < len;++i) {\n";
@@ -356,14 +328,7 @@ gd::String EventsCodeGenerator::GenerateBehaviorAction(const gd::String & object
}
else
{
- gd::String argumentsStr;
- for (std::size_t i = 2;i<arguments.size();++i)
- {
- if ( i != 2 ) argumentsStr += ", ";
- argumentsStr += arguments[i];
- }
-
- call = objectPart+instrInfos.codeExtraInformation.functionCallName+"("+argumentsStr+")";
+ call = objectPart+instrInfos.codeExtraInformation.functionCallName+"("+GenerateArgumentsList(arguments, 2)+")";
}
//Verify that object has behavior.

0 comments on commit 883129f

Please sign in to comment.