public void CanAddIntegers() { var scalar1 = ScalarValue.Create(1); var scalar2 = ScalarValue.Create(1); var op = new OperandPair(scalar1, scalar2); var result = calc.Add(op); Assert.IsInstanceOf<ScalarValue>(result); var expected = scalar1 + scalar2; Assert.AreEqual(expected, result); }
public void CanEqualsPIDLoop() { var pidLoop1 = new PIDLoop(); var pidLoop2 = new PIDLoop(); var op = new OperandPair(pidLoop1, pidLoop2); var result = calc.Equal(op); Assert.IsFalse((bool)result); op = new OperandPair(pidLoop1, pidLoop1); result = calc.Equal(op); Assert.IsTrue((bool)result); }
public void CanAddIntegers() { var scalar1 = ScalarValue.Create(1); var scalar2 = ScalarValue.Create(1); var op = new OperandPair(scalar1, scalar2); var result = calc.Add(op); Assert.IsInstanceOf <ScalarValue>(result); var expected = scalar1 + scalar2; Assert.AreEqual(expected, result); }
public override object Divide(OperandPair pair) { CheckPairForNull(pair, "Divide"); object result; if (TryInvokeExplicit(pair, "op_Division", out result)) { return result; } OperandPair resultPair; if (TryCoerceImplicit(pair, out resultPair)) { return Divide(resultPair); } throw new KOSException(GetMessage("/", pair)); }
public override object Add(OperandPair pair) { CheckPairForNull(pair, "Add"); object result; if (TryInvokeExplicit(pair, "op_Addition", out result)) { return result; } OperandPair resultPair; if (TryCoerceImplicit(pair, out resultPair)) { return Add(resultPair); } throw new KOSException(GetMessage("+", pair)); }
public override object GreaterThan(OperandPair pair) { CheckPairForNull(pair, "GreaterThan"); object result; if (TryInvokeExplicit(pair, "op_GreaterThan", out result)) { return result; } OperandPair resultPair; if (TryCoerceImplicit(pair, out resultPair)) { return GreaterThan(resultPair); } throw new KOSException(GetMessage(">", pair)); }
public override object Equal(OperandPair pair) { CheckPairForNull(pair, "Equal"); object result; if (TryInvokeExplicit(pair, "op_Equality", out result)) { return result; } OperandPair resultPair; if (TryCoerceImplicit(pair, out resultPair)) { return Equal(pair); } return pair.Left.Equals(pair.Right); }
public static Calculator GetCalculator(OperandPair operandPair) { var scalarCount = 0; var stringCount = 0; var specialCount = 0; var boolCount = 0; if (operandPair.Left is ScalarValue) scalarCount++; if (operandPair.Left is StringValue) stringCount++; if (operandPair.Left is ISuffixed) specialCount++; if (operandPair.Left is BooleanValue) boolCount++; if (operandPair.Right is ScalarValue) scalarCount++; if (operandPair.Right is StringValue) stringCount++; if (operandPair.Right is ISuffixed) specialCount++; if (operandPair.Right is BooleanValue) boolCount++; if (scalarCount == 2) return new CalculatorScalar(); if (stringCount > 0) return new CalculatorString(); if (boolCount > 0) return new CalculatorBool(); if (specialCount > 0) return new CalculatorStructure(); throw new NotImplementedException(string.Format("Can't operate types {0} and {1}", operandPair.Left.GetType(), operandPair.Right.GetType())); }
/// <summary> /// Describe the error in terms of the two operands and the verb/preposition /// being done with them. For example: /// </summary> /// <param name="pair">the left and right objects of the preposition</param> /// <param name="verb">present-tense singular conjugation of the operation's verb, i.e "add"</param> /// <param name="preposition">preposition usually used with the verb, i.e you add "to", but divide "by".</param> public KOSBinaryOperandTypeException(OperandPair pair, string verb, string preposition) : base(String.Format(TERSE_MSG_FMT, verb, pair.Left.GetType().Name, preposition, pair.Right.GetType().Name)) { }
public abstract object Subtract(OperandPair pair);
public abstract object NotEqual(OperandPair pair);
public abstract object LessThanEqual(OperandPair pair);
public abstract object Divide(OperandPair pair);
public override object NotEqual(OperandPair pair) { return !string.Equals(pair.Left.ToString(), pair.Right.ToString(), StringComparison.OrdinalIgnoreCase); }
public override object LessThanEqual(OperandPair pair) { int compareNum = string.Compare(pair.Left.ToString(), pair.Right.ToString(), StringComparison.OrdinalIgnoreCase); return compareNum <= 0; }
public override object GreaterThan(OperandPair pair) { int compareNum = string.Compare(pair.Left.ToString(), pair.Right.ToString(), StringComparison.OrdinalIgnoreCase); return compareNum > 0; }
public override object Add(OperandPair pair) { return new StringValue(string.Concat(pair.Left, pair.Right)); }
public override object Power(OperandPair pair) { throw new KOSBinaryOperandTypeException(pair, "exponentiate", "by"); }
public override object Subtract(OperandPair pair) { throw new KOSBinaryOperandTypeException(pair, "subtract", "from"); }
public abstract object Add(OperandPair pair);
public override object Add(OperandPair pair) { throw new KOSBinaryOperandTypeException(pair, "add", "to"); }
public abstract object GreaterThanEqual(OperandPair pair);
public override object Divide(OperandPair pair) { throw new KOSBinaryOperandTypeException(pair, "divide", "by"); }
public abstract object Multiply(OperandPair pair);
public override object GreaterThan(OperandPair pair) { throw new KOSBinaryOperandTypeException(pair, "ordinate", ">"); }
public abstract object Power(OperandPair pair);
public override object Multiply(OperandPair pair) { throw new KOSBinaryOperandTypeException(pair, "multiply", "by"); }
public override void Execute(ICpu cpu) { object right = cpu.PopValue(); object left = cpu.PopValue(); var operands = new OperandPair(left, right); Calculator calc = Calculator.GetCalculator(operands); Operands = operands; object result = ExecuteCalculation(calc); cpu.PushStack(result); }
public override object NotEqual(OperandPair pair) { return new BooleanValue(Convert.ToBoolean(pair.Left) != Convert.ToBoolean(pair.Right)); }
public override object LessThanEqual(OperandPair pair) { throw new KOSBinaryOperandTypeException(pair, "ordinate", "<="); }
private bool TryInvokeExplicit(OperandPair pair, string methodName, out object result) { MethodInfo method1 = pair.LeftType.GetMethod(methodName, FLAGS, null, new[] { pair.LeftType, pair.RightType }, null); if (method1 != null) { result = method1.Invoke(null, new[] {pair.Left, pair.Right}); return true; } MethodInfo method2 = pair.RightType.GetMethod(methodName, FLAGS, null, new[] { pair.LeftType, pair.RightType }, null); if (method2 != null) { result = method2.Invoke(null, new[] {pair.Left, pair.Right}); return true; } result = null; return false; }