-
Notifications
You must be signed in to change notification settings - Fork 0
/
RPNEvaluator.cs
31 lines (25 loc) · 1015 Bytes
/
RPNEvaluator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
namespace reverse_polish_notation;
public class RpnEvaluator : IExpressionEvaluator
{
private ICalculator _calculator;
public ICalculator Calculator { get; set; }
public IParser Parser { get; set; }
public RpnEvaluator(ICalculator calculator, IParser parser)
{
Parser = parser;
Calculator = calculator;
// TODO: set the supported operations for Parser obtained from calculator
}
public EvaluationResult Evaluate(string expression)
{
var parsed = Parser.Tokenize(expression);
var tokens = Parser.Lex(parsed);
var result = Calculator.Calculate(tokens);
return new EvaluationResult(result: result);
}
public string Help => "Enter expressions using RPN notation, for instance to calculate:" +
"\n\t2 + 3 * 4" +
"\n\tenter '2 3 4 * +'" +
"\nenter (o)ps to see available operations";
public string Description => "RPN Calculator";
}