Skip to content

Commit 430bbdb

Browse files
TheLastRartritao
authored andcommitted
Evaluate expressions for enums generated using GenerateEnumFromMacros (#1048)
* Evaluate expressions when generating enum from macros ExpressionEvaluator taken from https://github.com/codingseb/ExpressionEvaluator * Set namespace for enums generated from macros. * Add Tests
1 parent 066e8fc commit 430bbdb

File tree

6 files changed

+1505
-5
lines changed

6 files changed

+1505
-5
lines changed

src/Generator/Library.cs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,13 +91,14 @@ public static Enumeration.Item GenerateEnumItemFromMacro(this Enumeration @enum,
9191
{
9292
Name = macro.Name,
9393
Expression = macro.Expression,
94-
Value = ParseMacroExpression(macro.Expression),
94+
Value = ParseMacroExpression(macro.Expression, @enum),
9595
Namespace = @enum
9696
};
9797
}
9898

99-
static bool ParseToNumber(string num, out long val)
99+
static bool ParseToNumber(string num, Enumeration @enum, out long val)
100100
{
101+
//ExpressionEvaluator does not work with hex
101102
if (num.StartsWith("0x", StringComparison.CurrentCultureIgnoreCase))
102103
{
103104
num = num.Substring(2);
@@ -106,17 +107,35 @@ static bool ParseToNumber(string num, out long val)
106107
CultureInfo.CurrentCulture, out val);
107108
}
108109

109-
return long.TryParse(num, out val);
110+
ExpressionEvaluator evaluator = new ExpressionEvaluator();
111+
//Include values of past items
112+
evaluator.Variables = new Dictionary<string, object>();
113+
foreach (Enumeration.Item item in @enum.Items)
114+
{
115+
//ExpressionEvaluator is requires lowercase variables
116+
evaluator.Variables.Add(item.Name.ToLower(), item.Value);
117+
}
118+
try
119+
{
120+
var ret = evaluator.Evaluate("(long)" + num.ReplaceLineBreaks(" ").Replace('\\',' '));
121+
val = (long)ret;
122+
return true;
123+
}
124+
catch (ExpressionEvaluatorSyntaxErrorException)
125+
{
126+
val = 0;
127+
return false;
128+
}
110129
}
111130

112-
static ulong ParseMacroExpression(string expression)
131+
static ulong ParseMacroExpression(string expression, Enumeration @enum)
113132
{
114133
// TODO: Handle string expressions
115134
if (expression.Length == 3 && expression[0] == '\'' && expression[2] == '\'') // '0' || 'A'
116135
return expression[1]; // return the ASCI code of this character
117136

118137
long val;
119-
return ParseToNumber(expression, out val) ? (ulong)val : 0;
138+
return ParseToNumber(expression, @enum, out val) ? (ulong)val : 0;
120139
}
121140

122141
public static Enumeration GenerateEnumFromMacros(this ASTContext context, string name,
@@ -141,6 +160,10 @@ public static Enumeration GenerateEnumFromMacros(this ASTContext context, string
141160
if (@enum.Items.Exists(it => it.Name == macro.Name))
142161
continue;
143162

163+
// Set the namespace to the namespace we found the 1st item in
164+
if (@enum.Namespace == null)
165+
@enum.Namespace = unit;
166+
144167
var item = @enum.GenerateEnumItemFromMacro(macro);
145168
@enum.AddItem(item);
146169

0 commit comments

Comments
 (0)