/// <summary> /// Evaluates a math expression of 2 time spans. /// </summary> /// <param name="node">The AST node the evaluation is a part of.</param> /// <param name="lhs">The time on the left hand side</param> /// <param name="rhs">The time on the right hand side</param> /// <param name="op">The math operator.</param> /// <returns></returns> public static LBool CompareUnits(AstNode node, LUnit lhs, LUnit rhs, Operator op) { var result = false; if (op == Operator.LessThan) { result = lhs.BaseValue < rhs.BaseValue; } else if (op == Operator.LessThanEqual) { result = lhs.BaseValue <= rhs.BaseValue; } else if (op == Operator.MoreThan) { result = lhs.BaseValue > rhs.BaseValue; } else if (op == Operator.MoreThanEqual) { result = lhs.BaseValue >= rhs.BaseValue; } else if (op == Operator.EqualEqual) { result = lhs.BaseValue == rhs.BaseValue; } else if (op == Operator.NotEqual) { result = lhs.BaseValue != rhs.BaseValue; } return(new LBool(result)); }
/// <summary> /// Evaluates a math expression of 2 units. /// </summary> /// <param name="node">The AST node the evaluation is a part of.</param> /// <param name="left">The unit on the left</param> /// <param name="right">The unit on the right</param> /// <param name="op">The math operator</param> /// <param name="units"></param> /// <returns></returns> public static LObject CalcUnits(AstNode node, LUnit left, LUnit right, Operator op, Units units) { double baseUnitsValue = 0; if (op == Operator.Multiply) { baseUnitsValue = left.BaseValue * right.BaseValue; } else if (op == Operator.Divide) { baseUnitsValue = left.BaseValue / right.BaseValue; } else if (op == Operator.Add) { baseUnitsValue = left.BaseValue + right.BaseValue; } else if (op == Operator.Subtract) { baseUnitsValue = left.BaseValue - right.BaseValue; } else if (op == Operator.Modulus) { baseUnitsValue = left.BaseValue % right.BaseValue; } var relativeValue = units.ConvertToRelativeValue(baseUnitsValue, left.SubGroup, null); var result = new LUnit(); result.BaseValue = baseUnitsValue; result.Group = left.Group; result.SubGroup = left.SubGroup; result.Value = relativeValue; return(new LClass(result)); }
private static void ValidateUnits(LUnit u1, LUnit u2) { // Validate. if (u1 == null || u2 == null) { throw new ArgumentException("Can not add units when 1 unit is empty"); } // Check for matching groups e.g. length + length or weight + weight. if (u1.Group != u2.Group) { throw new ArgumentException("Can not add units " + u1.Group + " to " + u2.Group); } }
/// <summary> /// Add 2 unites together. /// </summary> /// <param name="u1"></param> /// <param name="u2"></param> /// <returns></returns> public static LUnit AddUnits(LUnit u1, LUnit u2) { // Validate LangTypeHelper.ValidateUnits(u1, u2); // Now convert the values to their base value. double totalBase = u1.BaseValue + u2.BaseValue; var unit = new LUnit(totalBase); unit.BaseValue = totalBase; unit.Group = u1.Group; unit.SubGroup = u1.SubGroup; // Set the value to the converted relative value // e.g. if u1.subgroup = feet and u2.subgroup == inches. // then multiply result.basevalue by the conversion value. // Now convert the units return(unit); }