Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support of Single-Dimensional Arrays initialization #245

Merged
merged 1 commit into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 34 additions & 16 deletions src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,16 @@ private Expression ParseNew()

var newType = ParseKnownType();
var args = new Expression[0];

if (newType.IsArray)
{
if (newType.GetArrayRank() != 1)
throw CreateParseException(_token.pos, ErrorMessages.UnsupportedMultidimensionalArrays, newType);

args = ParseArrayInitializerList();
return Expression.NewArrayInit(newType.GetElementType(), args);
}

if (_token.id == TokenId.OpenParen)
args = ParseArgumentList();
else
Expand All @@ -1190,6 +1200,13 @@ private Expression ParseNew()
return Expression.MemberInit(newExpr, memberBindings);
}

private Expression[] ParseArrayInitializerList()
{
return ParseArgumentList(TokenId.OpenCurlyBracket, ErrorMessages.OpenCurlyBracketExpected,
TokenId.CloseCurlyBracket, ErrorMessages.CloseCurlyBracketExpected,
allowTrailingComma: true);
}

private MemberBinding[] ParseObjectInitializer(Type newType)
{
ValidateToken(TokenId.OpenCurlyBracket, ErrorMessages.OpenCurlyBracketExpected);
Expand Down Expand Up @@ -1698,36 +1715,37 @@ private static Expression ParseDynamicIndex(Type type, Expression instance, Expr
return Expression.Dynamic(binder, typeof(object), argsDynamic);
}

private Expression[] ParseArgumentList()
private Expression[] ParseArgumentList(TokenId openToken, string missingOpenTokenMsg,
TokenId closeToken, string missingCloseTokenMsg,
bool allowTrailingComma = false)
{
ValidateToken(TokenId.OpenParen, ErrorMessages.OpenParenExpected);
NextToken();
var args = _token.id != TokenId.CloseParen ? ParseArguments() : new Expression[0];
ValidateToken(TokenId.CloseParen, ErrorMessages.CloseParenOrCommaExpected);
ValidateToken(openToken, missingOpenTokenMsg);
NextToken();
return args;
}

private Expression[] ParseArguments()
{
var argList = new List<Expression>();
while (true)
while (_token.id != closeToken)
{
argList.Add(ParseExpressionSegment());
if (_token.id != TokenId.Comma) break;
NextToken();
if (!allowTrailingComma && _token.id == closeToken)
throw CreateParseException(_token.pos, missingCloseTokenMsg);
}
ValidateToken(closeToken, missingCloseTokenMsg);
NextToken();
return argList.ToArray();
}

private Expression[] ParseArgumentList()
{
return ParseArgumentList(TokenId.OpenParen, ErrorMessages.OpenParenExpected,
TokenId.CloseParen, ErrorMessages.CloseParenOrCommaExpected);
}

private Expression ParseElementAccess(Expression expr)
{
var errorPos = _token.pos;
ValidateToken(TokenId.OpenBracket, ErrorMessages.OpenParenExpected);
NextToken();
var args = ParseArguments();
ValidateToken(TokenId.CloseBracket, ErrorMessages.CloseBracketOrCommaExpected);
NextToken();
var args = ParseArgumentList(TokenId.OpenBracket, ErrorMessages.OpenParenExpected,
TokenId.CloseBracket, ErrorMessages.CloseBracketOrCommaExpected);
if (expr.Type.IsArray)
{
if (expr.Type.GetArrayRank() != args.Length)
Expand Down
13 changes: 12 additions & 1 deletion src/DynamicExpresso.Core/Resources/ErrorMessages.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/DynamicExpresso.Core/Resources/ErrorMessages.resx
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,7 @@
<data name="AmbiguousConstructorInvocation" xml:space="preserve">
<value>Ambiguous invocation of constructor in type '{0}'</value>
</data>
<data name="UnsupportedMultidimensionalArrays" xml:space="preserve">
<value>Multidimensional arrays are not supported</value>
</data>
</root>
38 changes: 38 additions & 0 deletions test/DynamicExpresso.UnitTest/ConstructorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,44 @@ public void Ctor_params_array()
Assert.AreEqual(new MyClass(6, 5, 4, 3).MyArr, target.Eval("new MyClass(6, 5, 4, 3).MyArr"));
}

[Test]
public void Array_constructor()
{
var target = new Interpreter();
var arr = target.Eval<int[]>("new int[] { 1, 2 }");
Assert.AreEqual(2, arr.Length);
Assert.AreEqual(1, arr[0]);
Assert.AreEqual(2, arr[1]);
}

[Test]
public void Array_constructor_type_mismatch()
{
// Exception: an expression of type 'System.Char' cannot be used to initialize an array of type 'System.Int32'
var target = new Interpreter();
Assert.Throws<ParseException>(() => target.Eval<int[]>("new int[] { 1, 'a' }"));
}

[Test]
public void Jagged_array_constructor()
{
var target = new Interpreter();
var arr = target.Eval<int[][]>("new int[][] { new int[] { 1, 2, }, new int[] { 3, 4, }, }");
Assert.AreEqual(2, arr.Length);
Assert.AreEqual(1, arr[0][0]);
Assert.AreEqual(2, arr[0][1]);
Assert.AreEqual(3, arr[1][0]);
Assert.AreEqual(4, arr[1][1]);
}

[Test]
public void Array_multi_dimension_constructor()
{
// creating a multidimensional array is not supported
var target = new Interpreter();
Assert.Throws<ParseException>(() => target.Parse("new int[,] { { 1 }, { 2 } }"));
}

private class MyClass
{
public int IntField;
Expand Down