Example #1
0
void e_label(char* sub, evalparser* e) {
    char* start = sub;
    while (*sub) {
        if (*sub=='=') {
            int offset = (int)(sub-start);
            char* label = malloc(sizeof(char) * (offset+1));
            memcpy(label, start, offset);
            label[offset] = 0;
            int cond = *(sub+1)-'0';

            if (e_eval(label, cond)) {
                start = sub+3;
                sub = start;
                int par = 1;
                do {
                    sub++;
                    if (*sub=='{') {
                        par++;
                    }
                    if (*sub=='}') {
                        par--;
                    }
                } while (par);
                offset = (int)(sub-start);
                memcpy(e->nxt+strlen(e->nxt), start, offset);
                e->index+=offset;
                e->nxt[e->index]=0;
            }

            free0(label);
            return;
        }
        else {
            sub++;
        }
    }
}
Example #2
0
bool ExpressionAction::check(const QString &xmlFile, const TypeRule *rule,
                             SymFactory *factory)
{
    for (int i = 0; i < _exprList.size(); ++i)
        delete _exprList[i];
    _exprList.clear();
    _srcType = 0;
    _targetType = 0;
    _expr = 0;

    checkExprComplexity(xmlFile, _srcTypeStr, "source type");
    checkExprComplexity(xmlFile, _targetTypeStr, "target type");
    checkExprComplexity(xmlFile, _exprStr, "expression");

    QString srcId;

    // Check target type
    bool targetUsesId = false;
    _targetType = parseTypeStr(xmlFile, 0, factory, "target type",
                               _targetTypeStr, srcId, &targetUsesId);

    if (_targetType)
        _targetType = _targetType->dereferencedBaseType(BaseType::trLexical);

    // Check source type
    bool srcUsesId = false;
    _srcType = parseTypeStr(xmlFile, rule, factory, "source type",
                            _srcTypeStr, srcId, &srcUsesId);

    // Is the srcId valid?
    if (srcId.isEmpty())
        typeRuleError2(xmlFile, srcLine(), -1,
                       QString("The source type does not specify an identifier:"
                               " %1")
                            .arg(_srcTypeStr));

    // Make sure the expression contains the srcId
    if (!_exprStr.contains(QRegExp("\\b" + srcId + "\\b")))
        typeRuleError2(xmlFile, srcLine(), -1,
                       QString("The expression does not use identifier \"%1\" "
                               "which was defined in the source type.")
                            .arg(srcId));

    // Check complete expression
    AbstractSyntaxTree ast;
    ASTBuilder builder(&ast, factory);
    QString codeStr;
    // If the type was specified via ID, we have to use the pseudo typedef here
    if (srcUsesId)
        codeStr = QString("__0x%0__ %1;").arg((uint)_srcType->id(), 0, 16).arg(srcId);
    else
        codeStr = _srcTypeStr + ";";
    codeStr += " int __dest__ = " + _exprStr + ";";
    if (builder.buildFrom(codeStr.toAscii()) != 0)
        typeRuleError2(xmlFile, srcLine(), -1,
                       QString("Syntax error in expression: %1")
                            .arg(codeStr));

    // We should finde exatcely one initializer
    QList<const ASTNode*> init_nodes =
            ASTNodeFinder::find(nt_initializer, &ast);
    if (init_nodes.isEmpty())
        typeRuleError2(xmlFile, srcLine(), -1,
                       QString("Error parsing expression: %1")
                            .arg(codeStr));

    // Try to evaluate expression
    ASTTypeEvaluator t_eval(&ast, factory->memSpecs().sizeofLong,
                            factory->memSpecs().sizeofPointer, factory);
    ASTExpressionEvaluator e_eval(&t_eval, factory);
    ASTNodeNodeHash ptsTo;
    _expr = e_eval.exprOfNode(init_nodes.first(), ptsTo);
    if (_expr)
        // Expression belongs to the evaluator, we need to clone it
        _expr = _expr->copy(_exprList);
    else
        typeRuleError2(xmlFile, srcLine(), -1,
                       QString("Error evaluating expression: %1")
                            .arg(QString(_exprStr)));

    _altRefType.setId(_targetType->id());
    _altRefType.setExpr(_expr);

    return true;
}