public Object getReducedValue(Object ctx, Object thisValue, VariableResolverFactory factory) { try { String i = String.valueOf(soundslike.getReducedValue(ctx, thisValue, factory)); if (i == null) throw new ClassCastException(); String x = (String) stmt.getReducedValue(ctx, thisValue, factory); if (x == null) throw new CompileException("not a string: " + stmt.getName(), stmt.getExpr(), stmt.getStart()); return soundex(i).equals(soundex(x)); } catch (ClassCastException e) { throw new CompileException("not a string: " + soundslike.getName(), soundslike.getExpr(), soundslike.getStart()); } }
private static boolean isIntOptimizationviolation(BooleanNode bn, ASTNode bn2) { return (bn instanceof IntOptimized && bn2.getEgressType() != Integer.class); }
private static boolean isReductionOpportunity(ASTNode oper, ASTNode node) { ASTNode n = node; return (n != null && n.isLiteral() && (n = n.nextASTNode) != null && reducacbleOperator(n.getOperator()) && PTABLE[oper.getOperator()] <= PTABLE[n.getOperator()] && (n = n.nextASTNode) != null && n.isLiteral() && n.getLiteralValue() instanceof Number); }
public String getAbsoluteName() { if (firstUnion > start) { return new String(expr, start, getAbsoluteFirstPart() - start); } else { return getName(); } }
public Object getReducedValue(Object ctx, Object thisValue, VariableResolverFactory factory) { try { Class i = (Class) clsStmt.getReducedValue(ctx, thisValue, factory); if (i == null) throw new ClassCastException(); return i.isInstance(stmt.getReducedValue(ctx, thisValue, factory)); } catch (ClassCastException e) { throw new RuntimeException("not a class reference: " + clsStmt.getName()); } }
protected ASTNode verify(ParserContext pCtx, ASTNode tk) { if (tk.isOperator() && (tk.getOperator().equals(Operator.AND) || tk.getOperator().equals(Operator.OR))) { secondPassOptimization = true; if (tk.isDiscard() || tk.isOperator()) { return tk; else if (tk.isLiteral()) { return new LiteralNode(tk.getLiteralValue(), pCtx); if (tk.isIdentifier()) { PropertyVerifier propVerifier = new PropertyVerifier(expr, tk.getStart(), tk.getOffset(), pCtx); tk.setEgressType(returnType = propVerifier.analyze()); tk.setEgressType(returnType = propVerifier.analyze()); tk.setAsFQCNReference(); pCtx.addInput(tk.getAbsoluteName(), propVerifier.isDeepProperty() ? Object.class : returnType); pCtx.isStrongTyping() && !pCtx.isVariableVisible(tk.getAbsoluteName()) && !tk.isFQCN()) { throw new CompileException("no such identifier: " + tk.getAbsoluteName(), expr, tk.getStart()); else if (tk.isAssignment()) { Assignment a = (Assignment) tk; tk.setEgressType(returnType = propVerifier.analyze( ));
protected ASTNode verify(ParserContext pCtx, ASTNode tk) { if (tk.isOperator() && (tk.getOperator().equals(Operator.AND) || tk.getOperator().equals(Operator.OR))) { secondPassOptimization = true; if (tk.isDiscard() || tk.isOperator()) { return tk; else if (tk.isLiteral()) { return new LiteralNode(tk.getLiteralValue()); if (tk.isIdentifier()) { PropertyVerifier propVerifier = new PropertyVerifier(tk.getNameAsArray(), pCtx); tk.setEgressType(returnType = propVerifier.analyze()); tk.setEgressType(returnType = propVerifier.analyze()); pCtx.addInput(tk.getAbsoluteName(), propVerifier.isDeepProperty() ? Object.class : returnType); else if (tk.isAssignment()) { Assignment a = (Assignment) tk; tk.setEgressType(returnType = propVerifier.analyze()); pCtx.addInput(tk.getAbsoluteName(), returnType); String[] parms = ParseTools.parseMethodOrConstructor(tk.getNameAsArray()); if (parms != null) { for (String p : parms) {
if ((tk = astLinkedList.nextNode()).getFields() == -1) { optimizedAst.addTokenNode(tk); if ((tkOp = astLinkedList.nextNode()).getFields() == -1) { optimizedAst.addTokenNode(tk, tkOp); else if (tkOp.isOperator() && tkOp.getOperator() < 21) { int op = tkOp.getOperator(); int op2; throw new CompileException("illegal use of operator: " + tkOp.getName(), tkOp.getExpr(), tk.getStart()); if (tk.getEgressType() == Integer.class && tk2.getEgressType() == Integer.class) { bo = boOptimize(op, tk, tk2, pCtx); boolean inv = tkOp.isOperator(Operator.SUB); boolean reduc = tk.isLiteral() && isReductionOpportunity(tkOp, tk2); boolean p_inv = false; Object val = new BinaryOperation(oper.getOperator(), inv ? new LiteralNode(signNumber(tk2.getLiteralValue()), pCtx) : tk2, rightNode, pCtx) .getReducedValueAccelerated(null, null, null); && (reducacbleOperator(astLinkedList.peekNode().getOperator())) && astLinkedList.peekNext().isLiteral(); bo = new BinaryOperation(tkOp.getOperator(), tk, new LiteralNode(p_inv ? signNumber(val) : val, pCtx), pCtx); while (astLinkedList.hasMoreNodes() && (tkOp2 = astLinkedList.nextNode()).isOperator()
&& ((NestedStatement) tk).getNestedStatement() instanceof CompiledExpression) { sbuf.append("NEST [" + tk.getClass().getSimpleName() + "]: { " + tk.getName() + " }\n"); sbuf.append(decompile((CompiledExpression) ((NestedStatement) tk).getNestedStatement(), true, context)); sbuf.append("NEST [" + tk.getClass().getSimpleName() + "]: { " + tk.getName() + " }\n"); sbuf.append(decompile((CompiledExpression) ((Substatement) tk).getStatement(), true, context)); else if (tk.isDebuggingSymbol()) { sbuf.append("DEBUG_SYMBOL :: " + tk.toString()); else if (tk.isLiteral()) { sbuf.append("LITERAL :: ").append(tk.getLiteralValue()).append("'"); else if (tk.isOperator()) { sbuf.append("OPERATOR [").append(getOperatorName(tk.getOperator())).append("]: ") .append(tk.getName()); if (tk.isOperator(Operator.END_OF_STMT)) sbuf.append("\n"); else if (tk.isIdentifier()) { sbuf.append("REFERENCE :: ").append(tk.getClass().getSimpleName()).append(":").append(tk.getName()); sbuf.append("OPERATION [" + getOperatorName(bo.getOperation()) + "] {").append(bo.getLeft().getName()) .append("} {").append(bo.getRight().getName()).append("}"); sbuf.append("NODE [" + tk.getClass().getSimpleName() + "] :: " + tk.getName());
if ((tk = astLinkedList.nextNode()).getFields() == -1) { optimizedAst.addTokenNode(tk); if ((tkOp = astLinkedList.nextNode()).getFields() == -1) { optimizedAst.addTokenNode(tk, tkOp); else if (tkOp.isOperator() && tkOp.getOperator() < 21) { int op = tkOp.getOperator(); int op2; throw new CompileException("illegal use of operator: " + tkOp.getName()); if (tk.getEgressType() == Integer.class && tk2.getEgressType() == Integer.class) { switch (op) { case Operator.ADD: boolean inv = tkOp.isOperator(Operator.SUB); boolean reduc = isReductionOpportunity(tkOp, tk2); boolean p_inv = false; Object val = new BinaryOperation(oper.getOperator(), inv ? new LiteralNode(signNumber(tk2.getLiteralValue())) : tk2, rightNode) .getReducedValueAccelerated(null, null, null); && (reducacbleOperator(astLinkedList.peekNode().getOperator())) && astLinkedList.peekNext().isLiteral(); bo = new BinaryOperation(tkOp.getOperator(), tk,new LiteralNode(p_inv ? signNumber(val) : val));
returnType = tk.getEgressType(); String key = new String(expr, tk.getStart(), tk.getOffset()); Map<String, CompiledExpression> cec = pCtx.getCompiledExpressionCache(); Map<String, Class> rtc = pCtx.getReturnTypeCache(); Class rt = rtc.get(key); if (compiled == null) { ExpressionCompiler subCompiler = new ExpressionCompiler(expr, tk.getStart(), tk.getOffset(), pCtx); compiled = subCompiler._compile(); rt = subCompiler.getReturnType(); tk.setAccessor(compiled); returnType = rt; if (!verifyOnly && tk.isLiteral()) { if (literalOnly == -1) literalOnly = 1; if ((tkOp = nextTokenSkipSymbols()) != null && tkOp.isOperator() && !tkOp.isOperator(Operator.TERNARY) && !tkOp.isOperator(Operator.TERNARY_ELSE)) { if ((tkLA = nextTokenSkipSymbols()) != null && tkLA.isLiteral() && tkOp.getOperator() < 34 && ((lastOp == -1 || (lastOp < PTABLE.length && PTABLE[lastOp] < PTABLE[tkOp.getOperator()])))) { stk.push(tk.getLiteralValue(), tkLA.getLiteralValue(), op = tkOp.getOperator()); if (isBooleanOperator(tkOp2.getOperator())) { astBuild.addTokenNode(new LiteralNode(stk.pop(), pCtx), verify(pCtx, tkOp2)); break;
private static Serializable _optimizeTree(final CompiledExpression compiled) { /** * If there is only one token, and it's an identifier, we can optimize this as an accessor expression. */ if (compiled.isSingleNode()) { ASTNode tk = compiled.getFirstNode(); if (tk.isLiteral() && !tk.isThisVal()) { return new ExecutableLiteral(tk.getLiteralValue()); } return tk.canSerializeAccessor() ? new ExecutableAccessorSafe(tk, compiled.getKnownEgressType()) : new ExecutableAccessor(tk, compiled.getKnownEgressType()); } return compiled; }
if (lastNode.getLiteralValue() instanceof String) { char[] tmp = ((String) lastNode.getLiteralValue()).toCharArray(); TypeDescriptor tDescr = new TypeDescriptor(tmp, 0, tmp.length, 0); lastNode.setLiteralValue(getClassReference(pCtx, tDescr)); lastNode.discard(); if (lastNode.isLiteral() && lastNode.getLiteralValue() instanceof Class) { lastNode.discard(); (Class) lastNode.getLiteralValue(), fields | ASTNode.ASSIGN, pCtx)); lastNode.getLiteralValue(), pCtx)); throw new CompileException("unknown class or illegal statement: " + lastNode.getLiteralValue(), expr, cursor); throw new CompileException("unknown class or illegal statement: " + lastNode.getLiteralValue(), expr, cursor);
public static Class determineType(String name, CompiledExpression compiledExpression) { ASTIterator iter = new ASTLinkedList(compiledExpression.getFirstNode()); ASTNode node; while (iter.hasMoreNodes()) { if (name.equals((node = iter.nextNode()).getName()) && node.isAssignment()) { return node.getEgressType(); } } return null; }
@Override public Object getReducedValueAccelerated(Object ctx, Object thisValue, VariableResolverFactory factory) { return ((Integer) left.getReducedValueAccelerated(ctx, thisValue, factory)) + ((Integer) right.getReducedValueAccelerated(ctx, thisValue, factory)); }
if (statement instanceof ExecutableAccessor) { ExecutableAccessor accessor = (ExecutableAccessor) statement; return new CastExpression(node.getEgressType(), analyzeNode(accessor.getNode())); } else { ExecutableLiteral literal = (ExecutableLiteral) statement; return new CastExpression(node.getEgressType(), new FixedExpression(literal.getLiteral())); Accessor accessor = node.getAccessor(); Accessor accessor = node.getAccessor(); String variableName = node.getName(); int dot = variableName.indexOf('.'); if (dot > 0) { return new VariableExpression(variableName, analyzeExpressionNode(((AccessorNode) accessor).getNextNode(),node,variableType), variableType != null ? variableType : node.getEgressType()); return new VariableExpression(variableName, analyzeExpressionNode(accessorNode, node, variableType), variableType); } else { if (node.getLiteralValue() instanceof ParserContext) { ParserContext pCtx = (ParserContext)node.getLiteralValue(); return new EvaluatedExpression(new MethodInvocation(thisClass.getMethod(variableName))); } catch (NoSuchMethodException e) { if (node.getEgressType() == Class.class) {
public static Accessor extractAccessor(ASTNode n) { if (n instanceof LiteralNode) return new ExecutableLiteral(n.getLiteralValue()); else return new ExecutableAccessor(n, n.getEgressType()); }
public Object getReducedValueAccelerated(Object ctx, Object thisValue, VariableResolverFactory factory) { if (accessor != null) { return accessor.getValue(main.getReducedValueAccelerated(ctx, thisValue, factory), thisValue, factory); } else { AccessorOptimizer o = OptimizerFactory.getThreadAccessorOptimizer(); accessor = o.optimizeAccessor(getCurrentThreadParserContext(), name, main.getReducedValueAccelerated(ctx, thisValue, factory), thisValue, factory, false, main.getEgressType()); return o.getResultOptPass(); } }
public static void testMVELList() { ExecutableAccessor s = (ExecutableAccessor) MVEL.compileExpression("['Foo244','Bar','Foo244','Bar','Foo244','Bar','Foo244','Bar','Foo244','Bar']"); List list; s.getNode().getReducedValueAccelerated(null, null, null); for (int i = 0; i < COUNT; i++) { list = (List) s.getNode().getAccessor().getValue(null, null, null); assert "Foo244".equals(list.get(0)) && "Foo244".equals(list.get(2)) && list.size() == 10; } }