/
Exp.mo
8193 lines (7655 loc) · 228 KB
/
Exp.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
package Exp "
This file is part of OpenModelica.
Copyright (c) 1998-2006, Linköpings universitet, Department of
Computer and Information Science, PELAB
All rights reserved.
(The new BSD license, see also
http://www.opensource.org/licenses/bsd-license.php)
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
Neither the name of Linköpings universitet nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
\"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
file: Exp.mo
module: Exp
description: Expressions
This file contains the module `Exp\', which contains data types for
describing expressions, after they have been examined by the
static analyzer in the module `StaticExp\'. There are of course
great similarities with the expression types in the `Absyn\'
module, but there are also several important differences.
No overloading of operators occur, and subscripts have been
checked to see if they are slices. All expressions are also type
consistent, and all implicit type conversions in the AST are made
explicit here.
Some expression simplification and solving is also done here. This is used
for symbolic transformations before simulation, in order to rearrange
equations into a form needed by simulation tools. simplify, solve,
exp_contains, exp_equal are part of this code.
This module also contains functions for printing expressions, to io or to
strings. Also graphviz output is supported.
"
public import Absyn;
public import Graphviz;
public
type Ident = String "- Identifiers
Define `Ident\' as an alias for `string\' and use it for all
identifiers in Modelica." ;
public
uniontype Type "- Basic types
These types are not used as expression types (see the `Types\'
module for expression types). They are used to parameterize
operators which may work on several simple types."
record INT end INT;
record REAL end REAL;
record BOOL end BOOL;
record STRING end STRING;
record ENUM end ENUM;
record OTHER "e.g. complex types, etc." end OTHER;
record T_ARRAY
Type ty;
list<Option<Integer>> arrayDimensions "arrayDimensions" ;
end T_ARRAY;
end Type;
public
uniontype Exp "Expressions
The `Exp\' datatype closely corresponds to the `Absyn.Exp\'
datatype, but is used for statically analyzed expressions. It
includes explicit type promotions and typed (non-overloaded)
operators. It also contains expression indexing with the `ASUB\'
constructor. Indexing arbitrary array expressions is currently
not supported in Modelica, but it is needed here."
record ICONST
Integer integer "Integer constants" ;
end ICONST;
record RCONST
Real real "Real constants" ;
end RCONST;
record SCONST
String string "String constants" ;
end SCONST;
record BCONST
Boolean bool "Bool constants" ;
end BCONST;
record CREF "component references, e.g. a.b{2}.c{1}"
ComponentRef componentRef;
Type ty;
end CREF;
record BINARY "Binary operations, e.g. a+4"
Exp exp1;
Operator operator;
Exp exp2;
end BINARY;
record UNARY "Unary operations, -(4x)"
Operator operator;
Exp exp;
end UNARY;
record LBINARY "Logical binary operations: and, or"
Exp exp1;
Operator operator;
Exp exp2;
end LBINARY;
record LUNARY "Logical unary operations: not"
Operator operator;
Exp exp;
end LUNARY;
record RELATION "Relation, e.g. a <= 0"
Exp exp1;
Operator operator;
Exp exp2;
end RELATION;
record IFEXP "If expressions"
Exp expCond;
Exp expThen;
Exp expElse;
end IFEXP;
record CALL
Absyn.Path path;
list<Exp> expLst;
Boolean tuple_ "tuple" ;
Boolean builtin "builtin Function call" ;
Type ty "The type of the return value, if several return values this is undefined";
end CALL;
record ARRAY
Type ty;
Boolean scalar "scalar for codegen" ;
list<Exp> array "Array constructor, e.g. {1,3,4}" ;
end ARRAY;
record MATRIX
Type ty;
Integer integer;
list<list<tuple<Exp, Boolean>>> scalar "scalar Matrix constructor. e.g. {1,0;0,1}" ;
end MATRIX;
record RANGE
Type ty;
Exp exp;
Option<Exp> expOption;
Exp range "Range constructor, e.g. 1:0.5:10" ;
end RANGE;
record TUPLE
list<Exp> PR "PR. Tuples, used in func calls returning several
arguments" ;
end TUPLE;
record CAST "Cast operator"
Type ty;
Exp exp;
end CAST;
record ASUB "Array subscripts"
Exp exp;
Integer sub;
end ASUB;
record SIZE "The size operator"
Exp exp;
Option<Exp> sz;
end SIZE;
record CODE "Modelica AST constructor"
Absyn.CodeNode code;
Type ty;
end CODE;
record REDUCTION
Absyn.Path path;
Exp expr "expr" ;
Ident ident;
Exp range "range Reduction expression" ;
end REDUCTION;
record END "array index to last element, e.g. a{end}:=1;" end END;
end Exp;
public
uniontype Operator "Operators which are overloaded in the abstract syntax are here
made type-specific. The integer addition operator (`ADD(INT)\')
and the real addition operator (`ADD(REAL)\') are two distinct
operators."
record ADD
Type ty;
end ADD;
record SUB
Type ty;
end SUB;
record MUL
Type ty;
end MUL;
record DIV
Type ty;
end DIV;
record POW
Type ty;
end POW;
record UMINUS
Type ty;
end UMINUS;
record UPLUS
Type ty;
end UPLUS;
record UMINUS_ARR
Type ty;
end UMINUS_ARR;
record UPLUS_ARR
Type ty;
end UPLUS_ARR;
record ADD_ARR
Type ty;
end ADD_ARR;
record SUB_ARR
Type ty;
end SUB_ARR;
record MUL_SCALAR_ARRAY
Type ty "a { b, c }" ;
end MUL_SCALAR_ARRAY;
record MUL_ARRAY_SCALAR
Type ty "{a, b} c" ;
end MUL_ARRAY_SCALAR;
record MUL_SCALAR_PRODUCT
Type ty "{a, b} {c, d}" ;
end MUL_SCALAR_PRODUCT;
record MUL_MATRIX_PRODUCT
Type ty "{{..},..} {{..},{..}}" ;
end MUL_MATRIX_PRODUCT;
record DIV_ARRAY_SCALAR
Type ty "{a, b} / c" ;
end DIV_ARRAY_SCALAR;
record POW_ARR
Type ty;
end POW_ARR;
record AND end AND;
record OR end OR;
record NOT end NOT;
record LESS
Type ty;
end LESS;
record LESSEQ
Type ty;
end LESSEQ;
record GREATER
Type ty;
end GREATER;
record GREATEREQ
Type ty;
end GREATEREQ;
record EQUAL
Type ty;
end EQUAL;
record NEQUAL
Type ty;
end NEQUAL;
record USERDEFINED
Absyn.Path fqName "The FQ name of the overloaded operator function" ;
end USERDEFINED;
end Operator;
public
uniontype ComponentRef "- Component references
CREF_QUAL(...) is used for qualified component names, e.g. a.b.c
CREF_IDENT(..) is used for non-qualifed component names, e.g. x
"
record CREF_QUAL
Ident ident;
list<Subscript> subscriptLst;
ComponentRef componentRef;
end CREF_QUAL;
record CREF_IDENT
Ident ident;
list<Subscript> subscriptLst;
end CREF_IDENT;
end ComponentRef;
public
uniontype Subscript "The `Subscript\' and `ComponentRef\' datatypes are simple
translations of the corresponding types in the `Absyn\' module."
record WHOLEDIM "a{:,1}" end WHOLEDIM;
record SLICE
Exp exp "a{1:3,1}, a{1:2:10,2}" ;
end SLICE;
record INDEX
Exp exp "a[i+1]" ;
end INDEX;
end Subscript;
protected import RTOpts;
protected import Util;
protected import Print;
protected import ModUtil;
protected import Derive;
protected import Dump;
protected import Error;
protected import Debug;
protected import Static;
protected import Env;
protected constant Exp rconstone=RCONST(1.0);
public uniontype IntOp
record MULOP end MULOP;
record DIVOP end DIVOP;
record ADDOP end ADDOP;
record SUBOP end SUBOP;
record POWOP end POWOP;
end IntOp;
public function realToIntIfPossible
"converts to ICONST if possible. If it does not fit, a
RCONST is returned instead."
input Real inVal;
output Exp outVal;
algorithm
outVal := matchcontinue(inVal)
local
Integer i;
case (inVal)
equation
i = realInt(inVal);
then
ICONST(i);
case (inVal)
then
RCONST(inVal);
end matchcontinue;
end realToIntIfPossible;
public function safeIntOp
"Safe mul, add, sub or pow operations for integers.
The function returns an integer if possible, otherwise a real.
"
input Integer val1;
input Integer val2;
input IntOp op;
output Exp outv;
algorithm
outv :=
matchcontinue(val1, val2, op)
local
Real rv1,rv2,rv3;
case (val1,val2, MULOP)
equation
rv1 = intReal(val1);
rv2 = intReal(val2);
rv3 = rv1 *. rv2;
outv = realToIntIfPossible(rv3);
then
outv;
case (val1,val2, DIVOP)
local
Integer ires;
equation
ires = val1 / val2;
then
ICONST(ires);
case (val1,val2, SUBOP)
equation
rv1 = intReal(val1);
rv2 = intReal(val2);
rv3 = rv1 -. rv2;
outv = realToIntIfPossible(rv3);
then
outv;
case (val1,val2, ADDOP)
equation
rv1 = intReal(val1);
rv2 = intReal(val2);
rv3 = rv1 +. rv2;
outv = realToIntIfPossible(rv3);
then
outv;
case (val1,val2, POWOP)
equation
rv1 = intReal(val1);
rv2 = intReal(val2);
rv3 = realPow(rv1,rv2);
outv = realToIntIfPossible(rv3);
then
outv;
end matchcontinue;
end safeIntOp;
public function dumpExpWithTitle
input String title;
input Exp exp;
protected String str;
algorithm
str := dumpExpStr(exp,0);
print(title);
print(str);
print("\n");
end dumpExpWithTitle;
public function dumpExp
input Exp exp;
protected String str;
algorithm
str := dumpExpStr(exp,0);
print(str);
print("--------------------\n");
end dumpExp;
public function crefToPath "function: crefToPath
This function converts a `ComponentRef\' to a `Path\', if possible.
If the component reference contains subscripts, it will silently
fail.
"
input ComponentRef inComponentRef;
output Absyn.Path outPath;
algorithm
outPath:=
matchcontinue (inComponentRef)
local
Ident i;
Absyn.Path p;
ComponentRef c;
case CREF_IDENT(ident = i,subscriptLst = {}) then Absyn.IDENT(i);
case CREF_QUAL(ident = i,subscriptLst = {},componentRef = c)
equation
p = crefToPath(c);
then
Absyn.QUALIFIED(i,p);
end matchcontinue;
end crefToPath;
public function pathToCref "function: pathToCref
This function converts a `Absyn.Path\' to a `ComponentRef\'.
"
input Absyn.Path inPath;
output ComponentRef outComponentRef;
algorithm
outComponentRef:=
matchcontinue (inPath)
local
Ident i;
ComponentRef c;
Absyn.Path p;
case Absyn.IDENT(name = i) then CREF_IDENT(i,{});
case (Absyn.FULLYQUALIFIED(p)) then pathToCref(p);
case Absyn.QUALIFIED(name = i,path = p)
equation
c = pathToCref(p);
then
CREF_QUAL(i,{},c);
end matchcontinue;
end pathToCref;
public function crefStr "function: crefStr
This function simply converts a `ComponentRef\' to a `string\'.
"
input ComponentRef inComponentRef;
output String outString;
algorithm
outString:=
matchcontinue (inComponentRef)
local
Ident s,ns,s1,ss;
ComponentRef n;
case (CREF_IDENT(ident = s)) then s;
case (CREF_QUAL(ident = s,componentRef = n))
equation
ns = crefStr(n);
s1 = stringAppend(s, ".");
ss = stringAppend(s1, ns);
then
ss;
end matchcontinue;
end crefStr;
public function crefModelicaStr "function: crefModelicaStr
Same as cre_str, but uses \'_\' instead of \'.\'
"
input ComponentRef inComponentRef;
output String outString;
algorithm
outString:=
matchcontinue (inComponentRef)
local
Ident s,ns,s1,ss;
ComponentRef n;
case (CREF_IDENT(ident = s)) then s;
case (CREF_QUAL(ident = s,componentRef = n))
equation
ns = crefModelicaStr(n);
s1 = stringAppend(s, "_");
ss = stringAppend(s1, ns);
then
ss;
end matchcontinue;
end crefModelicaStr;
public function crefLastIdent "function: crefLastIdent
author: PA
Returns the last identfifier of a \'ComponentRef\'.
"
input ComponentRef inComponentRef;
output Ident outIdent;
algorithm
outIdent:=
matchcontinue (inComponentRef)
local
Ident id,res;
ComponentRef cr;
case (CREF_IDENT(ident = id)) then id;
case (CREF_QUAL(componentRef = cr))
equation
res = crefLastIdent(cr);
then
res;
end matchcontinue;
end crefLastIdent;
public function crefLastSubs "function: crefLastSubs
Return the last subscripts of a ComponentRef
"
input ComponentRef inComponentRef;
output list<Subscript> outSubscriptLst;
algorithm
outSubscriptLst:=
matchcontinue (inComponentRef)
local
Ident id;
list<Subscript> subs,res;
ComponentRef cr;
case (CREF_IDENT(ident = id,subscriptLst = subs)) then subs;
case (CREF_QUAL(componentRef = cr))
equation
res = crefLastSubs(cr);
then
res;
end matchcontinue;
end crefLastSubs;
public function crefStripPrefix "Strips a prefix/cref from a component reference
"
input ComponentRef cref;
input ComponentRef prefix;
output ComponentRef outCref;
algorithm
outCref := matchcontinue(cref,prefix)
local
list<Subscript> subs1,subs2;
ComponentRef cr1,cr2;
Ident id1,id2;
case(CREF_QUAL(id1,subs1,cr1),CREF_IDENT(id2,subs2))
equation
equality(id1=id2);
true = subscriptEqual(subs1,subs2);
then cr1;
case(CREF_QUAL(id1,subs1,cr1),CREF_QUAL(id2,subs2,cr2))
equation
equality(id1=id2);
true = subscriptEqual(subs1,subs2);
then crefStripPrefix(cr1,cr2);
end matchcontinue;
end crefStripPrefix;
public function crefStripLastIdent "Strips the last part of a component reference, i.e ident and subs"
input ComponentRef inCr;
output ComponentRef outCr;
algorithm
outCr := matchcontinue(inCr)
local Ident id;
list<Subscript> subs;
ComponentRef cr1,cr;
case( CREF_QUAL(id,subs,CREF_IDENT(_,_))) then CREF_IDENT(id,subs);
case(CREF_QUAL(id,subs,cr)) equation
cr1 = crefStripLastIdent(cr);
then CREF_QUAL(id,subs,cr1);
end matchcontinue;
end crefStripLastIdent;
public function crefStripFirstIdent "Strips the first part of a component reference,
i.e the identifier and eventual subscripts
"
input ComponentRef inCr;
output ComponentRef outCr;
algorithm
outCr := matchcontinue(inCr)
local Ident id;
list<Subscript> subs;
ComponentRef cr;
case( CREF_QUAL(id,subs,cr)) then cr;
end matchcontinue;
end crefStripFirstIdent;
public function crefFirstIdent "Returns the first part of a component reference,
i.e the identifier
"
input ComponentRef inCr;
output ComponentRef outCr;
algorithm
outCr := matchcontinue(inCr)
local Ident id;
list<Subscript> subs;
ComponentRef cr;
case( CREF_QUAL(id,subs,cr)) then CREF_IDENT(id,{});
case( CREF_IDENT(id,subs)) then CREF_IDENT(id,{});
end matchcontinue;
end crefFirstIdent;
public function crefStripLastSubs "function: crefStripLastSubs
Strips the last subscripts of a ComponentRef
"
input ComponentRef inComponentRef;
output ComponentRef outComponentRef;
algorithm
outComponentRef:=
matchcontinue (inComponentRef)
local
Ident id;
list<Subscript> subs,s;
ComponentRef cr_1,cr;
case (CREF_IDENT(ident = id,subscriptLst = subs)) then CREF_IDENT(id,{});
case (CREF_QUAL(ident = id,subscriptLst = s,componentRef = cr))
equation
cr_1 = crefStripLastSubs(cr);
then
CREF_QUAL(id,s,cr_1);
end matchcontinue;
end crefStripLastSubs;
public function crefStripLastSubsStringified "function crefStripLastSubsStringified
author: PA
Same as cref_strip_last_subs but works on a stringified component ref
instead.
"
input ComponentRef inComponentRef;
output ComponentRef outComponentRef;
algorithm
outComponentRef:=
matchcontinue (inComponentRef)
local
list<Ident> lst,lst_1;
Ident id_1,id;
ComponentRef cr;
case (CREF_IDENT(ident = id,subscriptLst = {}))
equation
//print("\n +++++++++++++++++++++++++++++ ");print(id);print("\n");
lst = Util.stringSplitAtChar(id, "[");
lst_1 = Util.listStripLast(lst);
id_1 = Util.stringDelimitList(lst_1, "[");
then
CREF_IDENT(id_1,{});
case (cr) then cr;
end matchcontinue;
end crefStripLastSubsStringified;
public function crefContainedIn "function: crefContainedIn
author: PA
Returns true if y is a sub component ref of x.
For instance, b.c. is a sub_component of a.b.c.
"
input ComponentRef inComponentRef1;
input ComponentRef inComponentRef2;
output Boolean outBoolean;
algorithm
outBoolean:=
matchcontinue (inComponentRef1,inComponentRef2)
local
ComponentRef x,y,cr2;
Boolean res;
case (x,y) /* x y */
equation
true = crefEqual(x, y);
then
true;
case (CREF_QUAL(componentRef = cr2),y)
equation
res = crefContainedIn(cr2,y);
then
res;
case (_,_) then false;
end matchcontinue;
end crefContainedIn;
public function crefIsIdent "returns true if ComponentRef is an ident, i.e a => true , a.b => false"
input ComponentRef cr;
output Boolean res;
algorithm
res := matchcontinue(cr)
case(CREF_IDENT(_,_)) then true;
case(_) then false;
end matchcontinue;
end crefIsIdent;
public function crefPrefixOf "function: crefPrefixOf
author: PA
Returns true if y is a prefix of x
For example, a.b is a prefix of a.b.c
"
input ComponentRef inComponentRef1;
input ComponentRef inComponentRef2;
output Boolean outBoolean;
algorithm
outBoolean:=
matchcontinue (inComponentRef1,inComponentRef2)
local
ComponentRef cr1,cr2;
Boolean res;
Ident id1,id2;
list<Subscript> ss1,ss2;
case (cr1,cr2) /* x y */
equation
true = crefEqual(cr1, cr2);
then
true;
case (CREF_QUAL(ident = id1,subscriptLst = ss1,componentRef = cr1),CREF_QUAL(ident = id2,subscriptLst = ss2,componentRef = cr2))
equation
equality(id1 = id2);
true = subscriptEqual(ss1, ss2);
res = crefPrefixOf(cr1, cr2);
then
res;
case (CREF_IDENT(ident = id1,subscriptLst = ss1),CREF_QUAL(ident = id2,subscriptLst = ss2))
equation
equality(id1 = id2);
res = subscriptEqual(ss1, ss2);
then
res;
case (_,_) then false;
end matchcontinue;
end crefPrefixOf;
public function identEqual "function: identEqual
author: PA
Compares two \'Ident\'.
"
input Ident inIdent1;
input Ident inIdent2;
output Boolean outBoolean;
algorithm
outBoolean:=
matchcontinue (inIdent1,inIdent2)
local Ident id1,id2;
case (id1,id2)
equation
equality(id1 = id2);
then
true;
case (_,_) then false;
end matchcontinue;
end identEqual;
public function isRange "function: isRange
Returns true if expression is a range expression.
"
input Exp inExp;
output Boolean outBoolean;
algorithm
outBoolean:=
matchcontinue (inExp)
case RANGE(ty = _) then true;
case _ then false;
end matchcontinue;
end isRange;
public function isOne "function: isOne
Returns true íf an expression is constant and has the value one,
otherwise false
"
input Exp inExp;
output Boolean outBoolean;
algorithm
outBoolean:=
matchcontinue (inExp)
local
Integer ival;
Real rzero,rval;
Boolean res;
Type t;
Exp e;
case (ICONST(integer = ival))
equation
(ival == 1) = true;
then
true;
case (RCONST(real = rval))
equation
rzero = intReal(1) "Due to bug in mmc, go trough a cast from int" ;
(rzero ==. rval) = true;
then
true;
case (CAST(ty = t,exp = e))
equation
res = isOne(e) "Casting to zero is still zero" ;
then
res;
case (_) then false;
end matchcontinue;
end isOne;
public function isZero "function: isZero
Returns true íf an expression is constant and has the value zero,
otherwise false
"
input Exp inExp;
output Boolean outBoolean;
algorithm
outBoolean:=
matchcontinue (inExp)
local
Integer ival;
Real rzero,rval;
Boolean res;
Type t;
Exp e;
case (ICONST(integer = ival))
equation
(ival == 0) = true;
then
true;
case (RCONST(real = rval))
equation
rzero = intReal(0) "Due to bug in mmc, go trough a cast from int" ;
(rzero ==. rval) = true;
then
true;
case (CAST(ty = t,exp = e))
equation
res = isZero(e) "Casting to zero is still zero" ;
then
res;
case (_) then false;
end matchcontinue;
end isZero;
public function isConst "function: isConst
Returns true íf an expression is constant
otherwise false
"
input Exp inExp;
output Boolean outBoolean;
algorithm
outBoolean:=
matchcontinue (inExp)
local
Integer ival;
Real rval;
Boolean bval,res,b1,b2;
Ident sval;
Operator op;
Exp e,e1,e2;
Type t;
case (ICONST(integer = ival)) then true;
case (RCONST(real = rval)) then true;
case (BCONST(bool = bval)) then true;
case (SCONST(string = sval)) then true;
case (UNARY(operator = op,exp = e))
equation
res = isConst(e);
then
res;
case (CAST(ty = t,exp = e)) /* Casting to zero is still zero */
equation
res = isConst(e);
then
res;
case (BINARY(e1,op,e2))
equation
b1 = isConst(e1);
b2 = isConst(e2);
res = boolAnd(b1,b2);
then
res;
case (_) then false;
end matchcontinue;
end isConst;
public function isNotConst "function isNotConst
author: PA
Check if expression is not constant.
"
input Exp e;
output Boolean nb;
Boolean b;
algorithm
b := isConst(e);
nb := boolNot(b);
end isNotConst;
public function isRelation "function: isRelation
Returns true if expression is a function expression.
"
input Exp inExp;
output Boolean outBoolean;
algorithm
outBoolean:=
matchcontinue (inExp)
local
Boolean b1,b2,res;
Exp e1,e2;
case (RELATION(exp1 = _)) then true;
case (LUNARY(exp = RELATION(exp1 = _))) then true;