/
Cevalfunc.mo
1765 lines (1673 loc) · 62.3 KB
/
Cevalfunc.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 Cevalfunc
"Copyright (C) MathCore Engineering AB, 2007
Author: Bjorn Zachrisson
file: Cevalfunc.mo
module: MATHCORE
description: This module constant evaluates userdefined functions, speeds up instantination process.
It includes Constant evaluations of function and algorithm statements.
RCS: $Id$
"
public import Env;
public import Values;
public import Absyn;
public import DAE;
public import SCode;
public import Prefix;
public import Connect;
public import ClassInf;
public import ConnectionGraph;
public import RTOpts;
public import HashTable2;
protected import Ceval;
protected import Util;
protected import Error;
protected import Exp;
protected import Debug;
protected import Lookup;
protected import Static;
protected import Inst;
protected import InnerOuter;
protected import Types;
protected import UnitAbsyn;
protected import ValuesUtil;
protected import ErrorExt;
protected import OptManager;
protected import Dump;
public function cevalUserFunc "Function: cevalUserFunc
This is the main funciton for the class. It will take a userdefined function and \"try\" to
evaluate it. This is to prevent multiple compilation of c files."
input Env.Env env "enviroment for the user-function";
input DAE.Exp callExp "DAE.CALL(userFunc)";
input list<Values.Value> inArgs;
input SCode.Class sc;
input DAE.DAElist daeList;
output Values.Value outVal "The output value";
algorithm
outVal :=
matchcontinue(env,callExp,inArgs,sc,daeList)
local
list<SCode.Element> elementList;
Env.Env env1,env2,env3;
Values.Value retVal;
list<Values.Value> retVals;
Absyn.Path funcpath;
list<DAE.Exp> crefArgs;
String str;
HashTable2.HashTable ht2;
list<tuple<DAE.ComponentRef, DAE.Exp>> replacements;
case(env,(callExp as DAE.CALL(path = funcpath,expLst = crefArgs)),inArgs,
sc as SCode.CLASS(partialPrefix=false,restriction=SCode.R_FUNCTION(),
classDef=SCode.PARTS(elementLst=elementList) ),daeList)
equation
ErrorExt.setCheckpoint("cevalUserFunc");
true = OptManager.setOption("envCache",false);
str = Absyn.pathString(funcpath);
replacements = createReplacementRules(inArgs,elementList);
ht2 = generateHashMap(replacements,HashTable2.emptyHashTable());
str = Util.stringAppendList({"cevalfunc_",str});
env3 = Env.openScope(env, false, SOME(str));
env1 = extendEnvWithInputArgs(env3,elementList,inArgs,crefArgs, ht2);
// print("evalfunc env: " +& Env.printEnvStr(env) +& "\n");
// print("evalfunc env1: " +& Env.printEnvStr(env1) +& "\n");
env2 = evaluateStatements(env1,sc,ht2);
retVals = getOutputVarValues(elementList, env2);
retVal = convertOutputVarValues(retVals);
ErrorExt.rollBack("cevalUserFunc");
true = OptManager.setOption("envCache",true);
then
retVal;
/* Reset sideeffects */
case(env,(callExp as DAE.CALL(path = funcpath,expLst = crefArgs)),inArgs,
sc as SCode.CLASS(partialPrefix=false,restriction=SCode.R_FUNCTION(),
classDef=SCode.PARTS(elementLst=elementList) ),daeList)
equation
ErrorExt.rollBack("cevalUserFunc");
_ = OptManager.setOption("envCache",true);
then fail();
case(env,(callExp as DAE.CALL(path = funcpath,expLst = crefArgs)),inArgs,
sc as SCode.CLASS(partialPrefix=false,restriction=SCode.R_FUNCTION(),
classDef=SCode.PARTS(elementLst=elementList) ),daeList)
equation
true = RTOpts.debugFlag("failtrace");
_ = extendEnvWithInputArgs(env,elementList,inArgs,crefArgs,HashTable2.emptyHashTable());
str = Absyn.pathString(funcpath);
str = Util.stringAppendList({"- Cevalfunc.evaluateStatements failed for function /* ",str," */\n"});
Debug.fprint("failtrace", str);
then
fail();
case(env,(callExp as DAE.CALL(path = funcpath,expLst = crefArgs)),inArgs,
sc as SCode.CLASS(partialPrefix=false,restriction=SCode.R_FUNCTION(),
classDef=SCode.PARTS(elementLst=elementList) ),daeList)
equation
true = RTOpts.debugFlag("failtrace");
failure(_ = extendEnvWithInputArgs(env,elementList,inArgs,crefArgs,HashTable2.emptyHashTable()));
str = Absyn.pathString(funcpath);
str = Util.stringAppendList({"- Cevalfunc.extendEnvWithInputArgs failed for function /* ",str," */"});
Debug.fprint("failtrace", str);
then
fail();
/* case(_,(callExp as DAE.CALL(path = funcpath,expLst = crefArgs)),_,_,_)
equation print("cevalUserFunc failed for: " +& Absyn.pathString(funcpath) +& "\n");
then
fail();*/
end matchcontinue;
end cevalUserFunc;
protected function extendEnvWithInputArgs "Function: extendEnvWithInputArgs
This function will extend the current functions enviroment with the input argument(/s)
and thier evaluated value."
input Env.Env env;
input list<SCode.Element> functionElements;
input list<Values.Value> elementValues;
input list<DAE.Exp> crefArgs;
input HashTable2.HashTable ht2;
output Env.Env envOut;
algorithm
envOut := matchcontinue(env, functionElements, elementValues, crefArgs, ht2)
local
SCode.Element ele1;
list<SCode.Element> eles1;
SCode.Mod mod1;
Values.Value val1,vv;
list<Values.Value> vals1;
Env.Env env1,env2,complexEnv;
Env.Frame compFrame;
String varName, str;
DAE.Var tvar;
DAE.Attributes tattr;
DAE.Type ty,tty;
DAE.Binding binding,tb;
DAE.ExpType ety;
DAE.Exp e1;
list<DAE.Exp> restExps;
Absyn.ArrayDim adim,ad;
Absyn.ComponentRef cr;
Absyn.Path apath;
ClassInf.State recordconst; // for complex env construction
list<DAE.Var> typeslst;
Option<DAE.Type> cto; // for complex env construction, to here
Option<Absyn.ArrayDim> optAD;
// nothing to do
case(env,{},_,_, ht2) then env;
// handle extends
case(env, (ele1 as SCode.EXTENDS(_,_,_))::eles1, vals1,restExps, ht2)
equation
env1 = extendEnvWithInputArgs(env,eles1,vals1,restExps, ht2);
then
env1;
// handle an input component definition = call(...)
case(env, ((ele1 as SCode.COMPONENT(component = varName,
typeSpec = Absyn.TPATH(path = apath), modifications=mod1, attributes = SCode.ATTR(direction = Absyn.INPUT() ) ))::eles1),
(val1::vals1),((e1 as DAE.CALL(path = _))::restExps), ht2)
equation
(tty as (DAE.T_COMPLEX(recordconst,typeslst,cto,_),_)) = makeComplexForEnv(e1, val1); //Types.expTypetoTypesType(ety);
compFrame = Env.newFrame(false);
complexEnv = makeComplexEnv({compFrame},typeslst);
env1 = Env.extendFrameV(env,
DAE.TYPES_VAR(varName,DAE.ATTR(false,false,SCode.RW(),SCode.VAR(),Absyn.BIDIR(),Absyn.UNSPECIFIED()),
false,tty,DAE.VALBOUND(val1),NONE()), NONE, Env.VAR_TYPED(), complexEnv);
env2 = extendEnvWithInputArgs(env1,eles1,vals1,restExps, ht2);
then
env2;
// handle an input component definition
case(env, ((ele1 as SCode.COMPONENT(component = varName,
typeSpec = Absyn.TPATH(path = apath), modifications=mod1, attributes = SCode.ATTR(direction = Absyn.INPUT() ,arrayDims=adim) ))::eles1),
(val1::vals1),((e1)::restExps), ht2)
equation
tty = Types.typeOfValue(val1);
env1 = Env.extendFrameV(env,
DAE.TYPES_VAR(varName,DAE.ATTR(false,false,SCode.RW(),SCode.VAR(),Absyn.BIDIR(),Absyn.UNSPECIFIED()),
false,tty,DAE.VALBOUND(val1),NONE()), NONE, Env.VAR_TYPED(), {});
env2 = extendEnvWithInputArgs(env1,eles1,vals1,restExps, ht2);
then
env2;
// failed to hanle an input component
case(_, ((ele1 as SCode.COMPONENT(component = varName, typeSpec = Absyn.TPATH(path = apath), attributes = SCode.ATTR(direction = Absyn.INPUT() ) ))::_), _,_, ht2)
equation
Debug.fprint("failtrace", "- Cevalfunc.extendEnvWithInputArgs with input variable failed\n");
then
fail();
//******************* INPUT ARGS ENDS **********************
//***********************************************************
//*************** FUNCTION VARIABLE BEGINS ******************
// no input no output, normal variables
case(env, ((ele1 as SCode.COMPONENT(component=varName,attributes = SCode.ATTR(arrayDims=adim, direction = Absyn.BIDIR()),
typeSpec = Absyn.TPATH(path = apath), modifications = mod1)) ::eles1),
(vals1),restExps, ht2)
equation
(tty as (DAE.T_COMPLEX(_,typeslst,_,_),_) )= getTypeFromName(apath,env);
binding = makeBinding(mod1,env,tty, ht2);
env1 = Env.extendFrameV(env,
DAE.TYPES_VAR(varName,DAE.ATTR(false,false,SCode.RW(),SCode.VAR(),Absyn.BIDIR(),Absyn.UNSPECIFIED()),
false,tty,binding,NONE()), NONE, Env.VAR_TYPED(), {});
env2 = extendEnvWithInputArgs(env1,eles1,vals1,restExps, ht2);
then
env2;
// any other variables (might be output) with arrray dimensions
case(env, ((ele1 as SCode.COMPONENT(component=varName,attributes = SCode.ATTR(arrayDims=adim),
typeSpec = Absyn.TPATH(path = apath,arrayDim = SOME(ad)), modifications = mod1)) ::eles1),
(vals1),restExps, ht2)
equation
tty = getTypeFromName(apath,env);
tty = addDims(tty,adim,env, ht2);
(binding as DAE.VALBOUND(vv)) = makeBinding(mod1,env,tty, ht2);
// print("extendEnvWithInputArgs -> SOME component: " +& varName +& " ty: " +& Types.printTypeStr(tty) +& " opt dim: " +& Dump.printArraydimStr(ad) +& "\n");
env1 = Env.extendFrameV(env,
DAE.TYPES_VAR(varName,DAE.ATTR(false,false,SCode.RW(),SCode.VAR(),Absyn.BIDIR(),Absyn.UNSPECIFIED()),
false,tty,binding,NONE()), NONE, Env.VAR_TYPED(), {});
env2 = extendEnvWithInputArgs(env1,eles1,vals1,restExps, ht2);
then
env2;
// any other variables (might be output) with no arrray dimensions
case(env, ((ele1 as SCode.COMPONENT(component=varName,attributes = SCode.ATTR(arrayDims=adim),
typeSpec = Absyn.TPATH(path = apath,arrayDim = NONE()), modifications = mod1)) ::eles1),
(vals1),restExps, ht2)
equation
// print("extendEnvWithInputArgs -> NONE component: " +& Absyn.pathString(apath) +& "\n");
tty = getTypeFromName(apath,env);
tty = addDims(tty,adim,env, ht2);
(binding as DAE.VALBOUND(vv)) = makeBinding(mod1,env,tty, ht2);
// print("extendEnvWithInputArgs -> NONE component: " +& varName +& " ty: " +& Types.printTypeStr(tty) +& " opt dim: " +& Dump.printArraydimStr(adim) +& "\n");
env1 = Env.extendFrameV(env,
DAE.TYPES_VAR(varName,DAE.ATTR(false,false,SCode.RW(),SCode.VAR(),Absyn.BIDIR(),Absyn.UNSPECIFIED()),
false,tty,binding,NONE()), NONE, Env.VAR_TYPED(), {});
env2 = extendEnvWithInputArgs(env1,eles1,vals1,restExps, ht2);
then
env2;
// failure
case(env, (_::eles1), (vals1),restExps, ht2)
equation
true = RTOpts.debugFlag("failtrace");
Debug.fprintln("failtrace", "- Cevalfunc.extendEnvWithInputArgs failed!");
//env1 = extendEnvWithInputArgs(env,eles1,vals1,restExps);
then
fail();
end matchcontinue;
end extendEnvWithInputArgs;
protected function makeComplexForEnv "Function: makeComplexForEnv
Special case for complex structure"
input DAE.Exp inExp "The call statement";
input Values.Value inVal "Values.RECORD";
output DAE.Type oType;
algorithm (oType) := matchcontinue(inExp, inVal)
local
Absyn.Path recordName;
DAE.ExpType ty;
DAE.Type cty,cty2;
list<DAE.Var> lv,lv2;
String pathName;
list<Values.Value> vals;
list<String> names;
case(DAE.CALL(recordName,_,_,_,ty,_), inVal as Values.RECORD(_,vals,names,-1))
equation
(cty as (DAE.T_COMPLEX(_,lv,_,_),_)) = Types.expTypetoTypesType(ty);
lv2 = setValuesInRecord(lv,names,vals);
cty2 = (DAE.T_COMPLEX(ClassInf.RECORD(recordName) ,lv2 , NONE, NONE),NONE);
then
cty2;
end matchcontinue;
end makeComplexForEnv;
protected function setValuesInRecord "Function: setValuesInRecord
This function sets Values in records. "
input list<DAE.Var> inVars;
input list<String> invarNames; // eq
input list<Values.Value> inValue; // eq
output list<DAE.Var> oType;
algorithm oType := matchcontinue(inVars,invarNames,inValue)
local
String varName;
list<String> varNames;
DAE.Attributes a ;
Boolean p ;
DAE.Type t ;
DAE.Binding b;
Values.Value val;
list<Values.Value> values;
DAE.Var tv,tv1;
list<DAE.Var> tvs,rest;
case({},_,_) then {};
case( tv1 :: rest , varName::varNames, val::values)
equation
tv = setValuesInRecord2(tv1,(varName::varNames),(val::values));
tvs = setValuesInRecord(rest,varNames,values);
then tv::tvs;
end matchcontinue;
end setValuesInRecord;
protected function setValuesInRecord2 "Function: setValuesInRecord2
helper function for setValuesInRecord"
input DAE.Var inVars;
input list<String> invarName; // eq
input list<Values.Value> inValue; // eq
output DAE.Var oType;
algorithm oType := matchcontinue(inVars,invarName,inValue)
local
String varName3,varName2;
list<String> varNames;
DAE.Attributes a;
Boolean p;
DAE.Type t,ty2;
DAE.Binding b;
Values.Value val;
list<Values.Value> values;
DAE.Var tv,tv1;
list<DAE.Var> tvs,rest;
Option<DAE.Const> constOfForIteratorRange;
list<DAE.Var> typeslst,lv2;
list<Values.Value> vals;
list<String> names;
Absyn.Path fpath;
// unbound, try to take the value from the type
case(DAE.TYPES_VAR(varName2,a,p,t,DAE.UNBOUND(),constOfForIteratorRange),{},{})
equation
val = typeOfValue(t);
tv = DAE.TYPES_VAR(varName2,a,p,t,DAE.VALBOUND(val),constOfForIteratorRange);
then
tv;
// value bound
case(tv as DAE.TYPES_VAR(binding = DAE.VALBOUND(val)),{},{})
then
tv;
// complex types (records)
case(DAE.TYPES_VAR(varName3,a,p,t as (DAE.T_COMPLEX(complexVarLst = typeslst),_),b,constOfForIteratorRange),
varName2::varNames, (val as Values.RECORD(fpath,vals,names,-1))::values)
equation
equality(varName3 = varName2);
lv2 = setValuesInRecord(typeslst,names,vals);
ty2 = (DAE.T_COMPLEX(ClassInf.RECORD(fpath) ,lv2 , NONE, NONE),NONE);
tv = DAE.TYPES_VAR(varName3,a,p,ty2,DAE.VALBOUND(val),constOfForIteratorRange);
then tv;
case(DAE.TYPES_VAR(varName3,a,p,t,b,constOfForIteratorRange) ,varName2::varNames, val::values)
equation
equality(varName3 = varName2);
tv = DAE.TYPES_VAR(varName3,a,p,t,DAE.VALBOUND(val),constOfForIteratorRange);
then tv;
case(tv1,varName3::varNames, val::values)
equation
tv = setValuesInRecord2(tv1,varNames,values);
then tv;
end matchcontinue;
end setValuesInRecord2;
protected function makeComplexEnv "Function: makeComplexEnv
This function extends the env with a complex var."
input Env.Env env;
input list<DAE.Var> tvars;
output Env.Env oenv;
algorithm oenv := matchcontinue(env, tvars)
local
ClassInf.State recordconst;
list<DAE.Var> typeslst;
Option<DAE.Type> cto;
String name;// matching
DAE.Attributes attr;
Boolean prot;
DAE.Type ty,bc_ty;
DAE.Binding bind;
Values.Value val;
list<DAE.Var> vars;// matching end
DAE.Var tv;
Env.Env env1,env2,complexEnv;
Option<DAE.Const> constOfForIteratorRange;
// handle nothing
case(env,{}) then env;
// take and flatten the type which should be simple
case(env, (tv as DAE.TYPES_VAR(type_ = ty))::vars)
equation
(ty,_) = Types.flattenArrayType(ty);
Types.simpleType(ty);
// print("makeComplexEnv -> 1 component: " +& name +& " ty: " +& Types.printTypeStr(ty) +& "\n");
env1 = Env.extendFrameV(env, tv, NONE, Env.VAR_TYPED(), {});
env2 = makeComplexEnv(env1, vars);
then
env2;
// record T_ARRAY
// ArrayDim arrayDim "arrayDim";
// Type arrayType "arrayType";
// end T_ARRAY;
case(env, (tv as DAE.TYPES_VAR(name,attr,prot,ty as (DAE.T_ARRAY(_,_),_),bind,constOfForIteratorRange))::vars)
local DAE.Type ty_flat;
equation
//print(" array :: fail " +& name +& ", " +& Types.printTypeStr(ty) +& "\n");
((ty_flat as (DAE.T_COMPLEX(_,typeslst,_,_),_)),_)=Types.flattenArrayType(ty);
//print(" is complex tough\n");
complexEnv = Env.newFrame(false);
//typeslst = Util.listMap1(typeslst,addbindingtodaevar,bind);
complexEnv = makeComplexEnv({complexEnv},typeslst);
// print("makeComplexEnv -> 2 component: " +& name +& " ty: " +& Types.printTypeStr(ty) +& "\n");
env1 = Env.extendFrameV(env,
DAE.TYPES_VAR(name,DAE.ATTR(false,false,SCode.RW(),SCode.VAR(),Absyn.BIDIR(),Absyn.UNSPECIFIED()),
false,ty,bind,constOfForIteratorRange), NONE, Env.VAR_TYPED(), complexEnv);
env2 = makeComplexEnv(env1, vars);
//print(" done complex array\n");
then
env2;
// type X = Real[x];
case(env, (tv as DAE.TYPES_VAR(name,attr,prot,ty as (DAE.T_COMPLEX(_,typeslst,SOME(bc_ty),_),_),bind,constOfForIteratorRange))::vars)
equation
env2 = makeComplexEnv(env, DAE.TYPES_VAR(name,attr,prot,bc_ty,bind,constOfForIteratorRange)::vars);
then
env2;
// no base type
case(env, (tv as DAE.TYPES_VAR(name,attr,prot,ty as (DAE.T_COMPLEX(_,typeslst,_,_),_),_,constOfForIteratorRange))::vars)
equation
complexEnv = Env.newFrame(false);
complexEnv = makeComplexEnv({complexEnv},typeslst);
// print("makeComplexEnv -> 3 component: " +& name +& " ty: " +& Types.printTypeStr(ty) +& "\n");
env1 = Env.extendFrameV(env,
DAE.TYPES_VAR(name,DAE.ATTR(false,false,SCode.RW(),SCode.VAR(),Absyn.BIDIR(),Absyn.UNSPECIFIED()),
false,ty,DAE.UNBOUND(),constOfForIteratorRange), NONE, Env.VAR_TYPED(), complexEnv);
env2 = makeComplexEnv(env1, vars);
then
env2;
// failure
case(_,_)
equation
Debug.fprint("failtrace", "- Cevalfunc.makeComplexEnv failed\n");
then fail();
end matchcontinue;
end makeComplexEnv;
protected function addbindingtodaevar "
add a binding to a variable in environment"
input DAE.Var v;
input DAE.Binding b;
output DAE.Var ov;
algorithm ov := matchcontinue(v,b)
local
String a1;
DAE.Attributes a2;
Boolean a3;
DAE.Type a4;
Option<DAE.Const> a6;
case(DAE.TYPES_VAR(a1,a2,a3,a4,_,a6),b) then DAE.TYPES_VAR(a1,a2,a3,a4,b,a6);
end matchcontinue;
end addbindingtodaevar;
protected function replaceComplex "
Author BZ
Replace CREF_QUAL with correspeonding constant value if availible."
input DAE.Exp inExp;
input HashTable2.HashTable ht2;
output DAE.Exp outExp;
algorithm
outExp := matchcontinue(inExp,ht2)
case(inExp,ht2)
equation
//print(" replace exp: " +& Exp.printExpStr(inExp) +& "\n");
((outExp,_)) = Exp.traverseExp(inExp,qualReplacer,ht2);
//print(" replaced exp: " +& Exp.printExpStr(outExp) +& "\n");
then
outExp;
case(inExp,_) then inExp;
end matchcontinue;
end replaceComplex;
protected function qualReplacer "
The Exp.traverseExp traverse function, "
input tuple<DAE.Exp, HashTable2.HashTable> inTpl;
output tuple<DAE.Exp,HashTable2.HashTable> outTpl;
algorithm
outTpl := matchcontinue(inTpl)
local
DAE.Exp e,tmpExp;
Integer cnt;
String tmpvar;
HashTable2.HashTable ht2;
DAE.ComponentRef cr;
case(((e as DAE.CREF(componentRef = (cr as DAE.CREF_QUAL(identType=_)))),ht2))
equation
tmpExp = HashTable2.get(cr,ht2);
then
((tmpExp,ht2));
case((e,ht2)) then ((e,ht2));
end matchcontinue;
end qualReplacer;
protected function evaluateStatements "Function: evaluateStatements
Intermediate step for evaluating algorithms."
input Env.Env env;
input SCode.Class sc;
input HashTable2.HashTable ht2;
output Env.Env outVal;
algorithm outVal := matchcontinue(env,sc,ht2)
local
list<SCode.Equation> eqs1,eqs2;
list<SCode.Algorithm> algs1,algs2;
Env.Env env1;
HashTable2.HashTable ht2;
case(env, SCode.CLASS(partialPrefix=false,restriction=SCode.R_FUNCTION(),
classDef=SCode.PARTS(normalEquationLst=eqs1,
initialEquationLst=eqs2,
normalAlgorithmLst=algs1,
initialAlgorithmLst=algs2)),
ht2)
equation
env1 = evaluateAlgorithmsList(env,algs1,ht2);
then
env1;
end matchcontinue;
end evaluateStatements;
protected function generateHashMap "
Author BZ
Construct hashmap for cref replacements."
input list<tuple<DAE.ComponentRef, DAE.Exp>> replacements;
input HashTable2.HashTable ht2;
output HashTable2.HashTable ht2_out;
algorithm
ht2_out := matchcontinue(replacements,ht2)
local
DAE.ComponentRef cr;
DAE.Exp e;
case({},ht2) then ht2;
case((cr,e)::replacements,ht2)
equation
ht2 = HashTable2.add((cr,e),ht2);
ht2 = generateHashMap(replacements,ht2);
then
ht2;
end matchcontinue;
end generateHashMap;
protected function evaluateAlgorithmsList "Function: evaluateAlgorithms
Intermediate step for evaluating algorithms."
input Env.Env env;
input list<SCode.Algorithm> inAlgs;
input HashTable2.HashTable ht2;
output Env.Env outEnv;
algorithm outEnv := matchcontinue(env,inAlgs,ht2)
local
list<SCode.Algorithm> algs;
SCode.Algorithm alg;
list<Absyn.Algorithm> alglst;
Env.Env env1,env2;
case(env,{},_) then env;
case(env, (alg as SCode.ALGORITHM(alglst,_)) :: algs,ht2)
equation
(env1) = evaluateAlgorithms(env,alglst,ht2);
(env2) = evaluateAlgorithmsList(env1,algs,ht2);
then
env2;
end matchcontinue;
end evaluateAlgorithmsList;
protected function evaluateAlgorithms "Function: evaluateAlgorithms
helper function for evaluateAlgorithmsList"
input Env.Env env;
input list<Absyn.Algorithm> inAlgs;
input HashTable2.HashTable ht2;
output Env.Env outEnv;
algorithm
outEnv := matchcontinue(env,inAlgs,ht2)
local
list<Absyn.Algorithm> algs;
Absyn.Algorithm alg;
Env.Env env1,env2;
case(env,{},_) then env;
case(env, alg :: algs, ht2)
equation
(env1) = evaluateAlgorithm(env, alg, ht2);
(env2) = evaluateAlgorithms(env1,algs, ht2);
then
env2;
case(env,alg::algs, ht2)
equation
Debug.fprintln("failtrace", "- Cevalfunc.evaluateAlgorithms failed on algorithm:" +& Dump.unparseAlgorithmStr(0, Absyn.ALGORITHMITEM(alg, NONE())));
then
fail();
end matchcontinue;
end evaluateAlgorithms;
protected function evaluateAlgorithm "Function: evaluateAlgorithm
This is the actuall evaluation function.
It matches the incoming algorithm types to a corresponding value/function."
input Env.Env env;
input Absyn.Algorithm alg;
input HashTable2.HashTable ht2;
output Env.Env outEnv;
algorithm
outEnv := matchcontinue(env,alg,ht2)
local
Absyn.Exp ae1,ae2,ae3,cond,msg;
list<Absyn.Exp> crefexps;
DAE.Exp econd,resExp,e1;
Absyn.ComponentRef acr;
DAE.Type t,ty;
Env.Env env1,env2,env3;
Values.Value value,start,step,stop;
list<Values.Value> values;
list<DAE.Type> types;
DAE.Properties prop;
list<Absyn.AlgorithmItem> algitemlst;
String varName;
String estr;
list<tuple<Absyn.Exp, list<Absyn.AlgorithmItem>>> elseifexpitemlist,branches1,branches2;
tuple<Absyn.Exp, list<Absyn.AlgorithmItem>> trueBranch;
list<Absyn.AlgorithmItem> algitemlist,elseitemlist;
// algorithm assign
case(env, Absyn.ALG_ASSIGN(ae1 as Absyn.CREF(_), ae2),ht2)
equation
(_,e1,DAE.PROP(t,_),_,_) = Static.elabExp(Env.emptyCache(),env,ae2,true,NONE,false);
e1 = replaceComplex(e1,ht2);
(_,value,_) = Ceval.ceval(Env.emptyCache(),env, e1, true, NONE, NONE, Ceval.MSG());
env1 = setValue(value, env, ae1);
then
env1;
// assign, tuple assign
case(env, Absyn.ALG_ASSIGN(assignComponent = Absyn.TUPLE(expressions = crefexps),value = ae1),ht2)
equation
(_,resExp,prop,_,_) = Static.elabExp(Env.emptyCache(),env, ae1, true, NONE,true);
resExp = replaceComplex(resExp,ht2);
((DAE.T_TUPLE(types),_)) = Types.getPropType(prop);
(_,Values.TUPLE(values),_) = Ceval.ceval(Env.emptyCache(),env, resExp, true, NONE, NONE, Ceval.MSG());
env1 = setValues(crefexps,types,values,env);
then
env1;
//while case
case(env, Absyn.ALG_WHILE(boolExpr = ae1,whileBody = algitemlst),ht2)
equation
value = evaluateSingleExpression(ae1,env,NONE,ht2);
env1 = evaluateConditionalStatement(value, ae1, algitemlst,env,ht2);
then
env1;
// for loop with a range without step
case(env, Absyn.ALG_FOR({(varName, SOME(Absyn.RANGE(start=ae1,step=NONE, stop=ae2)))},forBody = algitemlst),ht2)
equation
start = evaluateSingleExpression(ae1,env,NONE,ht2);
// constant range due to ceval of start/stop
env1 = addForLoopScope(env,varName,start,SCode.VAR(),SOME(DAE.C_CONST()));
stop = evaluateSingleExpression(ae2,env1,NONE,ht2);
step = Values.INTEGER(1);
env2 = evaluateForLoopRange(env1, varName, algitemlst, start, step, stop, ht2);
then
env2;
// for loop with a range with step
case(env, Absyn.ALG_FOR({(varName, SOME(Absyn.RANGE(start=ae1, step=SOME(ae2), stop=ae3)))},forBody = algitemlst),ht2)
equation
start = evaluateSingleExpression(ae1,env,NONE,ht2);
// constant range due to ceval of start/stop/step
env1 = addForLoopScope(env,varName,start,SCode.VAR(),SOME(DAE.C_CONST()));
stop = evaluateSingleExpression(ae3,env1,NONE,ht2);
step = evaluateSingleExpression(ae2,env1,NONE,ht2);
env2 = evaluateForLoopRange(env1, varName, algitemlst, start, step, stop, ht2);
then
env2;
// some other expression for range, such as an array!
case(env, Absyn.ALG_FOR({(varName, SOME(ae1))},forBody = algitemlst),ht2)
equation
(Values.ARRAY(valueLst = values)) = evaluateSingleExpression(ae1,env,NONE,ht2);
start = listNth(values,0);
// constant range due to ceval of range expression
env1 = addForLoopScope(env,varName,start,SCode.VAR(),SOME(DAE.C_CONST()));
env2 = evaluateForLoopArray(env1, varName, values, algitemlst, ht2);
then
env2;
// error for unknown range
case(env,Absyn.ALG_FOR(iterators = {(_,SOME(ae1))}),ht2)
equation
(_,e1,_,_,_) = Static.elabExp(Env.emptyCache(),env, ae1, true, NONE,true);
estr = Exp.printExpStr(e1);
Error.addMessage(Error.NOT_ARRAY_TYPE_IN_FOR_STATEMENT, {estr});
then
fail();
// if-case
case(env, Absyn.ALG_IF(ifExp = ae1,trueBranch = algitemlst,elseIfAlgorithmBranch = elseifexpitemlist, elseBranch = elseitemlist),ht2)
equation
trueBranch = (ae1,algitemlst);
branches1 = (trueBranch :: elseifexpitemlist);
branches2 = listAppend(branches1, {(Absyn.BOOL(true),elseitemlist)});
env1 = evaluateIfStatementLst(branches2, env,ht2);
then
env1;
// assert(true, ...) gives nothing!
case(env, Absyn.ALG_NORETCALL(functionCall = Absyn.CREF_IDENT(name = "assert"),
functionArgs = Absyn.FUNCTIONARGS(args = {cond,msg})),ht2)
equation
(_,econd,_,_,_) = Static.elabExp(Env.emptyCache(), env, cond, true, NONE,true);
(_,Values.BOOL(true),_) = Ceval.ceval(Env.emptyCache(),env, econd, true, NONE, NONE, Ceval.MSG());
then
env;
// assert(false, ...) gives error!
case(env, Absyn.ALG_NORETCALL(functionCall = Absyn.CREF_IDENT(name = "assert"),
functionArgs = Absyn.FUNCTIONARGS(args = {cond,msg})),ht2)
equation
(_,econd,_,_,_) = Static.elabExp(Env.emptyCache(), env, cond, true, NONE,true);
(_,Values.BOOL(false),_) = Ceval.ceval(Env.emptyCache(),env, econd, true, NONE, NONE, Ceval.MSG());
(_,e1,_,_,_) = Static.elabExp(Env.emptyCache(), env, msg, true, NONE,true);
(_,Values.STRING(varName),_) = Ceval.ceval(Env.emptyCache(),env, e1, true, NONE, NONE, Ceval.MSG());
Error.addMessage(Error.ASSERT_FAILED, {varName});
then
fail();
end matchcontinue;
end evaluateAlgorithm;
protected function evaluateIfStatementLst "
Evaluates all parts of a if statement (i.e. a list of exp statements)"
input list<tuple<Absyn.Exp, list<Absyn.AlgorithmItem>>> inIfs;
input Env.Env env;
input HashTable2.HashTable ht2;
output Env.Env oenv;
algorithm oenv := matchcontinue(inIfs,env,ht2)
local
Values.Value value;
Absyn.Exp ae1;
list<Absyn.AlgorithmItem> algitemlst;
list<tuple<Absyn.Exp, list<Absyn.AlgorithmItem>>> algrest;
Env.Env env1;
case({},env,ht2) then env;
case(((ae1,algitemlst)::algrest),env,ht2)
equation
value = evaluateSingleExpression(ae1,env,NONE,ht2);
env1 = evaluatePartOfIfStatement(value, ae1, algitemlst, algrest, env,ht2);
then
env1;
end matchcontinue;
end evaluateIfStatementLst;
protected function evaluatePartOfIfStatement "function: evaluatePartOfIfStatement
Evaluates one part of a if statement, i.e. one \"case\". If the condition is true, the algorithm items
associated with this condition are evaluated. The first argument returned is set to true if the
condition was evaluated to true. Fails if the value is not a boolean.
Note that we are sending the expression as an value, so that it does not need to be evaluated twice."
input Values.Value inValue;
input Absyn.Exp inExp;
input list<Absyn.AlgorithmItem> inAbsynAlgorithmItemLst;
input list<tuple<Absyn.Exp, list<Absyn.AlgorithmItem>>> inTplAbsynExpAbsynAlgorithmItemLstLst;
input Env.Env env;
input HashTable2.HashTable ht2;
output Env.Env oenv;
algorithm
oenv := matchcontinue (inValue,inExp,inAbsynAlgorithmItemLst,inTplAbsynExpAbsynAlgorithmItemLstLst,env,ht2)
local
Env.Env env1;
Boolean exp_val;
list<Absyn.AlgorithmItem> algitemlst;
list<tuple<Absyn.Exp, list<Absyn.AlgorithmItem>>> algrest;
String estr,tstr;
tuple<DAE.TType, Option<Absyn.Path>> vtype;
Values.Value value;
Absyn.Exp exp;
DAE.Exp daeExp;
// select if condition is true
case (Values.BOOL(boolean = true),_,algitemlst,_,env, ht2)
equation
env1 = evaluateConditionalStatement2(algitemlst, env, ht2);
then
env1;
// select if condition is false
case (Values.BOOL(boolean = false),_,algitemlst,algrest,env, ht2)
equation
env1 = evaluateIfStatementLst(algrest, env, ht2);
then
env1;
// handle failure, report type error
case (value,exp,algitemlst,algrest,env, ht2)
equation
(_,daeExp,_,_,_) = Static.elabExp(Env.emptyCache(),env,inExp,true,NONE,false);
estr = Exp.printExpStr(daeExp);
vtype = Types.typeOfValue(value);
tstr = Types.unparseType(vtype);
Error.addMessage(Error.IF_CONDITION_TYPE_ERROR, {estr,tstr});
then
fail();
end matchcontinue;
end evaluatePartOfIfStatement;
protected function evaluateSingleExpression "Function: evaluateSingleExpression
This function evaluates a single expression(mostly used in condition evaluation, for/while/if).
It also has an optional DAE.Type input if we want a specific type we will try to cast the input
to that type."
input Absyn.Exp inExp "The Absyn Expression to evaluate";
input Env.Env env "Current enviroment";
input Option<DAE.Type> expectedType "SOME(DAE.Type) (convert into that type)";
input HashTable2.HashTable ht2;
output Values.Value oval;
algorithm oval := matchcontinue(inExp,env,expectedType,ht2)
local
DAE.Exp e1,e2;
Values.Value value;
DAE.Type ty,ty2;
// no type to convert into
case(inExp,env,NONE,ht2)
equation
(_,e1,_,_,_) = Static.elabExp(Env.emptyCache(),env,inExp,true,NONE,false);
e1 = replaceComplex(e1,ht2);
(_,value,_) = Ceval.ceval(Env.emptyCache(),env, e1, true, NONE, NONE, Ceval.MSG());
then
value;
// some type we need to convert into
case(inExp,env,SOME(ty),ht2)
equation
(_,e1,DAE.PROP(ty2,_),_,_) = Static.elabExp(Env.emptyCache(),env,inExp,true,NONE,false);
(e2,_) = Types.matchType(e1,ty2,ty,true);
e2 = replaceComplex(e2,ht2);
(_,value,_) = Ceval.ceval(Env.emptyCache(),env, e2, true, NONE, NONE, Ceval.MSG());
then
value;
// failure
case(_,_,_,_)
equation
Debug.fprint("failtrace", "- Cevalfunc.evaluateSingleExpression failed\n");
then
fail();
end matchcontinue;
end evaluateSingleExpression;
protected function evaluateForLoopArray "Function: evaluateForLoopArray
Evaluates a forloop while the for-values are in an array."
input Env.Env env;
input String varName;
input list<Values.Value> forValues;
input list<Absyn.AlgorithmItem> statements;
input HashTable2.HashTable ht2;
output Env.Env oenv;
algorithm oenv:= matchcontinue(env,varName,forValues,statements,ht2)
local
list<Absyn.AlgorithmItem> statements;
Values.Value value;
list<Values.Value> values;
Env.Env env1,env2,env3;
case(env,_,{},_,_) then env;
case(env,varName, value::values, statements,ht2)
equation
env1 = setValue(value,env,Absyn.CREF(Absyn.CREF_IDENT(varName,{})));
env2 = evaluateConditionalStatement2(statements,env1, ht2);
env3 = evaluateForLoopArray(env2,varName,values,statements, ht2);
then
env3;
end matchcontinue;
end evaluateForLoopArray;
protected function evaluateForLoopRange "Function: evaluateForLoopArray
Evaluates a forloop while the for-values are a range ex. 1:1:10."
input Env.Env env;
input String varName;
input list<Absyn.AlgorithmItem> statements;
input Values.Value start;
input Values.Value step;
input Values.Value stop;
input HashTable2.HashTable ht2;
output Env.Env oenv;
algorithm oenv:= matchcontinue(env,varName,statements,start,step,stop,ht2)
local
list<Absyn.AlgorithmItem> statements;
Values.Value newVal;
list<Values.Value> values;
Env.Env env1,env2,env3;
case(env,varName,statements,start,step,stop,ht2)
equation
true = ValuesUtil.safeLessEq(start,stop);
env1 = setValue(start,env,Absyn.CREF(Absyn.CREF_IDENT(varName,{})));
env2 = evaluateConditionalStatement2(statements,env1,ht2);
newVal = ValuesUtil.safeIntRealOp(start,step,Values.ADDOP);
env3 = evaluateForLoopRange(env2,varName,statements,newVal,step,stop,ht2);
then
env3;
case(env,_,_,start,step,stop,ht2)
equation
false = ValuesUtil.safeLessEq(start,stop);
then env;
end matchcontinue;
end evaluateForLoopRange;
protected function evaluateConditionalStatement "Function: evaluateConditionalStatement
This function now only is used in while-loops.
It checks wheter the condition is true or false.
If true, evaluate body then update condition and call itself again.
"
input Values.Value cond "Current condition, Values.( true/false)";
input Absyn.Exp updateExp "The expression to generate next condition";
input list<Absyn.AlgorithmItem> algitemlst "The statements in the body";
input Env.Env env;
input HashTable2.HashTable ht2;
output Env.Env oenv;
algorithm oenv := matchcontinue(cond,updateExp,algitemlst,env,ht2)
local
Absyn.AlgorithmItem algi;
list<Absyn.AlgorithmItem> algis;
Env.Env env1,env2;
Values.Value value, value2;
DAE.Exp e1;
case(Values.BOOL(false),_,_ ,env,ht2) then env;
case(Values.BOOL(true), updateExp,algis,env,ht2)
equation
env1 = evaluateConditionalStatement2(algis,env,ht2);
value2 = evaluateSingleExpression(updateExp,env1,NONE,ht2);
env2 = evaluateConditionalStatement(value2,updateExp,algis,env1,ht2);
then
env2;
end matchcontinue;
end evaluateConditionalStatement;
protected function evaluateConditionalStatement2 "
A intermediate-function for evaluation algorithm statements.
"
input list<Absyn.AlgorithmItem> inalgs;
input Env.Env env;
input HashTable2.HashTable ht2;
output Env.Env oenv;
algorithm oenv := matchcontinue(inalgs,env,ht2)
local
Absyn.Algorithm alg;
Env.Env env1,env2;
list<Absyn.AlgorithmItem> rest;
case({},env,ht2) then env;
case(Absyn.ALGORITHMITEM(alg,_)::rest,env,ht2)
equation
env1 = evaluateAlgorithm(env,alg,ht2);
env2 = evaluateConditionalStatement2(rest,env1,ht2);
then
env2;
case(Absyn.ALGORITHMITEMANN(_)::rest,env,ht2)
equation
env1 = evaluateConditionalStatement2(rest,env,ht2);
then
env1;
end matchcontinue;
end evaluateConditionalStatement2;
protected function setValues "Function: setValues
This function set multiple(tuple) values.
"
input list<Absyn.Exp> tupleCrefs;
input list<DAE.Type> types;
input list<Values.Value> varValues;
input Env.Env env;
output Env.Env oEnv;
algorithm oEnv := matchcontinue(tupleCrefs, types, varValues, env)
local
Absyn.Exp cref;
DAE.Type ty;
Values.Value value;
list<Absyn.Exp> crefs;
list<DAE.Type> tys;
list<Values.Value> values;
Env.Env env1,env2;
String str;
list<Absyn.Subscript> subs;
case({},{},{},env) then env;
case(cref ::crefs, ty::tys, value::values, env)
equation
env1 = setValue(value,env,cref);
env2 = setValues(crefs,tys,values,env1);
then
env2;
case(_,_,_,_)
equation
Debug.fprint("failtrace", "- Cevalfunc.setValues failed\n");
then fail();
end matchcontinue;
end setValues;
protected function setValue "Function: setValue
This function updates a generic-variable in the enviroment."