This repository has been archived by the owner on May 18, 2019. It is now read-only.
/
NFExpression.mo
3283 lines (2803 loc) · 89.3 KB
/
NFExpression.mo
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF GPL VERSION 3 LICENSE OR
* THIS OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from OSMC, either from the above address,
* from the URLs: http://www.ida.liu.se/projects/OpenModelica or
* http://www.openmodelica.org, and in the OpenModelica distribution.
* GNU version 3 is obtained from: http://www.gnu.org/copyleft/gpl.html.
*
* This program is distributed WITHOUT ANY WARRANTY; without
* even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
* IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS OF OSMC-PL.
*
* See the full OSMC Public License conditions for more details.
*
*/
encapsulated uniontype NFExpression
protected
import Util;
import Absyn;
import List;
import Builtin = NFBuiltin;
import BuiltinCall = NFBuiltinCall;
import Expression = NFExpression;
import Function = NFFunction;
import RangeIterator = NFRangeIterator;
import NFPrefixes.Variability;
import Prefixes = NFPrefixes;
import Ceval = NFCeval;
import ComplexType = NFComplexType;
import MetaModelica.Dangerous.listReverseInPlace;
public
import Absyn.Path;
import DAE;
import NFInstNode.InstNode;
import Operator = NFOperator;
import Subscript = NFSubscript;
import Dimension = NFDimension;
import Type = NFType;
import ComponentRef = NFComponentRef;
import NFCall.Call;
import NFBinding.Binding;
import NFComponent.Component;
import NFClassTree.ClassTree;
import NFClass.Class;
record INTEGER
Integer value;
end INTEGER;
record REAL
Real value;
end REAL;
record STRING
String value;
end STRING;
record BOOLEAN
Boolean value;
end BOOLEAN;
record ENUM_LITERAL
Type ty;
String name;
Integer index;
end ENUM_LITERAL;
record CREF
Type ty;
ComponentRef cref;
end CREF;
record TYPENAME "Represents a type used as a range, e.g. Boolean."
Type ty;
end TYPENAME;
record ARRAY
Type ty;
list<Expression> elements;
end ARRAY;
record MATRIX "The array concatentation operator [a,b; c,d]; this should be removed during type-checking"
// Does not have a type since we only keep this operator before type-checking
list<list<Expression>> elements;
end MATRIX;
record RANGE
Type ty;
Expression start;
Option<Expression> step;
Expression stop;
end RANGE;
record TUPLE
Type ty;
list<Expression> elements;
end TUPLE;
record RECORD
Path path; // Maybe not needed since the type contains the name. Prefix?
Type ty;
list<Expression> elements;
end RECORD;
record CALL
Call call;
end CALL;
record SIZE
Expression exp;
Option<Expression> dimIndex;
end SIZE;
record END
end END;
record BINARY "Binary operations, e.g. a+4"
Expression exp1;
Operator operator;
Expression exp2;
end BINARY;
record UNARY "Unary operations, -(4x)"
Operator operator;
Expression exp;
end UNARY;
record LBINARY "Logical binary operations: and, or"
Expression exp1;
Operator operator;
Expression exp2;
end LBINARY;
record LUNARY "Logical unary operations: not"
Operator operator;
Expression exp;
end LUNARY;
record RELATION "Relation, e.g. a <= 0"
Expression exp1;
Operator operator;
Expression exp2;
end RELATION;
record IF
Expression condition;
Expression trueBranch;
Expression falseBranch;
end IF;
record CAST
Type ty;
Expression exp;
end CAST;
record UNBOX "MetaModelica value unboxing (similar to a cast)"
Expression exp;
Type ty;
end UNBOX;
record SUBSCRIPTED_EXP
Expression exp;
list<Expression> subscripts;
Type ty;
end SUBSCRIPTED_EXP;
record TUPLE_ELEMENT
Expression tupleExp;
Integer index;
Type ty;
end TUPLE_ELEMENT;
record BOX "MetaModelica boxed value"
Expression exp;
end BOX;
record MUTABLE
Mutable<Expression> exp;
end MUTABLE;
record EMPTY
end EMPTY;
function isCref
input Expression exp;
output Boolean isCref;
algorithm
isCref := match exp
case CREF() then true;
else false;
end match;
end isCref;
function isCall
input Expression exp;
output Boolean isCall;
algorithm
isCall := match exp
case CALL() then true;
else false;
end match;
end isCall;
function isTrue
input Expression exp;
output Boolean isTrue;
algorithm
isTrue := match exp
case BOOLEAN(true) then true;
else false;
end match;
end isTrue;
function isAllTrue
input Expression exp;
output Boolean isTrue;
algorithm
isTrue := match exp
case BOOLEAN(true) then true;
case ARRAY()
algorithm
for e in exp.elements loop
if not isAllTrue(e) then
isTrue := false;
return;
end if;
end for;
then
true;
else false;
end match;
end isAllTrue;
function isFalse
input Expression exp;
output Boolean isTrue;
algorithm
isTrue := match exp
case BOOLEAN(false) then true;
else false;
end match;
end isFalse;
function isEqual
"Returns true if the two expressions are equal, otherwise false."
input Expression exp1;
input Expression exp2;
output Boolean isEqual;
algorithm
isEqual := 0 == compare(exp1, exp2);
end isEqual;
function compare
"Checks whether two expressions are equal, and returns 0 if they are.
If the first expression is 'less' than the second it returns an integer
less than 0, otherwise an integer greater than 0."
input Expression exp1;
input Expression exp2;
output Integer comp;
algorithm
// Check if the expressions are the same object.
if referenceEq(exp1, exp2) then
comp := 0;
return;
end if;
// Return false if the expressions are of different kinds.
comp := Util.intCompare(valueConstructor(exp1), valueConstructor(exp2));
if comp <> 0 then
return;
end if;
comp := match (exp1)
local
Integer i;
Real r;
String s;
Boolean b;
ComponentRef cr;
Type ty;
list<Expression> expl;
Expression e1, e2, e3;
Option<Expression> oe;
Path p;
Operator op;
Call c;
case INTEGER()
algorithm
INTEGER(value = i) := exp2;
then
Util.intCompare(exp1.value, i);
case REAL()
algorithm
REAL(value = r) := exp2;
then
Util.realCompare(exp1.value, r);
case STRING()
algorithm
STRING(value = s) := exp2;
then
Util.stringCompare(exp1.value, s);
case BOOLEAN()
algorithm
BOOLEAN(value = b) := exp2;
then
Util.boolCompare(exp1.value, b);
case CREF()
algorithm
CREF(cref = cr) := exp2;
then
ComponentRef.compare(exp1.cref, cr);
case TYPENAME()
algorithm
TYPENAME(ty = ty) := exp2;
then
valueCompare(exp1.ty, ty);
case ARRAY()
algorithm
ARRAY(ty = ty, elements = expl) := exp2;
comp := valueCompare(ty, exp1.ty);
then
if comp then compareList(exp1.elements, expl) else comp;
case RANGE()
algorithm
RANGE(start = e1, step = oe, stop = e2) := exp2;
comp := compare(exp1.start, e1);
if comp == 0 then
comp := compare(exp1.stop, e2);
if comp == 0 then
comp := compareOpt(exp1.step, oe);
end if;
end if;
then
comp;
case TUPLE()
algorithm
TUPLE(elements = expl) := exp2;
then
compareList(exp1.elements, expl);
case CALL()
algorithm
CALL(call = c) := exp2;
then
Call.compare(exp1.call, c);
case SIZE()
algorithm
SIZE(exp = e1, dimIndex = oe) := exp2;
comp := compareOpt(exp1.dimIndex, oe);
then
if comp == 0 then compare(exp1.exp, e1) else comp;
case END() then 0;
case BINARY()
algorithm
BINARY(exp1 = e1, operator = op, exp2 = e2) := exp2;
comp := Operator.compare(exp1.operator, op);
if comp == 0 then
comp := compare(exp1.exp1, e1);
if comp == 0 then
comp := compare(exp1.exp2, e2);
end if;
end if;
then
comp;
case UNARY()
algorithm
UNARY(operator = op, exp = e1) := exp2;
comp := Operator.compare(exp1.operator, op);
then
if comp == 0 then compare(exp1.exp, e1) else comp;
case LBINARY()
algorithm
LBINARY(exp1 = e1, operator = op, exp2 = e2) := exp2;
comp := Operator.compare(exp1.operator, op);
if comp == 0 then
comp := compare(exp1.exp1, e1);
if comp == 0 then
comp := compare(exp1.exp2, e2);
end if;
end if;
then
comp;
case LUNARY()
algorithm
LUNARY(operator = op, exp = e1) := exp2;
comp := Operator.compare(exp1.operator, op);
then
if comp == 0 then compare(exp1.exp, e1) else comp;
case RELATION()
algorithm
RELATION(exp1 = e1, operator = op, exp2 = e2) := exp2;
comp := Operator.compare(exp1.operator, op);
if comp == 0 then
comp := compare(exp1.exp1, e1);
if comp == 0 then
comp := compare(exp1.exp2, e2);
end if;
end if;
then
comp;
case IF()
algorithm
IF(condition = e1, trueBranch = e2, falseBranch = e3) := exp2;
comp := compare(exp1.condition, e1);
if comp == 0 then
comp := compare(exp1.trueBranch, e2);
if comp == 0 then
comp := compare(exp1.falseBranch, e3);
end if;
end if;
then
comp;
case UNBOX()
algorithm
UNBOX(exp = e1) := exp2;
then
compare(exp1.exp, e1);
case CAST()
algorithm
e1 := match exp2
case CAST(exp = e1) then e1;
case e1 then e1;
end match;
then
compare(exp1.exp, e1);
case SUBSCRIPTED_EXP()
algorithm
SUBSCRIPTED_EXP(exp = e1, subscripts = expl) := exp2;
comp := compare(exp1.exp, e1);
if comp == 0 then
comp := compareList(exp1.subscripts, expl);
end if;
then
comp;
case TUPLE_ELEMENT()
algorithm
TUPLE_ELEMENT(tupleExp = e1, index = i) := exp2;
comp := Util.intCompare(exp1.index, i);
if comp == 0 then
comp := compare(exp1.tupleExp, e1);
end if;
then
comp;
else
algorithm
Error.assertion(false, getInstanceName() + " got unknown expression.", sourceInfo());
then
fail();
end match;
end compare;
function compareOpt
input Option<Expression> expl1;
input Option<Expression> expl2;
output Integer comp;
protected
Expression e1, e2;
algorithm
comp := match(expl1, expl2)
case (NONE(), NONE()) then 0;
case (NONE(), _) then -1;
case (_, NONE()) then 1;
case (SOME(e1), SOME(e2)) then compare(e1, e2);
end match;
end compareOpt;
function compareList
input list<Expression> expl1;
input list<Expression> expl2;
output Integer comp;
protected
Expression e2;
list<Expression> rest_expl2 = expl2;
algorithm
// Check that the lists have the same length, otherwise they can't be equal.
comp := Util.intCompare(listLength(expl1), listLength(expl2));
if comp <> 0 then
return;
end if;
for e1 in expl1 loop
e2 :: rest_expl2 := rest_expl2;
comp := compare(e1, e2);
// Return if the expressions are not equal.
if comp <> 0 then
return;
end if;
end for;
comp := 0;
end compareList;
function typeOf
input Expression exp;
output Type ty;
algorithm
ty := match exp
case INTEGER() then Type.INTEGER();
case REAL() then Type.REAL();
case STRING() then Type.STRING();
case BOOLEAN() then Type.BOOLEAN();
case ENUM_LITERAL() then exp.ty;
case CREF() then exp.ty;
case ARRAY() then exp.ty;
case RANGE() then exp.ty;
case TUPLE() then exp.ty;
case RECORD() then exp.ty;
case CALL() then Call.typeOf(exp.call);
case SIZE() then if isSome(exp.dimIndex) then
Type.INTEGER() else typeOf(exp.exp);
case END() then Type.INTEGER();
case BINARY() then Operator.typeOf(exp.operator);
case UNARY() then Operator.typeOf(exp.operator);
case LBINARY() then Operator.typeOf(exp.operator);
case LUNARY() then Operator.typeOf(exp.operator);
case RELATION() then Operator.typeOf(exp.operator);
case IF() then typeOf(exp.trueBranch);
case CAST() then exp.ty;
case UNBOX() then exp.ty;
case SUBSCRIPTED_EXP() then exp.ty;
case TUPLE_ELEMENT() then exp.ty;
case BOX() then Type.METABOXED(typeOf(exp.exp));
case MUTABLE() then typeOf(Mutable.access(exp.exp));
else Type.UNKNOWN();
end match;
end typeOf;
function setType
input Type ty;
input output Expression exp;
algorithm
() := match exp
case ENUM_LITERAL() algorithm exp.ty := ty; then ();
case CREF() algorithm exp.ty := ty; then ();
case TYPENAME() algorithm exp.ty := ty; then ();
case ARRAY() algorithm exp.ty := ty; then ();
case RANGE() algorithm exp.ty := ty; then ();
case TUPLE() algorithm exp.ty := ty; then ();
case RECORD() algorithm exp.ty := ty; then ();
case CALL() algorithm exp.call := Call.setType(exp.call, ty); then ();
case BINARY() algorithm exp.operator := Operator.setType(ty, exp.operator); then ();
case UNARY() algorithm exp.operator := Operator.setType(ty, exp.operator); then ();
case LBINARY() algorithm exp.operator := Operator.setType(ty, exp.operator); then ();
case LUNARY() algorithm exp.operator := Operator.setType(ty, exp.operator); then ();
case RELATION() algorithm exp.operator := Operator.setType(ty, exp.operator); then ();
case CAST() algorithm exp.ty := ty; then ();
case UNBOX() algorithm exp.ty := ty; then ();
case SUBSCRIPTED_EXP() algorithm exp.ty := ty; then ();
case TUPLE_ELEMENT() algorithm exp.ty := ty; then ();
else ();
end match;
end setType;
function typeCast
input Expression exp;
input Type castTy;
output Expression castExp = CAST(castTy, exp);
end typeCast;
function typeCastElements
input output Expression exp;
input Type ty;
algorithm
exp := match (exp, ty)
local
Type t;
list<Expression> el;
case (INTEGER(), Type.REAL())
then REAL(intReal(exp.value));
case (REAL(), Type.REAL()) then exp;
case (ARRAY(ty = t, elements = el), _)
algorithm
el := list(typeCastElements(e, ty) for e in el);
t := Type.setArrayElementType(t, ty);
then
ARRAY(t, el);
case (UNARY(), _)
then UNARY(exp.operator, typeCastElements(exp.exp, ty));
else
algorithm
t := typeOf(exp);
t := Type.setArrayElementType(t, ty);
then
CAST(t, exp);
end match;
end typeCastElements;
function realValue
input Expression exp;
output Real value;
algorithm
value := match exp
case REAL() then exp.value;
case INTEGER() then intReal(exp.value);
end match;
end realValue;
function integerValue
input Expression exp;
output Integer value;
algorithm
INTEGER(value=value) := exp;
end integerValue;
function applySubscripts
input list<Subscript> subscripts;
input output Expression exp;
algorithm
for sub in subscripts loop
exp := applySubscript(sub, exp);
end for;
end applySubscripts;
function applySubscript
input Subscript sub;
input Expression exp;
output Expression subscriptedExp;
algorithm
subscriptedExp := match sub
case Subscript.INDEX() then applyIndexSubscript(sub.index, exp);
case Subscript.SLICE() then applySliceSubscript(sub.slice, exp);
case Subscript.WHOLE() then exp;
else
algorithm
Error.assertion(false, getInstanceName() + " got untyped subscript " +
Subscript.toString(sub), sourceInfo());
then
fail();
end match;
end applySubscript;
function applyIndexSubscript
input Expression indexExp;
input output Expression exp;
protected
Boolean is_scalar_const;
Expression texp;
Type exp_ty;
ComponentRef cref;
algorithm
is_scalar_const := isScalarLiteral(indexExp);
// check exp has array type. Don't apply subs to scalar exp.
exp_ty := typeOf(exp);
if not Type.isArray(exp_ty) then
Error.assertion(false, getInstanceName() + ": Application of subs on non-array expression not allowed. " +
"Exp: " + toString(exp) + ", Exp type: " + Type.toString(exp_ty) + ", Sub: " + toString(indexExp), sourceInfo());
fail();
end if;
exp := match exp
case CREF()
algorithm
cref := ComponentRef.applyIndexSubscript(Subscript.INDEX(indexExp), exp.cref);
then
CREF(Type.unliftArray(exp.ty), cref);
case TYPENAME() guard is_scalar_const
then applyIndexSubscriptTypename(exp.ty, toInteger(indexExp));
case ARRAY()
algorithm
if is_scalar_const then
texp := listGet(exp.elements, toInteger(indexExp));
else
texp := SUBSCRIPTED_EXP(exp, {indexExp}, Type.unliftArray(exp.ty));
end if;
then
texp;
case RANGE() guard is_scalar_const
then applyIndexSubscriptRange(exp.start, exp.step, exp.stop, toInteger(indexExp));
case CALL(call = Call.TYPED_MAP_CALL())
then applyIndexSubscriptReduction(exp.call, indexExp);
case SUBSCRIPTED_EXP()
then SUBSCRIPTED_EXP(exp.exp, listAppend(exp.subscripts,{indexExp}), Type.unliftArray(exp.ty));
else SUBSCRIPTED_EXP(exp, {indexExp}, Type.unliftArray(exp_ty));
end match;
end applyIndexSubscript;
function applyIndexSubscriptTypename
input Type ty;
input Integer index;
output Expression subscriptedExp;
algorithm
subscriptedExp := match ty
case Type.BOOLEAN() guard index <= 2
then if index == 1 then Expression.BOOLEAN(false) else Expression.BOOLEAN(true);
case Type.ENUMERATION()
then Expression.ENUM_LITERAL(ty, Type.nthEnumLiteral(ty, index), index);
end match;
end applyIndexSubscriptTypename;
function applyIndexSubscriptRange
input Expression startExp;
input Option<Expression> stepExp;
input Expression stopExp;
input Integer index;
output Expression subscriptedExp;
protected
Integer iidx;
Real ridx;
algorithm
subscriptedExp := match (startExp, stepExp)
case (Expression.INTEGER(), SOME(Expression.INTEGER(iidx)))
then Expression.INTEGER(startExp.value + index * iidx - 1);
case (Expression.INTEGER(), _)
then Expression.INTEGER(startExp.value + index - 1);
case (Expression.REAL(), SOME(Expression.REAL(ridx)))
then Expression.REAL(startExp.value + index * ridx - 1);
case (Expression.REAL(), _)
then Expression.REAL(startExp.value + index - 1.0);
case (Expression.BOOLEAN(), _)
then if index == 1 then startExp else stopExp;
case (Expression.ENUM_LITERAL(index = iidx), _)
algorithm
iidx := iidx + index - 1;
then
ENUM_LITERAL(startExp.ty, Type.nthEnumLiteral(startExp.ty, iidx), iidx);
end match;
end applyIndexSubscriptRange;
function applyIndexSubscriptReduction
input Call call;
input Expression indexExp;
output Expression subscriptedExp;
protected
Type ty;
Variability var;
Expression exp, iter_exp;
list<tuple<InstNode, Expression>> iters;
InstNode iter;
algorithm
Call.TYPED_MAP_CALL(ty, var, exp, iters) := call;
((iter, iter_exp), iters) := List.splitLast(iters);
iter_exp := applyIndexSubscript(indexExp, iter_exp);
subscriptedExp := replaceIterator(exp, iter, iter_exp);
if not listEmpty(iters) then
subscriptedExp := CALL(Call.TYPED_MAP_CALL(Type.unliftArray(ty), var, subscriptedExp, iters));
end if;
end applyIndexSubscriptReduction;
function replaceIterator
input output Expression exp;
input InstNode iterator;
input Expression iteratorValue;
algorithm
exp := map(exp, function replaceIterator2(iterator = iterator, iteratorValue = iteratorValue));
end replaceIterator;
function replaceIterator2
input output Expression exp;
input InstNode iterator;
input Expression iteratorValue;
algorithm
exp := match exp
local
InstNode node;
case CREF(cref = ComponentRef.CREF(node = node))
then if InstNode.refEqual(iterator, node) then iteratorValue else exp;
else exp;
end match;
end replaceIterator2;
function applySliceSubscript
input Expression slice;
input Expression exp;
output Expression subscriptedExp;
protected
list<Expression> expl;
RangeIterator iter;
Expression e;
Type ty;
ComponentRef cref;
algorithm
// Replace the last dimension of the expression type with the dimension of the slice type.
ty := Type.liftArrayLeft(Type.unliftArray(typeOf(exp)), Type.nthDimension(typeOf(slice), 1));
iter := RangeIterator.fromExp(slice);
if RangeIterator.isValid(iter) then
// If the slice is a range of known size, apply each subscript in the slice
// to the expression and create a new array from the resulting elements.
expl := {};
while RangeIterator.hasNext(iter) loop
(iter, e) := RangeIterator.next(iter);
e := applyIndexSubscript(e, exp);
expl := e :: expl;
end while;
ty := Type.liftArrayLeft(Type.unliftArray(typeOf(exp)), Dimension.fromInteger(listLength(expl)));
subscriptedExp := ARRAY(ty, listReverseInPlace(expl));
else
// If the slice can't be expanded, just add it to the expression as a slice subscript.
subscriptedExp := match exp
case CREF()
algorithm
cref := ComponentRef.addSubscript(Subscript.SLICE(slice), exp.cref);
then
CREF(ty, cref);
case SUBSCRIPTED_EXP()
then SUBSCRIPTED_EXP(exp.exp, listAppend(exp.subscripts, {slice}), ty);
else SUBSCRIPTED_EXP(exp, {slice}, ty);
end match;
end if;
end applySliceSubscript;
function arrayFromList
input list<Expression> inExps;
input Type elemTy;
input list<Dimension> inDims;
output Expression outExp;
algorithm
outExp := arrayFromList_impl(inExps, elemTy, listReverse(inDims));
end arrayFromList;
function arrayFromList_impl
input list<Expression> inExps;
input Type elemTy;
input list<Dimension> inDims;
output Expression outExp;
protected
Dimension ldim;
list<Dimension> restdims;
Type ty;
list<Expression> newlst;
list<list<Expression>> partexps;
Integer dimsize;
algorithm
Error.assertion(not listEmpty(inDims), "Empty dimension list given in arrayFromList.", sourceInfo());
ldim::restdims := inDims;
dimsize := Dimension.size(ldim);
ty := Type.liftArrayLeft(elemTy, ldim);
if List.hasOneElement(inDims) then
Error.assertion(dimsize == listLength(inExps), "Length mismatch in arrayFromList.", sourceInfo());
outExp := ARRAY(ty,inExps);
return;
end if;
partexps := List.partition(inExps, dimsize);
newlst := {};
for arrexp in partexps loop
newlst := ARRAY(ty,arrexp)::newlst;
end for;
newlst := listReverse(newlst);
outExp := arrayFromList_impl(newlst, ty, restdims);
end arrayFromList_impl;
function makeEnumLiteral
input Type enumType;
input Integer index;
output Expression literal;
protected
list<String> literals;
algorithm
Type.ENUMERATION(literals = literals) := enumType;
literal := ENUM_LITERAL(enumType, listGet(literals, index), index);
end makeEnumLiteral;
function makeEnumLiterals
input Type enumType;
output list<Expression> literals;
protected
list<String> lits;
algorithm
Type.ENUMERATION(literals = lits) := enumType;
literals := list(ENUM_LITERAL(enumType, l, i)
threaded for l in lits, i in 1:listLength(lits));
end makeEnumLiterals;
function toInteger
input Expression exp;
output Integer i;
algorithm
i := match exp
case INTEGER() then exp.value;
case BOOLEAN() then if exp.value then 1 else 0;
case ENUM_LITERAL() then exp.index;
else fail();
end match;
end toInteger;
function toStringTyped
input Expression exp;
output String str;
algorithm
str := "/*" + Type.toString(typeOf(exp)) + "*/ " + toString(exp);
end toStringTyped;
function toString
input Expression exp;
output String str;
protected
Type t;
algorithm
str := match exp
case INTEGER() then intString(exp.value);
case REAL() then realString(exp.value);
case STRING() then "\"" + exp.value + "\"";
case BOOLEAN() then boolString(exp.value);
case ENUM_LITERAL(ty = t as Type.ENUMERATION())
then Absyn.pathString(t.typePath) + "." + exp.name;
case CREF() then ComponentRef.toString(exp.cref);
case TYPENAME() then Type.typenameString(Type.arrayElementType(exp.ty));
case ARRAY() then "{" + stringDelimitList(list(toString(e) for e in exp.elements), ", ") + "}";
case MATRIX() then "[" + stringDelimitList(list(stringDelimitList(list(toString(e) for e in el), ", ") for el in exp.elements), "; ") + "]";
case RANGE() then operandString(exp.start, exp, false) +
(
if isSome(exp.step)
then ":" + operandString(Util.getOption(exp.step), exp, false)
else ""
) + ":" + operandString(exp.stop, exp, false);
case TUPLE() then "(" + stringDelimitList(list(toString(e) for e in exp.elements), ", ") + ")";
case RECORD() then List.toString(exp.elements, toString, Absyn.pathString(exp.path), "(", ", ", ")", true);
case CALL() then Call.toString(exp.call);
case SIZE() then "size(" + toString(exp.exp) +
(
if isSome(exp.dimIndex)
then ", " + toString(Util.getOption(exp.dimIndex))
else ""
) + ")";
case END() then "end";
case BINARY() then operandString(exp.exp1, exp, true) +
Operator.symbol(exp.operator) +
operandString(exp.exp2, exp, false);