@@ -91,13 +91,14 @@ public static Enumeration.Item GenerateEnumItemFromMacro(this Enumeration @enum,
91
91
{
92
92
Name = macro . Name ,
93
93
Expression = macro . Expression ,
94
- Value = ParseMacroExpression ( macro . Expression ) ,
94
+ Value = ParseMacroExpression ( macro . Expression , @enum ) ,
95
95
Namespace = @enum
96
96
} ;
97
97
}
98
98
99
- static bool ParseToNumber ( string num , out long val )
99
+ static bool ParseToNumber ( string num , Enumeration @enum , out long val )
100
100
{
101
+ //ExpressionEvaluator does not work with hex
101
102
if ( num . StartsWith ( "0x" , StringComparison . CurrentCultureIgnoreCase ) )
102
103
{
103
104
num = num . Substring ( 2 ) ;
@@ -106,17 +107,35 @@ static bool ParseToNumber(string num, out long val)
106
107
CultureInfo . CurrentCulture , out val ) ;
107
108
}
108
109
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
+ }
110
129
}
111
130
112
- static ulong ParseMacroExpression ( string expression )
131
+ static ulong ParseMacroExpression ( string expression , Enumeration @enum )
113
132
{
114
133
// TODO: Handle string expressions
115
134
if ( expression . Length == 3 && expression [ 0 ] == '\' ' && expression [ 2 ] == '\' ' ) // '0' || 'A'
116
135
return expression [ 1 ] ; // return the ASCI code of this character
117
136
118
137
long val ;
119
- return ParseToNumber ( expression , out val ) ? ( ulong ) val : 0 ;
138
+ return ParseToNumber ( expression , @enum , out val ) ? ( ulong ) val : 0 ;
120
139
}
121
140
122
141
public static Enumeration GenerateEnumFromMacros ( this ASTContext context , string name ,
@@ -141,6 +160,10 @@ public static Enumeration GenerateEnumFromMacros(this ASTContext context, string
141
160
if ( @enum . Items . Exists ( it => it . Name == macro . Name ) )
142
161
continue ;
143
162
163
+ // Set the namespace to the namespace we found the 1st item in
164
+ if ( @enum . Namespace == null )
165
+ @enum . Namespace = unit ;
166
+
144
167
var item = @enum . GenerateEnumItemFromMacro ( macro ) ;
145
168
@enum . AddItem ( item ) ;
146
169
0 commit comments