/// <summary> /// Gets a Token of type NumberToken from the beginning of a line of text. /// </summary> /// <param name="line"></param> /// <returns></returns> public static Token ParseAsConstant(string line) { Token token = new NullToken(); if (NumberToken.__numberPattern.IsMatch(line)) { string matchText = NumberToken.__numberPattern.Matches(line)[0].Value; token = new NumberToken(matchText, "$"); } return(token); }
/// <summary> /// Gets a Token of type NumberToken from the beginning of a line of text. /// </summary> /// <param name="line"></param> /// <returns></returns> public new static Token Parse(string line) { Token token = new NullToken(); if (NumberToken.__numberPattern.IsMatch(line)) { string matchText = NumberToken.__numberPattern.Matches(line)[0].Value; string value = AsDecimal(matchText); token = new NumberToken(value, matchText); } return(token); }
/// <summary> /// Returns the negation of this NumberToken. /// </summary> /// <returns></returns> public Token Negate() { Token result = this; if (ToString().StartsWith("-")) { result = new NumberToken(ToString().Substring(1), Representation); } else { result = new NumberToken(String.Concat("-", ToString()), Representation); } return(result); }
/// <summary> /// Evaluates this expression. /// </summary> /// <returns></returns> public override Token Evaluate() { Token result = new NullToken(); Token left = EvaluateOperand(1); Token right = EvaluateOperand(0); double dLeft = 0.0; double dRight = 0.0; if (double.TryParse(left.ToString(), out dLeft) && double.TryParse(right.ToString(), out dRight)) { switch (Precedence) { case ExpressionPrecedence.Add: result = new NumberToken((dLeft + dRight).ToString()); break; case ExpressionPrecedence.Subtract: result = new NumberToken((dLeft - dRight).ToString()); break; case ExpressionPrecedence.MultiplyOrDivide: if (Operation.ToString().Equals("/", StringComparison.InvariantCultureIgnoreCase)) { if (right.ToString().Equals("0", StringComparison.InvariantCultureIgnoreCase)) { throw new DivideByZeroException(); } else { result = new NumberToken((dLeft / dRight).ToString()); } } else if (Operation.ToString().Equals("*")) { result = new NumberToken((dLeft * dRight).ToString()); } break; } } else { throw new FormatException(String.Format("Could not parse double value from {0} for arithmetic expression.", left.ToString())); } return(result); }
/// <summary> /// Gets a Token of type LastResultToken from the beginning of a line of text. The resulting token /// needs to be a value token, like BooleanToken or NumberToken. /// </summary> /// <param name="line"></param> /// <returns></returns> public new static Token Parse(string line) { Token token = new NullToken(); if (LastResultToken.__constantPattern.IsMatch(line)) { string matchText = LastResultToken.__constantPattern.Matches(line)[0].Value; if (matchText.Equals("$", StringComparison.InvariantCultureIgnoreCase)) { token = new LastResultToken(CalculatorContext.GetInstance().GetLastResult(), matchText); Token intermediateToken = new NullToken(); if (NumberToken.TryParse(token.ToString(), out intermediateToken, true) || BooleanToken.TryParse(token.ToString(), out intermediateToken, true)) { token = intermediateToken; } } } return(token); }
/// <summary> /// Evaluates this expression. /// </summary> /// <returns></returns> public override Token Evaluate() { Token result = new NullToken(); string op = Operation.ToString(); bool modeIsDegrees = CalculatorContext.GetInstance()[CalculatorContext.Mode].ToString().Equals("deg", StringComparison.InvariantCultureIgnoreCase); if (Operation is PrefixFunctionToken) { Token val = EvaluateOperand(0); double res = 0.0; if (double.TryParse(val.ToString(), out res)) { if (op.Equals("abs", StringComparison.InvariantCultureIgnoreCase)) { result = new NumberToken((Math.Abs(res)).ToString()); } else if (op.Equals("cosh", StringComparison.InvariantCultureIgnoreCase)) { if (modeIsDegrees) { res = ToRadians(res); } res = Math.Cosh(res); result = new NumberToken(res.ToString()); } else if (op.Equals("sinh", StringComparison.InvariantCultureIgnoreCase)) { if (modeIsDegrees) { res = ToRadians(res); } res = Math.Sinh(res); result = new NumberToken(res.ToString()); } else if (op.Equals("tanh", StringComparison.InvariantCultureIgnoreCase)) { if (modeIsDegrees) { res = ToRadians(res); } res = Math.Tanh(res); result = new NumberToken(res.ToString()); } else if (op.Equals("cos", StringComparison.InvariantCultureIgnoreCase)) { if (modeIsDegrees) { res = ToRadians(res); } res = Math.Cos(res); result = new NumberToken(res.ToString()); } else if (op.Equals("sin", StringComparison.InvariantCultureIgnoreCase)) { if (modeIsDegrees) { res = ToRadians(res); } res = Math.Sin(res); result = new NumberToken(res.ToString()); } else if (op.Equals("tan", StringComparison.InvariantCultureIgnoreCase)) { if (modeIsDegrees) { res = ToRadians(res); } res = Math.Tan(res); result = new NumberToken(res.ToString()); } else if (op.Equals("acos", StringComparison.InvariantCultureIgnoreCase)) { res = modeIsDegrees ? ToDegrees(Math.Acos(res)) : Math.Acos(res); result = new NumberToken(res.ToString()); } else if (op.Equals("asin", StringComparison.InvariantCultureIgnoreCase)) { res = modeIsDegrees ? ToDegrees(Math.Asin(res)) : Math.Acos(res); result = new NumberToken(res.ToString()); } else if (op.Equals("atan", StringComparison.InvariantCultureIgnoreCase)) { res = modeIsDegrees ? ToDegrees(Math.Atan(res)) : Math.Acos(res); result = new NumberToken(res.ToString()); } else if (op.Equals("log", StringComparison.InvariantCultureIgnoreCase)) { result = new NumberToken((Math.Log10(res)).ToString()); } else if (op.Equals("ln", StringComparison.InvariantCultureIgnoreCase)) { result = new NumberToken((Math.Log(res)).ToString()); } else if (op.Equals("lb", StringComparison.InvariantCultureIgnoreCase) || op.Equals("ld", StringComparison.InvariantCultureIgnoreCase) || op.Equals("lg", StringComparison.InvariantCultureIgnoreCase)) { result = new NumberToken((Math.Log(res) / Math.Log(2.0)).ToString()); } else if (op.Equals("deg", StringComparison.InvariantCultureIgnoreCase)) { result = new NumberToken((ToDegrees(res)).ToString()); } else if (op.Equals("rad", StringComparison.InvariantCultureIgnoreCase)) { result = new NumberToken((ToRadians(res)).ToString()); } else if (op.Equals("sqrt", StringComparison.InvariantCultureIgnoreCase)) { result = new NumberToken((Math.Sqrt(res)).ToString()); } else if (op.Equals("too", StringComparison.InvariantCultureIgnoreCase)) { CalculatorContext.GetInstance()[CalculatorContext.DisplayBase] = new NumberToken("8"); result = new NumberToken(val); } else if (op.Equals("tod", StringComparison.InvariantCultureIgnoreCase)) { CalculatorContext.GetInstance()[CalculatorContext.DisplayBase] = new NumberToken("10"); result = new NumberToken(val); } else if (op.Equals("toh", StringComparison.InvariantCultureIgnoreCase)) { CalculatorContext.GetInstance()[CalculatorContext.DisplayBase] = new NumberToken("16"); result = new NumberToken(val); } else if (op.Equals("tob", StringComparison.InvariantCultureIgnoreCase)) { CalculatorContext.GetInstance()[CalculatorContext.DisplayBase] = new NumberToken("2"); result = new NumberToken(val); } else if (op.Equals("-", StringComparison.InvariantCultureIgnoreCase)) { result = new NumberToken((res * (-1)).ToString()); } else if (op.Equals("!", StringComparison.InvariantCultureIgnoreCase)) { if (val is BooleanToken) { result = ((BooleanToken)val).Not(); } else if (val is NumberToken) { result = ((NumberToken)val).Negate(); } } } else { throw new FormatException(String.Format("Could not parse double value from {0}", val)); } } else if (Operation is InfixFunctionToken) { Token left = EvaluateOperand(1); Token right = EvaluateOperand(0); double dLeft = 0.0; double dRight = 0.0; if (double.TryParse(left.ToString(), out dLeft) && double.TryParse(right.ToString(), out dRight)) { if (op.Equals("**", StringComparison.InvariantCultureIgnoreCase)) { result = new NumberToken((Math.Pow(dLeft, dRight)).ToString()); } else if (op.Equals("%", StringComparison.InvariantCultureIgnoreCase)) { result = new NumberToken((dLeft % dRight).ToString()); } else if (op.Equals("//", StringComparison.InvariantCultureIgnoreCase)) { if ((left is NumberToken && right is NumberToken)) { long lLeft = 0; long lRight = 0; if ((long.TryParse(((NumberToken)left).WholePart(), out lLeft) && long.TryParse(((NumberToken)right).WholePart(), out lRight))) { long rem = 0; long whole = Math.DivRem(lLeft, lRight, out rem); result = new NumberToken(whole.ToString()); } } else { throw new FormatException(String.Format("Could not parse long values from {0} or {1}.", left, right)); } } } else { throw new FormatException(String.Format("Could not parse double values from {0} or {1}.", left, right)); } } else if (Operation is PostfixFunctionToken) { Token val = EvaluateOperand(0); double res = 0.0; if (double.TryParse(val.ToString(), out res)) { if (op.Equals("!", StringComparison.InvariantCultureIgnoreCase)) { if (Operation is PostfixFunctionToken) { double counter = res; double total = counter; while (counter > 1) { counter--; total *= counter; } result = new NumberToken(total.ToString()); } } } else { throw new FormatException(String.Format("Could not parse double value from {0}", val)); } } else { throw new InvalidTokenException(String.Format("Operation {0} not currently supported. Please submit a feature request.", Operation)); } return(result); }
/// <summary> /// Gets the next token while in the set container token parse state. /// </summary> /// <param name="internalLine"></param> /// <param name="token"></param> /// <param name="state"></param> /// <returns></returns> public override bool GetNextToken(string internalLine, out Token token, out TokenParseState state) { bool result = false; Token lastToken = CalculatorContext.GetInstance()[CalculatorContext.LastToken]; if ((result = SetContainerToken.TryParse(internalLine, out token) && ((SetContainerToken)token).SetContainerType == ((SetContainerToken)lastToken).SetContainerType)) { state = new SetContainerTokenParseState(); } else if ((result = CommentToken.TryParse(internalLine, out token))) { state = new NullTokenParseState(); } else if (((SetContainerToken)lastToken).SetContainerType == SetContainerTokenType.Open) { if ((result = PrefixFunctionToken.TryParse(internalLine, out token))) { state = new PrefixFunctionTokenParseState(); } else if ((result = BooleanToken.TryParse(internalLine, out token) || ConstantToken.TryParse(internalLine, out token) || NumberToken.TryParse(internalLine, out token) || LastResultToken.TryParse(internalLine, out token))) { state = new ValueTokenParseState(); } else if ((result = VariableToken.TryParse(internalLine, out token))) { state = new VariableTokenParseState(); } else { state = new InvalidTokenParseState(); } } else if ((result = InfixFunctionToken.TryParse(internalLine, out token) || ArithmeticOperatorToken.TryParse(internalLine, out token) || BinaryOperatorToken.TryParse(internalLine, out token) || BooleanOperatorToken.TryParse(internalLine, out token))) { state = new InfixOperationParseState(); } else if ((result = PostfixFunctionToken.TryParse(internalLine, out token))) { state = new PostfixFunctionParseState(); } else { state = new InvalidTokenParseState(); } return(result); }
/// <summary> /// Evaluates this expression. /// </summary> /// <returns></returns> public override Token Evaluate() { Token result = new NullToken(); Token left = EvaluateOperand(1); Token right = EvaluateOperand(0); if (left is NumberToken && right is NumberToken) { long lLeft = 0;; long lRight = 0;; if (long.TryParse(((NumberToken)left).WholePart(), out lLeft) && long.TryParse(((NumberToken)right).WholePart(), out lRight)) { if (Operation.ToString().Equals("&", StringComparison.InvariantCultureIgnoreCase)) { result = new NumberToken((lLeft & lRight).ToString()); } else if (Operation.ToString().Equals("|", StringComparison.InvariantCultureIgnoreCase)) { result = new NumberToken((lLeft | lRight).ToString()); } else if (Operation.ToString().Equals("^", StringComparison.InvariantCultureIgnoreCase)) { result = new NumberToken((lLeft ^ lRight).ToString()); } else { throw new FormatException(String.Format("Could not parse long value from {0} for binary expression.", left.ToString())); } } } else if (left is BooleanToken && right is BooleanToken) { bool bLeft = false; bool bRight = false; if (bool.TryParse(left.ToString(), out bLeft) && bool.TryParse(right.ToString(), out bRight)) { if (Operation.ToString().Equals("&", StringComparison.InvariantCultureIgnoreCase)) { result = new BooleanToken((bLeft & bRight).ToString()); } else if (Operation.ToString().Equals("|", StringComparison.InvariantCultureIgnoreCase)) { result = new BooleanToken((bLeft | bRight).ToString()); } else if (Operation.ToString().Equals("^", StringComparison.InvariantCultureIgnoreCase)) { result = new BooleanToken((bLeft ^ bRight).ToString()); } else { throw new FormatException(String.Format("Could not parse bool value from {0} for binary expression.", left.ToString())); } } } else { throw new MalformedExpressionException(String.Concat(ToString(), " is not a valid binary expression.")); } return(result); }
/// <summary> /// Gets the next token while within an infix operation parse state. /// </summary> /// <param name="internalLine"></param> /// <param name="token"></param> /// <param name="state"></param> /// <returns></returns> public override bool GetNextToken(string internalLine, out Token token, out TokenParseState state) { bool result = false; if ((result = PrefixFunctionToken.TryParse(internalLine, out token))) { state = new PrefixFunctionTokenParseState(); } else if ((result = BooleanToken.TryParse(internalLine, out token) || ConstantToken.TryParse(internalLine, out token) || NumberToken.TryParse(internalLine, out token) || LastResultToken.TryParse(internalLine, out token))) { state = new ValueTokenParseState(); } else if ((result = VariableToken.TryParse(internalLine, out token))) { state = new VariableTokenParseState(); } else if ((result = (PerenthesisToken.TryParse(internalLine, out token) && ((PerenthesisToken)token).PerenthesisType == PerenthesisTokenType.Open))) { state = new PerenthesisTokenParseState(); } else if ((result = CommentToken.TryParse(internalLine, out token))) { state = new NullTokenParseState(); } else { state = new InvalidTokenParseState(); } return(result); }
/// <summary> /// Gets the next token while within ta prefix function token parse state. /// </summary> /// <param name="internalLine"></param> /// <param name="token"></param> /// <param name="state"></param> /// <returns></returns> public override bool GetNextToken(string internalLine, out Token token, out TokenParseState state) { bool result = false; Token lastToken = CalculatorContext.GetInstance()[CalculatorContext.LastToken]; if ((result = (PerenthesisToken.TryParse(internalLine, out token) && ((PerenthesisToken)token).PerenthesisType == PerenthesisTokenType.Open))) { state = new PerenthesisTokenParseState(); } else if (lastToken.ToString().Equals("!", StringComparison.InvariantCultureIgnoreCase)) { if ((result = BooleanToken.TryParse(internalLine, out token) || ConstantToken.TryParse(internalLine, out token) || NumberToken.TryParse(internalLine, out token))) { state = new ValueTokenParseState(); } else if ((result = VariableToken.TryParse(internalLine, out token))) { state = new VariableTokenParseState(); } else { state = new InvalidTokenParseState(); } } else { state = new InvalidTokenParseState(); } return(result); }
/// <summary> /// Gets the next Token if the last token was a NullToken. It first tries to get a normal, /// valid first token. If that fails, then it checks for a normal secondary token, and if /// that passes then the context LastResultIsImplied is set to true. /// </summary> /// <param name="internalLine"></param> /// <param name="token"></param> /// <returns></returns> public override bool GetNextToken(string internalLine, out Token token, out TokenParseState state) { bool result = false; if ((result = PerenthesisToken.TryParse(internalLine, out token))) { state = new PerenthesisTokenParseState(); } else if ((result = PrefixFunctionToken.TryParse(internalLine, out token))) { state = new PrefixFunctionTokenParseState(); } else if ((result = BooleanToken.TryParse(internalLine, out token) || ConstantToken.TryParse(internalLine, out token) || NumberToken.TryParse(internalLine, out token) || LastResultToken.TryParse(internalLine, out token))) { state = new ValueTokenParseState(); } else if ((result = VariableToken.TryParse(internalLine, out token))) { state = new VariableTokenParseState(); } else if ((result = InfixFunctionToken.TryParse(internalLine, out token) || ArithmeticOperatorToken.TryParse(internalLine, out token) || BinaryOperatorToken.TryParse(internalLine, out token) || BooleanOperatorToken.TryParse(internalLine, out token))) { state = new InfixOperationParseState(); CalculatorContext.GetInstance().LastResultIsImplied = true; } else if ((result = PostfixFunctionToken.TryParse(internalLine, out token))) { state = new PostfixFunctionParseState(); CalculatorContext.GetInstance().LastResultIsImplied = true; } else { state = new InvalidTokenParseState(); } return(result); }