Skip to content

Commit 7d94fd8

Browse files
committed
Simple array with init in {}
1 parent 48aae3a commit 7d94fd8

File tree

1 file changed

+19
-6
lines changed

1 file changed

+19
-6
lines changed

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public class ExpressionEvaluator
4242
private static readonly Regex stringBeginningForEndBlockRegex = new Regex("[$]?[@]?[\"]$");
4343
private static readonly Regex lambdaExpressionRegex = new Regex(@"^\s*(?<args>(\s*[(]\s*([" + diactiticsKeywordsRegexPattern + @"][" + diactiticsKeywordsRegexPattern + @"0-9]*\s*([,]\s*[" + diactiticsKeywordsRegexPattern + @"][" + diactiticsKeywordsRegexPattern + @"0-9]*\s*)*)?[)])|[" + diactiticsKeywordsRegexPattern + @"][" + diactiticsKeywordsRegexPattern + @"0-9]*)\s*=>(?<expression>.*)$", RegexOptions.Singleline);
4444
private static readonly Regex lambdaArgRegex = new Regex(@"[" + diactiticsKeywordsRegexPattern + @"][" + diactiticsKeywordsRegexPattern + @"0-9]*");
45+
private static readonly Regex initInNewBeginningRegex = new Regex(@"^\s*{");
46+
private static readonly Regex OtherDimentionArrayInNewBeginningRegex = new Regex(@"^\s*\[");
4547

4648
private static readonly string instanceCreationWithNewKeywordRegexPattern = @"^new\s+(?<name>[" + diactiticsKeywordsRegexPattern + @"][" + diactiticsKeywordsRegexPattern + @"0-9.]*)\s*(?<isgeneric>[<](?>[^<>]+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?\s*((?<isfunction>[(])|(?<isArray>\[))?";
4749
private Regex instanceCreationWithNewKeywordRegex = new Regex(instanceCreationWithNewKeywordRegexPattern);
@@ -1365,22 +1367,33 @@ private bool EvaluateInstanceCreationWithNewKeyword(string expr, string restOfEx
13651367
else if(instanceCreationMatch.Groups["isArray"].Success)
13661368
{
13671369
List<string> arrayArgs = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets(expr, ref i, true, ",", "[", "]");
1370+
i++;
1371+
Array array = null;
13681372

13691373
if(arrayArgs.Count > 0)
13701374
{
1371-
stack.Push(Array.CreateInstance(type, arrayArgs.ConvertAll(subExpression => (int)Evaluate(subExpression)).ToArray()));
1375+
array = Array.CreateInstance(type, arrayArgs.ConvertAll(subExpression => (int)Evaluate(subExpression)).ToArray());
13721376
}
1373-
else
1377+
1378+
Match initInNewBeginningMatch = initInNewBeginningRegex.Match(expr.Substring(i));
1379+
1380+
if (initInNewBeginningMatch.Success)
13741381
{
1375-
//int[,] test = new int[4, 2];
1382+
i += initInNewBeginningMatch.Length;
13761383

1377-
//Array.CreateInstance()
1384+
List<string> arrayElements = GetExpressionsBetweenParenthesesOrOtherImbricableBrackets(expr, ref i, true, ",", "{", "}");
1385+
i++;
1386+
1387+
if (array == null)
1388+
array = Array.CreateInstance(type, arrayElements.Count);
13781389

1379-
//test[0, 1] = 3;
1390+
Array.Copy(arrayElements.ConvertAll(subExpression => Evaluate(subExpression)).ToArray(), array, arrayElements.Count);
13801391
}
1392+
1393+
stack.Push(array);
13811394
}
13821395
else
1383-
throw new ExpressionEvaluatorSyntaxErrorException($"A new expression requires that type be followed by (), [], or { "{}" } (Check : {instanceCreationMatch.Value})");
1396+
throw new ExpressionEvaluatorSyntaxErrorException($"A new expression requires that type be followed by () or [](Check : {instanceCreationMatch.Value})");
13841397

13851398
return true;
13861399
}

0 commit comments

Comments
 (0)