Esempio n. 1
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      */
Esempio n. 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      */
Esempio n. 3
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      */
Esempio n. 4
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      */
Esempio n. 5
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      */