private static object SubtractTypes(long leftValueType, long rightValueType, object leftValue, object rightValue) { if (leftValueType == LexemeType.NUMBER && rightValueType == LexemeType.NUMBER) { var literalSum = new Expression.LexemeTypeLiteral(); literalSum.literal = Convert.ToInt32(leftValue) - Convert.ToInt32(rightValue); literalSum.lexemeType = LexemeType.NUMBER; return(literalSum); } if (leftValueType == LexemeType.STRING && rightValueType == LexemeType.STRING) { throw new ArgumentException("can't subtract strings"); } throw new ArgumentNullException("can't subtract types"); }
private static object MultiplyTypes(long leftValueType, long rightValueType, object leftValue, object rightValue) { if (leftValueType == LexemeType.NUMBER && rightValueType == LexemeType.NUMBER) { var literalProduction = new Expression.LexemeTypeLiteral(); literalProduction.literal = Convert.ToInt32(leftValue) * Convert.ToInt32(rightValue); literalProduction.lexemeType = LexemeType.NUMBER; return(literalProduction); } if (leftValueType == LexemeType.STRING && rightValueType == LexemeType.STRING) { throw new ArgumentException("can't multiply strings"); } throw new ArgumentNullException("can't multiply types"); }
private static object IsLessThan(long leftValueType, long rightValueType, object leftValue, object rightValue) { if (leftValueType == LexemeType.NUMBER && rightValueType == LexemeType.NUMBER) { var literal = new Expression.LexemeTypeLiteral(); literal.literal = Convert.ToInt32(leftValue) < Convert.ToInt32(rightValue); literal.lexemeType = LexemeType.BOOL; return(literal); } if (leftValueType == LexemeType.STRING || rightValueType == LexemeType.STRING) { throw new ArgumentException("can't apply less than operator to strings"); } throw new ArgumentException("Can't compare types"); }
private static object DivideTypes(long leftValueType, long rightValueType, object leftValue, object rightValue) { if (leftValueType == LexemeType.NUMBER && rightValueType == LexemeType.NUMBER) { var literalProduction = new Expression.LexemeTypeLiteral(); int divisor = Convert.ToInt32(leftValue); int divident = Convert.ToInt32(rightValue); if (divisor == 0 || divident == 0) { throw new ArgumentException("Can't divide by zero"); } literalProduction.literal = Convert.ToInt32(leftValue) / Convert.ToInt32(rightValue); literalProduction.lexemeType = LexemeType.NUMBER; return(literalProduction); } if (leftValueType == LexemeType.STRING && rightValueType == LexemeType.STRING) { throw new ArgumentException("can't divide strings"); } throw new ArgumentNullException("can't divide types"); }