axmol/extensions/Particle3D/PU/CCPUScriptCompiler.cpp

411 lines
12 KiB
C++
Raw Normal View History

2015-02-11 18:14:22 +08:00
/****************************************************************************
2015-03-02 16:05:26 +08:00
Copyright (C) 2013 Henry van Merode. All rights reserved.
Copyright (c) 2015-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
https://axis-project.github.io/
2015-02-11 18:14:22 +08:00
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
2015-02-11 18:14:22 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2015-02-11 18:14:22 +08:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
2015-03-05 13:24:06 +08:00
#include "CCPUScriptCompiler.h"
#include "extensions/Particle3D/PU/CCPUTranslateManager.h"
2015-02-11 18:14:22 +08:00
#include "platform/CCFileUtils.h"
NS_AX_BEGIN
2015-02-11 18:14:22 +08:00
// ObjectAbstractNode
2021-12-25 10:04:45 +08:00
PUObjectAbstractNode::PUObjectAbstractNode(PUAbstractNode* ptr) : PUAbstractNode(ptr), id(0), abstract(false)
2015-02-11 18:14:22 +08:00
{
type = ANT_OBJECT;
}
2021-12-25 10:04:45 +08:00
PUAbstractNode* PUObjectAbstractNode::clone() const
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
PUObjectAbstractNode* node = new PUObjectAbstractNode(parent);
node->file = file;
node->line = line;
node->type = type;
node->name = name;
node->cls = cls;
node->id = id;
node->abstract = abstract;
node->context = context;
for (PUAbstractNodeList::const_iterator i = children.begin(); i != children.end(); ++i)
2015-02-11 18:14:22 +08:00
{
PUAbstractNode* newNode = (PUAbstractNode*)((*i)->clone());
2021-12-25 10:04:45 +08:00
newNode->parent = (PUAbstractNode*)node;
2015-02-11 18:14:22 +08:00
node->children.push_back(newNode);
}
2021-12-25 10:04:45 +08:00
for (PUAbstractNodeList::const_iterator i = values.begin(); i != values.end(); ++i)
2015-02-11 18:14:22 +08:00
{
PUAbstractNode* newNode = (PUAbstractNode*)((*i)->clone());
2021-12-25 10:04:45 +08:00
newNode->parent = (PUAbstractNode*)node;
2015-02-11 18:14:22 +08:00
node->values.push_back(newNode);
}
node->_env = _env;
return (PUAbstractNode*)node;
}
std::string PUObjectAbstractNode::getValue() const
{
return cls;
}
2021-12-26 23:26:34 +08:00
void PUObjectAbstractNode::addVariable(std::string_view inName)
2015-02-11 18:14:22 +08:00
{
_env.emplace(inName, "");
2015-02-11 18:14:22 +08:00
}
2021-12-26 23:26:34 +08:00
void PUObjectAbstractNode::setVariable(std::string_view inName, std::string_view value)
2015-02-11 18:14:22 +08:00
{
2021-12-26 23:26:34 +08:00
// _env[inName] = value;
hlookup::set_item(_env, inName, value);
2015-02-11 18:14:22 +08:00
}
2021-12-26 23:26:34 +08:00
std::pair<bool, std::string> PUObjectAbstractNode::getVariable(std::string_view inName) const
2015-02-11 18:14:22 +08:00
{
2021-12-26 23:26:34 +08:00
auto i = _env.find(inName);
2021-12-25 10:04:45 +08:00
if (i != _env.end())
2015-02-11 18:14:22 +08:00
return std::make_pair(true, i->second);
2021-12-25 10:04:45 +08:00
PUObjectAbstractNode* parentNode = (PUObjectAbstractNode*)this->parent;
while (parentNode)
2015-02-11 18:14:22 +08:00
{
i = parentNode->_env.find(inName);
2021-12-25 10:04:45 +08:00
if (i != parentNode->_env.end())
2015-02-11 18:14:22 +08:00
return std::make_pair(true, i->second);
parentNode = (PUObjectAbstractNode*)parentNode->parent;
}
return std::make_pair(false, "");
}
2021-12-26 23:26:34 +08:00
const hlookup::string_map<std::string>& PUObjectAbstractNode::getVariables() const
2015-02-11 18:14:22 +08:00
{
return _env;
}
PUObjectAbstractNode::~PUObjectAbstractNode()
{
2021-12-25 10:04:45 +08:00
for (auto iter : children)
{
2015-02-11 18:14:22 +08:00
delete iter;
}
2021-12-25 10:04:45 +08:00
for (auto iter : values)
{
2015-02-11 18:14:22 +08:00
delete iter;
}
2021-12-25 10:04:45 +08:00
for (auto iter : overrides)
{
2015-02-11 18:14:22 +08:00
delete iter;
}
}
// PropertyAbstractNode//
2021-12-25 10:04:45 +08:00
PUPropertyAbstractNode::PUPropertyAbstractNode(PUAbstractNode* ptr) : PUAbstractNode(ptr), id(0)
2015-02-11 18:14:22 +08:00
{
type = ANT_PROPERTY;
}
2021-12-25 10:04:45 +08:00
PUAbstractNode* PUPropertyAbstractNode::clone() const
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
PUPropertyAbstractNode* node = new PUPropertyAbstractNode(parent);
node->file = file;
node->line = line;
node->type = type;
node->name = name;
node->id = id;
for (PUAbstractNodeList::const_iterator i = values.begin(); i != values.end(); ++i)
2015-02-11 18:14:22 +08:00
{
PUAbstractNode* newNode = (PUAbstractNode*)((*i)->clone());
2021-12-25 10:04:45 +08:00
newNode->parent = (PUAbstractNode*)node;
2015-02-11 18:14:22 +08:00
node->values.push_back(newNode);
}
return (PUAbstractNode*)node;
}
std::string PUPropertyAbstractNode::getValue() const
{
return name;
}
PUPropertyAbstractNode::~PUPropertyAbstractNode()
{
2021-12-25 10:04:45 +08:00
for (auto iter : values)
{
2015-02-11 18:14:22 +08:00
delete iter;
}
}
2021-12-25 10:04:45 +08:00
PUAtomAbstractNode::PUAtomAbstractNode(PUAbstractNode* ptr) : PUAbstractNode(ptr), id(0)
2015-02-11 18:14:22 +08:00
{
type = ANT_ATOM;
}
2021-12-25 10:04:45 +08:00
PUAbstractNode* PUAtomAbstractNode::clone() const
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
PUAtomAbstractNode* node = new PUAtomAbstractNode(parent);
node->file = file;
node->line = line;
node->id = id;
node->type = type;
node->value = value;
2015-02-11 18:14:22 +08:00
return node;
}
std::string PUAtomAbstractNode::getValue() const
{
return value;
}
2021-12-25 10:04:45 +08:00
PUScriptCompiler::PUScriptCompiler() : _current(nullptr), _nodes(nullptr), _PUParticleSystem3D(nullptr) {}
2015-02-11 18:14:22 +08:00
PUScriptCompiler::~PUScriptCompiler()
{
2021-12-25 10:04:45 +08:00
for (const auto& iter : _compiledScripts)
{
for (auto miter : iter.second)
{
2015-02-11 18:14:22 +08:00
delete miter;
}
}
_compiledScripts.clear();
}
2021-12-26 23:26:34 +08:00
hlookup::string_map<PUAbstractNodeList>::iterator PUScriptCompiler::compile(const PUConcreteNodeList& nodes,
2021-12-28 11:00:34 +08:00
std::string_view file)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
if (nodes.empty())
2021-12-26 23:26:34 +08:00
return _compiledScripts.end();
2015-02-11 18:14:22 +08:00
PUAbstractNodeList aNodes;
2021-12-25 10:04:45 +08:00
convertToAST(nodes, aNodes);
2015-02-11 18:14:22 +08:00
2021-12-26 23:26:34 +08:00
return hlookup::set_item(_compiledScripts, file, aNodes); // _compiledScripts[file] = aNodes;
2021-12-25 10:04:45 +08:00
// for(PUAbstractNodeList::iterator i = aNodes.begin(); i != aNodes.end(); ++i)
2015-02-11 18:14:22 +08:00
//{
2021-12-25 10:04:45 +08:00
// PUScriptTranslator *translator = PUTranslateManager::Instance()->getTranslator(*i);
// if(translator){
// if (translator->isParticleSystemTranslator())
// {
// PUParticleSystem3DTranslator *ps = static_cast<PUParticleSystem3DTranslator *>(translator);
// if (ps) ps->setParticleSystem3D(_PUParticleSystem3D);
// }
// translator->translate(this, *i);
// }
// }
// for (auto iter : aNodes){
// delete iter;
// }
2021-12-26 23:26:34 +08:00
// return true;
2015-02-11 18:14:22 +08:00
}
2021-12-26 23:26:34 +08:00
const PUAbstractNodeList* PUScriptCompiler::compile(std::string_view file, bool& isFirstCompile)
2015-02-11 18:14:22 +08:00
{
auto iter = _compiledScripts.find(file);
2021-12-25 10:04:45 +08:00
if (iter != _compiledScripts.end())
{
2015-02-11 18:14:22 +08:00
isFirstCompile = false;
return &iter->second;
}
std::string data = FileUtils::getInstance()->getStringFromFile(file);
PUScriptLexer lexer;
PUScriptParser parser;
PUScriptTokenList tokenList;
PUConcreteNodeList creteNodeList;
lexer.openLexer(data, file, tokenList);
parser.parse(creteNodeList, tokenList);
2021-12-26 23:26:34 +08:00
auto it = compile(creteNodeList, file);
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
for (auto iter1 : creteNodeList)
{
2015-03-03 15:57:03 +08:00
delete iter1;
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
for (auto iter2 : tokenList)
{
2015-03-03 15:57:03 +08:00
delete iter2;
2015-02-11 18:14:22 +08:00
}
isFirstCompile = true;
2021-12-26 23:26:34 +08:00
if (it != _compiledScripts.end())
2021-12-25 10:04:45 +08:00
{
2021-12-26 23:26:34 +08:00
return &it->second;
2015-02-11 18:14:22 +08:00
}
return nullptr;
}
2021-12-25 10:04:45 +08:00
void PUScriptCompiler::convertToAST(const PUConcreteNodeList& nodes, PUAbstractNodeList& aNodes)
2015-02-11 18:14:22 +08:00
{
2015-02-11 18:14:22 +08:00
_current = NULL;
2021-12-25 10:04:45 +08:00
_nodes = &aNodes;
2015-02-11 18:14:22 +08:00
visitList(nodes);
}
// CNT_VARIABLE,
// CNT_VARIABLE_ASSIGN,
// CNT_WORD,need handle
// CNT_IMPORT,
// CNT_QUOTE,
// CNT_LBRACE,need handle
// CNT_RBRACE,need handle
// CNT_COLON
2021-12-25 10:04:45 +08:00
void PUScriptCompiler::visitList(const PUConcreteNodeList& nodes)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
for (const auto& node : nodes)
2015-02-11 18:14:22 +08:00
{
this->visit(node);
2015-02-11 18:14:22 +08:00
}
}
2021-12-25 10:04:45 +08:00
void PUScriptCompiler::visit(PUConcreteNode* node)
2015-02-11 18:14:22 +08:00
{
PUAbstractNode* asn = NULL;
2015-02-11 18:14:22 +08:00
// Handle properties and objects here
2021-12-25 10:04:45 +08:00
if (!node->children.empty())
2015-02-11 18:14:22 +08:00
{
// Grab the last two nodes
2021-12-25 10:04:45 +08:00
PUConcreteNode* temp1 = NULL;
PUConcreteNode* temp2 = NULL;
2015-02-11 18:14:22 +08:00
PUConcreteNodeList::reverse_iterator iter = node->children.rbegin();
2021-12-25 10:04:45 +08:00
if (iter != node->children.rend())
2015-02-11 18:14:22 +08:00
{
temp1 = *iter;
++iter;
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
if (iter != node->children.rend())
2015-02-11 18:14:22 +08:00
temp2 = *iter;
2021-12-25 10:04:45 +08:00
// brance inner
if (temp1 && temp1->type == CNT_RBRACE && temp2 && temp2->type == CNT_LBRACE)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
if (node->children.size() < 2)
2015-02-11 18:14:22 +08:00
{
return;
}
2021-12-25 10:04:45 +08:00
PUObjectAbstractNode* impl = new PUObjectAbstractNode(_current);
impl->line = node->line;
impl->file = node->file;
impl->abstract = false;
2015-02-11 18:14:22 +08:00
std::list<PUConcreteNode*> temp;
2015-02-11 18:14:22 +08:00
temp.push_back(node);
2021-12-25 10:04:45 +08:00
for (const auto& child : node->children)
2015-02-11 18:14:22 +08:00
{
temp.push_back(child);
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
// add brance type//
2015-02-11 18:14:22 +08:00
PUConcreteNodeList::const_iterator iter1 = temp.begin();
2021-12-25 10:04:45 +08:00
impl->cls = (*iter1)->token;
++iter1;
2015-02-11 18:14:22 +08:00
2021-12-25 10:04:45 +08:00
// add brance name//
if (iter1 != temp.end() && ((*iter1)->type == CNT_WORD))
2015-02-11 18:14:22 +08:00
{
impl->name = (*iter1)->token;
++iter1;
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
while (iter1 != temp.end() && (*iter1)->type != CNT_LBRACE)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
PUAtomAbstractNode* atom = new PUAtomAbstractNode(impl);
atom->file = (*iter1)->file;
atom->line = (*iter1)->line;
atom->type = ANT_ATOM;
atom->value = (*iter1)->token;
2015-02-11 18:14:22 +08:00
impl->values.push_back(atom);
++iter1;
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
asn = impl;
2015-02-11 18:14:22 +08:00
_current = impl;
visitList(temp2->children);
_current = impl->parent;
}
2021-12-25 10:04:45 +08:00
// no brance//
2015-02-11 18:14:22 +08:00
else
{
2021-12-25 10:04:45 +08:00
PUPropertyAbstractNode* impl = new PUPropertyAbstractNode(_current);
impl->line = node->line;
impl->file = node->file;
impl->name = node->token;
2021-12-25 10:04:45 +08:00
asn = impl;
2015-02-11 18:14:22 +08:00
_current = impl;
2015-02-11 18:14:22 +08:00
// Visit the children of the {
visitList(node->children);
2015-02-11 18:14:22 +08:00
// Go back up the stack
_current = impl->parent;
}
}
else
{
2021-12-25 10:04:45 +08:00
PUAtomAbstractNode* impl = new PUAtomAbstractNode(_current);
impl->line = node->line;
impl->file = node->file;
impl->value = node->token;
asn = impl;
2015-02-11 18:14:22 +08:00
}
2021-12-25 10:04:45 +08:00
if (asn)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
if (_current)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
if (_current->type == ANT_PROPERTY)
2015-02-11 18:14:22 +08:00
{
2021-12-25 10:04:45 +08:00
PUPropertyAbstractNode* impl = reinterpret_cast<PUPropertyAbstractNode*>(_current);
// PUAtomAbstractNode* assd = dynamic_cast<PUAtomAbstractNode*>(asn);
2015-02-11 18:14:22 +08:00
impl->values.push_back(asn);
}
else
{
2021-12-25 10:04:45 +08:00
PUObjectAbstractNode* impl = reinterpret_cast<PUObjectAbstractNode*>(_current);
2015-02-11 18:14:22 +08:00
impl->children.push_back(asn);
}
}
else
{
_nodes->push_back(asn);
}
}
}
2021-12-25 10:04:45 +08:00
void PUScriptCompiler::setParticleSystem3D(PUParticleSystem3D* pu)
2015-02-11 18:14:22 +08:00
{
2015-03-05 13:24:06 +08:00
_PUParticleSystem3D = pu;
2015-02-11 18:14:22 +08:00
}
PUScriptCompiler* PUScriptCompiler::Instance()
{
static PUScriptCompiler psc;
return &psc;
}
NS_AX_END