Beispiel #1
0
void Scene::visitNode(Node* node, const char* visitMethod)
{
    ScriptController* sc = Game::getInstance()->getScriptController();

    // Invoke the visit method for this node.
    bool result;
    if (!sc->executeFunction<bool>(visitMethod, "<Node>", &result, (void*)node) || !result)
        return;

    // If this node has a model with a mesh skin, visit the joint hierarchy within it
    // since we don't add joint hierarcies directly to the scene. If joints are never
    // visited, it's possible that nodes embedded within the joint hierarchy that contain
    // models will never get visited (and therefore never get drawn).
    Model* model = dynamic_cast<Model*>(node->getDrawable());
    if (model && model->_skin && model->_skin->_rootNode)
    {
        visitNode(model->_skin->_rootNode, visitMethod);
    }

    // Recurse for all children.
    for (Node* child = node->getFirstChild(); child != NULL; child = child->getNextSibling())
    {
        visitNode(child, visitMethod);
    }
}
Beispiel #2
0
GLSLValue* GLSLVisitor::visitNode(GLSLUnaryExpression* const unary)
{
    if (! unary)
        return 0;

    GLSLExpression* const operand = unary->getExpression();
    if (! operand)
        return 0;

    GLSLValue* res = 0;
    switch (operand->getNodeType())
    {
        case GLSLNodeTypes::NODE_POSTFIX_EXPRESSION:
            res = visitNode(static_cast<GLSLPostfixExpression* const>(operand));
            break;

        case GLSLNodeTypes::NODE_UNARY_EXPRESSION:
            res = visitNode(static_cast<GLSLUnaryExpression*>(operand));
            break;
    }   // switch

    // TODO: to "really" evaluate this expression, check opToken, value and
    // apply that operator.
    //Token* const opToken = unary->getToken();

    return res;
}
Beispiel #3
0
bool AnnotationVisitor::visit(ParseTreeNode* const node) {
    if (node == 0)
        return false;

    bool res = true;
    try {
        switch (node->getNodeType()) {
            case AnnotationNodeTypes::NODE_ANNOTATION:
                visitNode(dynamic_cast<AnnotationNode*>(node));
                break;

            case AnnotationNodeTypes::NODE_ANNOTATION_TAGS:
                visitNode(dynamic_cast<AnnotationTags*>(node));
                break;

            case AnnotationNodeTypes::NODE_ANNOTATION_TAG_NAME:
                visitNode(dynamic_cast<AnnotationTagName*>(node));
                break;

            case AnnotationNodeTypes::NODE_ANNOTATION_TAG_VALUES:
                visitNode(dynamic_cast<AnnotationTagValues*>(node));
                break;

            default:
                res = false;
                log_ << "AnnotationVisitor::visit() called for ParseTreeNode!\n";
                break;
        }   // switch
    } catch (std::runtime_error& e) {
        log_ << "  Exception: " << e.what() << "\n";
        return false;
    }

    return res;
}
void DefaultVisitor::visitStatement(StatementAst *node)
{
    visitNode(node->assignStmt);
    visitNode(node->ifStmt);
    visitNode(node->blockStmt);
    visitNode(node->returnStmt);
}
Beispiel #5
0
void GLSLVisitor::visitNode(GLSLExternalDeclaration* const extDecl)
{
    if (! extDecl)
        return;

    switch (extDecl->getNodeType())
    {
        case GLSLNodeTypes::NODE_DECLARATION:
            visitNode(static_cast<GLSLDeclaration* const>(extDecl));
            break;

        case GLSLNodeTypes::NODE_DECLARATION_LIST:
            visitNode(static_cast<GLSLDeclarationList* const>(extDecl));
            break;

        case GLSLNodeTypes::NODE_FUNCTION_DEFINITION:
            visitNode(static_cast<GLSLFunctionDefinition* const>(extDecl));
            break;

        default:
            std::cout << "GLSLVisitor::visitNode(): Error! Unknown external declaration type!\n";
            std::cout << "  node type = " << extDecl->getNodeType() << std::endl;
            break;
    }   // switch
}
Beispiel #6
0
GLSLValue* GLSLVisitor::visitNode(GLSLCondition* const cond)
{
    if (! cond)
        return 0;

    GLSLTypeSpecifier* const spec = cond->getTypeSpecifier();
    IdentifierToken* const id = cond->getIdentifier();
    if ((spec) && (id))
    {
        try {
            GLSLVariableSymbol symbol = visitNode(spec);
            symbol.setID(id->getValue());
            symbol.setIsArray(false);
            symbol.setNumArrayElements(0);

            activeSymbols_->insertSymbol(new GLSLVariableSymbol(symbol));
        } catch (std::runtime_error& e) {
            log_ << "  Exeption in GLSLVisitor::visitNode(GLSLCondition*): " << e.what() << "\n";
        }
    }

    // If spec and id where both not NULL, the expression is the initializer
    //
    // NOTE: initializer is however only an assignemnt expression, not
    // an arbitrary expression to be more precise.
    return visitNode(cond->getExpression());
}
Beispiel #7
0
int PreprocessorVisitor::visitNode(LogicalExpression* const n) {
    if (LogicalBinaryExpression* const lbex = dynamic_cast<LogicalBinaryExpression* const>(n))
        return visitNode(lbex);
    else if (DefinedOperator* const defop = dynamic_cast<DefinedOperator* const>(n))
        return visitNode(defop);

    throw std::runtime_error("unknown logical expression class!");
}
Beispiel #8
0
void GLSLVisitor::visitNode(GLSLDoWhileStatement* const dwhl)
{
    if (dwhl)
    {
        visitNode(dwhl->getBody());
        delete visitNode(dwhl->getCondition());
    }
}
Beispiel #9
0
void GLSLVisitor::visitNode(GLSLWhileStatement* const whl)
{
    if (whl)
    {
        delete visitNode(whl->getCondition());
        visitNode(whl->getBody());
    }
}
Beispiel #10
0
int PreprocessorVisitor::visitNode(ArithmeticExpression* const n) {
    if (IntConstant* const c = dynamic_cast<IntConstant* const>(n))
        return c->getValue();
    else if (BinaryExpression* const b = dynamic_cast<BinaryExpression* const>(n))
        return visitNode(b);
    else if (UnaryExpression* const u = dynamic_cast<UnaryExpression* const>(n))
        return visitNode(u);

    throw std::runtime_error("error: unknown arithmetic expression class!");
}
Beispiel #11
0
void GLSLVisitor::visitNode(GLSLForStatement* const fr)
{
    if (fr)
    {
        visitNode(fr->getInit());
        delete visitNode(fr->getCondition());
        delete visitNode(fr->getIterationExpr());
        visitNode(fr->getBody());
    }
}
Beispiel #12
0
GLSLValue* GLSLVisitor::visitNode(GLSLAssignmentExpression* const assign)
{
    if (! assign)
        return 0;

    // Perform semantic action for assignment expressions:
    // The action in this case is currently only to evaluate check the lefthand side value
    // (a unary expression) and check if its token is an IDENTIFIER. If so, check if
    // the the token's name starts with "gl_" and insert it into the symbol table.
    // In other cases check if a corresponding symbol is containing within the symbol table
    // and emit a warning that the variable has not been declared, if no symbol is yet present.
    //
    // NOTE: currently any undeclared variable will be inserted into the symbol table. In
    // addition, a warning will be issued (not an error!), unless the variable name starts
    // with "gl_" (in lower or upper case).
    //
    GLSLValue* rhs = visitNode(assign->getRValue());
    GLSLValue* lhs = visitNode(assign->getLValue());
    if ((! rhs) || (! lhs))
        return 0;

    if (lhs->getType() == GLSLValue::VALUE_ADDRESS)
    {
        GLSLValueAddress* const addr = static_cast<GLSLValueAddress* const>(lhs);
        const std::string& varName = addr->symbolName_;

        if (! activeSymbols_->findSymbol(varName))
        {
            GLSLVariableSymbol* const sym = new GLSLVariableSymbol(varName, GLSLSymbol::INTERNAL_VOID, 1, false);

            if (getPrefixLower(varName, 3) != "gl_")
            {
                activeSymbols_->insertSymbol(sym);

                ++numWarnings_;
                log_ << "warning: Variable '" << sym->getID() << "' has been assigned without previous declaration!" << std::endl;
            }
            else
                // add gl_*** symbols ALWAYS to global symbol table.
                globalSymbols_.insertSymbol(sym);
        }

        if ((varName == "gl_FragData") && (addr->isArray()))
            glFragDataElements_.insert(addr->arrayIndex_);
    }
    else
    {
        ++numWarnings_;

        log_ << "warning: lhs operand must be an lvalue !" << std::endl;
    }

    return lhs;
}
Beispiel #13
0
void DebugVisitor::visitScope( ScopeAst *node )
{
    kDebug(9024) << getIndent() << "BEGIN(scope)(" << getTokenInfo(node->startToken)  << ")";
    indent++;
    visitNode( node->functionArguments );
    visitNode( node->orOperator );
    visitNode( node->scopeBody );
    indent--;
    kDebug(9024) << getIndent() << "END(scope)(" << getTokenInfo(node->endToken)  << ")";

}
Beispiel #14
0
int PreprocessorVisitor::visitNode(Expression* const n) {
    if (ArithmeticExpression* const arex = dynamic_cast<ArithmeticExpression* const>(n))
        return visitNode(arex);
    else if (ParenthesisExpression* const pex = dynamic_cast<ParenthesisExpression* const>(n))
        return visitNode(pex);
    else if (LogicalExpression* const logex = dynamic_cast<LogicalExpression* const>(n))
        return visitNode(logex);
    else if (Macro* const m = dynamic_cast<Macro* const>(n))
        return visitNode(m);

    throw std::runtime_error("error: unknown expression class!");
}
Beispiel #15
0
void GLSLVisitor::visitNode(GLSLSwitchStatement* const swtch)
{
    if (swtch)
    {
        delete visitNode(swtch->getExpression());
        GLSLStatementList* const lst = swtch->getStatements();
        if (! lst)
            return;
        const std::vector<GLSLStatement*>& stmts = lst->getStatements();
        for (size_t i = 0; i < stmts.size(); ++i)
            visitNode(stmts[i]);
    }
}
Beispiel #16
0
void GLSLVisitor::visitNode(GLSLSelectionStatement* const sel)
{
    if (sel)
    {
        //GLSLExpression* const res =
            delete visitNode(sel->getCondition());
        // TODO: decide whether both case need to be evaluated / visited
        // depending on the evaluation of the condition. This can enable
        // short-circuit evaluation, which may be unwanted.
        visitNode(sel->getTrueStatement());
        visitNode(sel->getFalseStatement());
    }
}
void CloneTree::visitCons(Model::ConsItem *node)
{
  visitNode(node->mLeft);

  Model::Node *left = mTemps.top();
  mTemps.pop();

  visitNode(node->mRight);

  Model::Node *right = mTemps.top();
  mTemps.pop();

  mTemps.push(KDevPG::cons(left, right));
}
void CloneTree::visitAlternative(Model::AlternativeItem *node)
{
  visitNode(node->mLeft);

  Model::Node *left = mTemps.top();
  mTemps.pop();

  visitNode(node->mRight);

  Model::Node *right = mTemps.top();
  mTemps.pop();

  mTemps.push(KDevPG::alternative(left, right));
}
    int kthSmallest(TreeNode *pRoot, int k)
    {
        if (!pRoot)
            return 0;

        auto ret = 0;
        auto iterCount = 0;
        std::vector<TreeNode*> nodes;
        nodes.push_back(pRoot);
        while (!nodes.empty())
        {
            TreeNode *pNode = nodes.back();
            if (pushNodeIfNotNull(&nodes, pNode->left))
                continue;

            if (visitNode(pNode, &iterCount, k, &ret))
                break;

            if (pushNodeIfNotNull(&nodes, pNode->right))
                continue;

            bool found = false;
            TreeNode *pChild = pNode;
            TreeNode *pParent = nullptr;
            while (true)
            {
                nodes.pop_back();
                if (nodes.empty())
                    break;

                // 遍历完左子树的话,先回溯到父结点,再开始遍历右子树,如果有的话
                pParent = nodes.back();
                if (pChild == pParent->left)
                {
                    found = visitNode(pParent, &iterCount, k, &ret);
                    if (pushNodeIfNotNull(&nodes, pParent->right))
                        break;
                }

                pChild = pParent;
            }

            if (found)
                break;
        }

        return ret;
    }
void
GraphvizFunction::printGraph(
    ItemFactory* aFactory,
    const Item& in, std::fstream& os)
{
  // create helper qnames for comparison
  Item lNodeQName = aFactory->createQName("", "", "node");
  Item lEdgeQName = aFactory->createQName("", "", "edge");
  Item lRelQName  = aFactory->createQName("", "", "rel");

  // print the graph with all its children
  Item lGraphId;
  if (!getAttribute(aFactory, "id", in, lGraphId)) {
    GraphvizFunction::throwErrorWithQName(aFactory, "IM003", "GXL parse error: edge does not have an 'id' attribute");
  }

  os << "digraph \"" << lGraphId.getStringValue() << "\" {" << std::endl;

  // visit nodes and edges (TODO add rel elements)
  Iterator_t lChildren = in.getChildren();
  lChildren->open();
  Item item;
  while (lChildren->next(item)) {
    Item lNodeName;
    item.getNodeName(lNodeName);
    if (lNodeName.getLocalName() == lNodeQName.getLocalName()) {
      visitNode(aFactory, item, os);
    } else if (lNodeName.getLocalName() == lEdgeQName.getLocalName()) {
      visitEdge(aFactory, item, os);
    } 
  }

  os << "}" << std::endl;
} /* GraphvizFunction::printGraph */
Beispiel #21
0
void CodeGenerator::visitAlternative(Model::AlternativeItem *node)
{
  QList<Model::Node*> top_level_nodes;

  QStack<Model::Node*> working_list;
  working_list.push(node->mRight);
  working_list.push(node->mLeft);

  while (!working_list.empty())
    {
      Model::Node *n = working_list.top();
      working_list.pop();

      if (Model::AlternativeItem *a = nodeCast<Model::AlternativeItem*>(n))
        {
          working_list.push(a->mRight);
          working_list.push(a->mLeft);
        }
      else
        {
          top_level_nodes.push_back(n);
        }
    }

  QList<Model::Node*>::iterator it = top_level_nodes.begin();
  while (it != top_level_nodes.end())
    {
      Model::Node *n = *it;
      ++it;
      Model::ConditionItem *cond = nodeCast<Model::ConditionItem*>(n);

      out << "if (";

      if (cond)
        out << "(";

      generateTestCondition(n, out);

      if (cond)
        out << ") && (" << cond->mCode << ")";

      out << ") {" << endl;
      visitNode(n);
      out << "}";

      if (it != top_level_nodes.end())
        out << "else ";
      else
        {
          out << "else {" << endl;

          if (!mCurrentCatchId)
              out << "return false;" << endl;
          else
              out << "goto __catch_" << mCurrentCatchId << ";";

          out << "}" << endl;
        }
    }
}
Beispiel #22
0
void CodeGenerator::visitEvolve(Model::EvolveItem *node)
{
  out << "if (";

  Model::ConditionItem *cond = nodeCast<Model::ConditionItem*>(node->mItem);

  if (cond)
    out << "(";

  generateTestCondition(node, out);

  if (reducesToEpsilon(node->mItem))
    {
      out << " || ";
      generateCondition(globalSystem.follow(node->mSymbol), out);
    }

  if (cond)
    out << ") && (" << cond->mCode << ")";

  out << ") {" << endl;

  GenerateLocalDeclarations gen_locals(out, mNames);
  gen_locals(node->mItem);

  out << node->mCode;

  visitNode(node->mItem);

  if (globalSystem.start.contains(node->mSymbol))
    out << "if (Token_EOF != yytoken) { return false; }" << endl;

  out << "}" << endl;
}
Beispiel #23
0
pt_value invokeNative(pt_native *native, pt_node *node) {
	extern pt_value UNDEF;
	extern pt_value visitNode(pt_node *node);
	extern pt_value visitTerminal(pt_node *node);

	if(native == NULL || node == NULL) return UNDEF;

	switch(native->type) {
		case PNT_FUNCTION: {
				pt_node *args = node->next;
				int idx, count = node->count;
				pt_value *values = malloc(sizeof(pt_value) * count);
				if(values == NULL) return UNDEF;

				values[0] = visitTerminal(node);
				for(idx = 1; idx < count; idx++) {
					values[idx] = visitNode(args);
					if(values[idx].type == PT_UNDEF) {
						/* TODO */
						free(values);
						return UNDEF;
					}

					args = args->next;
				}

				return native->u.function(values, count);
			}
		case PNT_EXTENSION:
			return native->u.extension(node, node->count);
		default:
			/* TODO */
			return UNDEF;
	}
}
Beispiel #24
0
void GLSLVisitor::visitNode(GLSLDeclarationList* const decls) {
    if (decls == 0)
        return;

    GLSLTypeSpecifier* const typeSpec = decls->getTypeSpecifier();
    if (typeSpec == 0)
        throw std::runtime_error("visitNode(GLSLDeclarationList): given declarations are not specified!");

    try {
        GLSLVariableSymbol metaSymbol = visitNode(typeSpec);
        const std::vector<GLSLVariable*>& vars = decls->getVariables();

        for (size_t i = 0; i < vars.size(); ++i) {
            GLSLVariableSymbol* const symbol = new GLSLVariableSymbol(metaSymbol);

            symbol->setID(vars[i]->getName());
            symbol->setIsArray(vars[i]->isArray());
            int numArrayElements = 0;   // TODO: evaluate expression from vars[i] to determine array size
            symbol->setNumArrayElements(numArrayElements);

            std::vector<GLSLAnnotation*> leading = processAnnotation(decls->getLeadingAnnotation());
            std::vector<GLSLAnnotation*> trailing = processAnnotation(decls->getTrailingAnnotation());

            leading.insert(leading.end(), trailing.begin(), trailing.end());
            symbol->setAnnotations(leading);

            activeSymbols_->insertSymbol(symbol);
        }
    } catch (std::runtime_error& e) {
        log_ << "  Exeption: " << e.what() << "\n";
    }
}
Beispiel #25
0
static void _inorderTreeWalk(TreeNode * curNode, void (visitNode)(TreeNode * node)) {
    if (curNode) {
        _inorderTreeWalk(curNode->left, visitNode);
        visitNode(curNode);
        _inorderTreeWalk(curNode->right, visitNode);
    }
}
Beispiel #26
0
void GLSLVisitor::visitNode(GLSLDeclaration* const decl) {
    if (decl == 0)
        return;

    switch (decl->getNodeType())
    {
        case GLSLNodeTypes::NODE_DECLARATION_LIST:
            visitNode(static_cast<GLSLDeclarationList* const>(decl));
            break;

        case GLSLNodeTypes::NODE_STRUCT_DECLARATION:
            //visitNode(static_cast<GLSLStructDeclaration* const>(decl));
            break;

        case GLSLNodeTypes::NODE_FIELD_DECLARATION:
            //visitNode(static_cast<GLSLFieldDeclaration* const>(decl));
            break;

        case GLSLNodeTypes::NODE_FUNCTION_DECLARATION:
            //visitNode(static_cast<GLSLFunctionDeclaration* const>(decl));
            break;

        default:
            break;
    }   // switch (decl->getNodeType()
}
Beispiel #27
0
void PreprocessorVisitor::visitNode(ConditionalDirective* const n) {
    if (n == 0)
        return;

    ParseTreeNode* next = 0;
    Expression* const cond = n->getCondition();
    next = (visitNode(cond) != 0) ? n->getTrue() : n->getFalse();

    if (n->getTrue() == 0)
        log_ << "warning: empty if statement!\n";

    // add newline to translation for consistency for the GLSL parser
    //
    translation_ << "\n";

    if (next != 0) {
        const std::vector<ParseTreeNode*>& children = next->getChildren();
        for (size_t i = 0; (next != 0); next = children[i++]) {
            visit(next);

            if (i >= children.size())
                break;
        }
    }
}
Beispiel #28
0
void AnnotationVisitor::visitNode(AnnotationNode* const node) {
    if (node == 0)
        return;

    if (AnnotationTags* const n = dynamic_cast<AnnotationTags* const>(node))
        visitNode(n);
    else if (AnnotationTagName* const n = dynamic_cast<AnnotationTagName* const>(node))
        visitNode(n);
    else if (AnnotationTagValues* const n = dynamic_cast<AnnotationTagValues* const>(node)) {
        try {
            visitNode(n);
        } catch (std::runtime_error& e) {
            log_ << "  Exception: " << e.what() << "\n";
        }
    }
}
Beispiel #29
0
int PreprocessorVisitor::visitNode(UnaryExpression* const n) {
    switch (n->getSymbolID()) {
        case PreprocessorTerminals::ID_PLUS:
            return visitNode(n->getExpression());

        case PreprocessorTerminals::ID_DASH:
            return (-visitNode(n->getExpression()));

        case PreprocessorTerminals::ID_OP_COMPLEMENT:
            return (~ visitNode(n->getExpression()));

        case PreprocessorTerminals::ID_OP_NOT:
            return (! visitNode(n->getExpression()));
    }

    throw std::runtime_error("unknown unary operator!");
}
Beispiel #30
0
void CodeGenerator::visitStar(Model::StarItem *node)
{
  out << "while (";
  generateTestCondition(node, out);
  out << ") {" << endl;
  visitNode(node->mItem);
  out << "}" << endl;
}