コード例 #1
0
ファイル: Extension.cpp プロジェクト: cubemoon/GD
                virtual std::string Generate(gd::BaseEvent & event_, gd::EventsCodeGenerator & codeGenerator,
                                             gd::EventsCodeGenerationContext & /* The function has nothing to do with the current context */)
                {
                    FunctionEvent & event = dynamic_cast<FunctionEvent&>(event_);

                    //Declaring the pointer to the function parameters
                    codeGenerator.AddGlobalDeclaration(event.globalDeclaration);

                    //Declaring function prototype.
                    codeGenerator.AddGlobalDeclaration("void "+FunctionEvent::MangleFunctionName(event)+"(RuntimeContext *, std::map <std::string, std::vector<RuntimeObject*> *>);\n");

                    //Generating function code:
                    std::string functionCode;
                    functionCode += "\nvoid "+FunctionEvent::MangleFunctionName(event)+"(RuntimeContext * runtimeContext, std::map <std::string, std::vector<RuntimeObject*> *> objectsListsMap)\n{\n";

                    gd::EventsCodeGenerationContext callerContext;
                    {
                        std::vector<std::string> realObjects = codeGenerator.ExpandObjectsName(event.GetObjectsPassedAsArgument(), callerContext);
                        for (unsigned int i = 0;i<realObjects.size();++i)
                        {
                            callerContext.EmptyObjectsListNeeded(realObjects[i]);
                            functionCode += "std::vector<RuntimeObject*> "+ManObjListName(realObjects[i]) + ";\n";
                            functionCode += "if ( objectsListsMap[\""+realObjects[i]+"\"] != NULL ) "+ManObjListName(realObjects[i])+" = *objectsListsMap[\""+realObjects[i]+"\"];\n";
                        }
                    }
                    functionCode += "{";

                    gd::EventsCodeGenerationContext context;
                    context.InheritsFrom(callerContext);

                    //Generating function body code
                    std::string conditionsCode = codeGenerator.GenerateConditionsListCode(event.GetConditions(), context);
                    std::string actionsCode = codeGenerator.GenerateActionsListCode(event.GetActions(), context);
                    std::string subeventsCode = codeGenerator.GenerateEventsListCode(event.GetSubEvents(), context);

                    functionCode += codeGenerator.GenerateObjectsDeclarationCode(context);
                    std::string ifPredicat = "true";
                    for (unsigned int i = 0;i<event.GetConditions().size();++i)
                        ifPredicat += " && condition"+ToString(i)+"IsTrue";

                    functionCode += conditionsCode;
                    functionCode += "if (" +ifPredicat+ ")\n";
                    functionCode += "{\n";
                    functionCode += actionsCode;
                    if ( event.HasSubEvents() ) //Sub events
                    {
                        functionCode += "\n{\n";
                        functionCode += subeventsCode;
                        functionCode += "}\n";
                    }
                    functionCode += "}\n";

                    functionCode += "}\n"; //Context end
                    functionCode += "}\n"; //Function end
                    codeGenerator.AddCustomCodeOutsideMain(functionCode);

                    return "";
                }
コード例 #2
0
ファイル: BaseObjectExtension.cpp プロジェクト: manolaz/GDJS
            virtual std::string GenerateCode(gd::Instruction & instruction, gd::EventsCodeGenerator & codeGenerator, gd::EventsCodeGenerationContext & context)
            {
                std::string outputCode;

                std::vector<std::string> realObjects = codeGenerator.ExpandObjectsName(instruction.GetParameter(0).GetPlainString(), context);
                for (unsigned int i = 0;i<realObjects.size();++i)
                {
                    context.SetCurrentObject(realObjects[i]);
                    context.ObjectsListNeeded(realObjects[i]);

                    std::string newX, newY;

                    std::string expression1Code;
                    {
                        gd::CallbacksForGeneratingExpressionCode callbacks(expression1Code, codeGenerator, context);
                        gd::ExpressionParser parser(instruction.GetParameters()[2].GetPlainString());
                        if (!parser.ParseMathExpression(codeGenerator.GetPlatform(), codeGenerator.GetProject(), codeGenerator.GetLayout(), callbacks) || expression1Code.empty())
                            expression1Code = "0";
                    }

                    std::string expression2Code;
                    {
                        gd::CallbacksForGeneratingExpressionCode callbacks(expression2Code, codeGenerator, context);
                        gd::ExpressionParser parser(instruction.GetParameters()[4].GetPlainString());
                        if (!parser.ParseMathExpression(codeGenerator.GetPlatform(), codeGenerator.GetProject(), codeGenerator.GetLayout(), callbacks) || expression2Code.empty())
                            expression2Code = "0";
                    }

                    std::string op1 = instruction.GetParameter(1).GetPlainString();
                    if ( op1 == "=" || op1.empty() )
                        newX = expression1Code;
                    else if ( op1 == "/" || op1 == "*" || op1 == "-" || op1 == "+" )
                        newX = codeGenerator.GetObjectListName(realObjects[i], context)+"[i].getX() "+op1 + expression1Code;
                    else
                        return "";
                    std::string op2 = instruction.GetParameter(3).GetPlainString();
                    if ( op2 == "=" || op2.empty() )
                        newY = expression2Code;
                    else if ( op2 == "/" || op2 == "*" || op2 == "-" || op2 == "+" )
                        newY = codeGenerator.GetObjectListName(realObjects[i], context)+"[i].getY() "+op2 + expression2Code;
                    else
                        return "";

                    std::string call = codeGenerator.GetObjectListName(realObjects[i], context)+"[i].setPosition("+newX+","+newY+")";

                    outputCode += "for(var i = 0, len = "+codeGenerator.GetObjectListName(realObjects[i], context)+".length ;i < len;++i) {\n";
                    outputCode += "    "+call+";\n";
                    outputCode += "}\n";

                    context.SetNoCurrentObject();
                }

                return outputCode;
            };
コード例 #3
0
ファイル: Extension.cpp プロジェクト: cubemoon/GD
                virtual std::string GenerateCode(gd::Instruction & instruction, gd::EventsCodeGenerator & codeGenerator, gd::EventsCodeGenerationContext & context)
                {
                    codeGenerator.AddGlobalDeclaration(FunctionEvent::globalDeclaration);
                    std::string functionName = instruction.GetParameter(0).GetPlainString();
                    const gd::Project & project = codeGenerator.GetProject();
                    const gd::Layout & scene = codeGenerator.GetLayout();

                    const FunctionEvent * functionEvent = FunctionEvent::SearchForFunctionInEvents(scene.GetEvents(), functionName);
                    if ( !functionEvent )
                    {
                        std::cout << "Function \""+functionName+"\" not found!" << std::endl;
                        return "//Function \""+functionName+"\" not found.\n";
                    }

                    std::string code;

                    //Generate code for objects passed as arguments
                    std::string objectsAsArgumentCode;
                    {
                        objectsAsArgumentCode += "runtimeContext->ClearObjectListsMap()";
                        std::vector<std::string> realObjects = codeGenerator.ExpandObjectsName(functionEvent->GetObjectsPassedAsArgument(), context);
                        for (unsigned int i = 0;i<realObjects.size();++i)
                        {
                            context.EmptyObjectsListNeeded(realObjects[i]);
                            objectsAsArgumentCode += ".AddObjectListToMap(\""+codeGenerator.ConvertToString(realObjects[i])+"\", "+ManObjListName(realObjects[i])+")";
                        }
                        objectsAsArgumentCode += ".ReturnObjectListsMap()";
                    }

                    //Generate code for evaluating parameters
                    code += "std::vector<std::string> functionParameters;\n";
                    for (unsigned int i = 1;i<8;++i)
                    {
                        std::string parameterCode;
                        gd::CallbacksForGeneratingExpressionCode callbacks(parameterCode, codeGenerator, context);
                        gd::ExpressionParser parser(instruction.GetParameter(i).GetPlainString());
                        parser.ParseStringExpression(CppPlatform::Get(), project, scene, callbacks);
                        if (parameterCode.empty()) parameterCode = "\"\"";

                        code += "functionParameters.push_back("+parameterCode+");\n";
                    }
                    code += "std::vector<std::string> * oldFunctionParameters = currentFunctionParameters;\n";
                    code += "currentFunctionParameters = &functionParameters;\n";

                    code += FunctionEvent::MangleFunctionName(*functionEvent)+"(runtimeContext, "+objectsAsArgumentCode+");\n";
                    code += "currentFunctionParameters = oldFunctionParameters;\n";

                    return code;
                };