Skip to content

Commit e4d0dc0

Browse files
committed
Some DRY refactoring
1 parent 36e1b70 commit e4d0dc0

File tree

1 file changed

+38
-74
lines changed

1 file changed

+38
-74
lines changed

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 38 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2178,31 +2178,7 @@ where method.GetParameters()[0].ParameterType == objType // static extMethod(thi
21782178
{
21792179
if (varFuncMatch.Groups["assignationOperator"].Success)
21802180
{
2181-
if (stack.Count > 1)
2182-
throw new ExpressionEvaluatorSyntaxErrorException("The left part of an assignation must be a variable, a property or an indexer.");
2183-
2184-
string rightExpression = expression.Substring(i);
2185-
i = expression.Length;
2186-
2187-
if (rightExpression.Trim().Equals(string.Empty))
2188-
throw new ExpressionEvaluatorSyntaxErrorException("Right part is missing in assignation");
2189-
2190-
if (varFuncMatch.Groups["assignmentPrefix"].Success)
2191-
{
2192-
ExpressionOperator op = operatorsDictionary[varFuncMatch.Groups["assignmentPrefix"].Value];
2193-
2194-
varValue = OperatorsEvaluations.ToList().Find(dict => dict.ContainsKey(op))[op](varValue, Evaluate(rightExpression));
2195-
}
2196-
else
2197-
{
2198-
varValue = Evaluate(rightExpression);
2199-
}
2200-
2201-
if (varValue is BubbleExceptionContainer exceptionContainer)
2202-
throw exceptionContainer.Exception;
2203-
2204-
stack.Clear();
2205-
stack.Push(varValue);
2181+
varValue = ManageKindOfAssignation(expression, ref i, varFuncMatch, () => varValue, stack);
22062182
}
22072183
else if (varFuncMatch.Groups["postfixOperator"].Success)
22082184
{
@@ -2312,34 +2288,7 @@ where method.GetParameters()[0].ParameterType == objType // static extMethod(thi
23122288

23132289
if (varFuncMatch.Groups["assignationOperator"].Success)
23142290
{
2315-
if (stack.Count > 1)
2316-
throw new ExpressionEvaluatorSyntaxErrorException("The left part of an assignation must be a variable, a property or an indexer.");
2317-
2318-
string rightExpression = expression.Substring(i);
2319-
i = expression.Length;
2320-
2321-
if (rightExpression.Trim().Equals(string.Empty))
2322-
throw new ExpressionEvaluatorSyntaxErrorException("Right part is missing in assignation");
2323-
2324-
if (varFuncMatch.Groups["assignmentPrefix"].Success)
2325-
{
2326-
if (!Variables.ContainsKey(varFuncName))
2327-
throw new ExpressionEvaluatorSyntaxErrorException($"The variable[{varFuncName}] do not exists.");
2328-
2329-
ExpressionOperator op = operatorsDictionary[varFuncMatch.Groups["assignmentPrefix"].Value];
2330-
2331-
cusVarValueToPush = OperatorsEvaluations.ToList().Find(dict => dict.ContainsKey(op))[op](cusVarValueToPush, Evaluate(rightExpression));
2332-
}
2333-
else
2334-
{
2335-
cusVarValueToPush = Evaluate(rightExpression);
2336-
}
2337-
2338-
if (cusVarValueToPush is BubbleExceptionContainer exceptionContainer)
2339-
throw exceptionContainer.Exception;
2340-
2341-
stack.Clear();
2342-
stack.Push(cusVarValueToPush);
2291+
cusVarValueToPush = ManageKindOfAssignation(expression, ref i, varFuncMatch, () => cusVarValueToPush, stack);
23432292
}
23442293
else if (varFuncMatch.Groups["postfixOperator"].Success)
23452294
{
@@ -2717,27 +2666,7 @@ protected virtual bool EvaluateIndexing(string expression, Stack<object> stack,
27172666
}
27182667
else
27192668
{
2720-
string rightExpression = expression.Substring(i);
2721-
i = expression.Length;
2722-
2723-
if (rightExpression.Trim().Equals(string.Empty))
2724-
throw new ExpressionEvaluatorSyntaxErrorException("Right part is missing in assignation");
2725-
2726-
if (assignationOrPostFixOperatorMatch.Groups["assignmentPrefix"].Success)
2727-
{
2728-
ExpressionOperator prefixOp = operatorsDictionary[assignationOrPostFixOperatorMatch.Groups["assignmentPrefix"].Value];
2729-
2730-
valueToPush = OperatorsEvaluations[0][op](left, right);
2731-
2732-
valueToPush = OperatorsEvaluations.ToList().Find(dict => dict.ContainsKey(prefixOp))[prefixOp](valueToPush, Evaluate(rightExpression));
2733-
}
2734-
else
2735-
{
2736-
valueToPush = Evaluate(rightExpression);
2737-
}
2738-
2739-
if (valueToPush is BubbleExceptionContainer exceptionContainer)
2740-
throw exceptionContainer.Exception;
2669+
valueToPush = ManageKindOfAssignation(expression, ref i, assignationOrPostFixOperatorMatch, () => OperatorsEvaluations[0][op](left, right));
27412670

27422671
if (left is IDictionary<string, object> dictionaryLeft)
27432672
dictionaryLeft[right] = valueToPush;
@@ -3054,6 +2983,41 @@ public string RemoveComments(string scriptWithComments)
30542983

30552984
protected delegate dynamic InternalDelegate(params dynamic[] args);
30562985

2986+
protected virtual object ManageKindOfAssignation(string expression, ref int index, Match match, Func<object> getCurrentValue, Stack<object> stack = null)
2987+
{
2988+
if (stack?.Count > 1)
2989+
throw new ExpressionEvaluatorSyntaxErrorException("The left part of an assignation must be a variable, a property or an indexer.");
2990+
2991+
object result;
2992+
string rightExpression = expression.Substring(index);
2993+
index = expression.Length;
2994+
2995+
if (rightExpression.Trim().Equals(string.Empty))
2996+
throw new ExpressionEvaluatorSyntaxErrorException("Right part is missing in assignation");
2997+
2998+
if (match.Groups["assignmentPrefix"].Success)
2999+
{
3000+
ExpressionOperator prefixOp = operatorsDictionary[match.Groups["assignmentPrefix"].Value];
3001+
3002+
result = OperatorsEvaluations.ToList().Find(dict => dict.ContainsKey(prefixOp))[prefixOp](getCurrentValue(), Evaluate(rightExpression));
3003+
}
3004+
else
3005+
{
3006+
result = Evaluate(rightExpression);
3007+
}
3008+
3009+
if (result is BubbleExceptionContainer exceptionContainer)
3010+
throw exceptionContainer.Exception;
3011+
3012+
if (stack != null)
3013+
{
3014+
stack.Clear();
3015+
stack.Push(result);
3016+
}
3017+
3018+
return result;
3019+
}
3020+
30573021
protected virtual bool GetLambdaExpression(string expression, Stack<object> stack)
30583022
{
30593023
Match lambdaExpressionMatch = lambdaExpressionRegex.Match(expression);

0 commit comments

Comments
 (0)