//If the new operand is the start of a bracketed term, continue evaluating with an accumulator to collect the terms inside the brackets. //Otherwise, apply the multiplication/division operation now to ensure left evaluation. private static float evaluate(float f, ProportionalOp op, List<object> tail){ if(tail[0].GetType()==typeof(OpenBracket)) return evaluate(f, op, new List<object>(), tail.GetRange(1, tail.Count-1)); return evaluate(MathResolver.resolve((MathOp)op, f, (float)tail[0]), tail.GetRange(1, tail.Count-1)); }
//If the new operand is the start of a bracketed term, continue evaluating with an accumulator to collect the terms inside the brackets. //Otherwise, apply the multiplication/division operation now to ensure left evaluation and continue evaluation with the incomplete addition/subtraction in tow. private static float evaluate(float f1, IncrementalOp op1, float f2, ProportionalOp op2, List<object> tail){ if(tail[0].GetType()==typeof(OpenBracket)) return evaluate(f1, op1, f2, op2, new List<object>(), tail.GetRange(1, tail.Count-1)); return evaluate(f1, op1, MathResolver.resolve((MathOp)op2, f2, (float)tail[0]), tail.GetRange(1, tail.Count-1)); }
//Accumulate all terms in acc unless they are the matching bracket from the start of the accumulator. //If the matching bracket is found, continue the evaluation using the evaluation of the bracketed term now fully contained in acc //as the right operand of the open multiplcation/division. Resolving this now ensures left evaluation is maintained. public static float evaluate(float f1, IncrementalOp op1, float f2, ProportionalOp op2, List<object> acc, List<object> tail, int brackets = 0){ if(tail[0].GetType()==typeof(CloseBracket)){ if(brackets==0) return evaluate(f1, op1, MathResolver.resolve(op2, f2, evaluate(acc)), tail.GetRange(1, tail.Count-1)); else{ acc.Add(tail[0]); return evaluate(f1, op1, f2, op2, acc, tail.GetRange(1, tail.Count-1), brackets-1); } }else if(tail[0].GetType()==typeof(OpenBracket)){ acc.Add(tail[0]); return evaluate(f1, op1, f2, op2, acc, tail.GetRange(1, tail.Count-1), brackets+1); } acc.Add(tail[0]); return evaluate(f1, op1, f2, op2, acc, tail.GetRange(1, tail.Count-1), brackets); }