Skip to content

Commit 2200166

Browse files
committed
grrr
1 parent 8aabe25 commit 2200166

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

docs/document/Csharp Design Patterns/docs/Behavioural/Interpreter.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ Examples using Interpreter pattern:
2020
Lexing means extracting textual content into different parts by certain rules.
2121
Lexing results are called **tokens**.
2222

23+
> [!NOTE]
24+
> This solution only lex integers.
25+
2326
```cs
2427
using System.Text;
2528

@@ -120,15 +123,18 @@ class Number<TValue> : INumericExpressionElement<TValue> where TValue : INumber<
120123
```
121124

122125
Sub-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
126132
enum BinaryOperationType { Addition, Substraction }
127133
class 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

Comments
 (0)