@@ -38,7 +38,12 @@ public class Token
3838 double exponent = stack . Pop ( ) ;
3939 stack . Push ( Math . Log ( exponent , stack . Pop ( ) ) ) ;
4040 }
41- } ,
41+ }
42+ } ;
43+
44+ protected static Dictionary < string , Action < Stack < double > > > _constantActions = new Dictionary
45+ < string , Action < Stack < double > > >
46+ {
4247 { "pi" , stack => stack . Push ( Math . PI ) } ,
4348 { "e" , stack => stack . Push ( Math . E ) }
4449 } ;
@@ -108,7 +113,7 @@ public static Token<double> ReadNumberToken(string input, ref int index)
108113 }
109114
110115 /// <summary>
111- /// Reads a bracket, operator or function token in the input string starting at the specified index.
116+ /// Reads a bracket, constant, operator or function token in the input string starting at the specified index.
112117 /// </summary>
113118 /// <param name="input">The input string.</param>
114119 /// <param name="index">The index to start at.</param>
@@ -123,9 +128,9 @@ public static Token<string> ReadStringToken(string input, ref int index)
123128 {
124129 endIndex ++ ;
125130 string currentString = input . Substring ( index , endIndex - index ) ;
126- if ( _functionActions . ContainsKey ( currentString ) || _operatorActions . ContainsKey ( currentString ) ||
127- currentString . IsBracket ( ) ) // Last two conditions to parse e.g. "* sin(5)" correctly
128- break ;
131+ if ( _functionActions . ContainsKey ( currentString ) || _operatorActions . ContainsKey ( currentString ) || _constantActions . ContainsKey ( currentString ) ||
132+ currentString . IsBracket ( ) ) // Last two conditions to parse e.g. "* sin(5)" correctly: As soon as the current token has finished, we should stop reading.
133+ break ; // This indicates that it has been found in the dictionary, so that is already it.
129134 }
130135
131136 string stringToken = input . Substring ( index , endIndex - index ) ;
@@ -136,6 +141,8 @@ public static Token<string> ReadStringToken(string input, ref int index)
136141 newType = TokenType . Operator ;
137142 else if ( stringToken . IsBracket ( ) )
138143 newType = TokenType . Bracket ;
144+ else if ( _constantActions . ContainsKey ( stringToken ) )
145+ newType = TokenType . Constant ;
139146 else
140147 throw new ParserException (
141148 $ "Invalid token: { stringToken } is not a valid function/operator, bracket or number equivalent.") ;
@@ -167,6 +174,11 @@ public void Evaluate(Stack<double> stack)
167174 if ( token2 != null )
168175 _operatorActions [ token2 . Value ] ( stack ) ;
169176 break ;
177+ case TokenType . Constant :
178+ var token3 = this as Token < string > ;
179+ if ( token3 != null )
180+ _constantActions [ token3 . Value ] ( stack ) ;
181+ break ;
170182 }
171183 }
172184
@@ -184,7 +196,7 @@ public static IEnumerable<Token> CalculateInfixTokens(string term)
184196 {
185197 char current = term [ i ] ;
186198 if ( char . IsLetter ( current ) || CharEx . IsMathematicOperator ( current ) || CharEx . IsBracket ( current ) )
187- // Functions/Operators
199+ // Functions/Operators/Constants
188200 {
189201 if ( ( current == '+' || current == '-' ) &&
190202 ( i == 0 ||
@@ -231,6 +243,7 @@ public Token(T value, TokenType type)
231243 switch ( type )
232244 {
233245 case TokenType . Number :
246+ case TokenType . Constant :
234247 Priority = 100 ;
235248 break ;
236249 case TokenType . Operator :
0 commit comments