forked from jdehaan2014/GearLanguage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathumath.pas
More file actions
620 lines (537 loc) · 21.3 KB
/
umath.pas
File metadata and controls
620 lines (537 loc) · 21.3 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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
unit uMath;
{ This unit contains standard math functions used in the interpreter.
Copyright (C) 2018 J. de Haan jdehaan2014@gmail.com
This source is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
A copy of the GNU General Public License is available on the World Wide Web at
<http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free
Software Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.
}
{$mode objfpc}{$H+}
{$modeswitch advancedrecords}
interface
uses
Classes, SysUtils, uToken, uError, math, Variants;
type
TMath = record
private const
ErrIncompatibleOperands = 'Incompatible operand types for "%s" operation.';
ErrMustBeBothNumber = 'Both operands must be a number for "%s" operation.';
ErrMustBeNumber = 'Operand must be a number for "%s" operation.';
ErrMustBeBoolean = 'Operand must be a boolean for "%s" operation.';
ErrMustBeBothBoolean = 'Both operands must be a boolean.';
ErrDivByZero = 'Division by zero.';
ErrConcatNotAllowed = 'Both types must be array for concat operation.';
ErrArrayWrongTypes = 'Both variables must be array of same type.';
ErrArrayMismatchElem = 'Mismatch in number of array elements in array operation.';
ErrIncompatibleTypes = 'Incompatible types in assignment: %s vs. %s.';
public
class function _Add(const Left, Right: Variant; Op: TToken): Variant; static;
class function _Sub(const Left, Right: Variant; Op: TToken): Variant; static;
class function _Mul(const Left, Right: Variant; Op: TToken): Variant; static;
class function _Div(const Left, Right: Variant; Op: TToken): Variant; static;
class function _Rem(const Left, Right: Variant; Op: TToken): Variant; static;
class function _Or(const Left, Right: Variant; Op: TToken): Variant; static;
class function _And(const Left, Right: Variant; Op: TToken): Variant; static;
class function _XOr(const Left, Right: Variant; Op: TToken): Variant; static;
class function _Shl(const Left, Right: Variant; Op: TToken): Variant; static;
class function _Shr(const Left, Right: Variant; Op: TToken): Variant; static;
class function _Pow(const Left, Right: Variant; Op: TToken): Variant; static;
class function _Neg(const Value: Variant; Op: TToken): Variant; static;
class function _Not(const Value: Variant; Op: TToken): Variant; static;
class function _EQ(const Left, Right: Variant; Op: TToken): Variant; static;
class function _NEQ(const Left, Right: Variant; Op: TToken): Variant; static;
class function _GT(const Left, Right: Variant; Op: TToken): Variant; static;
class function _GE(const Left, Right: Variant; Op: TToken): Variant; static;
class function _LT(const Left, Right: Variant; Op: TToken): Variant; static;
class function _LE(const Left, Right: Variant; Op: TToken): Variant; static;
class function _In(const Left, Right: Variant; Op: TToken): Variant; static;
class function _Is(const Left, Right: Variant; Op: TToken): Variant; static;
// boolean checks
class function areBothNumber(const Value1, Value2: Variant): Boolean; static;
class function areBothString(const Value1, Value2: Variant): Boolean; static;
class function areBothBoolean(const Value1, Value2: Variant): Boolean; static;
class function oneOfBothBoolean(const Value1, Value2: Variant): Boolean; static;
class function oneOfBothNull(const Value1, Value2: Variant): Boolean; static;
// array math
class function areBothArray(const Left, Right: Variant): Boolean; static;
class function _Concat(const Left, Right: Variant; Op: TToken): Variant; static;
class function sameArrayTypes(const Left, Right: Variant): Boolean; static;
class function addTwoArrays(const Left, Right: Variant; Op: TToken
): Variant; static;
class function addToArray(const Left, Right: Variant; Op: TToken): Variant; static;
class function subTwoArrays(const Left, Right: Variant; Op: TToken
): Variant;static;
class function subNumberFromArray(const Left, Right: Variant; Op: TToken
): Variant;static;
class function mulTwoArrays(const Left, Right: Variant; Op: TToken
): Variant;static;
class function mulNumberToArray(const Left, Right: Variant; Op: TToken
): Variant;static;
class function divTwoArrays(const Left, Right: Variant; Op: TToken
): Variant;static;
class function divArrayByNumber(const Left, Right: Variant; Op: TToken
): Variant;static;
class function negArray(const Value: Variant; Op: TToken): Variant; static;
class function eqTwoArrays(const Left, Right: Variant; Op: TToken): Variant; static;
class function _Dot(const Left, Right: Variant; Op: TToken): Variant;static;
// enum
class procedure CheckSameEnumTypes(const Left, Right: Variant; Token: TToken
); static;
class function areBothEnum(const Left, Right: Variant): Boolean; static;
class function sameEnumTypes(const Left, Right: Variant): Boolean; static;
end;
implementation
uses uClassIntf, uArrayIntf, uArray, uVariantSupport, uEnumIntf;
{ TMath }
class function TMath._Add(const Left, Right: Variant; Op: TToken): Variant;
begin
if VarIsStr(Left) then
Exit(Left + Right.toString);
if VarIsBool(Left) then
Raise ERuntimeError.Create(Op, Format(ErrIncompatibleOperands, ['+']));
if areBothNumber(Left, Right) then
Exit(Left + Right);
if areBothArray(Left, Right) then
Exit(addTwoArrays(Left, Right, Op));
if VarSupports(Left, IArrayInstance) then
Exit(addToArray(Left, Right, Op));
Raise ERuntimeError.Create(Op, Format(ErrIncompatibleOperands, ['+']));
end;
class function TMath._Sub(const Left, Right: Variant; Op: TToken): Variant;
begin
if oneOfBothBoolean(Left, Right) then
Raise ERuntimeError.Create(Op, Format(ErrIncompatibleOperands, ['-']));
if areBothNumber(Left, Right) then
Exit(Left - Right);
if areBothArray(Left, Right) then
Exit(subTwoArrays(Left, Right, Op));
if VarSupports(Left, IArrayInstance) then
Exit(subNumberFromArray(Left, Right, Op));
Raise ERuntimeError.Create(Op, Format(ErrMustBeBothNumber, ['-']));
end;
class function TMath._Mul(const Left, Right: Variant; Op: TToken): Variant;
begin
if oneOfBothBoolean(Left, Right) then
Raise ERuntimeError.Create(Op, Format(ErrIncompatibleOperands, ['*']));
if areBothNumber(Left, Right) then
Exit(Left * Right);
if areBothArray(Left, Right) then
Exit(mulTwoArrays(Left, Right, Op));
if VarSupports(Left, IArrayInstance) then
Exit(mulNumberToArray(Left, Right, Op));
Raise ERuntimeError.Create(Op, Format(ErrMustBeBothNumber, ['*']));
end;
class function TMath._Div(const Left, Right: Variant; Op: TToken): Variant;
begin
if oneOfBothBoolean(Left, Right) then
Raise ERuntimeError.Create(Op, Format(ErrIncompatibleOperands, ['/']));
if areBothNumber(Left, Right) then begin
if Right <> 0 then
Exit(Left / Right)
else
Raise ERuntimeError.Create(Op, ErrDivByZero);
end;
if areBothArray(Left, Right) then
Exit(divTwoArrays(Left, Right, Op));
if VarSupports(Left, IArrayInstance) then
Exit(divArrayByNumber(Left, Right, Op));
Raise ERuntimeError.Create(Op, Format(ErrMustBeBothNumber, ['/']));
end;
class function TMath._Rem(const Left, Right: Variant; Op: TToken): Variant;
begin
if oneOfBothBoolean(Left, Right) then
Raise ERuntimeError.Create(Op, Format(ErrIncompatibleOperands, ['%']));
try
Result := Left mod Right;
Exit(Result);
except
Raise ERuntimeError.Create(Op, Format(ErrMustBeBothNumber, ['%']));
end;
end;
class function TMath._Or(const Left, Right: Variant; Op: TToken): Variant;
begin
if areBothBoolean(Left, Right) then begin
if Left then
Result := Left
else
Result := Right;
end
else if oneOfBothBoolean(Left, Right) then
Raise ERuntimeError.Create(Op, ErrMustBeBothBoolean)
else if areBothNumber(Left, Right) then
Result := Left or Right
else
Raise ERuntimeError.Create(Op, ErrMustBeBothBoolean);
end;
class function TMath._And(const Left, Right: Variant; Op: TToken): Variant;
begin
if areBothBoolean(Left, Right) then begin
if not Left then
Result := Left
else
Result := Right;
end
else if oneOfBothBoolean(Left, Right) then
Raise ERuntimeError.Create(Op, ErrMustBeBothBoolean)
else if areBothNumber(Left, Right) then
Result := Left and Right
else
Raise ERuntimeError.Create(Op, ErrMustBeBothBoolean);
end;
class function TMath._XOr(const Left, Right: Variant; Op: TToken): Variant;
begin
if areBothBoolean(Left, Right) then
Result := Left xor Right
else if oneOfBothBoolean(Left, Right) then
Raise ERuntimeError.Create(Op, ErrMustBeBothBoolean)
else if areBothNumber(Left, Right) then
Result := Left xor Right
else
Raise ERuntimeError.Create(Op, ErrMustBeBothBoolean);
end;
class function TMath._Shl(const Left, Right: Variant; Op: TToken): Variant;
begin
if oneOfBothBoolean(Left, Right) then
Raise ERuntimeError.Create(Op, Format(ErrIncompatibleOperands, ['<<']));
try
Result := Left shl Right;
except
Raise ERuntimeError.Create(Op, Format(ErrMustBeBothNumber, ['<<']));
end;
end;
class function TMath._Shr(const Left, Right: Variant; Op: TToken): Variant;
begin
if oneOfBothBoolean(Left, Right) then
Raise ERuntimeError.Create(Op, Format(ErrIncompatibleOperands, ['>>']));
try
Result := Left shr Right;
except
Raise ERuntimeError.Create(Op, Format(ErrMustBeBothNumber, ['>>']));
end;
end;
class function TMath._Pow(const Left, Right: Variant; Op: TToken): Variant;
begin
if oneOfBothBoolean(Left, Right) then
Raise ERuntimeError.Create(Op, Format(ErrIncompatibleOperands, ['^']));
try
Result := Power(Left, Right);
except
Raise ERuntimeError.Create(Op, Format(ErrMustBeBothNumber, ['^']));
end;
end;
class function TMath._Neg(const Value: Variant; Op: TToken): Variant;
begin
if VarIsBool(Value) then
Raise ERuntimeError.Create(Op, Format(ErrMustBeNumber, ['-']));
if VarType(Value) = varDouble then
Exit( -Value);
if VarSupports(Value, IArrayInstance) then
Exit(negArray(Value, Op));
Raise ERuntimeError.Create(Op, Format(ErrMustBeNumber, ['-']));
end;
class function TMath._Not(const Value: Variant; Op: TToken): Variant;
begin
if VarType(Value) = varBoolean then
Result := not Value
else
Raise ERuntimeError.Create(Op, Format(ErrMustBeBoolean, ['!']));
end;
class function TMath._EQ(const Left, Right: Variant; Op: TToken): Variant;
begin
if oneOfBothNull(Left, Right) then
Exit(Left = Right);
if areBothBoolean(Left, Right) then
Exit(Left = Right);
if areBothNumber(Left, Right) then
Exit(Left = Right);
if areBothString(Left, Right) then
Exit(Left = Right);
if areBothArray(Left, Right) then
Exit(eqTwoArrays(Left, Right, Op));
if areBothEnum(Left, Right) and sameEnumTypes(Left, Right) then
Exit(IEnumInstance(Left).ElemName = IEnumInstance(Right).ElemName);
Raise ERuntimeError.Create(Op, Format(ErrIncompatibleOperands, ['equal']));
end;
class function TMath._NEQ(const Left, Right: Variant; Op: TToken): Variant;
begin
Result := not _EQ(Left, Right, Op);
end;
class function TMath._GT(const Left, Right: Variant; Op: TToken): Variant;
begin
if oneOfBothNull(Left, Right) then
Exit(False);
if areBothNumber(Left, Right) then
Exit(Left > Right);
if areBothString(Left, Right) then
Exit(Left > Right);
Raise ERuntimeError.Create(Op, Format(ErrIncompatibleOperands, ['>']));
end;
class function TMath._GE(const Left, Right: Variant; Op: TToken): Variant;
begin
if oneOfBothNull(Left, Right) then
Exit(False);
if areBothNumber(Left, Right) then
Exit(Left >= Right);
if areBothString(Left, Right) then
Exit(Left >= Right);
Raise ERuntimeError.Create(Op, Format(ErrIncompatibleOperands, ['>=']));
end;
class function TMath._LT(const Left, Right: Variant; Op: TToken): Variant;
begin
if oneOfBothNull(Left, Right) then
Exit(False);
if areBothNumber(Left, Right) then
Exit(Left < Right);
if areBothString(Left, Right) then
Exit(Left < Right);
Raise ERuntimeError.Create(Op, Format(ErrIncompatibleOperands, ['<']));
end;
class function TMath._LE(const Left, Right: Variant; Op: TToken): Variant;
begin
if oneOfBothNull(Left, Right) then
Exit(False);
if areBothNumber(Left, Right) then
Exit(Left <= Right);
if areBothString(Left, Right) then
Exit(Left <= Right);
Raise ERuntimeError.Create(Op, Format(ErrIncompatibleOperands, ['<=']));
end;
class function TMath._In(const Left, Right: Variant; Op: TToken): Variant;
begin
if oneOfBothNull(Left, Right) then
Exit(False);
if VarSupports(Right, IArrayInstance) then
Exit(IArrayInstance(Right).Elements.Find(Left, Op) >= 0);
if VarSupports(Left, IEnumInstance) then
Exit(IEnumInstance(Left).ElemSetName = Right);
Raise ERuntimeError.Create(Op, Format(ErrMustBeBoolean, ['in']));
end;
class function TMath._Is(const Left, Right: Variant; Op: TToken): Variant;
begin
if oneOfBothNull(Left, Right) then
Exit(False);
if VarSupports(Left, IGearInstance) and VarSupports(Right, IClassable) then
Exit(IGearInstance(Left).ClassName = IClassable(Right).Name);
Raise ERuntimeError.Create(Op, Format(ErrIncompatibleOperands, ['is']));
end;
class function TMath.areBothNumber(const Value1, Value2: Variant): Boolean;
begin
Result := VarIsNumeric(Value1) and VarIsNumeric(Value2);
end;
class function TMath.areBothString(const Value1, Value2: Variant): Boolean;
begin
Result := VarIsStr(Value1) and VarIsStr(Value2);
end;
class function TMath.areBothBoolean(const Value1, Value2: Variant): Boolean;
begin
Result := VarIsBool(Value1) and VarIsBool(Value2);
end;
class function TMath.oneOfBothBoolean(const Value1, Value2: Variant): Boolean;
begin
Result := VarIsBool(Value1) or VarIsBool(Value2);
end;
class function TMath.oneOfBothNull(const Value1, Value2: Variant): Boolean;
begin
Result := VarIsNull(Value1) or VarIsNull(Value2);
end;
// ARRAY MATH
class function TMath.areBothArray(const Left, Right: Variant): Boolean;
begin
Result := VarSupports(Left, IArrayInstance) and
VarSupports(Right, IArrayInstance);
end;
class function TMath.sameArrayTypes(const Left, Right: Variant): Boolean;
begin
Result := IArrayInstance(Left).getTypeName = IArrayInstance(Right).getTypeName;
end;
class function TMath._Concat(const Left, Right: Variant; Op: TToken): Variant;
var
newArray: IArrayInstance;
begin
if not areBothArray(Left, Right) then
Raise ERuntimeError.Create(Op, ErrConcatNotAllowed);
if not sameArrayTypes(Left, Right) then
Raise ERuntimeError.Create(Op, ErrArrayWrongTypes);
newArray := IArrayInstance(TArrayInstance.Create(
IArrayInstance(Left).getArrayClass as TArrayClass));
newArray.Elements.AddRange(IArrayInstance(Left).Elements);
newArray.Elements.AddRange(IArrayInstance(Right).Elements);
Result := newArray;
end;
class function TMath.addTwoArrays(const Left, Right: Variant; Op: TToken):Variant;
var
i: Integer;
newArray: IArrayInstance;
begin
if not sameArrayTypes(Left, Right) then
Raise ERuntimeError.Create(Op, ErrArrayWrongTypes);
if IArrayInstance(Left).Count <> IArrayInstance(Right).Count then
Raise ERuntimeError.Create(Op, ErrArrayMismatchElem);
newArray := IArrayInstance(TArrayInstance.Create(
IArrayInstance(Left).getArrayClass as TArrayClass));
for i := 0 to IArrayInstance(Left).Count-1 do
newArray.Elements.Add(
_Add(IArrayInstance(Left)[i], IArrayInstance(Right)[i], Op));
Result := newArray;
end;
class function TMath.addToArray(const Left, Right: Variant; Op: TToken): Variant;
var
i: Integer;
newArray: IArrayInstance;
begin
newArray := IArrayInstance(TArrayInstance.Create(
IArrayInstance(Left).getArrayClass as TArrayClass));
for i := 0 to IArrayInstance(Left).Count-1 do
newArray.Elements.Add(_Add(IArrayInstance(Left)[i], Right, Op));
Result := newArray;
end;
class function TMath.subTwoArrays(
const Left, Right: Variant; Op: TToken): Variant;
var
i: Integer;
newArray: IArrayInstance;
begin
if not sameArrayTypes(Left, Right) then
Raise ERuntimeError.Create(Op, ErrArrayWrongTypes);
if IArrayInstance(Left).Count <> IArrayInstance(Right).Count then
Raise ERuntimeError.Create(Op, ErrArrayMismatchElem);
newArray := IArrayInstance(TArrayInstance.Create(
IArrayInstance(Left).getArrayClass as TArrayClass));
for i := 0 to IArrayInstance(Left).Count-1 do
newArray.Elements.Add(
_Sub(IArrayInstance(Left)[i], IArrayInstance(Right)[i], Op));
Result := newArray;
end;
class function TMath.subNumberFromArray(
const Left, Right: Variant; Op: TToken): Variant;
var
i: Integer;
newArray: IArrayInstance;
begin
newArray := IArrayInstance(TArrayInstance.Create(
IArrayInstance(Left).getArrayClass as TArrayClass));
for i := 0 to IArrayInstance(Left).Count-1 do
newArray.Elements.Add(_Sub(IArrayInstance(Left)[i], Right, Op));
Result := newArray;
end;
class function TMath.mulNumberToArray(
const Left, Right: Variant; Op: TToken): Variant;
var
i: Integer;
newArray: IArrayInstance;
begin
newArray := IArrayInstance(TArrayInstance.Create(
IArrayInstance(Left).getArrayClass as TArrayClass));
for i := 0 to IArrayInstance(Left).Count-1 do
newArray.Elements.Add(_Mul(IArrayInstance(Left)[i], Right, Op));
Result := newArray;
end;
class function TMath.mulTwoArrays(
const Left, Right: Variant; Op: TToken): Variant;
var
i: Integer;
newArray: IArrayInstance;
begin
if not sameArrayTypes(Left, Right) then
Raise ERuntimeError.Create(Op, ErrArrayWrongTypes);
if IArrayInstance(Left).Count <> IArrayInstance(Right).Count then
Raise ERuntimeError.Create(Op, ErrArrayMismatchElem);
newArray := IArrayInstance(TArrayInstance.Create(
IArrayInstance(Left).getArrayClass as TArrayClass));
for i := 0 to IArrayInstance(Left).Count-1 do
newArray.Elements.Add(
_Mul(IArrayInstance(Left)[i], IArrayInstance(Right)[i], Op));
Result := newArray;
end;
class function TMath.divArrayByNumber(
const Left, Right: Variant; Op: TToken): Variant;
var
i: Integer;
newArray: IArrayInstance;
begin
newArray := IArrayInstance(TArrayInstance.Create(
IArrayInstance(Left).getArrayClass as TArrayClass));
for i := 0 to IArrayInstance(Left).Count-1 do
newArray.Elements.Add(_Div(IArrayInstance(Left)[i], Right, Op));
Result := newArray;
end;
class function TMath.divTwoArrays(
const Left, Right: Variant; Op: TToken): Variant;
var
i: Integer;
newArray: IArrayInstance;
begin
if not sameArrayTypes(Left, Right) then
Raise ERuntimeError.Create(Op, ErrArrayWrongTypes);
if IArrayInstance(Left).Count <> IArrayInstance(Right).Count then
Raise ERuntimeError.Create(Op, ErrArrayMismatchElem);
newArray := IArrayInstance(TArrayInstance.Create(
IArrayInstance(Left).getArrayClass as TArrayClass));
for i := 0 to IArrayInstance(Left).Count-1 do
newArray.Elements.Add(
_Div(IArrayInstance(Left)[i], IArrayInstance(Right)[i], Op));
Result := newArray;
end;
class function TMath.negArray(const Value: Variant; Op: TToken): Variant;
var
i: Integer;
newArray: IArrayInstance;
begin
newArray := IArrayInstance(TArrayInstance.Create(
IArrayInstance(Value).getArrayClass as TArrayClass));
for i := 0 to IArrayInstance(Value).Count-1 do
newArray.Elements.Add(_Neg(IArrayInstance(Value)[i], Op));
Result := newArray;
end;
class function TMath.eqTwoArrays(const Left, Right: Variant; Op: TToken
): Variant;
var
i: Integer;
begin
if not sameArrayTypes(Left, Right) then
Exit(False);
if IArrayInstance(Left).Count <> IArrayInstance(Right).Count then
Exit(False);
Result := True;
for i := 0 to IArrayInstance(Left).Count-1 do
if _NEQ(IArrayInstance(Left)[i], IArrayInstance(Right)[i], Op) then
Exit(False);
end;
class function TMath._Dot(const Left, Right: Variant; Op: TToken): Variant;
var
temp: IArrayInstance;
i: Integer;
begin
if areBothArray(Left, Right) then begin
temp := _Mul(IArrayInstance(Left), IArrayInstance(Right), Op);
Result := 0;
for i := 0 to temp.Count-1 do
Result += temp[i];
end
else
Raise ERuntimeError.Create(Op, Format(ErrArrayWrongTypes, ['::']));
end;
{ ENUM }
class function TMath.areBothEnum(const Left, Right: Variant): Boolean;
begin
Result := VarSupports(Left, IEnumInstance) and VarSupports(Right, IEnumInstance);
end;
class function TMath.sameEnumTypes(const Left, Right: Variant): Boolean;
begin
Result := IEnumInstance(Left).EnumName = IEnumInstance(Right).EnumName;
end;
class procedure TMath.CheckSameEnumTypes(const Left, Right: Variant; Token: TToken);
begin
if not VarSupports(Right, IEnumInstance) then
Raise ERuntimeError.Create(Token, Format(ErrIncompatibleTypes,
[IEnumInstance(Left).EnumName, VarTypeAsText(VarType(Right))]));
if not sameEnumTypes(Left, Right) then
Raise ERuntimeError.Create(Token, Format(ErrIncompatibleTypes,
[IEnumInstance(Left).EnumName, IEnumInstance(Right).EnumName]));
end;
end.