Skip to content

Commit

Permalink
Refactor arguments code generation using GenerateArgumentsList (4ian#335
Browse files Browse the repository at this point in the history
)
  • Loading branch information
4ian authored Jan 4, 2017
1 parent dea86d7 commit 883129f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 116 deletions.
62 changes: 18 additions & 44 deletions Core/GDCore/Events/CodeGeneration/EventsCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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);

Expand All @@ -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";
}
Expand All @@ -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";
Expand Down Expand Up @@ -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+")"
Expand All @@ -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),
Expand Down
7 changes: 7 additions & 0 deletions Core/GDCore/Events/CodeGeneration/EventsCodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
36 changes: 4 additions & 32 deletions GDCpp/GDCpp/Events/CodeGeneration/EventsCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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.
Expand Down
45 changes: 5 additions & 40 deletions GDJS/GDJS/Events/CodeGeneration/EventsCodeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 883129f

Please sign in to comment.