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

Added support of default operator #168

Merged
merged 3 commits into from
Oct 13, 2021
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
8 changes: 7 additions & 1 deletion src/DynamicExpresso.Core/LanguageConstants.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using DynamicExpresso.Parsing;
using DynamicExpresso.Parsing;
using System;
using System.Linq.Expressions;

Expand Down Expand Up @@ -35,9 +35,15 @@ public static class LanguageConstants
new ReferenceType("string", typeof(string)),
new ReferenceType("char", typeof(char)),
new ReferenceType("bool", typeof(bool)),
new ReferenceType("sbyte", typeof(sbyte)),
new ReferenceType("byte", typeof(byte)),
new ReferenceType("short", typeof(short)),
new ReferenceType("ushort", typeof(ushort)),
new ReferenceType("int", typeof(int)),
new ReferenceType("uint", typeof(uint)),
new ReferenceType("long", typeof(long)),
new ReferenceType("ulong", typeof(ulong)),
new ReferenceType("float", typeof(float)),
new ReferenceType("double", typeof(double)),
new ReferenceType("decimal", typeof(decimal))
};
Expand Down
20 changes: 20 additions & 0 deletions src/DynamicExpresso.Core/Parsing/Parser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,8 @@ private Expression ParseIdentifier()
return ParseNew();
if (_token.text == ParserConstants.KeywordTypeof)
return ParseTypeof();
if (_token.text == ParserConstants.KeywordDefault)
return ParseDefaultOperator();

if (_arguments.TryGetIdentifier(_token.text, out Expression keywordExpression))
{
Expand Down Expand Up @@ -997,6 +999,24 @@ private Expression ParseTypeof()
return constExp;
}

private Expression ParseDefaultOperator()
{
NextToken();

ValidateToken(TokenId.OpenParen, ErrorMessages.OpenParenExpected);
NextToken();
ValidateToken(TokenId.Identifier);

var name = _token.text;
if (_arguments.TryGetKnownType(name, out var type))
type = ParseFullType(type);

ValidateToken(TokenId.CloseParen, ErrorMessages.CloseParenOrCommaExpected);
NextToken();

return Expression.Default(type);
}

private Expression GenerateConditional(Expression test, Expression expr1, Expression expr2, int errorPos)
{
if (IsDynamicExpression(test))
Expand Down
6 changes: 4 additions & 2 deletions src/DynamicExpresso.Core/Parsing/ParserConstants.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Linq.Expressions;
using System.Linq.Expressions;

namespace DynamicExpresso.Parsing
{
Expand All @@ -10,12 +10,14 @@ internal static class ParserConstants
public const string KeywordIs = "is";
public const string KeywordNew = "new";
public const string KeywordTypeof = "typeof";
public const string KeywordDefault = "default";

public static readonly string[] ReservedKeywords = {
KeywordAs,
KeywordIs,
KeywordNew,
KeywordTypeof
KeywordTypeof,
KeywordDefault
};
}
}
67 changes: 67 additions & 0 deletions test/DynamicExpresso.UnitTest/DefaultOperatorTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using NUnit.Framework;

namespace DynamicExpresso.UnitTest
{
[TestFixture]
public class DefaultOperatorTest
{
[Test]
public void Default_value_type()
{
var target = new Interpreter();

Assert.AreEqual(default(bool), target.Eval("default(bool)"));
Assert.AreEqual(default(char), target.Eval("default(char)"));
Assert.AreEqual(default(sbyte), target.Eval("default(sbyte)"));
Assert.AreEqual(default(byte), target.Eval("default(byte)"));
Assert.AreEqual(default(short), target.Eval("default(short)"));
Assert.AreEqual(default(ushort), target.Eval("default(ushort)"));
Assert.AreEqual(default(int), target.Eval("default(int)"));
Assert.AreEqual(default(uint), target.Eval("default(uint)"));
Assert.AreEqual(default(long), target.Eval("default(long)"));
Assert.AreEqual(default(ulong), target.Eval("default(ulong)"));
Assert.AreEqual(default(float), target.Eval("default(float)"));
Assert.AreEqual(default(double), target.Eval("default(double)"));
Assert.AreEqual(default(decimal), target.Eval("default(decimal)"));
Assert.AreEqual(default(System.DateTime), target.Eval("default(DateTime)"));
Assert.AreEqual(default(System.TimeSpan), target.Eval("default(TimeSpan)"));
Assert.AreEqual(default(System.Guid), target.Eval("default(Guid)"));

Assert.AreEqual(typeof(bool), target.Eval("default(bool)").GetType());
Assert.AreEqual(typeof(char), target.Eval("default(char)").GetType());
Assert.AreEqual(typeof(sbyte), target.Eval("default(sbyte)").GetType());
Assert.AreEqual(typeof(byte), target.Eval("default(byte)").GetType());
Assert.AreEqual(typeof(short), target.Eval("default(short)").GetType());
Assert.AreEqual(typeof(ushort), target.Eval("default(ushort)").GetType());
Assert.AreEqual(typeof(int), target.Eval("default(int)").GetType());
Assert.AreEqual(typeof(uint), target.Eval("default(uint)").GetType());
Assert.AreEqual(typeof(long), target.Eval("default(long)").GetType());
Assert.AreEqual(typeof(ulong), target.Eval("default(ulong)").GetType());
Assert.AreEqual(typeof(float), target.Eval("default(float)").GetType());
Assert.AreEqual(typeof(double), target.Eval("default(double)").GetType());
Assert.AreEqual(typeof(decimal), target.Eval("default(decimal)").GetType());
Assert.AreEqual(typeof(System.DateTime), target.Eval("default(DateTime)").GetType());
Assert.AreEqual(typeof(System.TimeSpan), target.Eval("default(TimeSpan)").GetType());
Assert.AreEqual(typeof(System.Guid), target.Eval("default(Guid)").GetType());
}

[Test]
public void Default_reference_type()
{
var target = new Interpreter();

Assert.AreEqual(default(object), target.Eval("default(object)"));
Assert.AreEqual(default(string), target.Eval("default(string)"));
}

[Test]
public void Default_nullable_type()
{
var target = new Interpreter();

Assert.AreEqual(default(int?), target.Eval("default(int?)"));
Assert.AreEqual(default(double?), target.Eval("default(double?)"));
Assert.AreEqual(default(System.DateTime?), target.Eval("default(DateTime?)"));
}
}
}