CCPUScriptCompiler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /****************************************************************************
  2. Copyright (C) 2013 Henry van Merode. All rights reserved.
  3. Copyright (c) 2015-2017 Chukong Technologies Inc.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #include "CCPUScriptCompiler.h"
  22. #include "extensions/Particle3D/PU/CCPUTranslateManager.h"
  23. #include "platform/CCFileUtils.h"
  24. NS_CC_BEGIN
  25. // ObjectAbstractNode
  26. PUObjectAbstractNode::PUObjectAbstractNode(PUAbstractNode *ptr)
  27. :PUAbstractNode(ptr), id(0), abstract(false)
  28. {
  29. type = ANT_OBJECT;
  30. }
  31. PUAbstractNode *PUObjectAbstractNode::clone() const
  32. {
  33. PUObjectAbstractNode *node = new (std::nothrow) PUObjectAbstractNode(parent);
  34. node->file = file;
  35. node->line = line;
  36. node->type = type;
  37. node->name = name;
  38. node->cls = cls;
  39. node->id = id;
  40. node->abstract = abstract;
  41. node->context = context;
  42. for(PUAbstractNodeList::const_iterator i = children.begin(); i != children.end(); ++i)
  43. {
  44. PUAbstractNode* newNode = (PUAbstractNode*)((*i)->clone());
  45. newNode->parent = (PUAbstractNode*)node;
  46. node->children.push_back(newNode);
  47. }
  48. for(PUAbstractNodeList::const_iterator i = values.begin(); i != values.end(); ++i)
  49. {
  50. PUAbstractNode* newNode = (PUAbstractNode*)((*i)->clone());
  51. newNode->parent = (PUAbstractNode*)node;
  52. node->values.push_back(newNode);
  53. }
  54. node->_env = _env;
  55. return (PUAbstractNode*)node;
  56. }
  57. std::string PUObjectAbstractNode::getValue() const
  58. {
  59. return cls;
  60. }
  61. void PUObjectAbstractNode::addVariable(const std::string &inName)
  62. {
  63. _env.emplace(inName, "");
  64. }
  65. void PUObjectAbstractNode::setVariable(const std::string &inName, const std::string &value)
  66. {
  67. _env[inName] = value;
  68. }
  69. std::pair<bool,std::string> PUObjectAbstractNode::getVariable(const std::string &inName) const
  70. {
  71. std::map<std::string,std::string>::const_iterator i = _env.find(inName);
  72. if(i != _env.end())
  73. return std::make_pair(true, i->second);
  74. PUObjectAbstractNode *parentNode = (PUObjectAbstractNode*)this->parent;
  75. while(parentNode)
  76. {
  77. i = parentNode->_env.find(inName);
  78. if(i != parentNode->_env.end())
  79. return std::make_pair(true, i->second);
  80. parentNode = (PUObjectAbstractNode*)parentNode->parent;
  81. }
  82. return std::make_pair(false, "");
  83. }
  84. const map<std::string,std::string> &PUObjectAbstractNode::getVariables() const
  85. {
  86. return _env;
  87. }
  88. PUObjectAbstractNode::~PUObjectAbstractNode()
  89. {
  90. for (auto iter : children){
  91. delete iter;
  92. }
  93. for (auto iter : values){
  94. delete iter;
  95. }
  96. for (auto iter : overrides){
  97. delete iter;
  98. }
  99. }
  100. // PropertyAbstractNode//
  101. PUPropertyAbstractNode::PUPropertyAbstractNode(PUAbstractNode *ptr)
  102. :PUAbstractNode(ptr), id(0)
  103. {
  104. type = ANT_PROPERTY;
  105. }
  106. PUAbstractNode *PUPropertyAbstractNode::clone() const
  107. {
  108. PUPropertyAbstractNode *node = new (std::nothrow) PUPropertyAbstractNode(parent);
  109. node->file = file;
  110. node->line = line;
  111. node->type = type;
  112. node->name = name;
  113. node->id = id;
  114. for(PUAbstractNodeList::const_iterator i = values.begin(); i != values.end(); ++i)
  115. {
  116. PUAbstractNode* newNode = (PUAbstractNode*)((*i)->clone());
  117. newNode->parent = (PUAbstractNode*)node;
  118. node->values.push_back(newNode);
  119. }
  120. return (PUAbstractNode*)node;
  121. }
  122. std::string PUPropertyAbstractNode::getValue() const
  123. {
  124. return name;
  125. }
  126. PUPropertyAbstractNode::~PUPropertyAbstractNode()
  127. {
  128. for (auto iter : values){
  129. delete iter;
  130. }
  131. }
  132. PUAtomAbstractNode::PUAtomAbstractNode(PUAbstractNode *ptr)
  133. :PUAbstractNode(ptr), id(0)
  134. {
  135. type = ANT_ATOM;
  136. }
  137. PUAbstractNode *PUAtomAbstractNode::clone() const
  138. {
  139. PUAtomAbstractNode *node = new (std::nothrow) PUAtomAbstractNode(parent);
  140. node->file = file;
  141. node->line = line;
  142. node->id = id;
  143. node->type = type;
  144. node->value = value;
  145. return node;
  146. }
  147. std::string PUAtomAbstractNode::getValue() const
  148. {
  149. return value;
  150. }
  151. PUScriptCompiler::PUScriptCompiler():_current(nullptr),_nodes(nullptr), _PUParticleSystem3D(nullptr)
  152. {
  153. }
  154. PUScriptCompiler::~PUScriptCompiler()
  155. {
  156. for (auto iter : _compiledScripts){
  157. for (auto miter : iter.second){
  158. delete miter;
  159. }
  160. }
  161. _compiledScripts.clear();
  162. }
  163. bool PUScriptCompiler::compile(const PUConcreteNodeList &nodes, const std::string &file)
  164. {
  165. if (nodes.empty()) return false;
  166. PUAbstractNodeList aNodes;
  167. convertToAST(nodes,aNodes);
  168. _compiledScripts[file] = aNodes;
  169. //for(PUAbstractNodeList::iterator i = aNodes.begin(); i != aNodes.end(); ++i)
  170. //{
  171. // PUScriptTranslator *translator = PUTranslateManager::Instance()->getTranslator(*i);
  172. // if(translator){
  173. // if (translator->isParticleSystemTranslator())
  174. // {
  175. // PUParticleSystem3DTranslator *ps = static_cast<PUParticleSystem3DTranslator *>(translator);
  176. // if (ps) ps->setParticleSystem3D(_PUParticleSystem3D);
  177. // }
  178. // translator->translate(this, *i);
  179. // }
  180. //}
  181. //for (auto iter : aNodes){
  182. // delete iter;
  183. //}
  184. return true;
  185. }
  186. const PUAbstractNodeList* PUScriptCompiler::compile(const std::string &file, bool &isFirstCompile)
  187. {
  188. auto iter = _compiledScripts.find(file);
  189. if (iter != _compiledScripts.end()){
  190. isFirstCompile = false;
  191. return &iter->second;
  192. }
  193. std::string data = FileUtils::getInstance()->getStringFromFile(file);
  194. PUScriptLexer lexer;
  195. PUScriptParser parser;
  196. PUScriptTokenList tokenList;
  197. PUConcreteNodeList creteNodeList;
  198. lexer.openLexer(data, file, tokenList);
  199. parser.parse(creteNodeList, tokenList);
  200. bool state = compile(creteNodeList, file);
  201. for (auto iter1 : creteNodeList){
  202. delete iter1;
  203. }
  204. for (auto iter2 : tokenList){
  205. delete iter2;
  206. }
  207. isFirstCompile = true;
  208. if (state){
  209. return &_compiledScripts[file];
  210. }
  211. return nullptr;
  212. }
  213. void PUScriptCompiler::convertToAST(const PUConcreteNodeList &nodes,PUAbstractNodeList &aNodes)
  214. {
  215. _current = NULL;
  216. _nodes = &aNodes;
  217. visitList(nodes);
  218. }
  219. // CNT_VARIABLE,
  220. // CNT_VARIABLE_ASSIGN,
  221. // CNT_WORD,need handle
  222. // CNT_IMPORT,
  223. // CNT_QUOTE,
  224. // CNT_LBRACE,need handle
  225. // CNT_RBRACE,need handle
  226. // CNT_COLON
  227. void PUScriptCompiler::visitList(const PUConcreteNodeList &nodes)
  228. {
  229. for(const auto& node : nodes)
  230. {
  231. this->visit(node);
  232. }
  233. }
  234. void PUScriptCompiler::visit(PUConcreteNode *node)
  235. {
  236. PUAbstractNode* asn = NULL;
  237. // Handle properties and objects here
  238. if(!node->children.empty())
  239. {
  240. // Grab the last two nodes
  241. PUConcreteNode* temp1 = NULL;
  242. PUConcreteNode* temp2 = NULL;
  243. PUConcreteNodeList::reverse_iterator iter = node->children.rbegin();
  244. if(iter != node->children.rend())
  245. {
  246. temp1 = *iter;
  247. ++iter;
  248. }
  249. if(iter != node->children.rend())
  250. temp2 = *iter;
  251. //brance inner
  252. if(temp1 && temp1->type == CNT_RBRACE && temp2 && temp2->type == CNT_LBRACE)
  253. {
  254. if(node->children.size() < 2)
  255. {
  256. return;
  257. }
  258. PUObjectAbstractNode *impl = new (std::nothrow) PUObjectAbstractNode(_current);
  259. impl->line = node->line;
  260. impl->file = node->file;
  261. impl->abstract = false;
  262. list<PUConcreteNode*> temp;
  263. temp.push_back(node);
  264. for(const auto& child : node->children)
  265. {
  266. temp.push_back(child);
  267. }
  268. //add brance type//
  269. PUConcreteNodeList::const_iterator iter1 = temp.begin();
  270. impl->cls = (*iter1)->token;
  271. ++iter1;
  272. //add brance name//
  273. if(iter1 != temp.end() && ((*iter1)->type == CNT_WORD))
  274. {
  275. impl->name = (*iter1)->token;
  276. ++iter1;
  277. }
  278. while(iter1 != temp.end() && (*iter1)->type != CNT_LBRACE)
  279. {
  280. PUAtomAbstractNode *atom = new (std::nothrow) PUAtomAbstractNode(impl);
  281. atom->file = (*iter1)->file;
  282. atom->line = (*iter1)->line;
  283. atom->type = ANT_ATOM;
  284. atom->value = (*iter1)->token;
  285. impl->values.push_back(atom);
  286. ++iter1;
  287. }
  288. asn = impl;
  289. _current = impl;
  290. visitList(temp2->children);
  291. _current = impl->parent;
  292. }
  293. //no brance//
  294. else
  295. {
  296. PUPropertyAbstractNode *impl = new (std::nothrow) PUPropertyAbstractNode(_current);
  297. impl->line = node->line;
  298. impl->file = node->file;
  299. impl->name = node->token;
  300. asn = impl;
  301. _current = impl;
  302. // Visit the children of the {
  303. visitList(node->children);
  304. // Go back up the stack
  305. _current = impl->parent;
  306. }
  307. }
  308. else
  309. {
  310. PUAtomAbstractNode *impl = new (std::nothrow) PUAtomAbstractNode(_current);
  311. impl->line = node->line;
  312. impl->file = node->file;
  313. impl->value = node->token;
  314. asn = impl;
  315. }
  316. if(asn)
  317. {
  318. if(_current)
  319. {
  320. if(_current->type == ANT_PROPERTY)
  321. {
  322. PUPropertyAbstractNode *impl = reinterpret_cast<PUPropertyAbstractNode*>(_current);
  323. //PUAtomAbstractNode* assd = dynamic_cast<PUAtomAbstractNode*>(asn);
  324. impl->values.push_back(asn);
  325. }
  326. else
  327. {
  328. PUObjectAbstractNode *impl = reinterpret_cast<PUObjectAbstractNode*>(_current);
  329. impl->children.push_back(asn);
  330. }
  331. }
  332. else
  333. {
  334. _nodes->push_back(asn);
  335. }
  336. }
  337. }
  338. void PUScriptCompiler::setParticleSystem3D( PUParticleSystem3D *pu )
  339. {
  340. _PUParticleSystem3D = pu;
  341. }
  342. PUScriptCompiler* PUScriptCompiler::Instance()
  343. {
  344. static PUScriptCompiler psc;
  345. return &psc;
  346. }
  347. NS_CC_END