Skip to content

Commit e9c6448

Browse files
author
Sébastien Geiser
committed
Typed variable in progress
1 parent cd6f3d3 commit e9c6448

File tree

5 files changed

+52
-2
lines changed

5 files changed

+52
-2
lines changed

CodingSeb.ExpressionEvaluator.Tests/ExpressionEvaluatorScriptEvaluateTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,14 @@ public static IEnumerable<TestCaseData> TestCasesForScriptEvaluateTests
12931293
.SetCategory("=")
12941294
.Returns("0,1,2,3,4,");
12951295

1296+
yield return new TestCaseData(Resources.Script0067, null, null, null, null)
1297+
.SetCategory("Script")
1298+
.SetCategory("primaryTyped variable")
1299+
.SetCategory("List")
1300+
.SetCategory("int")
1301+
.SetCategory("=")
1302+
.Returns("[1,2,3,4]");
1303+
12961304
#endregion
12971305

12981306
#region More complex script

CodingSeb.ExpressionEvaluator.Tests/Resources.Designer.cs

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CodingSeb.ExpressionEvaluator.Tests/Resources.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,7 @@
316316
<data name="Script0066" type="System.Resources.ResXFileRef, System.Windows.Forms">
317317
<value>resources\script0066.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
318318
</data>
319+
<data name="Script0067" type="System.Resources.ResXFileRef, System.Windows.Forms">
320+
<value>resources\script0067.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8</value>
321+
</data>
319322
</root>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/* Script0067 */
2+
List<int> myList = new List<int>{1, 2, 3, 4}.Json;

CodingSeb.ExpressionEvaluator/ExpressionEvaluator.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/******************************************************************************************************
22
Title : ExpressionEvaluator (https://github.com/codingseb/ExpressionEvaluator)
3-
Version : 1.4.2.1
3+
Version : 1.4.2.2
44
(if last digit (the forth) is not a zero, the version is an intermediate version and can be unstable)
55
66
Author : Coding Seb
@@ -33,7 +33,7 @@ public partial class ExpressionEvaluator
3333
protected const string primaryTypesGroupPattern = "(?<primaryType>object|string|bool[?]?|byte[?]?|char[?]?|decimal[?]?|double[?]?|short[?]?|int[?]?|long[?]?|sbyte[?]?|float[?]?|ushort[?]?|uint[?]?|ulong[?]?|void)";
3434
protected const string primaryTypesRegexPattern = "(?<=^|[^" + diactiticsKeywordsRegexPattern + "])" + primaryTypesGroupPattern + "(?=[^a-zA-Z_]|$)";
3535

36-
protected static readonly Regex varOrFunctionRegEx = new Regex($@"^((?<sign>[+-])|(?<prefixOperator>[+][+]|--)|(?<varKeyword>var)\s+|(?<dynamicKeyword>dynamic)\s+|{primaryTypesGroupPattern}\s+|(?<inObject>(?<nullConditional>[?])?\.)?)(?<name>[{ diactiticsKeywordsRegexPattern }](?>[{ diactiticsKeywordsRegexPattern }0-9]*))(?>\s*)((?<assignationOperator>(?<assignmentPrefix>[+\-*/%&|^]|<<|>>)?=(?![=>]))|(?<postfixOperator>([+][+]|--)(?![{ diactiticsKeywordsRegexPattern}0-9]))|((?<isgeneric>[<](?>([{ diactiticsKeywordsRegexPattern }](?>[{ diactiticsKeywordsRegexPattern }0-9]*)|(?>\s+)|[,\.])+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?(?<isfunction>[(])?))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
36+
protected static readonly Regex varOrFunctionRegEx = new Regex($@"^((?<sign>[+-])|(?<prefixOperator>[+][+]|--)|(?<varKeyword>var)\s+|(?<dynamicKeyword>dynamic)\s+|(?<inObject>(?<nullConditional>[?])?\.)?)(?<name>[{ diactiticsKeywordsRegexPattern }](?>[{ diactiticsKeywordsRegexPattern }0-9]*))(?>\s*)((?<assignationOperator>(?<assignmentPrefix>[+\-*/%&|^]|<<|>>)?=(?![=>]))|(?<postfixOperator>([+][+]|--)(?![{ diactiticsKeywordsRegexPattern}0-9]))|((?<isgeneric>[<](?>([{ diactiticsKeywordsRegexPattern }](?>[{ diactiticsKeywordsRegexPattern }0-9]*)|(?>\s+)|[,\.])+|(?<gentag>[<])|(?<-gentag>[>]))*(?(gentag)(?!))[>])?(?<isfunction>[(])?))", RegexOptions.IgnoreCase | RegexOptions.Compiled);
3737

3838
protected const string numberRegexOrigPattern = @"^(?<sign>[+-])?([0-9][0-9_{1}]*[0-9]|\d)(?<hasdecimal>{0}?([0-9][0-9_]*[0-9]|\d)(e[+-]?([0-9][0-9_]*[0-9]|\d))?)?(?<type>ul|[fdulm])?";
3939
protected string numberRegexPattern = null;
@@ -2117,6 +2117,19 @@ protected virtual bool EvaluateVarOrFunc(string expression, Stack<object> stack,
21172117
else if ((Variables.TryGetValue(varFuncName, out object cusVarValueToPush) || varFuncMatch.Groups["assignationOperator"].Success)
21182118
&& (cusVarValueToPush == null || !TypesToBlock.Contains(cusVarValueToPush.GetType())))
21192119
{
2120+
StronglyTypedVariable stronglyTypedVariable;
2121+
2122+
if (stack.Count == 1 && stack.Peek() is ClassOrEnumType classOrEnum && varFuncMatch.Groups["assignationOperator"].Success)
2123+
{
2124+
stack.Pop();
2125+
2126+
stronglyTypedVariable = new StronglyTypedVariable
2127+
{
2128+
Type = classOrEnum.Type,
2129+
Value = !varFuncMatch.Groups["assignationOperator"].Success && classOrEnum.Type.IsValueType ? Activator.CreateInstance(classOrEnum.Type) : null,
2130+
};
2131+
}
2132+
21202133
stack.Push(cusVarValueToPush);
21212134

21222135
if (OptionVariableAssignationActive)
@@ -3599,6 +3612,13 @@ public partial class ClassOrEnumType
35993612
public Type Type { get; set; }
36003613
}
36013614

3615+
public partial class StronglyTypedVariable
3616+
{
3617+
public Type Type { get; set; }
3618+
3619+
public object Value { get; set; }
3620+
}
3621+
36023622
public partial class ExpressionEvaluatorSyntaxErrorException : Exception
36033623
{
36043624
public ExpressionEvaluatorSyntaxErrorException() : base()

0 commit comments

Comments
 (0)