Search in sources :

Example 36 with ASTNode

use of org.mvel2.ast.ASTNode in project drools by kiegroup.

the class MVELConditionEvaluator method evaluateIfNecessary.

private void evaluateIfNecessary(InternalFactHandle handle, ReteEvaluator reteEvaluator, Tuple tuple, ASTNode node) {
    if (!isEvaluated(node)) {
        ASTNode next = node.nextASTNode;
        node.nextASTNode = null;
        evaluate(createMvelEvaluator(evaluator, asCompiledExpression(node)), handle, reteEvaluator, tuple);
        node.nextASTNode = next;
    }
}
Also used : ASTNode(org.mvel2.ast.ASTNode)

Example 37 with ASTNode

use of org.mvel2.ast.ASTNode in project drools by kiegroup.

the class MVELConditionEvaluator method ensureCompleteEvaluation.

private void ensureCompleteEvaluation(InternalFactHandle handle, ReteEvaluator reteEvaluator, Tuple tuple) {
    if (!evaluated) {
        ASTNode rootNode = getRootNode(executableStatement);
        if (rootNode != null) {
            ensureCompleteEvaluation(rootNode, handle, reteEvaluator, tuple);
        }
        evaluated = true;
    }
}
Also used : ASTNode(org.mvel2.ast.ASTNode)

Example 38 with ASTNode

use of org.mvel2.ast.ASTNode in project mvel by mikebrock.

the class AbstractParser method _captureBlock.

private ASTNode _captureBlock(ASTNode node, final char[] expr, boolean cond, int type) {
    skipWhitespace();
    int startCond = 0;
    int endCond = 0;
    int blockStart;
    int blockEnd;
    String name;
    /**
     * Functions are a special case we handle differently from the rest of block parsing
     */
    switch(type) {
        case FUNCTION:
            {
                int st = cursor;
                captureToNextTokenJunction();
                if (cursor == end) {
                    throw new CompileException("unexpected end of statement", expr, st);
                }
                /**
                 * Check to see if the name is legal.
                 */
                if (isReservedWord(name = createStringTrimmed(expr, st, cursor - st)) || isNotValidNameorLabel(name))
                    throw new CompileException("illegal function name or use of reserved word", expr, cursor);
                if (pCtx == null)
                    pCtx = getParserContext();
                FunctionParser parser = new FunctionParser(name, cursor, end - cursor, expr, fields, pCtx, splitAccumulator);
                Function function = parser.parse();
                cursor = parser.getCursor();
                return lastNode = function;
            }
        case PROTO:
            if (ProtoParser.isUnresolvedWaiting()) {
                if (pCtx == null)
                    pCtx = getParserContext();
                ProtoParser.checkForPossibleUnresolvedViolations(expr, cursor, pCtx);
            }
            int st = cursor;
            captureToNextTokenJunction();
            if (isReservedWord(name = createStringTrimmed(expr, st, cursor - st)) || isNotValidNameorLabel(name))
                throw new CompileException("illegal prototype name or use of reserved word", expr, cursor);
            if (expr[cursor = nextNonBlank()] != '{') {
                throw new CompileException("expected '{' but found: " + expr[cursor], expr, cursor);
            }
            cursor = balancedCaptureWithLineAccounting(expr, st = cursor + 1, end, '{', pCtx);
            if (pCtx == null)
                pCtx = getParserContext();
            ProtoParser parser = new ProtoParser(expr, st, cursor, name, pCtx, fields, splitAccumulator);
            Proto proto = parser.parse();
            if (pCtx == null)
                pCtx = getParserContext();
            pCtx.addImport(proto);
            proto.setCursorPosition(st, cursor);
            cursor = parser.getCursor();
            ProtoParser.notifyForLateResolution(proto);
            return lastNode = proto;
        default:
            if (cond) {
                if (expr[cursor] != '(') {
                    throw new CompileException("expected '(' but encountered: " + expr[cursor], expr, cursor);
                }
                /**
                 * This block is an: IF, FOREACH or WHILE node.
                 */
                endCond = cursor = balancedCaptureWithLineAccounting(expr, startCond = cursor, end, '(', pCtx);
                startCond++;
                cursor++;
            }
    }
    skipWhitespace();
    if (cursor >= end) {
        throw new CompileException("unexpected end of statement", expr, end);
    } else if (expr[cursor] == '{') {
        blockEnd = cursor = balancedCaptureWithLineAccounting(expr, blockStart = cursor, end, '{', pCtx);
    } else {
        blockStart = cursor - 1;
        captureToEOSorEOL();
        blockEnd = cursor + 1;
    }
    if (type == ASTNode.BLOCK_IF) {
        IfNode ifNode = (IfNode) node;
        if (node != null) {
            if (!cond) {
                return ifNode.setElseBlock(expr, st = trimRight(blockStart + 1), trimLeft(blockEnd) - st, pCtx);
            } else {
                return ifNode.setElseIf((IfNode) createBlockToken(startCond, endCond, trimRight(blockStart + 1), trimLeft(blockEnd), type));
            }
        } else {
            return createBlockToken(startCond, endCond, blockStart + 1, blockEnd, type);
        }
    } else if (type == ASTNode.BLOCK_DO) {
        cursor++;
        skipWhitespace();
        st = cursor;
        captureToNextTokenJunction();
        if ("while".equals(name = new String(expr, st, cursor - st))) {
            skipWhitespace();
            startCond = cursor + 1;
            endCond = cursor = balancedCaptureWithLineAccounting(expr, cursor, end, '(', pCtx);
            return createBlockToken(startCond, endCond, trimRight(blockStart + 1), trimLeft(blockEnd), type);
        } else if ("until".equals(name)) {
            skipWhitespace();
            startCond = cursor + 1;
            endCond = cursor = balancedCaptureWithLineAccounting(expr, cursor, end, '(', pCtx);
            return createBlockToken(startCond, endCond, trimRight(blockStart + 1), trimLeft(blockEnd), ASTNode.BLOCK_DO_UNTIL);
        } else {
            throw new CompileException("expected 'while' or 'until' but encountered: " + name, expr, cursor);
        }
    } else // DON"T REMOVE THIS COMMENT!
    // else if (isFlag(ASTNode.BLOCK_FOREACH) || isFlag(ASTNode.BLOCK_WITH)) {
    {
        return createBlockToken(startCond, endCond, trimRight(blockStart + 1), trimLeft(blockEnd), type);
    }
}
Also used : FunctionParser(org.mvel2.util.FunctionParser) ProtoParser(org.mvel2.util.ProtoParser) CompileException(org.mvel2.CompileException)

Example 39 with ASTNode

use of org.mvel2.ast.ASTNode in project mvel by mikebrock.

the class CompiledExpression method toString.

public String toString() {
    StringBuilder appender = new StringBuilder();
    ASTNode node = firstNode;
    while (node != null) {
        appender.append(node.toString()).append(";\n");
        node = node.nextASTNode;
    }
    return appender.toString();
}
Also used : ASTNode(org.mvel2.ast.ASTNode)

Example 40 with ASTNode

use of org.mvel2.ast.ASTNode in project mvel by mikebrock.

the class MVELInterpretedRuntime method parseAndExecuteInterpreted.

/**
 * Main interpreter loop.
 *
 * @return value
 */
private Object parseAndExecuteInterpreted() {
    ASTNode tk = null;
    int operator;
    lastWasIdentifier = false;
    try {
        while ((tk = nextToken()) != null) {
            holdOverRegister = null;
            if (lastWasIdentifier && lastNode.isDiscard()) {
                stk.discard();
            }
            /**
             * If we are at the beginning of a statement, then we immediately push the first token
             * onto the stack.
             */
            if (stk.isEmpty()) {
                stk.push(tk.getReducedValue(ctx, ctx, variableFactory));
                /**
                 * If this is a substatement, we need to move the result into the d-stack to preserve
                 * proper execution order.
                 */
                if (tk instanceof Substatement && (tk = nextToken()) != null) {
                    if (isArithmeticOperator(operator = tk.getOperator())) {
                        stk.push(nextToken().getReducedValue(ctx, ctx, variableFactory), operator);
                        if (procBooleanOperator(arithmeticFunctionReduction(operator)) == -1)
                            return stk.peek();
                        else
                            continue;
                    }
                } else {
                    continue;
                }
            }
            if (variableFactory.tiltFlag()) {
                return stk.pop();
            }
            switch(procBooleanOperator(operator = tk.getOperator())) {
                case RETURN:
                    variableFactory.setTiltFlag(true);
                    return stk.pop();
                case OP_TERMINATE:
                    return stk.peek();
                case OP_RESET_FRAME:
                    continue;
                case OP_OVERFLOW:
                    if (!tk.isOperator()) {
                        if (!(stk.peek() instanceof Class)) {
                            throw new CompileException("unexpected token or unknown identifier:" + tk.getName(), expr, st);
                        }
                        variableFactory.createVariable(tk.getName(), null, (Class) stk.peek());
                    }
                    continue;
            }
            stk.push(nextToken().getReducedValue(ctx, ctx, variableFactory), operator);
            switch((operator = arithmeticFunctionReduction(operator))) {
                case OP_TERMINATE:
                    return stk.peek();
                case OP_RESET_FRAME:
                    continue;
            }
            if (procBooleanOperator(operator) == OP_TERMINATE)
                return stk.peek();
        }
        if (holdOverRegister != null) {
            return holdOverRegister;
        }
    } catch (CompileException e) {
        throw ErrorUtil.rewriteIfNeeded(e, expr, start);
    } catch (NullPointerException e) {
        if (tk != null && tk.isOperator()) {
            CompileException ce = new CompileException("incomplete statement: " + tk.getName() + " (possible use of reserved keyword as identifier: " + tk.getName() + ")", expr, st, e);
            ce.setExpr(expr);
            ce.setLineNumber(line);
            ce.setCursor(cursor);
            throw ce;
        } else {
            throw e;
        }
    }
    return stk.peek();
}
Also used : ASTNode(org.mvel2.ast.ASTNode) Substatement(org.mvel2.ast.Substatement)

Aggregations

ASTNode (org.mvel2.ast.ASTNode)37 CompileException (org.mvel2.CompileException)12 LiteralNode (org.mvel2.ast.LiteralNode)11 CompiledExpression (org.mvel2.compiler.CompiledExpression)8 Interceptor (org.mvel2.integration.Interceptor)8 VariableResolverFactory (org.mvel2.integration.VariableResolverFactory)8 MapVariableResolverFactory (org.mvel2.integration.impl.MapVariableResolverFactory)8 HashMap (java.util.HashMap)7 BinaryOperation (org.mvel2.ast.BinaryOperation)7 WithNode (org.mvel2.ast.WithNode)7 ExecutableStatement (org.mvel2.compiler.ExecutableStatement)7 ParserContext (org.mvel2.ParserContext)6 Substatement (org.mvel2.ast.Substatement)6 ExecutableLiteral (org.mvel2.compiler.ExecutableLiteral)6 ExpressionCompiler (org.mvel2.compiler.ExpressionCompiler)6 AstNode (com.linkedin.pinot.pql.parsers.pql2.ast.AstNode)5 OperatorNode (org.mvel2.ast.OperatorNode)5 Union (org.mvel2.ast.Union)5 ExecutableAccessor (org.mvel2.compiler.ExecutableAccessor)5 MethodAccessor (org.mvel2.optimizers.impl.refl.nodes.MethodAccessor)5