@@ -20,6 +20,9 @@ Examples using Interpreter pattern:
2020Lexing means extracting textual content into different parts by certain rules.
2121Lexing results are called ** tokens** .
2222
23+ > [ !NOTE]
24+ > This solution only lex integers.
25+
2326``` cs
2427using System .Text ;
2528
@@ -120,15 +123,18 @@ class Number<TValue> : INumericExpressionElement<TValue> where TValue : INumber<
120123```
121124
122125Sub-Expression can have many kinds, like binary operation, unary operation and more...
123- ** In this example we only handles the binary operation.**
126+ An operation should also act like an evaluable object as numeric literal did.
127+
128+ > [ !NOTE]
129+ > ** In this example we only handles the binary operation for integers.**
124130
125131``` cs
126132enum BinaryOperationType { Addition , Substraction }
127133class BinaryOperation <TValue >(
128134 INumericExpressionElement <TValue > left ,
129135 INumericExpressionElement <TValue > right ,
130136 BinaryOperationType type )
131- : INumericExpressionElement <TValue > where TValue : INumber <TValue >
137+ : INumericExpressionElement <TValue > where TValue : INumber <TValue > // [!code highlight]
132138 {
133139
134140 public BinaryOperation () : this (default ! , default ! , default ) { }
@@ -203,3 +209,12 @@ static INumericExpressionElement<TValue> Parse<TValue>(ReadOnlySpan<Token> token
203209 return operation ;
204210}
205211```
212+
213+ Finally we can test it out.
214+
215+ ``` cs
216+ var tokens = Lex (" (1 + 3) - (5 + 2)" );
217+ var operation = Parse <int >(CollectionsMarshal .AsSpan (tokens ));
218+ Console .WriteLine (operation .Value ); // -3
219+ ```
220+ ## ANTLR
0 commit comments