-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCalculator.cs
269 lines (257 loc) · 11.7 KB
/
Calculator.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using RT.Util;
namespace TankIconMaker
{
class Calculator
{
protected string Description;
protected string Input;
protected int Pos;
protected char? Cur { get { return Pos >= Input.Length ? null : (char?) Input[Pos]; } }
protected void ConsumeWhitespace()
{
while (Pos < Input.Length && (Input[Pos] == ' ' || Input[Pos] == '\t' || Input[Pos] == '\r' || Input[Pos] == '\n'))
Pos++;
}
protected Exception NewParseException(string message)
{
return new StyleUserError(App.Translation.Calculator.CouldNotParseExpression + ".\n\n"
+ "*{0}:* {1}\n".Fmt(EggsML.Escape(App.Translation.Calculator.ErrLabel_Error), EggsML.Escape(message))
+ "*{0}:* {1}<Red>=\"<\"{2}\">\"={3}".Fmt(
EggsML.Escape(App.Translation.Calculator.ErrLabel_Expression),
EggsML.Escape(Input.Substring(0, Pos)),
EggsML.Escape(App.Translation.Calculator.Err_LocationMarker),
EggsML.Escape(Input.Substring(Pos)))
+ Description,
formatted: true);
}
public double Parse(string expression, string descriptionEggsML = null)
{
Description = string.IsNullOrEmpty(descriptionEggsML) ? "" : ("\n\n" + descriptionEggsML);
Input = expression;
Pos = 0;
ConsumeWhitespace();
double result = ParseExpression();
if (Cur != null)
throw NewParseException(App.Translation.Calculator.Err_ExpectedEndOfExpression);
if (double.IsInfinity(result))
throw NewParseException(App.Translation.Calculator.Err_ResultInfinite);
if (double.IsNaN(result))
throw NewParseException(App.Translation.Calculator.Err_ResultNaN);
return result;
}
private double ParseExpression()
{
double left = ParseExpressionMul(); // additive expressions are left-associative, so build result as we go. First one is mandatory.
while (true)
{
var op = Cur;
if (op != '+' && op != '-')
return left;
Pos++;
ConsumeWhitespace();
double right = ParseExpressionMul();
if (op == '+')
left = left + right;
else if (op == '-')
left = left - right;
else
throw new Exception();
}
}
private double ParseExpressionMul()
{
double left = ParseExpressionPwr(); // multiplicative expressions are left-associative, so build result as we go. First one is mandatory.
while (true)
{
var op = Cur;
if (op != '*' && op != '/' && op != '%')
return left;
Pos++;
ConsumeWhitespace();
double right = ParseExpressionPwr();
if (op == '*')
left = left * right;
else if (op == '/')
left = left / right;
else if (op == '%')
left = Math.IEEERemainder(left, right);
else
throw new Exception();
}
}
private double ParseExpressionPwr()
{
// Power expressions are right-associative, so must parse them all first
var sequence = new List<double>();
sequence.Add(ParseExpressionPrimary()); // the first primary is mandatory
while (true)
{
if (Cur != '^')
break;
Pos++;
ConsumeWhitespace();
sequence.Add(ParseExpressionPrimary());
}
sequence.Reverse();
double result = sequence[0];
foreach (var next in sequence.Skip(1))
result = Math.Pow(next, result);
return result;
}
private double ParseExpressionPrimary()
{
if (Cur == null)
throw NewParseException(App.Translation.Calculator.Err_UnexpectedEndOfExpression);
else if (Cur == '(') // parentheses for precedence
{
Pos++;
ConsumeWhitespace();
var result = ParseExpression();
if (Cur != ')')
throw NewParseException(App.Translation.Calculator.Err_ExpectedParenthesisOrOperator);
Pos++;
ConsumeWhitespace();
return result;
}
else if (Cur == '-') // unary minus
{
Pos++;
ConsumeWhitespace();
return -ParseExpressionPrimary();
}
else if (Cur == '+') // unary plus, a no-op
{
Pos++;
ConsumeWhitespace();
return ParseExpressionPrimary();
}
else if (Cur >= '0' && Cur <= '9') // a numeric literal
{
int fromPos = Pos;
while (Cur >= '0' && Cur <= '9')
Pos++;
if (Cur == '.') // fractional part
{
Pos++;
while (Cur >= '0' && Cur <= '9')
Pos++;
}
string num = Input.Substring(fromPos, Pos - fromPos);
double result;
if (!double.TryParse(num, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, CultureInfo.InvariantCulture, out result))
throw NewParseException(App.Translation.Calculator.Err_CannotParseNumber.Fmt(num));
ConsumeWhitespace();
return result;
}
else if (char.IsLetter(Cur.Value)) // a word or a sequence of words separated by dots
{
int fromPos = Pos;
while (Cur == '.' || (Cur != null && char.IsLetter(Cur.Value)))
Pos++;
string word = Input.Substring(fromPos, Pos - fromPos);
ConsumeWhitespace();
if (Cur == '(')
return EvalFunction(word);
else
return EvalVariable(word);
}
else
throw NewParseException(App.Translation.Calculator.Err_UnexpectedCharacter.Fmt(Cur));
}
private List<double> ParseParameters()
{
if (Cur != '(')
throw new Exception("24567837");
Pos++;
ConsumeWhitespace();
var parameters = new List<double>();
if (Cur == ')')
{
Pos++;
ConsumeWhitespace();
return parameters;
}
while (true)
{
parameters.Add(ParseExpression());
if (Cur == ',')
{
Pos++;
ConsumeWhitespace();
}
else if (Cur == ')')
{
Pos++;
ConsumeWhitespace();
return parameters;
}
else
throw NewParseException(App.Translation.Calculator.Err_ExpectedCommaOrParenthesis);
}
}
protected virtual double EvalVariable(string variable)
{
if (variable.EqualsNoCase("pi"))
return Math.PI;
else if (variable.EqualsNoCase("e"))
return Math.E;
else
throw NewParseException(App.Translation.Calculator.Err_UnknownVariable.Fmt(variable));
}
protected virtual double EvalFunction(string function)
{
List<double> parameters = null;
Action<int, int> parseParams = (int minRequired, int maxRequired) =>
{
parameters = ParseParameters();
if (minRequired == maxRequired && minRequired != -1 && parameters.Count != minRequired)
throw NewParseException(App.Translation.Calculator.Err_FunctionParamCountExact.Fmt(App.Translation, function, minRequired, parameters.Count));
else if (minRequired != -1 && parameters.Count < minRequired)
throw NewParseException(App.Translation.Calculator.Err_FunctionParamCountAtLeast.Fmt(App.Translation, function, minRequired, parameters.Count));
else if (maxRequired != -1 && parameters.Count > maxRequired)
throw NewParseException(App.Translation.Calculator.Err_FunctionParamCountAtMost.Fmt(App.Translation, function, maxRequired, parameters.Count));
};
switch (function.ToLower())
{
case "sqrt": parseParams(1, 1); return Math.Sqrt(parameters[0]);
case "abs": parseParams(1, 1); return Math.Abs(parameters[0]);
case "sign": parseParams(1, 1); return Math.Sign(parameters[0]);
case "ceil": parseParams(1, 1); return Math.Ceiling(parameters[0]);
case "floor": parseParams(1, 1); return Math.Floor(parameters[0]);
case "round": parseParams(1, 1); return Math.Round(parameters[0]);
case "trunc": parseParams(1, 1); return Math.Truncate(parameters[0]);
case "sin": parseParams(1, 1); return Math.Sin(parameters[0]);
case "cos": parseParams(1, 1); return Math.Cos(parameters[0]);
case "tan": parseParams(1, 1); return Math.Tan(parameters[0]);
case "sinh": parseParams(1, 1); return Math.Sinh(parameters[0]);
case "cosh": parseParams(1, 1); return Math.Cosh(parameters[0]);
case "tanh": parseParams(1, 1); return Math.Tanh(parameters[0]);
case "acos": parseParams(1, 1); return Math.Acos(parameters[0]);
case "asin": parseParams(1, 1); return Math.Asin(parameters[0]);
case "atan": parseParams(1, 1); return Math.Atan(parameters[0]);
case "atan2": parseParams(2, 2); return Math.Atan2(parameters[0], parameters[1]);
case "deg": parseParams(1, 1); return Math.PI * parameters[0] / 180.0;
case "exp": parseParams(1, 1); return Math.Exp(parameters[0]);
case "log10": parseParams(1, 1); return Math.Log10(parameters[0]);
case "log2": parseParams(1, 1); return Math.Log(parameters[0], 2);
case "ln": parseParams(1, 1); return Math.Log(parameters[0]);
case "log":
parseParams(1, 2);
if (parameters.Count == 1)
return Math.Log(parameters[0]);
else if (parameters.Count == 2)
return Math.Log(parameters[1], parameters[0]); // base first, number second, like in the actual notation
else
throw new Exception("3135623"); // not reachable
case "min": parseParams(1, -1); return parameters.Min();
case "max": parseParams(1, -1); return parameters.Max();
default:
throw NewParseException(App.Translation.Calculator.Err_UnknownFunction.Fmt(function));
}
}
}
}