CCPUScriptCompiler.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __CC_PU_SCRIPT_COMPILER_H__
  23. #define __CC_PU_SCRIPT_COMPILER_H__
  24. #include "base/CCRef.h"
  25. #include "extensions/Particle3D/PU/CCPUScriptParser.h"
  26. #include <list>
  27. #include <unordered_map>
  28. NS_CC_BEGIN
  29. /** This enum holds the types of the possible abstract nodes */
  30. enum PUAbstractNodeType
  31. {
  32. ANT_UNKNOWN,
  33. ANT_ATOM,
  34. ANT_OBJECT,
  35. ANT_PROPERTY,
  36. ANT_IMPORT,
  37. ANT_VARIABLE_SET,
  38. ANT_VARIABLE_ACCESS
  39. };
  40. class CC_DLL PUAbstractNode;
  41. typedef std::list<PUAbstractNode*> PUAbstractNodeList;
  42. class CC_DLL PUAbstractNode
  43. {
  44. public:
  45. std::string file;
  46. unsigned int line;
  47. PUAbstractNodeType type;
  48. PUAbstractNode *parent;
  49. Ref *context;
  50. //contextd//
  51. // Any context; // A holder for translation context data
  52. public:
  53. PUAbstractNode(PUAbstractNode *ptr):line(0), type(ANT_UNKNOWN), parent(ptr), context(nullptr)
  54. {
  55. }
  56. virtual ~PUAbstractNode(){}
  57. /// Returns a new AbstractNode which is a replica of this one.
  58. virtual PUAbstractNode *clone() const = 0;
  59. /// Returns a string value depending on the type of the AbstractNode.
  60. virtual std::string getValue() const = 0;
  61. };
  62. /** This specific abstract node represents a script object */
  63. class CC_DLL PUObjectAbstractNode : public PUAbstractNode
  64. {
  65. private:
  66. std::unordered_map<std::string,std::string> _env;
  67. public:
  68. std::string name, cls;
  69. std::vector<std::string> bases;
  70. unsigned int id;
  71. bool abstract;
  72. PUAbstractNodeList children;
  73. PUAbstractNodeList values;
  74. PUAbstractNodeList overrides; // For use when processing object inheritance and overriding
  75. public:
  76. PUObjectAbstractNode(PUAbstractNode *ptr);
  77. virtual ~PUObjectAbstractNode();
  78. PUAbstractNode *clone() const;
  79. std::string getValue() const;
  80. void addVariable(const std::string &name);
  81. void setVariable(const std::string &name, const std::string &value);
  82. std::pair<bool,std::string> getVariable(const std::string &name) const;
  83. const std::unordered_map<std::string,std::string> &getVariables() const;
  84. };
  85. /** This abstract node represents a script property */
  86. class CC_DLL PUPropertyAbstractNode : public PUAbstractNode
  87. {
  88. public:
  89. std::string name;
  90. unsigned int id;
  91. PUAbstractNodeList values;
  92. public:
  93. PUPropertyAbstractNode(PUAbstractNode *ptr);
  94. virtual ~PUPropertyAbstractNode();
  95. PUAbstractNode *clone() const;
  96. std::string getValue() const;
  97. };
  98. /** This is an abstract node which cannot be broken down further */
  99. class CC_DLL PUAtomAbstractNode : public PUAbstractNode
  100. {
  101. public:
  102. std::string value;
  103. unsigned int id;
  104. public:
  105. PUAtomAbstractNode(PUAbstractNode *ptr);
  106. PUAbstractNode *clone() const;
  107. std::string getValue() const;
  108. private:
  109. void parseNumber() const;
  110. };
  111. class CC_DLL PUParticleSystem3D;
  112. class CC_DLL PUScriptCompiler
  113. {
  114. private:
  115. bool compile(const PUConcreteNodeList &nodes, const std::string &file);
  116. //is it excluded?//
  117. bool isNameExcluded(const std::string &cls, PUAbstractNode *parent);
  118. public:
  119. typedef std::unordered_map<std::string,unsigned int> IdMap;
  120. static PUScriptCompiler* Instance();
  121. void setParticleSystem3D(PUParticleSystem3D *pu);
  122. const PUAbstractNodeList* compile(const std::string &file, bool &isFirstCompile);
  123. void convertToAST(const PUConcreteNodeList &nodes,PUAbstractNodeList &aNodes);
  124. std::unordered_map<std::string,std::string> env;
  125. private:
  126. PUScriptCompiler();
  127. virtual ~PUScriptCompiler();
  128. void visitList(const PUConcreteNodeList &nodes);
  129. void visit(PUConcreteNode *node);
  130. private:
  131. std::unordered_map<std::string, PUAbstractNodeList> _compiledScripts;
  132. PUAbstractNode *_current;
  133. PUAbstractNodeList *_nodes;
  134. PUParticleSystem3D *_PUParticleSystem3D;
  135. };
  136. NS_CC_END
  137. #endif /* defined(__ssaafsdf__CCScriptCompile__) */