CCPUScriptCompiler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. #include "CCPUScriptCompiler.h"
  23. #include "extensions/Particle3D/PU/CCPUTranslateManager.h"
  24. #include "platform/CCFileUtils.h"
  25. NS_CC_BEGIN
  26. // ObjectAbstractNode
  27. PUObjectAbstractNode::PUObjectAbstractNode(PUAbstractNode *ptr)
  28. :PUAbstractNode(ptr), id(0), abstract(false)
  29. {
  30. type = ANT_OBJECT;
  31. }
  32. PUAbstractNode *PUObjectAbstractNode::clone() const
  33. {
  34. PUObjectAbstractNode *node = new (std::nothrow) PUObjectAbstractNode(parent);
  35. node->file = file;
  36. node->line = line;
  37. node->type = type;
  38. node->name = name;
  39. node->cls = cls;
  40. node->id = id;
  41. node->abstract = abstract;
  42. node->context = context;
  43. for(PUAbstractNodeList::const_iterator i = children.begin(); i != children.end(); ++i)
  44. {
  45. PUAbstractNode* newNode = (PUAbstractNode*)((*i)->clone());
  46. newNode->parent = (PUAbstractNode*)node;
  47. node->children.push_back(newNode);
  48. }
  49. for(PUAbstractNodeList::const_iterator i = values.begin(); i != values.end(); ++i)
  50. {
  51. PUAbstractNode* newNode = (PUAbstractNode*)((*i)->clone());
  52. newNode->parent = (PUAbstractNode*)node;
  53. node->values.push_back(newNode);
  54. }
  55. node->_env = _env;
  56. return (PUAbstractNode*)node;
  57. }
  58. std::string PUObjectAbstractNode::getValue() const
  59. {
  60. return cls;
  61. }
  62. void PUObjectAbstractNode::addVariable(const std::string &inName)
  63. {
  64. _env.emplace(inName, "");
  65. }
  66. void PUObjectAbstractNode::setVariable(const std::string &inName, const std::string &value)
  67. {
  68. _env[inName] = value;
  69. }
  70. std::pair<bool,std::string> PUObjectAbstractNode::getVariable(const std::string &inName) const
  71. {
  72. std::unordered_map<std::string,std::string>::const_iterator i = _env.find(inName);
  73. if(i != _env.end())
  74. return std::make_pair(true, i->second);
  75. PUObjectAbstractNode *parentNode = (PUObjectAbstractNode*)this->parent;
  76. while(parentNode)
  77. {
  78. i = parentNode->_env.find(inName);
  79. if(i != parentNode->_env.end())
  80. return std::make_pair(true, i->second);
  81. parentNode = (PUObjectAbstractNode*)parentNode->parent;
  82. }
  83. return std::make_pair(false, "");
  84. }
  85. const std::unordered_map<std::string,std::string> &PUObjectAbstractNode::getVariables() const
  86. {
  87. return _env;
  88. }
  89. PUObjectAbstractNode::~PUObjectAbstractNode()
  90. {
  91. for (auto iter : children){
  92. delete iter;
  93. }
  94. for (auto iter : values){
  95. delete iter;
  96. }
  97. for (auto iter : overrides){
  98. delete iter;
  99. }
  100. }
  101. // PropertyAbstractNode//
  102. PUPropertyAbstractNode::PUPropertyAbstractNode(PUAbstractNode *ptr)
  103. :PUAbstractNode(ptr), id(0)
  104. {
  105. type = ANT_PROPERTY;
  106. }
  107. PUAbstractNode *PUPropertyAbstractNode::clone() const
  108. {
  109. PUPropertyAbstractNode *node = new (std::nothrow) PUPropertyAbstractNode(parent);
  110. node->file = file;
  111. node->line = line;
  112. node->type = type;
  113. node->name = name;
  114. node->id = id;
  115. for(PUAbstractNodeList::const_iterator i = values.begin(); i != values.end(); ++i)
  116. {
  117. PUAbstractNode* newNode = (PUAbstractNode*)((*i)->clone());
  118. newNode->parent = (PUAbstractNode*)node;
  119. node->values.push_back(newNode);
  120. }
  121. return (PUAbstractNode*)node;
  122. }
  123. std::string PUPropertyAbstractNode::getValue() const
  124. {
  125. return name;
  126. }
  127. PUPropertyAbstractNode::~PUPropertyAbstractNode()
  128. {
  129. for (auto iter : values){
  130. delete iter;
  131. }
  132. }
  133. PUAtomAbstractNode::PUAtomAbstractNode(PUAbstractNode *ptr)
  134. :PUAbstractNode(ptr), id(0)
  135. {
  136. type = ANT_ATOM;
  137. }
  138. PUAbstractNode *PUAtomAbstractNode::clone() const
  139. {
  140. PUAtomAbstractNode *node = new (std::nothrow) PUAtomAbstractNode(parent);
  141. node->file = file;
  142. node->line = line;
  143. node->id = id;
  144. node->type = type;
  145. node->value = value;
  146. return node;
  147. }
  148. std::string PUAtomAbstractNode::getValue() const
  149. {
  150. return value;
  151. }
  152. PUScriptCompiler::PUScriptCompiler():_current(nullptr),_nodes(nullptr), _PUParticleSystem3D(nullptr)
  153. {
  154. }
  155. PUScriptCompiler::~PUScriptCompiler()
  156. {
  157. for (auto iter : _compiledScripts){
  158. for (auto miter : iter.second){
  159. delete miter;
  160. }
  161. }
  162. _compiledScripts.clear();
  163. }
  164. bool PUScriptCompiler::compile(const PUConcreteNodeList &nodes, const std::string &file)
  165. {
  166. if (nodes.empty()) return false;
  167. PUAbstractNodeList aNodes;
  168. convertToAST(nodes,aNodes);
  169. _compiledScripts[file] = aNodes;
  170. //for(PUAbstractNodeList::iterator i = aNodes.begin(); i != aNodes.end(); ++i)
  171. //{
  172. // PUScriptTranslator *translator = PUTranslateManager::Instance()->getTranslator(*i);
  173. // if(translator){
  174. // if (translator->isParticleSystemTranslator())
  175. // {
  176. // PUParticleSystem3DTranslator *ps = static_cast<PUParticleSystem3DTranslator *>(translator);
  177. // if (ps) ps->setParticleSystem3D(_PUParticleSystem3D);
  178. // }
  179. // translator->translate(this, *i);
  180. // }
  181. //}
  182. //for (auto iter : aNodes){
  183. // delete iter;
  184. //}
  185. return true;
  186. }
  187. const PUAbstractNodeList* PUScriptCompiler::compile(const std::string &file, bool &isFirstCompile)
  188. {
  189. auto iter = _compiledScripts.find(file);
  190. if (iter != _compiledScripts.end()){
  191. isFirstCompile = false;
  192. return &iter->second;
  193. }
  194. std::string data = FileUtils::getInstance()->getStringFromFile(file);
  195. PUScriptLexer lexer;
  196. PUScriptParser parser;
  197. PUScriptTokenList tokenList;
  198. PUConcreteNodeList creteNodeList;
  199. lexer.openLexer(data, file, tokenList);
  200. parser.parse(creteNodeList, tokenList);
  201. bool state = compile(creteNodeList, file);
  202. for (auto iter1 : creteNodeList){
  203. delete iter1;
  204. }
  205. for (auto iter2 : tokenList){
  206. delete iter2;
  207. }
  208. isFirstCompile = true;
  209. if (state){
  210. return &_compiledScripts[file];
  211. }
  212. return nullptr;
  213. }
  214. void PUScriptCompiler::convertToAST(const PUConcreteNodeList &nodes,PUAbstractNodeList &aNodes)
  215. {
  216. _current = NULL;
  217. _nodes = &aNodes;
  218. visitList(nodes);
  219. }
  220. // CNT_VARIABLE,
  221. // CNT_VARIABLE_ASSIGN,
  222. // CNT_WORD,need handle
  223. // CNT_IMPORT,
  224. // CNT_QUOTE,
  225. // CNT_LBRACE,need handle
  226. // CNT_RBRACE,need handle
  227. // CNT_COLON
  228. void PUScriptCompiler::visitList(const PUConcreteNodeList &nodes)
  229. {
  230. for(const auto& node : nodes)
  231. {
  232. this->visit(node);
  233. }
  234. }
  235. void PUScriptCompiler::visit(PUConcreteNode *node)
  236. {
  237. PUAbstractNode* asn = NULL;
  238. // Handle properties and objects here
  239. if(!node->children.empty())
  240. {
  241. // Grab the last two nodes
  242. PUConcreteNode* temp1 = NULL;
  243. PUConcreteNode* temp2 = NULL;
  244. PUConcreteNodeList::reverse_iterator iter = node->children.rbegin();
  245. if(iter != node->children.rend())
  246. {
  247. temp1 = *iter;
  248. ++iter;
  249. }
  250. if(iter != node->children.rend())
  251. temp2 = *iter;
  252. //brance inner
  253. if(temp1 && temp1->type == CNT_RBRACE && temp2 && temp2->type == CNT_LBRACE)
  254. {
  255. if(node->children.size() < 2)
  256. {
  257. return;
  258. }
  259. PUObjectAbstractNode *impl = new (std::nothrow) PUObjectAbstractNode(_current);
  260. impl->line = node->line;
  261. impl->file = node->file;
  262. impl->abstract = false;
  263. std::list<PUConcreteNode*> temp;
  264. temp.push_back(node);
  265. for(const auto& child : node->children)
  266. {
  267. temp.push_back(child);
  268. }
  269. //add brance type//
  270. PUConcreteNodeList::const_iterator iter1 = temp.begin();
  271. impl->cls = (*iter1)->token;
  272. ++iter1;
  273. //add brance name//
  274. if(iter1 != temp.end() && ((*iter1)->type == CNT_WORD))
  275. {
  276. impl->name = (*iter1)->token;
  277. ++iter1;
  278. }
  279. while(iter1 != temp.end() && (*iter1)->type != CNT_LBRACE)
  280. {
  281. PUAtomAbstractNode *atom = new (std::nothrow) PUAtomAbstractNode(impl);
  282. atom->file = (*iter1)->file;
  283. atom->line = (*iter1)->line;
  284. atom->type = ANT_ATOM;
  285. atom->value = (*iter1)->token;
  286. impl->values.push_back(atom);
  287. ++iter1;
  288. }
  289. asn = impl;
  290. _current = impl;
  291. visitList(temp2->children);
  292. _current = impl->parent;
  293. }
  294. //no brance//
  295. else
  296. {
  297. PUPropertyAbstractNode *impl = new (std::nothrow) PUPropertyAbstractNode(_current);
  298. impl->line = node->line;
  299. impl->file = node->file;
  300. impl->name = node->token;
  301. asn = impl;
  302. _current = impl;
  303. // Visit the children of the {
  304. visitList(node->children);
  305. // Go back up the stack
  306. _current = impl->parent;
  307. }
  308. }
  309. else
  310. {
  311. PUAtomAbstractNode *impl = new (std::nothrow) PUAtomAbstractNode(_current);
  312. impl->line = node->line;
  313. impl->file = node->file;
  314. impl->value = node->token;
  315. asn = impl;
  316. }
  317. if(asn)
  318. {
  319. if(_current)
  320. {
  321. if(_current->type == ANT_PROPERTY)
  322. {
  323. PUPropertyAbstractNode *impl = reinterpret_cast<PUPropertyAbstractNode*>(_current);
  324. //PUAtomAbstractNode* assd = dynamic_cast<PUAtomAbstractNode*>(asn);
  325. impl->values.push_back(asn);
  326. }
  327. else
  328. {
  329. PUObjectAbstractNode *impl = reinterpret_cast<PUObjectAbstractNode*>(_current);
  330. impl->children.push_back(asn);
  331. }
  332. }
  333. else
  334. {
  335. _nodes->push_back(asn);
  336. }
  337. }
  338. }
  339. void PUScriptCompiler::setParticleSystem3D( PUParticleSystem3D *pu )
  340. {
  341. _PUParticleSystem3D = pu;
  342. }
  343. PUScriptCompiler* PUScriptCompiler::Instance()
  344. {
  345. static PUScriptCompiler psc;
  346. return &psc;
  347. }
  348. NS_CC_END