GDevelop Core
Core library for developing platforms and tools compatible with GDevelop.
ExpressionCodeGenerator.h
1 /*
2  * GDevelop Core
3  * Copyright 2008-present Florian Rival ([email protected]). All rights
4  * reserved. This project is released under the MIT License.
5  */
6 #if defined(GD_IDE_ONLY)
7 #ifndef GDCORE_ExpressionCodeGenerator_H
8 #define GDCORE_ExpressionCodeGenerator_H
9 
10 #include <memory>
11 #include <vector>
12 #include "GDCore/Events/Parsers/ExpressionParser2Node.h"
13 #include "GDCore/Events/Parsers/ExpressionParser2NodeWorker.h"
14 #include "GDCore/String.h"
15 namespace gd {
16 class Expression;
17 class ObjectsContainer;
18 class Platform;
19 class ParameterMetadata;
20 class ExpressionMetadata;
21 class EventsCodeGenerationContext;
22 class EventsCodeGenerator;
23 } // namespace gd
24 
25 namespace gd {
26 
36  public:
37  ExpressionCodeGenerator(const gd::String &rootType_,
38  const gd::String &rootObjectName_,
39  EventsCodeGenerator& codeGenerator_,
41  : rootType(rootType_), rootObjectName(rootObjectName_), codeGenerator(codeGenerator_), context(context_){};
42  virtual ~ExpressionCodeGenerator(){};
43 
58  static gd::String GenerateExpressionCode(EventsCodeGenerator& codeGenerator,
60  const gd::String& type,
61  const gd::Expression& expression,
62  const gd::String& objectName = "",
63  const gd::String& extraInfo = "");
64 
65  const gd::String& GetOutput() { return output; };
66 
67  protected:
68  void OnVisitSubExpressionNode(SubExpressionNode& node) override;
69  void OnVisitOperatorNode(OperatorNode& node) override;
70  void OnVisitUnaryOperatorNode(UnaryOperatorNode& node) override;
71  void OnVisitNumberNode(NumberNode& node) override;
72  void OnVisitTextNode(TextNode& node) override;
73  void OnVisitVariableNode(VariableNode& node) override;
74  void OnVisitVariableAccessorNode(VariableAccessorNode& node) override;
75  void OnVisitVariableBracketAccessorNode(
76  VariableBracketAccessorNode& node) override;
77  void OnVisitIdentifierNode(IdentifierNode& node) override;
78  void OnVisitObjectFunctionNameNode(ObjectFunctionNameNode& node) override;
79  void OnVisitFunctionCallNode(FunctionCallNode& node) override;
80  void OnVisitEmptyNode(EmptyNode& node) override;
81 
82  private:
83  gd::String GenerateFreeFunctionCode(
84  const std::vector<std::unique_ptr<ExpressionNode>>& parameters,
85  const ExpressionMetadata& expressionMetadata);
86  gd::String GenerateObjectFunctionCode(
87  const gd::String& type,
88  const gd::String& objectName,
89  const std::vector<std::unique_ptr<ExpressionNode>>& parameters,
90  const ExpressionMetadata& expressionMetadata);
91  gd::String GenerateBehaviorFunctionCode(
92  const gd::String& type,
93  const gd::String& objectName,
94  const gd::String& behaviorName,
95  const std::vector<std::unique_ptr<ExpressionNode>>& parameters,
96  const ExpressionMetadata& expressionMetadata);
97  gd::String GenerateParametersCodes(
98  const std::vector<std::unique_ptr<ExpressionNode>>& parameters,
99  const ExpressionMetadata& expressionMetadata,
100  size_t initialParameterIndex);
101  gd::String GenerateDefaultValue(const gd::String& type);
102  static std::vector<gd::Expression> PrintParameters(
103  const std::vector<std::unique_ptr<ExpressionNode>>& parameters);
104 
105  gd::String output;
106  gd::String objectNameToUseForVariableAccessor;
107  EventsCodeGenerator& codeGenerator;
109  const gd::String rootType;
110  const gd::String rootObjectName;
111 };
112 
113 } // namespace gd
114 
115 #endif // GDCORE_ExpressionCodeGenerator_H
116 #endif
Used to manage the context when generating code for events.
Definition: EventsCodeGenerationContext.h:27
Internal class used to generate code from events.
Definition: EventsCodeGenerator.h:41
Generate code for a parsed expression.
Definition: ExpressionCodeGenerator.h:35
Class representing an expression used as a parameter of a gd::Instruction. This class is nothing more...
Definition: Expression.h:30
Describe user-friendly information about an expression, its parameters and the function name as well ...
Definition: ExpressionMetadata.h:47
The interface for any worker class ("visitor" pattern) that want to interact with the nodes of a pars...
Definition: ExpressionParser2NodeWorker.h:36
String represents an UTF8 encoded string.
Definition: String.h:33
Definition: CommonTools.h:24
An empty node, used when parsing failed/a syntax error was encountered and any other node could not m...
Definition: ExpressionParser2Node.h:423
A function call node (either free function, object function or object behavior function)....
Definition: ExpressionParser2Node.h:371
An identifier node, usually representing an object or a variable with an optional function name or ch...
Definition: ExpressionParser2Node.h:204
A number node. For example: "123". Its type is always "number".
Definition: ExpressionParser2Node.h:161
The name of a function to call on an object or the behavior For example: "MyObject....
Definition: ExpressionParser2Node.h:321
An operator node. For example: "lhs + rhs".
Definition: ExpressionParser2Node.h:129
Definition: ExpressionParser2Node.h:115
A text node. For example: "Hello World". Its type is always "string".
Definition: ExpressionParser2Node.h:177
A unary operator node. For example: "-2".
Definition: ExpressionParser2Node.h:145
A direct accessor to a child variable. Example: MyChild in MyVariable.MyChild.
Definition: ExpressionParser2Node.h:282
A bracket accessor to a child variable. Example: ["MyChild"] (in MyVariable["MyChild"]).
Definition: ExpressionParser2Node.h:300
A variable, or object variable, with bracket accessor or at least 2 "dot" accessors.
Definition: ExpressionParser2Node.h:261