private void ProcessValue(LinkedList <LexemeContainer> list, Func <Lexeme, double, double, double> processFunc, params Lexeme[] lexemes) { var current = list.First; while (true) { var value = current.Value; if (!lexemes.Contains(value.Lexeme)) { current = current.Next; if (current == null) { break; } continue; } var rvalue = GetOperandValue(current.Previous.Value); var lvalue = GetOperandValue(current.Next.Value); var result = processFunc(value.Lexeme, rvalue, lvalue); var newValue = new LexemeContainer(Lexeme.DOUBLE, result.ToString()); var prev = current.Previous.Previous; var next = current.Next.Next; var linkedList = current.List; linkedList.Remove(current.Next); linkedList.Remove(current.Previous); linkedList.Remove(current); if (prev != null) { linkedList.AddAfter(prev, newValue); } else { linkedList.AddFirst(newValue); } current = next; if (current == null) { break; } } }
private double GetOperandValue(LexemeContainer lexeme) { if (lexeme.Lexeme == Lexeme.VAR) { if (_variables.TryGetValue(lexeme.Value, out var value)) { return(value); } return(0); } if (lexeme.Lexeme == Lexeme.INT) { return(Convert.ToInt32(lexeme.Value, 8)); } if (lexeme.Lexeme == Lexeme.DOUBLE) { return(double.Parse(lexeme.Value)); } if (lexeme.Lexeme == Lexeme.SIN) { var contentValue = GetOperandValue(lexeme.FuncContent); return(Math.Sin(contentValue)); } if (lexeme.Lexeme == Lexeme.COS) { var contentValue = GetOperandValue(lexeme.FuncContent); return(Math.Cos(contentValue)); } if (lexeme.Lexeme == Lexeme.ABS) { var contentValue = GetOperandValue(lexeme.FuncContent); return(Math.Abs(contentValue)); } throw new Exception("Can't get operand value for lexeme " + lexeme.Lexeme); }