/// <summary> /// Combines two types to create a new one /// </summary> /// <param name="right"></param> /// <param name="Operator"></param> /// <returns></returns> public override Types.Type CombineType(Types.Type right, Interpreter.BinaryExpression.OPERATOR Operator) { Types.Type retVal = null; Function function = right as Function; if (function != null) { if (this.ReturnType == function.ReturnType) { if (this.FormalParameters.Count >= function.FormalParameters.Count) { retVal = this; } else { retVal = function; } } else { AddError("Cannot combine types " + this.ReturnType.Name + " and " + function.ReturnType.Name); } } else if (right.IsDouble()) { retVal = this; } else { AddError("Cannot combine types " + this.ReturnType.Name + " and " + right.Name); } return(retVal); }
/// <summary> /// Provides the segments of the graph which satisfy the operator with the value provided /// </summary> /// <param name="Operator">The operator to apply</param> /// <param name="value">The value to compare with the values of the graph</param> /// <returns></returns> public List <Segment> GetSegments(Interpreter.BinaryExpression.OPERATOR Operator, double value) { List <Segment> retVal = new List <Segment>(); foreach (Segment segment in Segments) { if (Segment.Curve.isFlat(segment.Expression)) { switch (Operator) { case Interpreter.BinaryExpression.OPERATOR.GREATER: if (segment.Expression.v0 > value) { retVal.Add(segment); } break; case Interpreter.BinaryExpression.OPERATOR.GREATER_OR_EQUAL: if (segment.Expression.v0 >= value) { retVal.Add(segment); } break; case Interpreter.BinaryExpression.OPERATOR.LESS_OR_EQUAL: if (segment.Expression.v0 <= value) { retVal.Add(segment); } break; case Interpreter.BinaryExpression.OPERATOR.LESS: if (segment.Expression.v0 < value) { retVal.Add(segment); } break; case Interpreter.BinaryExpression.OPERATOR.EQUAL: if (segment.Expression.v0 == value) { retVal.Add(segment); } break; default: throw new Exception("Invalid operator " + Operator + " for segment comparison"); } } else { throw new Exception("Non flat segments cannot be checked for " + Operator + " against " + value.ToString()); } } return(retVal); }
/// <summary> /// Performs the arithmetic operation based on the type of the result /// </summary> /// <param name="context">The context used to perform this operation</param> /// <param name="left"></param> /// <param name="Operation"></param> /// <param name="right"></param> /// <returns></returns> public override Values.IValue PerformArithmericOperation(Interpreter.InterpretationContext context, Values.IValue left, Interpreter.BinaryExpression.OPERATOR Operation, Values.IValue right) // left +/-/*/div/exp right { Values.DoubleValue retVal = null; double double1 = getValue(left); double double2 = getValue(right); switch (Operation) { case Interpreter.BinaryExpression.OPERATOR.EXP: retVal = new Values.DoubleValue(this, Math.Pow(double1, double2)); break; case Interpreter.BinaryExpression.OPERATOR.MULT: retVal = new Values.DoubleValue(this, (double1 * double2)); break; case Interpreter.BinaryExpression.OPERATOR.DIV: if (double2 == 0) { throw new Exception("Division by zero"); } else { retVal = new Values.DoubleValue(this, (double1 / double2)); } break; case Interpreter.BinaryExpression.OPERATOR.ADD: retVal = new Values.DoubleValue(this, (double1 + double2)); break; case Interpreter.BinaryExpression.OPERATOR.SUB: retVal = new Values.DoubleValue(this, (double1 - double2)); break; } return(retVal); }
/// <summary> /// Performs the arithmetic operation based on the type of the result /// </summary> /// <param name="context">The context used to perform this operation</param> /// <param name="left"></param> /// <param name="Operation"></param> /// <param name="right"></param> /// <returns></returns> public override Values.IValue PerformArithmericOperation(Interpreter.InterpretationContext context, Values.IValue left, Interpreter.BinaryExpression.OPERATOR Operation, Values.IValue right) // left +/-/*/div/exp right { Values.IValue retVal = null; Constants.EnumValue enumValue = left as Constants.EnumValue; if (enumValue != null) { left = enumValue.Value; } enumValue = right as Constants.EnumValue; if (enumValue != null) { right = enumValue.Value; } Values.IntValue int1 = left as Values.IntValue; Values.IntValue int2 = right as Values.IntValue; if (int1 == null || int2 == null) { retVal = EFSSystem.DoubleType.PerformArithmericOperation(context, left, Operation, right); } else { retVal = EFSSystem.IntegerType.PerformArithmericOperation(context, left, Operation, right); } return(retVal); }
/// <summary> /// Performs the arithmetic operation based on the type of the result /// </summary> /// <param name="context">The context used to perform this operation</param> /// <param name="left"></param> /// <param name="Operation"></param> /// <param name="right"></param> /// <returns></returns> public override Values.IValue PerformArithmericOperation(Interpreter.InterpretationContext context, Values.IValue left, Interpreter.BinaryExpression.OPERATOR Operation, Values.IValue right) // left +/-/*/div/exp right { Values.IntValue retVal = null; Values.IntValue int1 = left as Values.IntValue; Values.IntValue int2 = right as Values.IntValue; switch (Operation) { case Interpreter.BinaryExpression.OPERATOR.EXP: retVal = new Values.IntValue(EFSSystem.IntegerType, (Decimal)Math.Pow((double)int1.Val, (double)int2.Val)); break; case Interpreter.BinaryExpression.OPERATOR.MULT: retVal = new Values.IntValue(EFSSystem.IntegerType, (int1.Val * int2.Val)); break; case Interpreter.BinaryExpression.OPERATOR.DIV: if (int2.Val == 0) { throw new Exception("Division by zero"); } else { retVal = new Values.IntValue(EFSSystem.IntegerType, (int1.Val / int2.Val)); } break; case Interpreter.BinaryExpression.OPERATOR.ADD: retVal = new Values.IntValue(EFSSystem.IntegerType, (int1.Val + int2.Val)); break; case Interpreter.BinaryExpression.OPERATOR.SUB: retVal = new Values.IntValue(EFSSystem.IntegerType, (int1.Val - int2.Val)); break; } return(retVal); }