private CompiledExpression asCompiledExpression(ASTNode node) { return new CompiledExpression(new ASTLinkedList(node), null, Object.class, parserConfiguration, false); }
private static void optimizeOperator(int operator, ASTNode tk, ASTNode tkOp, ASTLinkedList astLinkedList, ASTLinkedList optimizedAst) { switch (operator) { case Operator.REGEX: optimizedAst.addTokenNode(new RegExMatchNode(tk, astLinkedList.nextNode())); break; case Operator.CONTAINS: optimizedAst.addTokenNode(new Contains(tk, astLinkedList.nextNode())); break; case Operator.INSTANCEOF: optimizedAst.addTokenNode(new Instance(tk, astLinkedList.nextNode())); break; case Operator.CONVERTABLE_TO: optimizedAst.addTokenNode((new Convertable(tk, astLinkedList.nextNode()))); break; case Operator.SIMILARITY: optimizedAst.addTokenNode(new Strsim(tk, astLinkedList.nextNode())); break; case Operator.SOUNDEX: optimizedAst.addTokenNode(new Soundslike(tk, astLinkedList.nextNode())); break; default: optimizedAst.addTokenNode(tk, tkOp); } }
public CompiledExpression(ASTLinkedList astMap, String sourceName, Class egressType, ParserContext ctx, boolean literalOnly) { this.firstNode = astMap.firstNode(); this.sourceName = sourceName; this.knownEgressType = astMap.isSingleNode() ? astMap.firstNonSymbol().getEgressType() : egressType; this.literalOnly = literalOnly; setParserContext(ctx); }
public void finish() { reset(); ASTNode last = null; ASTNode curr; while (hasMoreNodes()) { if ((curr = nextNode()).isDiscard()) { if (last == null) { last = firstASTNode = nextNode(); } else { last.nextASTNode = nextNode(); } continue; } if (!hasMoreNodes()) break; last = curr; } this.last = last; reset(); }
ASTLinkedList optimizedAst = new ASTLinkedList(); ASTNode tk, tkOp, tkOp2; while (astLinkedList.hasMoreNodes()) { if ((tk = astLinkedList.nextNode()).getFields() == -1) { optimizedAst.addTokenNode(tk); else if (astLinkedList.hasMoreNodes()) { if ((tkOp = astLinkedList.nextNode()).getFields() == -1) { optimizedAst.addTokenNode(tk, tkOp); ASTNode tk2 = astLinkedList.nextNode(); BinaryOperation bo; ASTNode oper = astLinkedList.nextNode(); ASTNode rightNode = astLinkedList.nextNode(); if (!astLinkedList.hasMoreNodes() && BlankLiteral.INSTANCE.equals(val)) { optimizedAst.addTokenNode(tk); continue; reduc = astLinkedList.hasMoreNodes() && (reducacbleOperator(astLinkedList.peekNode().getOperator())) && astLinkedList.peekNext().isLiteral(); while (astLinkedList.hasMoreNodes() && (tkOp2 = astLinkedList.nextNode()).isOperator() && tkOp2.getFields() != -1 && (op2 = tkOp2.getOperator()) != -1 && op2 < 21) { bo.setRightMost(boOptimize(op2, bo.getRightMost(), astLinkedList.nextNode(), pCtx));
ASTLinkedList astBuild = new ASTLinkedList(); stk = new ExecutionStack(); dStack = new ExecutionStack(); astBuild.addTokenNode(tk); continue; astBuild.addTokenNode(new LiteralNode(stk.pop(), pCtx), verify(pCtx, tkOp2)); break; astBuild.addTokenNode(new LiteralNode(getStackValueResult(), pCtx)); astBuild.addTokenNode(new OperatorNode(tkOp2.getOperator(), expr, st, pCtx), verify(pCtx, tkLA2)); break; astBuild.addTokenNode(new LiteralNode(getStackValueResult(), pCtx)); astBuild.addTokenNode(new LiteralNode(getStackValueResult(), pCtx), tkOp2); if (tkLA2 != null) astBuild.addTokenNode(verify(pCtx, tkLA2)); astBuild.addTokenNode(new LiteralNode(getStackValueResult(), pCtx)); astBuild.addTokenNode(verify(pCtx, tk), verify(pCtx, tkOp)); if (tkLA != null) astBuild.addTokenNode(verify(pCtx, tkLA)); continue; astBuild.addTokenNode(verify(pCtx, tk)); if (tkOp != null) astBuild.addTokenNode(verify(pCtx, tkOp));
private boolean compileReduce(int opCode, ASTLinkedList astBuild) { switch (arithmeticFunctionReduction(opCode)) { case -1: /** * The reduction failed because we encountered a non-literal, * so we must now back out and cleanup. */ stk.xswap_op(); astBuild.addTokenNode(new LiteralNode(stk.pop(), pCtx)); astBuild.addTokenNode( (OperatorNode) splitAccumulator.pop(), verify(pCtx, (ASTNode) splitAccumulator.pop()) ); return false; case -2: /** * Back out completely, pull everything back off the stack and add the instructions * to the output payload as they are. */ LiteralNode rightValue = new LiteralNode(stk.pop(), pCtx); OperatorNode operator = new OperatorNode((Integer) stk.pop(), expr, st, pCtx); astBuild.addTokenNode(new LiteralNode(stk.pop(), pCtx), operator); astBuild.addTokenNode(rightValue, (OperatorNode) splitAccumulator.pop()); astBuild.addTokenNode(verify(pCtx, (ASTNode) splitAccumulator.pop())); } return true; }
ASTLinkedList optimizedAst = new ASTLinkedList(); ASTNode tk, tkOp, tkOp2; while (astLinkedList.hasMoreNodes()) { if ((tk = astLinkedList.nextNode()).getFields() == -1) { optimizedAst.addTokenNode(tk); else if (astLinkedList.hasMoreNodes()) { if ((tkOp = astLinkedList.nextNode()).getFields() == -1) { optimizedAst.addTokenNode(tk, tkOp); ASTNode tk2 = astLinkedList.nextNode(); BinaryOperation bo; ASTNode oper = astLinkedList.nextNode(); ASTNode rightNode = astLinkedList.nextNode(); if (!astLinkedList.hasMoreNodes() && BlankLiteral.INSTANCE.equals(val)) { optimizedAst.addTokenNode(tk); continue; reduc = astLinkedList.hasMoreNodes() && (reducacbleOperator(astLinkedList.peekNode().getOperator())) && astLinkedList.peekNext().isLiteral(); while (astLinkedList.hasMoreNodes() && (tkOp2 = astLinkedList.nextNode()).isOperator() && tkOp2.getFields() != -1 && (op2 = tkOp2.getOperator()) != -1 && op2 < 21) { bo.setRightMost(boOptimize(op2, bo.getRightMost(), astLinkedList.nextNode(), pCtx));
ASTLinkedList astBuild = new ASTLinkedList(); stk = new ExecutionStack(); dStack = new ExecutionStack(); astBuild.addTokenNode(tk); continue; astBuild.addTokenNode(new LiteralNode(stk.pop(), pCtx), verify(pCtx, tkOp2)); break; astBuild.addTokenNode(new LiteralNode(getStackValueResult(), pCtx)); astBuild.addTokenNode(new OperatorNode(tkOp2.getOperator(), expr, st, pCtx), verify(pCtx, tkLA2)); break; astBuild.addTokenNode(new LiteralNode(getStackValueResult(), pCtx)); astBuild.addTokenNode(new LiteralNode(getStackValueResult(), pCtx), tkOp2); if (tkLA2 != null) astBuild.addTokenNode(verify(pCtx, tkLA2)); astBuild.addTokenNode(new LiteralNode(getStackValueResult(), pCtx)); astBuild.addTokenNode(verify(pCtx, tk), verify(pCtx, tkOp)); if (tkLA != null) astBuild.addTokenNode(verify(pCtx, tkLA)); continue; astBuild.addTokenNode(verify(pCtx, tk)); if (tkOp != null) astBuild.addTokenNode(verify(pCtx, tkOp));
public void finish() { reset(); ASTNode last = null; ASTNode curr; while (hasMoreNodes()) { if ((curr = nextNode()).isDiscard()) { if (last == null) { last = firstASTNode = nextNode(); } else { last.nextASTNode = nextNode(); } continue; } if (!hasMoreNodes()) break; last = curr; } this.last = last; reset(); }
private boolean compileReduce(int opCode, ASTLinkedList astBuild) { switch (arithmeticFunctionReduction(opCode)) { case -1: /** * The reduction failed because we encountered a non-literal, * so we must now back out and cleanup. */ stk.xswap_op(); astBuild.addTokenNode(new LiteralNode(stk.pop())); astBuild.addTokenNode( (OperatorNode) splitAccumulator.pop(), verify(pCtx, (ASTNode) splitAccumulator.pop()) ); return false; case -2: /** * Back out completely, pull everything back off the stack and add the instructions * to the output payload as they are. */ LiteralNode rightValue = new LiteralNode(stk.pop()); OperatorNode operator = new OperatorNode((Integer) stk.pop()); astBuild.addTokenNode(new LiteralNode(stk.pop()), operator); astBuild.addTokenNode(rightValue, (OperatorNode) splitAccumulator.pop()); astBuild.addTokenNode(verify(pCtx, (ASTNode) splitAccumulator.pop())); } return true; }
ASTLinkedList optimizedAst = new ASTLinkedList(); ASTNode tk, tkOp, tkOp2; while (astLinkedList.hasMoreNodes()) { if ((tk = astLinkedList.nextNode()).getFields() == -1) { optimizedAst.addTokenNode(tk); else if (astLinkedList.hasMoreNodes()) { if ((tkOp = astLinkedList.nextNode()).getFields() == -1) { optimizedAst.addTokenNode(tk, tkOp); ASTNode tk2 = astLinkedList.nextNode(); BinaryOperation bo; ASTNode oper = astLinkedList.nextNode(); ASTNode rightNode = astLinkedList.nextNode(); if (!astLinkedList.hasMoreNodes() && BlankLiteral.INSTANCE.equals(val)) { optimizedAst.addTokenNode(tk); continue; reduc = astLinkedList.hasMoreNodes() && (reducacbleOperator(astLinkedList.peekNode().getOperator())) && astLinkedList.peekNext().isLiteral(); while (astLinkedList.hasMoreNodes() && (tkOp2 = astLinkedList.nextNode()).isOperator() && tkOp2.getFields() != -1 && (op2 = tkOp2.getOperator()) != -1 && op2 < 21) { bo.setRightMost(new BinaryOperation(op2, bo.getRightMost(), astLinkedList.nextNode(), ctx));
ASTLinkedList astBuild = new ASTLinkedList(); stk = new ExecutionStack(); dStack = new ExecutionStack(); astBuild.addTokenNode(tk); continue; astBuild.addTokenNode(new LiteralNode(stk.pop()), verify(pCtx, tkOp2)); break; astBuild.addTokenNode(new LiteralNode(getStackValueResult())); astBuild.addTokenNode(new OperatorNode(tkOp2.getOperator()), verify(pCtx, tkLA2)); break; astBuild.addTokenNode(new LiteralNode(getStackValueResult())); astBuild.addTokenNode(new LiteralNode(getStackValueResult()), tkOp2); if (tkLA2 != null) astBuild.addTokenNode(verify(pCtx, tkLA2)); astBuild.addTokenNode(new LiteralNode(getStackValueResult())); astBuild.addTokenNode(verify(pCtx, tk), verify(pCtx, tkOp)); if (tkLA != null) astBuild.addTokenNode(verify(pCtx, tkLA)); continue; astBuild.addTokenNode(verify(pCtx, tk)); if (tkOp != null) astBuild.addTokenNode(verify(pCtx, tkOp));
public CompiledExpression(ASTLinkedList astMap, String sourceName, Class egressType, ParserConfiguration parserConfiguration, boolean literalOnly) { this.firstNode = astMap.firstNode(); this.sourceName = sourceName; this.knownEgressType = astMap.isSingleNode() ? astMap.firstNonSymbol().getEgressType() : egressType; this.literalOnly = literalOnly; this.parserConfiguration = parserConfiguration; this.importInjectionRequired = !parserConfiguration.getImports().isEmpty(); }
public void finish() { reset(); ASTNode last = null; ASTNode curr; while (hasMoreNodes()) { if ((curr = nextNode()).isDiscard()) { if (last == null) { last = firstASTNode = nextNode(); } else { last.nextASTNode = nextNode(); } continue; } if (!hasMoreNodes()) break; last = curr; } this.last = last; reset(); }
/** * Returns an ordered Map of all functions declared within an compiled script. * * @param compile * @return - ordered Map */ public static Map<String, Function> extractAllDeclaredFunctions(CompiledExpression compile) { Map<String, Function> allFunctions = new LinkedHashMap<String, Function>(); ASTIterator instructions = new ASTLinkedList(compile.getFirstNode()); ASTNode n; while (instructions.hasMoreNodes()) { if ((n = instructions.nextNode()) instanceof Function) { allFunctions.put(n.getName(), (Function) n); } } return allFunctions; }
private static void optimizeOperator(int operator, ASTNode tk, ASTNode tkOp, ASTLinkedList astLinkedList, ASTLinkedList optimizedAst, ParserContext pCtx) { switch (operator) { case Operator.REGEX: optimizedAst.addTokenNode(new RegExMatchNode(tk, astLinkedList.nextNode(), pCtx)); break; case Operator.CONTAINS: optimizedAst.addTokenNode(new Contains(tk, astLinkedList.nextNode(), pCtx)); break; case Operator.INSTANCEOF: optimizedAst.addTokenNode(new Instance(tk, astLinkedList.nextNode(), pCtx)); break; case Operator.CONVERTABLE_TO: optimizedAst.addTokenNode((new Convertable(tk, astLinkedList.nextNode(), pCtx))); break; case Operator.SIMILARITY: optimizedAst.addTokenNode(new Strsim(tk, astLinkedList.nextNode(), pCtx)); break; case Operator.SOUNDEX: optimizedAst.addTokenNode(new Soundslike(tk, astLinkedList.nextNode(), pCtx)); break; case TERNARY: if ( pCtx.isStrongTyping() && tk.getEgressType() != Boolean.class && tk.getEgressType() != Boolean.TYPE ) throw new RuntimeException( "Condition of ternary operator is not of type boolean. Found " + tk.getEgressType() ); default: optimizedAst.addTokenNode(tk, tkOp); } }
astBuild.addTokenNode(new LiteralNode(stk.pop(), pCtx)); astBuild.addTokenNode( (OperatorNode) splitAccumulator.pop(), verify(pCtx, (ASTNode) splitAccumulator.pop()) OperatorNode operator = new OperatorNode((Integer) stk.pop(), expr, st, pCtx); astBuild.addTokenNode(new LiteralNode(stk.pop(), pCtx), operator); astBuild.addTokenNode(rightValue, (OperatorNode) splitAccumulator.pop()); astBuild.addTokenNode(verify(pCtx, (ASTNode) splitAccumulator.pop())); return false; case OP_NOT_LITERAL: ASTNode tkLA2 = (ASTNode) stk.pop(); Integer tkOp2 = (Integer) stk.pop(); astBuild.addTokenNode(new LiteralNode(getStackValueResult(), pCtx)); astBuild.addTokenNode(new OperatorNode(tkOp2, expr, st, pCtx), verify(pCtx, tkLA2)); return false;
public CompiledExpression(ASTLinkedList astMap, String sourceName, Class egressType, ParserConfiguration parserConfiguration, boolean literalOnly) { this.firstNode = astMap.firstNode(); this.sourceName = sourceName; this.knownEgressType = astMap.isSingleNode() ? astMap.firstNonSymbol().getEgressType() : egressType; this.literalOnly = literalOnly; this.parserConfiguration = parserConfiguration; this.importInjectionRequired = !parserConfiguration.getImports().isEmpty(); }
/** * Returns an ordered Map of all functions declared within an compiled script. * * @param compile * @return - ordered Map */ public static Map<String, Function> extractAllDeclaredFunctions(CompiledExpression compile) { Map<String, Function> allFunctions = new LinkedHashMap<String, Function>(); ASTIterator instructions = new ASTLinkedList(compile.getFirstNode()); ASTNode n; while (instructions.hasMoreNodes()) { if ((n = instructions.nextNode()) instanceof Function) { allFunctions.put(n.getName(), (Function) n); } } return allFunctions; }