Exemple #1
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);
  }
Exemple #2
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);
    }
  }
Exemple #3
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;
  }