Ejemplo n.º 1
0
  public static Operation astToDml(ASTOperation node, boolean optimized) throws SyntaxException {

    // Verify that this really is a DML block.
    assert (node.getOperationType() == OperationType.DML);

    // There must be at least one child of a DML statement.
    int count = node.jjtGetNumChildren();
    assert (count >= 1);

    // Create an array to hold the expressions.
    Operation[] operations = new Operation[count];

    SourceRange sourceRange = node.getSourceRange();

    // If there are any children, then process them in post-traversal order.
    if (count > 0) {
      for (int i = 0; i < count; i++) {
        SimpleNode n = (SimpleNode) node.jjtGetChild(i);
        operations[i] = astToOperation(n);

        // Update the source range for this operation. This must be done
        // afterwards because the source range for the children may be
        // modified by iterating over them.
        sourceRange = SourceRange.combineSourceRanges(sourceRange, n.getSourceRange());
      }
    }

    if (optimized) {
      return DML.getInstance(sourceRange, operations);
    } else {
      return DML.getUnoptimizedInstance(sourceRange, operations);
    }
  }
Ejemplo n.º 2
0
  private static Operation astToIfElse(ASTOperation node) throws SyntaxException {

    int count = node.jjtGetNumChildren();
    assert (count >= 2);

    Operation condition = astToDml((ASTOperation) node.jjtGetChild(0), true);

    Operation trueClause = astToDml((ASTOperation) node.jjtGetChild(1), true);

    Operation falseClause = Undef.VALUE;
    if (count == 3) {
      falseClause = astToDml((ASTOperation) node.jjtGetChild(2), true);
    }

    return IfElse.newOperation(node.getSourceRange(), condition, trueClause, falseClause);
  }
Ejemplo n.º 3
0
  private static Operation astToAssign(ASTOperation node) throws SyntaxException {

    int count = node.jjtGetNumChildren();
    Operation[] ops = new Operation[count];

    // The last child is the DML block giving the value. Put that first into
    // the operations.
    ops[0] = astToOperation((SimpleNode) node.jjtGetChild(count - 1));

    // Convert all of the rest to SetValue operations. This is done in
    // reverse order.
    for (int i = count - 2; i >= 0; i--) {
      try {
        int index = count - 1 - i;
        ops[index] = astToSetValue((ASTVariable) node.jjtGetChild(i));
      } catch (ClassCastException cce) {
        throw CompilerError.create(MSG_ASSIGNMENT_HAS_NON_VARIABLE_CHILD);
      }
    }

    return new Assign(node.getSourceRange(), ops);
  }
Ejemplo n.º 4
0
  private static Operation astToOperation(SimpleNode node) throws SyntaxException {

    Operation op = null;

    switch (node.getId()) {
      case PanParserTreeConstants.JJTOPERATION:
        ASTOperation onode = (ASTOperation) node;
        switch (onode.getOperationType()) {
          case DML:
            op = astToDml(onode, true);
            break;
          case PLUS:
            op =
                UnaryPlus.newOperation(
                    onode.getSourceRange(), astToOperation((SimpleNode) node.jjtGetChild(0)));
            break;
          case MINUS:
            op =
                UnaryMinus.newOperation(
                    onode.getSourceRange(), astToOperation((SimpleNode) node.jjtGetChild(0)));
            break;
          case NOT:
            op =
                LogicalNot.newOperation(
                    onode.getSourceRange(), astToOperation((SimpleNode) node.jjtGetChild(0)));
            break;
          case BIT_NOT:
            op =
                BitNot.newOperation(
                    onode.getSourceRange(), astToOperation((SimpleNode) node.jjtGetChild(0)));
            break;
          case LITERAL:
            op = onode.getOperation();
            break;
          case ASSIGN:
            op = astToAssign(onode);
            break;
          case IF:
            op = astToIfElse(onode);
            break;
          case WHILE:
            op =
                While.newOperation(
                    onode.getSourceRange(),
                    astToOperation((SimpleNode) node.jjtGetChild(0)),
                    astToOperation((SimpleNode) node.jjtGetChild(1)));
            break;
          case FOREACH:
            op =
                new Foreach(
                    onode.getSourceRange(),
                    astToSetValue((ASTVariable) node.jjtGetChild(0)),
                    astToSetValue((ASTVariable) node.jjtGetChild(1)),
                    astToOperation((SimpleNode) node.jjtGetChild(2)),
                    astToOperation((SimpleNode) node.jjtGetChild(3)));
            break;
          case FOR:
            op =
                new For(
                    onode.getSourceRange(),
                    astToOperation((SimpleNode) node.jjtGetChild(0)),
                    astToOperation((SimpleNode) node.jjtGetChild(1)),
                    astToOperation((SimpleNode) node.jjtGetChild(2)),
                    astToOperation((SimpleNode) node.jjtGetChild(3)));
            break;
          default:
            op = null;
        }
        break;
      case PanParserTreeConstants.JJTVARIABLE:
        op = astToVariable((ASTVariable) node, false);
        break;
      case PanParserTreeConstants.JJTLOGICALOREXPRESSION:
        op =
            LogicalOr.newOperation(
                node.getSourceRange(),
                astToOperation((SimpleNode) node.jjtGetChild(0)),
                astToOperation((SimpleNode) node.jjtGetChild(1)));
        break;
      case PanParserTreeConstants.JJTLOGICALANDEXPRESSION:
        op =
            LogicalAnd.newOperation(
                node.getSourceRange(),
                astToOperation((SimpleNode) node.jjtGetChild(0)),
                astToOperation((SimpleNode) node.jjtGetChild(1)));
        break;
      case PanParserTreeConstants.JJTBITWISEINCLUSIVEOROPERATION:
        op =
            BitIOR.newOperation(
                node.getSourceRange(),
                astToOperation((SimpleNode) node.jjtGetChild(0)),
                astToOperation((SimpleNode) node.jjtGetChild(1)));
        break;
      case PanParserTreeConstants.JJTBITWISEEXCLUSIVEOROPERATION:
        op =
            BitXOR.newOperation(
                node.getSourceRange(),
                astToOperation((SimpleNode) node.jjtGetChild(0)),
                astToOperation((SimpleNode) node.jjtGetChild(1)));
        break;
      case PanParserTreeConstants.JJTBITWISEANDOPERATION:
        op =
            BitAnd.newOperation(
                node.getSourceRange(),
                astToOperation((SimpleNode) node.jjtGetChild(0)),
                astToOperation((SimpleNode) node.jjtGetChild(1)));
        break;
      case PanParserTreeConstants.JJTEQOPERATION:
        op =
            LogicalEQ.newOperation(
                node.getSourceRange(),
                astToOperation((SimpleNode) node.jjtGetChild(0)),
                astToOperation((SimpleNode) node.jjtGetChild(1)));
        break;
      case PanParserTreeConstants.JJTNEOPERATION:
        op =
            LogicalNE.newOperation(
                node.getSourceRange(),
                astToOperation((SimpleNode) node.jjtGetChild(0)),
                astToOperation((SimpleNode) node.jjtGetChild(1)));
        break;
      case PanParserTreeConstants.JJTLTOPERATION:
        op =
            LogicalLT.newOperation(
                node.getSourceRange(),
                astToOperation((SimpleNode) node.jjtGetChild(0)),
                astToOperation((SimpleNode) node.jjtGetChild(1)));
        break;
      case PanParserTreeConstants.JJTGTOPERATION:
        op =
            LogicalGT.newOperation(
                node.getSourceRange(),
                astToOperation((SimpleNode) node.jjtGetChild(0)),
                astToOperation((SimpleNode) node.jjtGetChild(1)));
        break;
      case PanParserTreeConstants.JJTLEOPERATION:
        op =
            LogicalLE.newOperation(
                node.getSourceRange(),
                astToOperation((SimpleNode) node.jjtGetChild(0)),
                astToOperation((SimpleNode) node.jjtGetChild(1)));
        break;
      case PanParserTreeConstants.JJTGEOPERATION:
        op =
            LogicalGE.newOperation(
                node.getSourceRange(),
                astToOperation((SimpleNode) node.jjtGetChild(0)),
                astToOperation((SimpleNode) node.jjtGetChild(1)));
        break;
      case PanParserTreeConstants.JJTADDOPERATION:
        op =
            Add.newOperation(
                node.getSourceRange(),
                astToOperation((SimpleNode) node.jjtGetChild(0)),
                astToOperation((SimpleNode) node.jjtGetChild(1)));
        break;
      case PanParserTreeConstants.JJTSUBOPERATION:
        op =
            Sub.newOperation(
                node.getSourceRange(),
                astToOperation((SimpleNode) node.jjtGetChild(0)),
                astToOperation((SimpleNode) node.jjtGetChild(1)));
        break;
      case PanParserTreeConstants.JJTMULOPERATION:
        op =
            Mult.newOperation(
                node.getSourceRange(),
                astToOperation((SimpleNode) node.jjtGetChild(0)),
                astToOperation((SimpleNode) node.jjtGetChild(1)));
        break;
      case PanParserTreeConstants.JJTDIVOPERATION:
        op =
            Div.newOperation(
                node.getSourceRange(),
                astToOperation((SimpleNode) node.jjtGetChild(0)),
                astToOperation((SimpleNode) node.jjtGetChild(1)));
        break;
      case PanParserTreeConstants.JJTMODOPERATION:
        op =
            Mod.newOperation(
                node.getSourceRange(),
                astToOperation((SimpleNode) node.jjtGetChild(0)),
                astToOperation((SimpleNode) node.jjtGetChild(1)));
        break;
      case PanParserTreeConstants.JJTFUNCTION:
        op = astToFunction((ASTFunction) node);
        break;
      default:
        assert (false);
        break;
    }
    assert (op != null);
    return op;
  }
Ejemplo n.º 5
0
  private static FullType astToFullType(String source, ASTFullTypeSpec ast) throws SyntaxException {
    Operation withDml = null;
    Element defaultValue = null;

    // Sanity checks.
    assert (ast.getId() == PanParserTreeConstants.JJTFULLTYPESPEC);
    assert (ast.jjtGetNumChildren() >= 1);

    // Retrieve the base type of this full type.
    ASTTypeSpec typeSpec = (ASTTypeSpec) ast.jjtGetChild(0);
    BaseType baseType = astToType(source, typeSpec);
    assert (baseType != null);

    // Create the source range for the full type specification.
    SourceRange sourceRange = SourceRange.combineSourceRanges(typeSpec.getSourceRange());

    // Loop over the rest of the children which must be with, default, or
    // description nodes.
    int nchild = ast.jjtGetNumChildren();
    for (int i = 1; i < nchild; i++) {
      SimpleNode child = (SimpleNode) ast.jjtGetChild(i);
      assert (child.getId() == PanParserTreeConstants.JJTOPERATION);
      ASTOperation op = (ASTOperation) child;
      switch (op.getOperationType()) {
        case DEFAULT:
          assert (op.jjtGetNumChildren() == 1);
          assert (op.jjtGetChild(0) instanceof ASTOperation);
          ASTOperation dml = (ASTOperation) op.jjtGetChild(0);
          assert (dml.getOperationType() == OperationType.DML);

          // Do not optimize DML. This guarantees that the returned value
          // is actually a DML object with the SourceRange information.
          DML defaultDml = (DML) astToDml(dml, false);
          defaultValue = runDefaultDml(defaultDml);
          sourceRange = SourceRange.combineSourceRanges(sourceRange, dml.getSourceRange());
          break;
        case WITH:
          assert (op.jjtGetNumChildren() == 1);
          assert (op.jjtGetChild(0) instanceof ASTOperation);
          ASTOperation with = (ASTOperation) op.jjtGetChild(0);
          assert (with.getOperationType() == OperationType.DML);
          withDml = astToDml(with, false);
          sourceRange = SourceRange.combineSourceRanges(sourceRange, with.getSourceRange());
          break;
        default:
          throw CompilerError.create(MSG_REACHED_IMPOSSIBLE_BRANCH);
      }
    }

    return new FullType(source, ast.getSourceRange(), baseType, defaultValue, withDml);
  }