use of com.linkedin.pinot.pql.parsers.pql2.ast.AstNode in project drools by kiegroup.
the class MvelConditionEvaluator method evaluateIfNecessary.
private void evaluateIfNecessary(InternalFactHandle handle, InternalWorkingMemory workingMemory, Tuple tuple, ASTNode node) {
if (!isEvaluated(node)) {
ASTNode next = node.nextASTNode;
node.nextASTNode = null;
evaluate(asCompiledExpression(node), handle, workingMemory, tuple);
node.nextASTNode = next;
}
}
use of com.linkedin.pinot.pql.parsers.pql2.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();
}
use of com.linkedin.pinot.pql.parsers.pql2.ast.AstNode in project mvel by mikebrock.
the class MVELRuntime method execute.
/**
* Main interpreter.
*
* @param debugger Run in debug mode
* @param expression The compiled expression object
* @param ctx The root context object
* @param variableFactory The variable factory to be injected
* @return The resultant value
* @see org.mvel2.MVEL
*/
public static Object execute(boolean debugger, final CompiledExpression expression, final Object ctx, VariableResolverFactory variableFactory) {
Object v1, v2;
ExecutionStack stk = new ExecutionStack();
variableFactory.setTiltFlag(false);
ASTNode tk = expression.getFirstNode();
Integer operator;
if (tk == null)
return null;
try {
do {
if (tk.fields == -1) {
/**
* This may seem silly and redundant, however, when an MVEL script recurses into a block
* or substatement, a new runtime loop is entered. Since the debugger state is not
* passed through the AST, it is not possible to forward the state directly. So when we
* encounter a debugging symbol, we check the thread local to see if there is are registered
* breakpoints. If we find them, we assume that we are debugging.
*
* The consequence of this of course, is that it's not ideal to compileShared expressions with
* debugging symbols which you plan to use in a production enviroment.
*/
if (debugger || (debugger = hasDebuggerContext())) {
try {
debuggerContext.get().checkBreak((LineLabel) tk, variableFactory, expression);
} catch (NullPointerException e) {
// do nothing for now. this isn't as calus as it seems.
}
}
continue;
} else if (stk.isEmpty()) {
stk.push(tk.getReducedValueAccelerated(ctx, ctx, variableFactory));
}
if (variableFactory.tiltFlag()) {
return stk.pop();
}
switch(operator = tk.getOperator()) {
case RETURN:
variableFactory.setTiltFlag(true);
return stk.pop();
case NOOP:
continue;
case TERNARY:
if (!stk.popBoolean()) {
// noinspection StatementWithEmptyBody
while (tk.nextASTNode != null && !(tk = tk.nextASTNode).isOperator(TERNARY_ELSE)) ;
}
stk.clear();
continue;
case TERNARY_ELSE:
return stk.pop();
case END_OF_STMT:
/**
* If the program doesn't end here then we wipe anything off the stack that remains.
* Althought it may seem like intuitive stack optimizations could be leveraged by
* leaving hanging values on the stack, trust me it's not a good idea.
*/
if (tk.nextASTNode != null) {
stk.clear();
}
continue;
}
stk.push(tk.nextASTNode.getReducedValueAccelerated(ctx, ctx, variableFactory), operator);
try {
while (stk.isReduceable()) {
if ((Integer) stk.peek() == CHOR) {
stk.pop();
v1 = stk.pop();
v2 = stk.pop();
if (!isEmpty(v2) || !isEmpty(v1)) {
stk.clear();
stk.push(!isEmpty(v2) ? v2 : v1);
} else
stk.push(null);
} else {
stk.op();
}
}
} catch (ClassCastException e) {
throw new CompileException("syntax error or incomptable types", new char[0], 0, e);
} catch (CompileException e) {
throw e;
} catch (Exception e) {
throw new CompileException("failed to compileShared sub expression", new char[0], 0, e);
}
} while ((tk = tk.nextASTNode) != null);
return stk.peek();
} catch (NullPointerException e) {
if (tk != null && tk.isOperator() && tk.nextASTNode != null) {
throw new CompileException("incomplete statement: " + tk.getName() + " (possible use of reserved keyword as identifier: " + tk.getName() + ")", tk.getExpr(), tk.getStart());
} else {
throw e;
}
}
}
use of com.linkedin.pinot.pql.parsers.pql2.ast.AstNode in project pinot by linkedin.
the class Pql2AstListener method popNode.
private void popNode() {
AstNode topNode = _nodeStack.pop();
topNode.doneProcessingChildren();
}
use of com.linkedin.pinot.pql.parsers.pql2.ast.AstNode in project pinot by linkedin.
the class Pql2Compiler method compileToExpressionTree.
@Override
public TransformExpressionTree compileToExpressionTree(String expression) {
CharStream charStream = new ANTLRInputStream(expression);
PQL2Lexer lexer = new PQL2Lexer(charStream);
lexer.setTokenFactory(new CommonTokenFactory(true));
TokenStream tokenStream = new UnbufferedTokenStream<CommonToken>(lexer);
PQL2Parser parser = new PQL2Parser(tokenStream);
parser.setErrorHandler(new BailErrorStrategy());
// Parse
ParseTree parseTree = parser.expression();
ParseTreeWalker walker = new ParseTreeWalker();
Pql2AstListener listener = new Pql2AstListener(expression);
walker.walk(listener, parseTree);
final AstNode rootNode = listener.getRootNode();
return TransformExpressionTree.buildTree(rootNode);
}
Aggregations