-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathParser.cs
More file actions
140 lines (108 loc) · 3.38 KB
/
MathParser.cs
File metadata and controls
140 lines (108 loc) · 3.38 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
namespace MathExpressionParser;
internal class MathParser(List<Token> tokens) {
public readonly List<Token> Tokens = tokens;
public int Position;
public bool Finished() {
return Check(TokenType.Eof);
}
private Token Peek(int offset = 0) {
return Tokens[Position + offset];
}
private void Advance(int amount = 1) {
Position += amount;
}
private bool Check(params TokenType[] acceptables) {
return acceptables.Contains(Peek().Type);
}
private bool Match(params TokenType[] acceptables) {
bool accepted = Check(acceptables);
if (accepted) {
Advance();
}
return accepted;
}
public Expression[] ExprList() {
var expressions = new List<Expression>();
Advance(); // open par
if (Match(TokenType.ClosePar)) {
return expressions.ToArray();
}
do {
var expr = Expr();
expressions.Add(expr);
} while (Match(TokenType.Comma));
if (!Match(TokenType.ClosePar)) {
throw new MathParseException("Expected )");
}
return expressions.ToArray();
}
public Expression Expr() {
return Subtraction();
}
public Expression Subtraction() {
var left = Addition();
while (Match(TokenType.Minus)) {
var right = Addition();
left = new BinaryExpr(left, TokenType.Minus, right);
}
return left;
}
public Expression Addition() {
var left = Division();
while (Match(TokenType.Plus)) {
var right = Division();
left = new BinaryExpr(left, TokenType.Plus, right);
}
return left;
}
public Expression Division() {
var left = Multiplication();
while (Match(TokenType.Slash)) {
var right = Multiplication();
left = new BinaryExpr(left, TokenType.Slash, right);
}
return left;
}
public Expression Multiplication() {
var left = Unary();
while (Match(TokenType.Star)) {
var right = Unary();
left = new BinaryExpr(left, TokenType.Star, right);
}
return left;
}
public Expression Unary() {
if (Match(TokenType.Minus)) {
return new UnaryExpr(Unary(), TokenType.Minus);
}
return Exponent();
}
public Expression Exponent() {
var left = Primary();
if (Match(TokenType.Caret)) {
return new BinaryExpr(left, TokenType.Caret, Exponent());
}
return left;
}
public Expression Primary() {
if (Match(TokenType.Number)) {
var token = Peek(-1);
return new NumberExpr((decimal) token.Value!);
}
if (Match(TokenType.Identifier)) {
string id = (string) Peek(-1).Value!;
if (Check(TokenType.OpenPar)) {
return new CallExpr(id, ExprList());
}
return new VariableExpr(id);
}
if (Match(TokenType.OpenPar)) {
var nestedExpr = Expr();
if (!Match(TokenType.ClosePar)) {
throw new MathParseException("Expected )");
}
return nestedExpr;
}
throw new MathParseException("Unexpected token");
}
}