using System;
-namespace Mono.CSharp {
+namespace Mono.MonoBASIC {
public class ConstantFold {
if (right is EnumConstant)
right = ((EnumConstant) right).Child;
}
-
+
+ Type wrap_as;
+ Constant result = null;
switch (oper){
case Binary.Operator.BitwiseOr:
DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
break;
case Binary.Operator.Addition:
- Constant result;
bool left_is_string = left is StringConstant;
bool right_is_string = right is StringConstant;
// one is a string, and the other is not, then defer
// to runtime concatenation
//
+ wrap_as = null;
if (left_is_string || right_is_string){
if (left_is_string && right_is_string)
return new StringConstant (
//
// note that E operator + (E x, E y) is invalid
//
- Type wrap_as = null;
if (left is EnumConstant){
if (right is EnumConstant){
return null;
return result;
case Binary.Operator.Subtraction:
+ //
+ // handle "E operator - (E x, U y)"
+ // handle "E operator - (Y y, E x)"
+ // handle "U operator - (E x, E y)"
+ //
+ wrap_as = null;
+ if (left is EnumConstant){
+ if (right is EnumConstant){
+ if (left.Type == right.Type)
+ wrap_as = TypeManager.EnumToUnderlying (left.Type);
+ else
+ return null;
+ }
+ if (((EnumConstant) left).Child.Type != right.Type)
+ return null;
+
+ wrap_as = left.Type;
+ } else if (right is EnumConstant){
+ if (((EnumConstant) right).Child.Type != left.Type)
+ return null;
+ wrap_as = right.Type;
+ }
+
DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);
if (left == null || right == null)
return null;
res = unchecked (((DoubleConstant) left).Value -
((DoubleConstant) right).Value);
- return new DoubleConstant (res);
+ result = new DoubleConstant (res);
} else if (left is FloatConstant){
float res;
res = unchecked (((FloatConstant) left).Value -
((FloatConstant) right).Value);
- return new FloatConstant (res);
+ result = new FloatConstant (res);
} else if (left is ULongConstant){
ulong res;
res = unchecked (((ULongConstant) left).Value -
((ULongConstant) right).Value);
- return new ULongConstant (res);
+ result = new ULongConstant (res);
} else if (left is LongConstant){
long res;
res = unchecked (((LongConstant) left).Value -
((LongConstant) right).Value);
- return new LongConstant (res);
+ result = new LongConstant (res);
} else if (left is UIntConstant){
uint res;
res = unchecked (((UIntConstant) left).Value -
((UIntConstant) right).Value);
- return new UIntConstant (res);
+ result = new UIntConstant (res);
} else if (left is IntConstant){
int res;
res = unchecked (((IntConstant) left).Value -
((IntConstant) right).Value);
- return new IntConstant (res);
+ result = new IntConstant (res);
} else {
throw new Exception ( "Unexepected input: " + left);
}
} catch (OverflowException){
Error_CompileTimeOverflow (loc);
}
- break;
+ if (wrap_as != null)
+ return new EnumConstant (result, wrap_as);
+ else
+ return result;
case Binary.Operator.Multiply:
DoConstantNumericPromotions (ec, oper, ref left, ref right, loc);