public Expression(string textOfCurrentLine) { this.input = textOfCurrentLine; Tokens = new Tokens(input); ReturnMessage = new ShuntingYard().ConvertToPostFixed(Tokens.AsList()); if (ReturnMessage.Success == false) { failureMessage = ReturnMessage.Message; } else { PostFixedTokens = (List<Token>)ReturnMessage.ReturnValue; //Put postfixed Tokens into the tree foreach (Token t in PostFixedTokens) { ReturnMessage = ParseTree.AddToken(t); if (ReturnMessage.Success == false) { failureMessage = ReturnMessage.Message; } } } //Set the outputString if (ReturnMessage.Success) { NumericalEvaluation = ParseTree.SetRoot().Evaluate(); SystemLog.Add(NumericalEvaluation, LogObjectType.value); OutputString = NumericalEvaluation.ToString(); } else { NumericalEvaluation = null; SystemLog.Add(failureMessage, LogObjectType.failureMessage); OutputString = failureMessage; } }
public void SetCurrentLine(string line) { lineNumber++; currentLine = line; Value = null; if(currentLine.Count() > 0){ localExpression = new Expression(currentLine); if (localExpression.NumericalEvaluation != null) { Value = localExpression.NumericalEvaluation; OutputString = localExpression.OutputString; } else { OutputString = "\""+line+"\" " + localExpression.OutputString; } } }
static NumericalValue times(NumericalValue p1, NumericalValue p2) { return new NumericalValue(p1.AsDecimal() * p2.AsDecimal()); }
static NumericalValue plus(NumericalValue p1, NumericalValue p2) { return new NumericalValue(p1.AsDecimal() + p2.AsDecimal()); }
static NumericalValue power(NumericalValue p1, NumericalValue p2) { return new NumericalValue((decimal)Math.Pow((double)p1.AsDecimal(), (double)p2.AsDecimal())); //TODO: Handle all overflow exceptions //TODO: Catch overflow exceptions //Get a mathematics library that can do computations without overflow or divide by zero //Report to UI or save the value without evaluating }
static NumericalValue dividedBy(NumericalValue p1, NumericalValue p2) { return new NumericalValue(p1.AsDecimal() / p2.AsDecimal()); }
public override void SetValue(LinkedList<Node> childrenToAdd) { if (childrenToAdd.Count != 2) throw new Exception("A two parameter operator must have two parameters"); value = InfixOperators.GetOpInfo[this.op].Compute (childrenToAdd.Last().GetValue(), childrenToAdd.First().GetValue()); Children = childrenToAdd; }
public NumberNode(NumericalValue val) { this.value = val; }
public NumberNode(double val) { this.value = new NumericalValue(val); }
public NumberNode(decimal val) { this.value = new NumericalValue(val); }