@@ -43,6 +43,10 @@ public class MathEvaluator : IDisposable
43
43
private List < string > _innerFunctions ;
44
44
private uint _nestedFunctionDepth ;
45
45
private StringReader _expressionReader ;
46
+ private VariableDictionary _variables ;
47
+ private ReadOnlyCollection < string > _functions ;
48
+ private char _currentChar ;
49
+
46
50
47
51
/// <summary>
48
52
/// Initializes a new instance of the <see cref="MathEvaluator"/> class.
@@ -62,7 +66,6 @@ public MathEvaluator()
62
66
_nestedFunctionDepth = 0 ;
63
67
}
64
68
65
- private VariableDictionary _variables ;
66
69
67
70
/// <summary>
68
71
/// Gets the variables collections.
@@ -73,8 +76,6 @@ public VariableDictionary Variables
73
76
get { return _variables ; }
74
77
}
75
78
76
- private ReadOnlyCollection < string > _functions ;
77
-
78
79
/// <summary>Gets the functions available to <see cref="MathEvaluator"/>.</summary>
79
80
/// <value>The functions for <see cref="MathEvaluator"/>.</value>
80
81
/// <seealso cref="RegisterFunction"/>
@@ -149,54 +150,54 @@ internal bool IsFunction(string name)
149
150
150
151
private void ParseExpressionToQueue ( )
151
152
{
152
- char l = '\0 ' ;
153
- char c = '\0 ' ;
153
+ char lastChar = '\0 ' ;
154
+ _currentChar = '\0 ' ;
154
155
155
156
do
156
157
{
157
158
// last non white space char
158
- if ( ! char . IsWhiteSpace ( c ) )
159
- l = c ;
159
+ if ( ! char . IsWhiteSpace ( _currentChar ) )
160
+ lastChar = _currentChar ;
160
161
161
- c = ( char ) _expressionReader . Read ( ) ;
162
+ _currentChar = ( char ) _expressionReader . Read ( ) ;
162
163
163
- if ( char . IsWhiteSpace ( c ) )
164
+ if ( char . IsWhiteSpace ( _currentChar ) )
164
165
continue ;
165
166
166
- if ( TryNumber ( c , l ) )
167
+ if ( TryNumber ( lastChar ) )
167
168
continue ;
168
169
169
- if ( TryString ( c ) )
170
+ if ( TryString ( ) )
170
171
continue ;
171
172
172
- if ( TryStartGroup ( c ) )
173
+ if ( TryStartGroup ( ) )
173
174
continue ;
174
175
175
- if ( TryComma ( c ) )
176
+ if ( TryComma ( ) )
176
177
continue ;
177
178
178
- if ( TryOperator ( c ) )
179
+ if ( TryOperator ( ) )
179
180
continue ;
180
181
181
- if ( TryEndGroup ( c ) )
182
+ if ( TryEndGroup ( ) )
182
183
continue ;
183
184
184
- if ( TryConvert ( c ) )
185
+ if ( TryConvert ( ) )
185
186
continue ;
186
187
187
- throw new ParseException ( Resources . InvalidCharacterEncountered + c ) ;
188
+ throw new ParseException ( Resources . InvalidCharacterEncountered + _currentChar ) ;
188
189
} while ( _expressionReader . Peek ( ) != - 1 ) ;
189
190
190
191
ProcessSymbolStack ( ) ;
191
192
}
192
193
193
- private bool TryConvert ( char c )
194
+ private bool TryConvert ( )
194
195
{
195
- if ( c != '[' )
196
+ if ( _currentChar != '[' )
196
197
return false ;
197
198
198
199
_buffer . Length = 0 ;
199
- _buffer . Append ( c ) ;
200
+ _buffer . Append ( _currentChar ) ;
200
201
201
202
char p = ( char ) _expressionReader . Peek ( ) ;
202
203
while ( char . IsLetter ( p ) || char . IsWhiteSpace ( p ) || p == '-' || p == '>' || p == ']' )
@@ -222,13 +223,13 @@ private bool TryConvert(char c)
222
223
throw new ParseException ( Resources . InvalidConvertionExpression + _buffer ) ;
223
224
}
224
225
225
- private bool TryString ( char c )
226
+ private bool TryString ( )
226
227
{
227
- if ( ! char . IsLetter ( c ) )
228
+ if ( ! char . IsLetter ( _currentChar ) )
228
229
return false ;
229
230
230
231
_buffer . Length = 0 ;
231
- _buffer . Append ( c ) ;
232
+ _buffer . Append ( _currentChar ) ;
232
233
233
234
char p = ( char ) _expressionReader . Peek ( ) ;
234
235
while ( char . IsLetter ( p ) || char . IsNumber ( p ) )
@@ -256,34 +257,34 @@ private bool TryString(char c)
256
257
throw new ParseException ( Resources . InvalidVariableEncountered + _buffer ) ;
257
258
}
258
259
259
- private bool TryStartGroup ( char c )
260
+ private bool TryStartGroup ( )
260
261
{
261
- if ( c != '(' )
262
+ if ( _currentChar != '(' )
262
263
return false ;
263
264
264
265
if ( PeekNextNonWhitespaceChar ( ) == ',' )
265
266
{
266
267
throw new ParseException ( Resources . InvalidCharacterEncountered + "," ) ;
267
268
}
268
269
269
- _symbolStack . Push ( c . ToString ( ) ) ;
270
+ _symbolStack . Push ( _currentChar . ToString ( ) ) ;
270
271
return true ;
271
272
}
272
273
273
- private bool TryComma ( char c )
274
+ private bool TryComma ( )
274
275
{
275
- if ( c != ',' )
276
+ if ( _currentChar != ',' )
276
277
return false ;
277
278
278
279
if ( _nestedFunctionDepth <= 0 )
279
280
{
280
- throw new ParseException ( Resources . InvalidCharacterEncountered + c ) ;
281
+ throw new ParseException ( Resources . InvalidCharacterEncountered + _currentChar ) ;
281
282
}
282
283
283
284
char nextChar = PeekNextNonWhitespaceChar ( ) ;
284
285
if ( nextChar == ')' || nextChar == ',' )
285
286
{
286
- throw new ParseException ( Resources . InvalidCharacterEncountered + c ) ;
287
+ throw new ParseException ( Resources . InvalidCharacterEncountered + _currentChar ) ;
287
288
}
288
289
289
290
return true ;
@@ -301,9 +302,9 @@ private char PeekNextNonWhitespaceChar()
301
302
}
302
303
303
304
304
- private bool TryEndGroup ( char c )
305
+ private bool TryEndGroup ( )
305
306
{
306
- if ( c != ')' )
307
+ if ( _currentChar != ')' )
307
308
return false ;
308
309
309
310
bool hasStart = false ;
@@ -340,13 +341,13 @@ private bool TryEndGroup(char c)
340
341
return true ;
341
342
}
342
343
343
- private bool TryOperator ( char c )
344
+ private bool TryOperator ( )
344
345
{
345
- if ( ! OperatorExpression . IsSymbol ( c ) )
346
+ if ( ! OperatorExpression . IsSymbol ( _currentChar ) )
346
347
return false ;
347
348
348
349
bool repeat ;
349
- string s = c . ToString ( ) ;
350
+ string s = _currentChar . ToString ( ) ;
350
351
351
352
do
352
353
{
@@ -369,23 +370,24 @@ private bool TryOperator(char c)
369
370
return true ;
370
371
}
371
372
372
- private bool TryNumber ( char c , char l )
373
+ private bool TryNumber ( char lastChar )
373
374
{
374
- bool isNumber = NumberExpression . IsNumber ( c ) ;
375
+ bool isNumber = NumberExpression . IsNumber ( _currentChar ) ;
375
376
// only negative when last char is group start or symbol
376
- bool isNegative = NumberExpression . IsNegativeSign ( c ) &&
377
- ( l == '\0 ' || l == '(' || OperatorExpression . IsSymbol ( l ) ) ;
377
+ bool isNegative = NumberExpression . IsNegativeSign ( _currentChar ) &&
378
+ ( lastChar == '\0 ' || lastChar == '(' || OperatorExpression . IsSymbol ( lastChar ) ) ;
378
379
379
380
if ( ! isNumber && ! isNegative )
380
381
return false ;
381
382
382
383
_buffer . Length = 0 ;
383
- _buffer . Append ( c ) ;
384
+ _buffer . Append ( _currentChar ) ;
384
385
385
386
char p = ( char ) _expressionReader . Peek ( ) ;
386
387
while ( NumberExpression . IsNumber ( p ) )
387
388
{
388
- _buffer . Append ( ( char ) _expressionReader . Read ( ) ) ;
389
+ _currentChar = ( char ) _expressionReader . Read ( ) ;
390
+ _buffer . Append ( _currentChar ) ;
389
391
p = ( char ) _expressionReader . Peek ( ) ;
390
392
}
391
393
0 commit comments