예제 #1
0
 protected Type checkRelationalOp(Type t1, Type t2, Token op) {
   // Checks Relational operators <, >, <=, >=, ==, and !=
   if (t1.isArrayKind()) {
     System.out.println(
         "line " + op.getLine() + ": invalid argument 1 -- arrays cannot be compared");
     errorCount++;
   }
   if (t2.isArrayKind()) {
     System.out.println(
         "line " + op.getLine() + ": invalid argument 2 -- arrays cannot be compared");
     errorCount++;
   }
   if (!t1.isErrorType() && !t2.isErrorType()) {
     if (!t1.isSameType(t2)) {
       if (!t1.isNumericType() || !t2.isNumericType()) {
         System.out.println(
             "line "
                 + op.getLine()
                 + ": operands to relational operator `"
                 + op.getText()
                 + "' have incompatible types");
         errorCount++;
       }
     }
   }
   return Type.boolValue;
 } /*      End of checkRelationalOp      */
예제 #2
0
  protected Type checkBinaryNumericOp(Node exp1, Node exp2, Token op) {
    // Checks Binary operators +, -, *, /, and %
    // Do not forget to handle + for pairs of strings.
    Type t1 = null; // the type attribute of exp1
    Type t2 = null; // the type attribute of exp2

    // Below are the error messages to be printed when
    // the type is wrong.  The first is for t1 and second for t2.

    System.out.println(
        "line "
            + op.getLine()
            + ": invalid argument 1 ` "
            + exp1
            + "' for arithmetic operator `"
            + op.getText()
            + "'\n"
            + "          -- expects a numeric argument but found "
            + t1.toString());

    System.out.println(
        "line "
            + op.getLine()
            + ": invalid argument 2 ` "
            + exp2
            + "' for arithmetic operator `"
            + op.getText()
            + "'\n"
            + "          -- expects a numeric argument but found "
            + t2.toString());

    return null;
  } /*      End of checkBinaryNumericOp      */
예제 #3
0
 protected Type checkUnaryNumericOp(Type t, Token op) {
   // Checks Unary operators + and -
   if (t.isArrayKind()) {
     System.out.println(
         "line "
             + op.getLine()
             + ": invalid argument to operator `"
             + op.getText()
             + "'\n"
             + "          -- expects a numeric argument but found "
             + t.toString()
             + "[]");
     errorCount++;
     return Type.errorType;
   }
   if (t.isNumericType()) {
     if (t.isCharType() && op.getText().equals("-")) {
       return Type.intValue;
     } else {
       return t;
     }
   } else {
     System.out.println(
         "line "
             + op.getLine()
             + ": invalid argument type for unary operator `"
             + op.getText()
             + "'\n"
             + "          -- expects a numeric argument but found "
             + t.toString());
     errorCount++;
     return Type.errorType;
   }
 } /*      End of checkUnaryNumericOp      */
예제 #4
0
 protected Type checkUnaryBooleanOp(Type t, Token op) {
   // Checks Unary operator !
   if (t.isArrayKind()) {
     System.out.println(
         "line "
             + op.getLine()
             + ": invalid argument to operator `"
             + op.getText()
             + "'\n"
             + "          -- expects a boolean argument but found "
             + t.toString()
             + "[]");
     errorCount++;
     return Type.boolValue;
   }
   if (t.isBoolType()) {
     return t;
   } else {
     System.out.println(
         "line "
             + op.getLine()
             + ": unary operator `!' expects a boolean argument "
             + "but found "
             + t.toString());
     errorCount++;
     return Type.boolValue;
   }
 } /*      End of checkUnaryBooleanOp      */
예제 #5
0
 protected Type checkCondition(Type t1, Token op) {
   // Checks conditional expressions in if and while statements
   if (t1.isArrayKind()) {
     System.out.println(
         "line " + op.getLine() + ": a conditional expression must be a boolean type");
     errorCount++;
     return Type.boolValue;
   }
   if (!t1.isBoolType() && !t1.isErrorType()) {
     System.out.println(
         "line " + op.getLine() + ": a conditional expression must be a boolean type");
     errorCount++;
   }
   return Type.boolValue;
 } /*      End of checkCondition      */
예제 #6
0
 protected Type checkLogicalOp(Type t1, Type t2, Token op) {
   // Checks Logical operators || and &&
   if (t1.isArrayKind()) {
     System.out.println(
         "line "
             + op.getLine()
             + ": invalid argument 1 to operator `"
             + op.getText()
             + "'\n"
             + "          -- expects a boolean argument but found "
             + t1.toString()
             + "[]");
     errorCount++;
     return Type.boolValue;
   }
   if (t2.isArrayKind()) {
     System.out.println(
         "line "
             + op.getLine()
             + ": invalid argument 2 to operator `"
             + op.getText()
             + "'\n"
             + "          -- expects a boolean argument but found "
             + t2.toString()
             + "[]");
     errorCount++;
     return Type.boolValue;
   }
   if (!t1.isBoolType() && !t1.isErrorType()) {
     System.out.println(
         "line "
             + op.getLine()
             + ": invalid argument 1 to operator `"
             + op.getText()
             + "'\n"
             + "          -- expects a boolean argument but found "
             + t1.toString());
     errorCount++;
   }
   if (!t2.isBoolType() && !t2.isErrorType()) {
     System.out.println(
         "line "
             + op.getLine()
             + ": invalid argument 2 to operator `"
             + op.getText()
             + "'\n"
             + "          -- expects a boolean argument but found "
             + t2.toString());
     errorCount++;
   }
   return Type.boolValue;
 } /*      End of checkLogicalOp      */