Package org.mvel2.ast

Examples of org.mvel2.ast.ASTNode


    public static ASTBinaryTree buildTree(ASTIterator input) {
        ASTIterator iter = new ASTLinkedList(input.firstNode());
        ASTBinaryTree tree = new ASTBinaryTree(iter.nextNode());
        while (iter.hasMoreNodes()) {
            ASTNode node = iter.nextNode();
            if (node instanceof EndOfStatement) {
                if (iter.hasMoreNodes()) tree = new ASTBinaryTree(iter.nextNode());
            } else {
                tree = tree.append(node);
            }
View Full Code Here


    throw new RuntimeException("unimplemented");
  }

  public ASTNode nodesAhead(int offset) {
    if (current == null) return null;
    ASTNode cursor = null;
    for (int i = 0; i < offset; i++) {
      if ((cursor = current.nextASTNode) == null) return null;
    }
    return cursor;
  }
View Full Code Here

  }

  public void finish() {
    reset();

    ASTNode last = null;
    ASTNode curr;

    while (hasMoreNodes()) {
      if ((curr = nextNode()).isDiscard()) {
        if (last == null) {
          last = firstASTNode = nextNode();
View Full Code Here

                               VariableResolverFactory variableFactory) {

    Object v1, v2;
    ExecutionStack stk = new ExecutionStack();

    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;
      }
    }
View Full Code Here

   * 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()) {
          if ((tk.fields & ASTNode.STACKLANG) != 0) {
            stk.push(tk.getReducedValue(stk, ctx, variableFactory));
            Object o = stk.peek();
            if (o instanceof Integer) {
              arithmeticFunctionReduction((Integer) o);
            }
          }
          else {
            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);
View Full Code Here

      case TERNARY:
        if (!stk.popBoolean()) {
          stk.clear();

          ASTNode tk;

          for (; ; ) {
            if ((tk = nextToken()) == null || tk.isOperator(Operator.TERNARY_ELSE))
              break;
          }
        }

        return OP_RESET_FRAME;
View Full Code Here

   *
   * @param operator -
   * @return -
   */
  private boolean unwindStatement(int operator) {
    ASTNode tk;

    switch (operator) {
      case AND:
        while ((tk = nextToken()) != null && !tk.isOperator(Operator.END_OF_STMT) && !tk.isOperator(Operator.OR)) {
          //nothing
        }
        break;
      default:
        while ((tk = nextToken()) != null && !tk.isOperator(Operator.END_OF_STMT)) {
          //nothing
        }
    }
    return tk == null;
  }
View Full Code Here

    public int node = 0;
  }

  private static String decompile(CompiledExpression cExp, boolean nest, DecompileContext context) {
    ASTIterator iter = new ASTLinkedList(cExp.getFirstNode());
    ASTNode tk;

    StringBuffer sbuf = new StringBuffer();

    if (!nest) {
      sbuf.append("Expression Decompile\n-------------\n");
    }

    while (iter.hasMoreNodes()) {
      sbuf.append("(").append(context.node++).append(") ");

      if ((tk = iter.nextNode()) instanceof NestedStatement
          && ((NestedStatement) tk).getNestedStatement() instanceof CompiledExpression) {
        //noinspection StringConcatenationInsideStringBufferAppend
        sbuf.append("NEST [" + tk.getClass().getSimpleName() + "]: { " + tk.getName() + " }\n");
        sbuf.append(decompile((CompiledExpression) ((NestedStatement) tk).getNestedStatement(), true, context));
      }
      if (tk instanceof Substatement
          && ((Substatement) tk).getStatement() instanceof CompiledExpression) {
        //noinspection StringConcatenationInsideStringBufferAppend
        sbuf.append("NEST [" + tk.getClass().getSimpleName() + "]: { " + tk.getName() + " }\n");
        sbuf.append(decompile((CompiledExpression) ((Substatement) tk).getStatement(), true, context));
      }
//            else if (tk instanceof Function) {
//                sbuf.append("FUNCTION [" + tk.getName() + "]: ")
//                        .append(decompile((CompiledExpression) ((Function)tk).getCompiledBlock(), true, context));
//            }
      else if (tk.isDebuggingSymbol()) {
        //noinspection StringConcatenationInsideStringBufferAppend
        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());
      }
      else if (tk instanceof BinaryOperation) {
        BinaryOperation bo = (BinaryOperation) tk;
        sbuf.append("OPERATION [" + getOperatorName(bo.getOperation()) + "] {").append(bo.getLeft().getName())
            .append("} {").append(bo.getRight().getName()).append("}");
      }
      else {
        //noinspection StringConcatenationInsideStringBufferAppend
        sbuf.append("NODE [" + tk.getClass().getSimpleName() + "] :: " + tk.getName());
      }

      sbuf.append("\n");

    }
View Full Code Here

    return "UNKNOWN_OPERATOR";
  }

  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;
  }
View Full Code Here

   * 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);
View Full Code Here

TOP

Related Classes of org.mvel2.ast.ASTNode

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.