|
| 1 | +using Rubberduck.Parsing.Grammar; |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Diagnostics; |
| 5 | + |
| 6 | +namespace Rubberduck.Inspections.Concrete.UnreachableCaseInspection |
| 7 | +{ |
| 8 | + public interface IParseTreeExpressionEvaluator |
| 9 | + { |
| 10 | + IParseTreeValue Evaluate(IParseTreeValue LHS, IParseTreeValue RHS, string opSymbol); |
| 11 | + IParseTreeValue Evaluate(IParseTreeValue LHS, string opSymbol, string requestedResultType); |
| 12 | + } |
| 13 | + |
| 14 | + public class ParseTreeExpressionEvaluator : IParseTreeExpressionEvaluator |
| 15 | + { |
| 16 | + private readonly IParseTreeValueFactory _valueFactory; |
| 17 | + |
| 18 | + private static Dictionary<string, Func<double, double, double>> MathOpsBinary = new Dictionary<string, Func<double, double, double>>() |
| 19 | + { |
| 20 | + [MathSymbols.MULTIPLY] = delegate (double LHS, double RHS) { return LHS * RHS; }, |
| 21 | + [MathSymbols.DIVIDE] = delegate (double LHS, double RHS) { return LHS / RHS; }, |
| 22 | + [MathSymbols.INTEGER_DIVIDE] = delegate (double LHS, double RHS) { return Math.Truncate(Convert.ToDouble(Convert.ToInt64(LHS) / Convert.ToInt64(RHS))); }, |
| 23 | + [MathSymbols.PLUS] = delegate (double LHS, double RHS) { return LHS + RHS; }, |
| 24 | + [MathSymbols.MINUS] = delegate (double LHS, double RHS) { return LHS - RHS; }, |
| 25 | + [MathSymbols.EXPONENT] = Math.Pow, |
| 26 | + [MathSymbols.MODULO] = delegate (double LHS, double RHS) { return LHS % RHS; }, |
| 27 | + }; |
| 28 | + |
| 29 | + private static Dictionary<string, Func<double, double, bool>> LogicOpsBinary = new Dictionary<string, Func<double, double, bool>>() |
| 30 | + { |
| 31 | + [LogicSymbols.EQ] = delegate (double LHS, double RHS) { return LHS == RHS; }, |
| 32 | + [LogicSymbols.NEQ] = delegate (double LHS, double RHS) { return LHS != RHS; }, |
| 33 | + [LogicSymbols.LT] = delegate (double LHS, double RHS) { return LHS < RHS; }, |
| 34 | + [LogicSymbols.LTE] = delegate (double LHS, double RHS) { return LHS <= RHS; }, |
| 35 | + [LogicSymbols.GT] = delegate (double LHS, double RHS) { return LHS > RHS; }, |
| 36 | + [LogicSymbols.GTE] = delegate (double LHS, double RHS) { return LHS >= RHS; }, |
| 37 | + [LogicSymbols.AND] = delegate (double LHS, double RHS) { return Convert.ToBoolean(LHS) && Convert.ToBoolean(RHS); }, |
| 38 | + [LogicSymbols.OR] = delegate (double LHS, double RHS) { return Convert.ToBoolean(LHS) || Convert.ToBoolean(RHS); }, |
| 39 | + [LogicSymbols.XOR] = delegate (double LHS, double RHS) { return Convert.ToBoolean(LHS) ^ Convert.ToBoolean(RHS); }, |
| 40 | + [LogicSymbols.EQV] = delegate (double LHS, double RHS) { return Convert.ToBoolean(LHS).Equals(Convert.ToBoolean(RHS)); }, |
| 41 | + [LogicSymbols.IMP] = delegate (double LHS, double RHS) { return Convert.ToBoolean(LHS).Equals(Convert.ToBoolean(RHS)) || Convert.ToBoolean(RHS); }, |
| 42 | + }; |
| 43 | + |
| 44 | + private static Dictionary<string, Func<double, double>> MathOpsUnary = new Dictionary<string, Func<double, double>>() |
| 45 | + { |
| 46 | + [MathSymbols.ADDITIVE_INVERSE] = delegate (double value) { return value * -1.0; } |
| 47 | + }; |
| 48 | + |
| 49 | + private static Dictionary<string, Func<double, bool>> LogicOpsUnary = new Dictionary<string, Func<double, bool>>() |
| 50 | + { |
| 51 | + [LogicSymbols.NOT] = delegate (double value) { return !(Convert.ToBoolean(value)); } |
| 52 | + }; |
| 53 | + |
| 54 | + private static List<string> ResultTypeRanking = new List<string>() |
| 55 | + { |
| 56 | + Tokens.Currency, |
| 57 | + Tokens.Double, |
| 58 | + Tokens.Single, |
| 59 | + Tokens.Long, |
| 60 | + Tokens.Integer, |
| 61 | + Tokens.Byte, |
| 62 | + Tokens.Boolean, |
| 63 | + }; |
| 64 | + |
| 65 | + public ParseTreeExpressionEvaluator(IParseTreeValueFactory valueFactory) |
| 66 | + { |
| 67 | + _valueFactory = valueFactory; |
| 68 | + } |
| 69 | + |
| 70 | + public IParseTreeValue Evaluate(IParseTreeValue LHS, IParseTreeValue RHS, string opSymbol) |
| 71 | + { |
| 72 | + var isMathOp = MathOpsBinary.ContainsKey(opSymbol); |
| 73 | + var isLogicOp = LogicOpsBinary.ContainsKey(opSymbol); |
| 74 | + Debug.Assert(isMathOp || isLogicOp); |
| 75 | + |
| 76 | + var opResultTypeName = isMathOp ? DetermineMathResultType(LHS, RHS) : Tokens.Boolean; |
| 77 | + var operands = PrepareOperands(new string[] { LHS.ValueText, RHS.ValueText }); |
| 78 | + |
| 79 | + if (operands.Count == 2) |
| 80 | + { |
| 81 | + if (isMathOp) |
| 82 | + { |
| 83 | + var mathResult = MathOpsBinary[opSymbol](operands[0], operands[1]); |
| 84 | + return _valueFactory.Create(mathResult.ToString(), opResultTypeName); |
| 85 | + } |
| 86 | + var logicResult = LogicOpsBinary[opSymbol](operands[0], operands[1]); |
| 87 | + return _valueFactory.Create(logicResult.ToString(), opResultTypeName); |
| 88 | + } |
| 89 | + return _valueFactory.Create($"{LHS.ValueText} {opSymbol} {RHS.ValueText}", opResultTypeName); |
| 90 | + } |
| 91 | + |
| 92 | + public IParseTreeValue Evaluate(IParseTreeValue value, string opSymbol, string requestedResultType) |
| 93 | + { |
| 94 | + var isMathOp = MathOpsUnary.ContainsKey(opSymbol); |
| 95 | + var isLogicOp = LogicOpsUnary.ContainsKey(opSymbol); |
| 96 | + Debug.Assert(isMathOp || isLogicOp); |
| 97 | + |
| 98 | + var operands = PrepareOperands(new string[] { value.ValueText }); |
| 99 | + if (operands.Count == 1) |
| 100 | + { |
| 101 | + if (isMathOp) |
| 102 | + { |
| 103 | + var mathResult = MathOpsUnary[opSymbol](operands[0]); |
| 104 | + return _valueFactory.Create(mathResult.ToString(), requestedResultType); |
| 105 | + } |
| 106 | + |
| 107 | + //Unary Not (!) operator |
| 108 | + if (!value.TypeName.Equals(Tokens.Boolean) && ParseTreeValue.TryConvertValue(value, out long opValue)) |
| 109 | + { |
| 110 | + var bitwiseComplement = ~opValue; |
| 111 | + return _valueFactory.Create(Convert.ToBoolean(bitwiseComplement).ToString(), requestedResultType); |
| 112 | + } |
| 113 | + else if (value.TypeName.Equals(Tokens.Boolean)) |
| 114 | + { |
| 115 | + var logicResult = LogicOpsUnary[opSymbol](operands[0]); |
| 116 | + return _valueFactory.Create(logicResult.ToString(), requestedResultType); |
| 117 | + } |
| 118 | + } |
| 119 | + return _valueFactory.Create($"{opSymbol} {value.ValueText}", requestedResultType); |
| 120 | + } |
| 121 | + |
| 122 | + private static string DetermineMathResultType(IParseTreeValue LHS, IParseTreeValue RHS) |
| 123 | + { |
| 124 | + var lhsTypeNameIndex = ResultTypeRanking.FindIndex(el => el.Equals(LHS.TypeName)); |
| 125 | + var rhsTypeNameIndex = ResultTypeRanking.FindIndex(el => el.Equals(RHS.TypeName)); |
| 126 | + return lhsTypeNameIndex <= rhsTypeNameIndex ? LHS.TypeName : RHS.TypeName; |
| 127 | + } |
| 128 | + |
| 129 | + private static List<double> PrepareOperands(string[] args) |
| 130 | + { |
| 131 | + var results = new List<double>(); |
| 132 | + foreach (var arg in args) |
| 133 | + { |
| 134 | + string parseArg = arg; |
| 135 | + if (arg.Equals(Tokens.True) || arg.Equals(Tokens.False)) |
| 136 | + { |
| 137 | + parseArg = arg.Equals(Tokens.True) ? "-1" : "0"; |
| 138 | + } |
| 139 | + |
| 140 | + if (double.TryParse(parseArg, out double result)) |
| 141 | + { |
| 142 | + results.Add(result); |
| 143 | + } |
| 144 | + } |
| 145 | + return results; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + internal static class MathSymbols |
| 150 | + { |
| 151 | + private static string _multiply; |
| 152 | + private static string _divide; |
| 153 | + private static string _plus; |
| 154 | + private static string _minusSign; |
| 155 | + private static string _exponent; |
| 156 | + |
| 157 | + public static string MULTIPLY => _multiply ?? LoadSymbols(VBAParser.MULT); |
| 158 | + public static string DIVIDE => _divide ?? LoadSymbols(VBAParser.DIV); |
| 159 | + public static string INTEGER_DIVIDE => @"\"; |
| 160 | + public static string PLUS => _plus ?? LoadSymbols(VBAParser.PLUS); |
| 161 | + public static string MINUS => _minusSign ?? LoadSymbols(VBAParser.MINUS); |
| 162 | + public static string ADDITIVE_INVERSE => MINUS; |
| 163 | + public static string EXPONENT => _exponent ?? LoadSymbols(VBAParser.POW); |
| 164 | + public static string MODULO => Tokens.Mod; |
| 165 | + |
| 166 | + private static string LoadSymbols(int target) |
| 167 | + { |
| 168 | + _multiply = VBAParser.DefaultVocabulary.GetLiteralName(VBAParser.MULT).Replace("'", ""); |
| 169 | + _divide = VBAParser.DefaultVocabulary.GetLiteralName(VBAParser.DIV).Replace("'", ""); |
| 170 | + _plus = VBAParser.DefaultVocabulary.GetLiteralName(VBAParser.PLUS).Replace("'", ""); |
| 171 | + _minusSign = VBAParser.DefaultVocabulary.GetLiteralName(VBAParser.MINUS).Replace("'", ""); |
| 172 | + _exponent = VBAParser.DefaultVocabulary.GetLiteralName(VBAParser.POW).Replace("'", ""); |
| 173 | + return VBAParser.DefaultVocabulary.GetLiteralName(target).Replace("'", ""); |
| 174 | + } |
| 175 | + } |
| 176 | +} |
0 commit comments