-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpression.cs
More file actions
488 lines (458 loc) · 17.4 KB
/
Expression.cs
File metadata and controls
488 lines (458 loc) · 17.4 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NewPaloAltoTB;
/// <summary>
/// Expression evaluation service
/// </summary>
internal class Expression { //: IExpression { -- embarassing, you can't base an internal class on an interface
/// <summary>
/// Accessable singleton object for this class.
/// </summary>
internal static Expression Shared => shared.Value;
private static readonly Lazy<Expression> shared = new(() => new Expression());
internal CodeParser Parser = CodeParser.Shared;
/// <summary>
/// Evaluate an expression fragment, at 1st level of operator precedence (comparison operators).
/// This type of operation cannot be repeated (1<2<3 and a=b<=c are illegal).
/// </summary>
/// <returns>True if successful, with result in out parameter value.</returns>
/// <exception cref="RuntimeException">Thrown if parsing or calculation fails</exception>
internal bool TryEvaluateExpr(out Value? value) {
/*
;*
;**************************************************************
;*
;* *** EXPR ***
;*
;* 'EXPR' EVALUATES ARITHMETICAL OR LOGICAL EXPRESSIONS.
;* <EXPR>::=<EXPR2>
;* <EXPR2><REL.OP.><EXPR2>
;* WHERE <REL.OP.> IS ONE OF THE OPERATORSs IN TAB8 AND THE
;* RESULT OF THESE OPERATIONS IS 1 IFF TRUE AND 0 IFF FALSE.
;* <EXPR2>::=(+ OR -)<EXPR3>(+ OR -<EXPR3>)(....)
;* WHERE () ARE OPTIONAL AND (....) ARE OPTIONAL REPEATS.
;* <EXPR3>::=<EXPR4>(<* OR /><EXPR4>)(....)
;* <EXPR4>::=<VARIABLE>
;* <FUNCTION>
;* (<EXPR>)
;* <EXPR> IS RECURSIVE SO THAT VARIABLE '@' CAN HAVE AN <EXPR>
;* AS INDEX, FUNCTIONS CAN HAVE AN <EXPR> AS ARGUMENTS, AND
;* <EXPR4> CAN BE AN <EXPR> IN PARENTHESES.
;*
expression tree (for now) <=>, +-, * /,
*/
Value? a, b;
//var oldLinePos = Parser.LinePosition;
try {
a = TryExprComparableTerm();
if (a == null) {
value = null;
return false;
}
var whichOp = Parser.ScanStringTableEntry(["=", "<=", "<>", "<", ">=", ">", "#"]);
if (whichOp == null) { //only found left operand, so return it as result
value = a;
return true;
}
//fix terms into signed short range (-32768..32767)
b = TryExprComparableTerm();
if (b == null) {
throw new RuntimeException($"Value expected after comparison operator.");
}
//if (notbothcompatiblecomparables) { } //but = may allow more types than other relops
a = whichOp switch {
0 => a.EqualTo(b),
1 => a.LessThanOrEqualTo(b),
2 => a.NotEqualTo(b),
3 => a.LessThan(b),
4 => a.GreaterThanOrEqualTo(b),
5 => a.GreaterThan(b),
6 => a.NotEqualTo(b),
_ => null,
};
value = a;
return true;
} catch (Exception) {
value = null;
//return new Value(false);
throw;
}
}
/// <summary>
/// Evaluate an expression fragment, at 2nd level of operator precedence ( [-] a (+|-) b )
/// </summary>
/// <returns></returns>
/// <exception cref="RuntimeException"></exception>
private Value? TryExprComparableTerm() {
Value? a, b;
if (Parser.ScanString("-")) {
//- prefix
a = TryExprAddSubTerm();
if (a == null) {
return null;
}
a = a.NegativeValue();
} else {
a = TryExprAddSubTerm();
if (a == null) {
return null;
}
}
int? match;
while ((match = Parser.ScanStringTableEntry(["+", "-"])) != null) {
b = TryExprAddSubTerm();
if (b == null) {
throw new RuntimeException($"OValue expected after addition/subtraction operator.");
}
//TODO: trap overflow/underflow/incompatible types/etc?
a = match switch {
0 => a.Add(b),
1 => a.Subtract(b),
_ => null,
};
}
//if (a < short.MinValue) {
// throw new RuntimeException("Arithmetic underflow");
//}
//if (a > short.MaxValue) {
// throw new RuntimeException("Arithmetic overflow");
//}
return a;
}
private Value? TryExprAddSubTerm() {
Value? a, b;
a = TryExprMulDivTerm();
if (a == null) {
return null;
}
int? match;
while (true) {
Parser.SkipSpaces();
match = Parser.ScanStringTableEntry(["*", "/", "%", "mod"]);
if (match == null) {
break;
}
b = TryExprMulDivTerm();
if (b == null) {
throw new RuntimeException($"Value expected after multiplication/division operator.");
}
if ((match == 1 || match == 2 || match == 3) && b.IsZero()) {
throw new RuntimeException("Division by zero.");
}
a = match switch {
0 => a.Multiply(b),
1 => a.Divide(b),
2 => a.Modulo(b),
3 => a.Modulo(b),
_ => a,
};
}
//if (a < short.MinValue) {
// throw new RuntimeException("Arithmetic underflow");
//}
//if (a > short.MaxValue) {
// throw new RuntimeException("Arithmetic overflow");
//}
return a!;
}
private Value? TryExprMulDivTerm() {
//test for fn
Value? rslt;
Parser.SkipSpaces();
if (!TryGetLiteral(out rslt) &&
!TryGetFunction(out rslt) &&
!TryGetParen(out rslt) &&
!TryGetVariable(out rslt)) {
//throw new RuntimeException("OValue expected.");
return null;
}
return rslt;
}
private bool TryGetLiteral(out Value value) {
Parser.SkipSpaces();
Value rslt;
if (Parser.ScanLiteralValue(out rslt)) {
value = rslt;
return true;
} else {
value = Value.NullValue;
return false;
}
}
private bool TryGetFunction(out Value value) {
var oldPos = Parser.LinePosition;
var rslt = false;
value = Value.NullValue;
var whichMatch = Parser.ScanStringTableEntry(["RND", "INP", "PEEK", "USR", "ABS", "SIZE"]);
if (whichMatch.HasValue) {
switch (whichMatch.Value) {
case 0: //RND(n)
var a = ParenExpr();
value = new Value(Random.Shared.Next((int)(a!.OValue ?? 1)));
rslt = true;
break;
case 1: //INP(port#)
throw new RuntimeException("Port I/O not supported.");
//break;
case 2: //PEEK(addr)
throw new RuntimeException("Random access to memory not supported.");
//break;
case 3: //USR(address)
throw new RuntimeException("Random memory address execution not supported.");
//break;
case 4: //ABS(n)
a = ParenExpr();
value = new Value(Math.Abs((int)a!.OValue!));
value.OValue = int.Abs((int)(a!.OValue!));
rslt = true;
break;
case 5: //SIZE()
throw new RuntimeException("Function not supported.");
//if (Parser.ScanEmptyParens()) {
// value = short.MaxValue;
// rslt = true;
//}
//break;
default:
//value = 0;
break;
}
}
if (rslt == false) {
Parser.LinePosition = oldPos;
}
return rslt;
}
private bool TryGetVariable(out Value value) {
var oldPos = Parser.LinePosition;
var rslt = false;
value = Value.NullValue;
//two choices here - autoinit undeclared var to zero, or return false.
var vName = Parser.ScanName();
if (vName != null) {
var vVar = Variable.FindVariable(vName);
if (vVar == null) {
//var not previously created, so reject if vname(index...)
if (Parser.ScanChar('(', true) != null) {
throw new RuntimeException("An array must be declared before referencing it.");
} else {
throw new RuntimeException("A variable must be declared before referencing it.");
}
//create scalar with value = 0
//vVar = new Variable(vName: vName, value: 0, autoAddToStore: false); //constructing Variable adds it to Variable.VariableStore
//value = vVar.VarValue;
//rslt = true;
} else {
var indices = Parser.ScanIndices(vVar.DimensionCount);
if (indices == null && vVar.IsArray) {
throw new RuntimeException("Missing or incorrect index value list after array variable.");
}
if (indices != null && !vVar.IsArray) {
throw new RuntimeException("Unexpected index value list following scalar variable.");
}
if (vVar.IsArray && indices != null & vVar.DimensionCount != indices!.Count) {
throw new RuntimeException("Missing or incorrect index value list after array variable.");
}
if (vVar.IsArray) {
value = vVar.ElementValue(indices!) ?? new(0);
} else {
value = vVar.ScalarValue() ?? new(0);
}
rslt = true;
//variable exists, look for index problems
//switch (vVar.VType) {
// case ValueType.Short:
// value = vVar.VValue; // .ShortValue ?? 0;
// if (Parser.ScanChar('(') != null) {
// throw new RuntimeException("Unexpected array index value list after scalar variable.");
// }
// rslt = true;
// break;
// case ValueType.ShortArray:
// var indices = Parser.ScanIndices(vVar.DimensionCount);
// if (indices == null) {
// throw new RuntimeException("Missing or incorrect index value list after array variable.");
// } else {
// value = vVar.ElementValue(indices) ?? 0;
// rslt = true;
// break;
// }
// //if (Parser.ScanChar('[', true) == null) {
// // throw new RuntimeException("Expected: [arrayindex]");
// //}
// //short arrIndex;
// //if (!TryEvaluateExpr(out arrIndex)) {
// // throw new RuntimeException("Expected: arrayindex");
// //}
// //if (Parser.ScanChar(']', true) == null) {
// // throw new RuntimeException("Expected: ]");
// //}
// //value = vVar.ShortElementValue(arrIndex);
// default:
// Parser.LinePosition = oldPos;
// rslt = false;
// break;
//}
}
}
return rslt;
}
//internal bool TryGetVariable(out short value) {
// var oldPos = Parser.LinePosition;
// var rslt = false;
// value = 0;
// //two choices here - autoinit undeclared var to zero, or return false.
// var c = Parser.CurrentChar ?? ' ';
// c = char.ToUpperInvariant(c);
// if (char.IsLetter(c)) {
// var c2 = char.ToUpperInvariant(Parser.NextChar ?? ' ');
// if (!char.IsLetter(c2)) {
// //we found a variable (a single letter followed by nothing or a non-letter) (easy dumb parsing rule)
// var variableName = c.ToString();
// Parser.LinePosition++;
// Variable? vValue;
// if (VariableStore.Shared.Globals.TryGetValue(variableName, out vValue)) {
// var variableValue = vValue.ShortValue ?? 0;
// value = variableValue;
// rslt = true;
// } else {
// vValue = new Variable(variableName, 0);
// VariableStore.Shared.Globals[variableName] = vValue;
// value = 0;
// rslt = true; //allow referencing an uninitialized var (for now)
// }
// }
// } else if (c == '@') {
// var arrIndex = ParenExpr();
// var arr = VariableStore.Shared.Globals["@"];
// if (arr == null) {
// arr = new Variable("@", new short[16384]);
// VariableStore.Shared.Globals["@"] = arr;
// }
// value = ((short[])arr.VValue)[arrIndex];
// rslt = true;
// //the '@[n]' array
// }
// if (rslt == false) {
// Parser.LinePosition = oldPos;
// }
// return rslt;
//}
public Value? ParenExpr() {
Value? rslt = null;
if (Parser.ScanString("(")) {
if (TryEvaluateExpr(out rslt)) {
Parser.SkipSpaces();
if (!Parser.ScanString(")")) {
throw new RuntimeException("Expected ')'.");
}
} else {
throw new RuntimeException("Expected expression.");
}
} else {
throw new RuntimeException("Expected '('.");
}
return rslt;
}
public bool TryGetParen(out Value? value) {
var oldPos = Parser.LinePosition;
Value? rValue = null;
var rslt = false;
if (Parser.ScanString("(")) {
if (TryEvaluateExpr(out rValue)) {
Parser.SkipSpaces();
if (Parser.ScanString(")")) {
rslt = true;
}
} else {
//throw new RuntimeException("Expected ')'."); just return false
}
}
if (rslt == false) {
Parser.LinePosition = oldPos;
}
value = rValue;
return rslt;
}
internal bool TryEvaluateIntExpr(out int indexVal) => throw new NotImplementedException();
}
/*
C++ operator precedence:
https://learn.microsoft.com/en-us/cpp/cpp/cpp-built-in-operators-precedence-and-associativity?view=msvc-170
Operator Description Operator Alternative
Group 1 precedence, no associativity
Scope resolution ::
Group 2 precedence, left to right associativity
Member selection (object or pointer) . or ->
Array subscript []
Function call ()
Postfix increment ++
Postfix decrement --
ValueType name typeid
Constant type conversion const_cast
Dynamic type conversion dynamic_cast
Reinterpreted type conversion reinterpret_cast
Static type conversion static_cast
Group 3 precedence, right to left associativity
Size of object or type sizeof
Prefix increment ++
Prefix decrement --
One's complement ~ compl
Logical not ! not
Unary negation -
Unary plus +
Address-of &
Indirection *
Create object new
Destroy object delete
Cast ()
Group 4 precedence, left to right associativity
Pointer-to-member (objects or pointers) .* or ->*
Group 5 precedence, left to right associativity
Multiplication *
Division /
Modulus %
Group 6 precedence, left to right associativity
Addition +
Subtraction -
Group 7 precedence, left to right associativity
Left shift <<
Right shift >>
Group 8 precedence, left to right associativity
Less than <
Greater than >
Less than or equal to <=
Greater than or equal to >=
Group 9 precedence, left to right associativity
Equality ==
Inequality != not_eq
Group 10 precedence left to right associativity
Bitwise AND & bitand
Group 11 precedence, left to right associativity
Bitwise exclusive OR ^ xor
Group 12 precedence, left to right associativity
Bitwise inclusive OR | bitor
Group 13 precedence, left to right associativity
Logical AND && and
Group 14 precedence, left to right associativity
Logical OR || or
Group 15 precedence, right to left associativity
Conditional ? :
Assignment =
Multiplication assignment *=
Division assignment /=
Modulus assignment %=
Addition assignment +=
Subtraction assignment -=
Left-shift assignment <<=
Right-shift assignment >>=
Bitwise AND assignment &= and_eq
Bitwise inclusive OR assignment |= or_eq
Bitwise exclusive OR assignment ^= xor_eq
throw expression throw
Group 16 precedence, left to right associativity
Comma ,
*/