public object Evaluate(string expression, CalcMode calMode, IntegerBits intBits, IntegerFormat intFmt) { Expression.CurCalcMode = calMode; Expression.CurIntBits = intBits; Expression.CurIntFmt = intFmt; var x = _cache != null ? _cache[expression] : Parse(expression); return Token.FormatValue(x.Evaluate(), calMode, intBits, intFmt); }
public Object Eva(String str, CalcMode calcMode, IntegerFormat fmt, IntegerBits numBits, bool updateAnswer) { Int64 result = 0; if (str.Length < 1) return false; try { m_numFmt = fmt; switch (fmt) { case IntegerFormat.DEC: Object obj = m_calcEngine.Evaluate(str, calcMode, numBits, fmt); if (obj != null && updateAnswer) { if (m_calcEngine.Variables.ContainsKey("ans")) UpdateVariable("ans", obj); else AddVariable("ans", obj); } return obj; case IntegerFormat.HEX: return Int64.Parse(str, System.Globalization.NumberStyles.HexNumber); case IntegerFormat.BIN: if (ParseBinStr(str, ref result)) return result; else throw new ArgumentException(); default: return null; } } catch (Exception _ex) { throw _ex; } }
public static object FormatValue(object val, CalcMode calcMode, IntegerBits intBits, IntegerFormat intFmt) { double v; if (val is string) return val; if (val is double) { v = (double)val; } // handle booleans if (val is bool) { v = 1.0; } // handle nulls if (val == null) { v = 0; } // handle everything else v = (double)Convert.ChangeType(val, typeof(double)); if (calcMode == CalcMode.FLOAT) { return (double)v; } else if (calcMode == CalcMode.INTEGER_SIGNED) { if (intBits == IntegerBits.BITS_64) { return (Int64)v; } else if (intBits == IntegerBits.BITS_32) { return (Int32)v; } else if (intBits == IntegerBits.BITS_16) { return (Int16)v; } else if (intBits == IntegerBits.BITS_8) { return (SByte)v; } else { throw new Exception("Invalid Integer Bits"); } } else if (calcMode == CalcMode.INTEGER_UNSIGNED) { if (intBits == IntegerBits.BITS_64) { return (UInt64)v; } else if (intBits == IntegerBits.BITS_32) { return (UInt32)v; } else if (intBits == IntegerBits.BITS_16) { return (UInt16)v; } else if (intBits == IntegerBits.BITS_8) { return (Byte)v; } else { throw new Exception("Invalid Integer Bits"); } } else { throw new Exception("Invalid Calculate Mode"); } }