Ejemplo n.º 1
0
        public override void CheckTypes(ErrorHandler errorHandler)
        {
            LHS.CheckTypes(errorHandler);
            RHS.CheckTypes(errorHandler);
            bool canApply   = true;
            var  intType    = new PrimType(PrimTypeName.INT);
            var  floatType  = new PrimType(PrimTypeName.FLOAT);
            var  boolType   = new PrimType(PrimTypeName.BOOL);
            var  stringType = new PrimType(PrimTypeName.STRING);

            if (Op == BinaryOp.ADD && !CanBothConvertTo(stringType, errorHandler) &&
                !CanBothConvertTo(intType, errorHandler) && !CanBothConvertTo(floatType, errorHandler))
            {
                canApply = false;
            }
            if ((Op == BinaryOp.AND || Op == BinaryOp.OR) && !CanBothConvertTo(boolType, errorHandler))
            {
                canApply = false;
            }
            if ((Op == BinaryOp.DIV || Op == BinaryOp.MUL || Op == BinaryOp.SUB) &&
                !CanBothConvertTo(intType, errorHandler) && !CanBothConvertTo(floatType, errorHandler))
            {
                canApply = false;
            }
            if (Op == BinaryOp.MOD && !CanBothConvertTo(intType, errorHandler))
            {
                canApply = false;
            }
            if ((Op == BinaryOp.L || Op == BinaryOp.LE || Op == BinaryOp.G || Op == BinaryOp.GE || Op == BinaryOp.NE ||
                 Op == BinaryOp.EQ) && !CanBothConvertTo(boolType, errorHandler))
            {
                canApply = false;
            }
            if ((Op == BinaryOp.B_AND || Op == BinaryOp.B_NOT || Op == BinaryOp.B_OR || Op == BinaryOp.B_SHL ||
                 Op == BinaryOp.B_SHR || Op == BinaryOp.B_XOR) && !CanBothConvertTo(intType, errorHandler))
            {
                canApply = false;
            }

            if (!canApply)
            {
                errorHandler.AddMessage(Severity.Error, $"Cannot apply operator {OpName(Op)} to operands of types {LHS.GetExprType(errorHandler)} ans {RHS.GetExprType(errorHandler)}", OpToken);
            }
        }