Ejemplo n.º 1
0
static char *EvalConcat(const StringExpression *expr, VarRefEvaluator evalfn, void *param)
{
    char *lhs, *rhs, *res;

    lhs = EvalStringExpression(expr->val.concat.lhs, evalfn, param);
    if (!lhs)
    {
        return NULL;
    }

    rhs = EvalStringExpression(expr->val.concat.rhs, evalfn, param);
    if (!rhs)
    {
        free(lhs);
        return NULL;
    }

    xasprintf(&res, "%s%s", lhs, rhs);
    free(lhs);
    free(rhs);
    return res;
}
Ejemplo n.º 2
0
static char *EvalVarRef(const StringExpression *expr, VarRefEvaluator evalfn, void *param)
{
    char *name, *eval;

    name = EvalStringExpression(expr->val.varref.name, evalfn, param);
    if (!name)
    {
        return NULL;
    }

    eval = (*evalfn) (name, expr->val.varref.type, param);
    free(name);
    return eval;
}
Ejemplo n.º 3
0
ExpressionValue EvalExpression(const Expression *expr,
                               NameEvaluator nameevalfn, VarRefEvaluator varrefevalfn, void *param)
{
    switch (expr->op)
    {
    case OR:
    case AND:
    {
        ExpressionValue lhs = EXP_ERROR, rhs = EXP_ERROR;

        lhs = EvalExpression(expr->val.andor.lhs, nameevalfn, varrefevalfn, param);
        if (lhs == EXP_ERROR)
        {
            return EXP_ERROR;
        }

        rhs = EvalExpression(expr->val.andor.rhs, nameevalfn, varrefevalfn, param);

        if (rhs == EXP_ERROR)
        {
            return EXP_ERROR;
        }

        if (expr->op == OR)
        {
            return lhs || rhs;
        }
        else
        {
            return lhs && rhs;
        }
    }

    case NOT:
    {
        ExpressionValue arg = EvalExpression(expr->val.not.arg,
                                             nameevalfn,
                                             varrefevalfn,
                                             param);

        if (arg == EXP_ERROR)
        {
            return EXP_ERROR;
        }
        else
        {
            return !arg;
        }
    }

    case EVAL:
    {
        ExpressionValue ret = EXP_ERROR;
        char *name = EvalStringExpression(expr->val.eval.name,
                                          varrefevalfn,
                                          param);

        if (name == NULL)
        {
            return EXP_ERROR;
        }

        ret = (*nameevalfn) (name, param);
        free(name);
        return ret;
    }

    default:
        FatalError("Unexpected class expression type is found: %d", expr->op);
    }
}
Ejemplo n.º 4
0
ExpressionValue EvalExpression(const Expression *expr,
                               NameEvaluator nameevalfn, VarRefEvaluator varrefevalfn, void *param)
{
    switch (expr->op)
    {
    case LOGICAL_OP_OR:
    case LOGICAL_OP_AND:
    {
        ExpressionValue lhs = EXPRESSION_VALUE_ERROR, rhs = EXPRESSION_VALUE_ERROR;

        lhs = EvalExpression(expr->val.andor.lhs, nameevalfn, varrefevalfn, param);
        if (lhs == EXPRESSION_VALUE_ERROR)
        {
            return EXPRESSION_VALUE_ERROR;
        }

        rhs = EvalExpression(expr->val.andor.rhs, nameevalfn, varrefevalfn, param);

        if (rhs == EXPRESSION_VALUE_ERROR)
        {
            return EXPRESSION_VALUE_ERROR;
        }

        if (expr->op == LOGICAL_OP_OR)
        {
            return lhs || rhs;
        }
        else
        {
            return lhs && rhs;
        }
    }

    case LOGICAL_OP_NOT:
    {
        ExpressionValue arg = EvalExpression(expr->val.not.arg,
                                             nameevalfn,
                                             varrefevalfn,
                                             param);

        if (arg == EXPRESSION_VALUE_ERROR)
        {
            return EXPRESSION_VALUE_ERROR;
        }
        else
        {
            return !arg;
        }
    }

    case LOGICAL_OP_EVAL:
    {
        ExpressionValue ret = EXPRESSION_VALUE_ERROR;
        char *name = EvalStringExpression(expr->val.eval.name,
                                          varrefevalfn,
                                          param);

        if (name == NULL)
        {
            return EXPRESSION_VALUE_ERROR;
        }
        else if (0 == strcmp("true", name))
        {
            ret =  EXPRESSION_VALUE_TRUE;
        }
        else if (0 == strcmp("false", name))
        {
            ret =  EXPRESSION_VALUE_FALSE;
        }
        else
        {
            ret = (*nameevalfn) (name, param);
        }

        free(name);
        return ret;
    }

    default:
        ProgrammingError("Unexpected class expression type is found: %d", expr->op);
    }
}