/
Interactive.mo
18903 lines (17726 loc) · 690 KB
/
Interactive.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-CurrentYear, Linköping University,
* 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
* AND THIS OSMC PUBLIC LICENSE (OSMC-PL).
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES RECIPIENT'S
* ACCEPTANCE OF THE OSMC PUBLIC LICENSE.
*
* The OpenModelica software and the Open Source Modelica
* Consortium (OSMC) Public License (OSMC-PL) are obtained
* from Linköping University, 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 package Interactive
" file: Interactive.mo
package: Interactive
description: This module contain functionality for model management,
expression evaluation, etc. in the interactive environment.
$Id$
This module contain functionality for model management, expression evaluation, etc.
in the interactive environment.
The module defines a symboltable used in the interactive environment containing:
- Modelica models (described using Absyn AST)
- Variable bindings
- Compiled functions (so they do not need to be recompiled)
- Instantiated classes (that can be reused, not impl. yet)
- Modelica models in SCode form (to speed up instantiation. not impl. yet)"
//public imports
public import Absyn;
public import AbsynDep;
public import ConnectionGraph;
public import DAE;
public import Env;
public import OptManager;
public import SCode;
public import SCodeUtil;
public import Settings;
public import Values;
// protected imports
protected import Builtin;
protected import Ceval;
protected import ClassInf;
protected import ClassLoader;
protected import ComponentReference;
protected import Connect;
protected import ConnectUtil;
protected import Constants;
protected import DAEUtil;
protected import Debug;
protected import Dependency;
protected import Dump;
protected import Error;
protected import ErrorExt;
protected import Expression;
protected import ExpressionDump;
protected import ExpressionSimplify;
protected import InnerOuter;
protected import Inst;
protected import List;
protected import Lookup;
protected import MetaUtil;
protected import Mod;
protected import Parser;
protected import Prefix;
protected import Print;
protected import Refactor;
protected import RTOpts;
protected import SCodeFlatten;
protected import Static;
protected import System;
protected import Types;
protected import UnitAbsyn;
protected import Util;
protected import ValuesUtil;
/*
** CompiledCFunction
** Absyn.Path = ie object path in class hierarcy
** DAE.Type = The type of the return value
** Integer = functionHandler
*/
//type CompiledCFunction = tuple<Absyn.Path, DAE.Type, Integer>;
public
uniontype CompiledCFunction
record CFunction
Absyn.Path path;
DAE.Type retType;
Integer funcHandle;
Real buildTime "the build time for this function";
String loadedFromFile "the file we loaded this function from";
end CFunction;
end CompiledCFunction;
public
uniontype Statement
"An Statement given in the interactive environment can either be
an Algorithm statement or an expression.
- Interactive Statement"
record IALG
Absyn.AlgorithmItem algItem "algItem" ;
end IALG;
record IEXP
Absyn.Exp exp "exp" ;
end IEXP;
end Statement;
public
uniontype Statements
"Several interactive statements are used in Modelica scripts.
- Interactive Statements"
record ISTMTS
list<Statement> interactiveStmtLst "interactiveStmtLst" ;
Boolean semicolon "semicolon; true = statement ending with a semicolon. The result will not be shown in the interactive environment." ;
end ISTMTS;
end Statements;
public
uniontype InstantiatedClass "- Instantiated Class"
record INSTCLASS
Absyn.Path qualName "qualName ; The F.Q.name of the inst:ed class" ;
DAE.DAElist daeElementLst "daeElementLst ; The list of DAE elements" ;
Env.Env env "env ; The env of the inst:ed class" ;
end INSTCLASS;
end InstantiatedClass;
public
uniontype Variable "- Interactive Variable"
record IVAR
Absyn.Ident varIdent "varIdent ; The variable identifier" ;
Values.Value value "value ; The value" ;
DAE.Type type_ "type ; The type of the expression" ;
end IVAR;
end Variable;
public
uniontype LoadedFile
"@author adrpo
A file entry holder, needed to cache the file information
so files are not loaded if not really necessary"
record FILE
String fileName "The path of the file";
Real loadTime "The time the file was loaded";
list<Absyn.Path> classNamesQualified "The names of the classes from the file";
end FILE;
end LoadedFile;
public
uniontype SymbolTable "- Interactive Symbol Table"
record SYMBOLTABLE
Absyn.Program ast "ast ; The ast" ;
AbsynDep.Depends depends "the dependency information";
Option<SCode.Program> explodedAst "the explodedAst is invalidated every time the program is updated";
list<InstantiatedClass> instClsLst "instClsLst ; List of instantiated classes" ;
list<Variable> lstVarVal "lstVarVal ; List of variables with values" ;
list<CompiledCFunction> compiledFunctions "compiledFunctions ; List of compiled functions, F.Q name + type + functionhandler" ;
list<LoadedFile> loadedFiles "The list of the loaded files with their load time." ;
end SYMBOLTABLE;
end SymbolTable;
public
uniontype Component "- a component in a class
this is used in extracting all the components in all the classes"
record COMPONENTITEM
Absyn.Path the1 "the class where the component is" ;
Absyn.Path the2 "the type of the component" ;
Absyn.ComponentRef the3 "the name of the component" ;
end COMPONENTITEM;
record EXTENDSITEM
Absyn.Path the1 "the class which is extended" ;
Absyn.Path the2 "the class which is the extension" ;
end EXTENDSITEM;
end Component;
public
uniontype Components
record COMPONENTS
list<Component> componentLst;
Integer the "the number of components in list. used to optimize the get_dependency_on_class" ;
end COMPONENTS;
end Components;
public
uniontype ComponentReplacement
record COMPONENTREPLACEMENT
Absyn.Path which1 "which class contain the old cref" ;
Absyn.ComponentRef the2 "the old cref" ;
Absyn.ComponentRef the3 "the new cref" ;
end COMPONENTREPLACEMENT;
end ComponentReplacement;
public
uniontype ComponentReplacementRules
record COMPONENTREPLACEMENTRULES
list<ComponentReplacement> componentReplacementLst;
Integer the "the number of rules" ;
end COMPONENTREPLACEMENTRULES;
end ComponentReplacementRules;
protected uniontype AnnotationType
record ICON_ANNOTATION end ICON_ANNOTATION;
record DIAGRAM_ANNOTATION end DIAGRAM_ANNOTATION;
end AnnotationType;
public constant SymbolTable emptySymboltable =
SYMBOLTABLE(Absyn.PROGRAM({},Absyn.TOP(),Absyn.dummyTimeStamp),
AbsynDep.DEPENDS(AbsynDep.AVLTREENODE(NONE(),0,NONE(),NONE()),AbsynDep.AVLTREENODE(NONE(),0,NONE(),NONE())),
NONE(),
{},
{},
{},
{}) "Empty Interactive Symbol Table" ;
public function evaluate
"function: evaluate
This function evaluates expressions or statements feed interactively to the compiler.
inputs: (Statements, SymbolTable, bool /* verbose */)
outputs: string:
The resulting string after evaluation. If an error has occurred, this string
will be empty. The error messages can be retrieved by calling print_messages_str()
in Error.mo.
SymbolTable"
input Statements inStatements;
input SymbolTable inSymbolTable;
input Boolean inBoolean;
output String outString;
output SymbolTable outSymbolTable;
algorithm
(outString,outSymbolTable) := matchcontinue (inStatements,inSymbolTable,inBoolean)
local
String res,res_1,res2,res_2;
SymbolTable newst,st,newst_1;
Boolean echo,semicolon,verbose;
Statement x;
list<Statement> xs;
case (ISTMTS(interactiveStmtLst = {x},semicolon = semicolon),st,verbose)
equation
showStatement(x, semicolon);
(res,newst) = evaluate2(ISTMTS({x},verbose), st);
echo = getEcho();
res_1 = selectResultstr(res, semicolon, verbose, echo);
then
(res_1,newst);
case (ISTMTS(interactiveStmtLst = (x :: xs),semicolon = semicolon),st,verbose)
equation
showStatement(x, semicolon);
(res,newst) = evaluate2(ISTMTS({x},semicolon), st);
echo = getEcho();
res_1 = selectResultstr(res, semicolon, verbose, echo);
(res2,newst_1) = evaluate(ISTMTS(xs,semicolon), newst, verbose);
res_2 = stringAppendList({res_1,res2});
then
(res_2,newst_1);
end matchcontinue;
end evaluate;
public function evaluateToStdOut
"function: evaluateToStdOut
This function evaluates expressions or statements feed interactively to the compiler.
The resulting string after evaluation is printed.
If an error has occurred, this string will be empty.
The error messages can be retrieved by calling print_messages_str() in Error.mo."
input Statements inStatements;
input SymbolTable inSymbolTable;
input Boolean inBoolean;
output SymbolTable outSymbolTable;
algorithm
outSymbolTable := matchcontinue (inStatements,inSymbolTable,inBoolean)
local
String res,res_1;
SymbolTable newst,st,newst_1;
Boolean echo,semicolon,verbose;
Statement x;
Statements new;
list<Statement> xs;
case (ISTMTS(interactiveStmtLst = {x},semicolon = semicolon),st,verbose)
equation
showStatement(x,semicolon);
new = ISTMTS({x},verbose);
(res,newst) = evaluate2(new, st);
echo = getEcho();
res_1 = selectResultstr(res, semicolon, verbose, echo);
print(res_1);
then
newst;
case (ISTMTS(interactiveStmtLst = (x :: xs),semicolon = semicolon),st,verbose)
equation
showStatement(x,semicolon);
new = ISTMTS({x},semicolon);
(res,newst) = evaluate2(new, st);
echo = getEcho();
res_1 = selectResultstr(res, semicolon, verbose, echo);
print(res_1);
newst_1 = evaluateToStdOut(ISTMTS(xs,semicolon), newst, verbose);
then
newst_1;
end matchcontinue;
end evaluateToStdOut;
protected function showStatement
input Statement s;
input Boolean semicolon;
algorithm
_:= matchcontinue(s, semicolon)
case (s, semicolon)
equation
true = RTOpts.debugFlag("showStatement");
print("Evaluating: " +& Dump.printIstmtStr(ISTMTS({s}, semicolon)) +& "\n");
then
();
else ();
end matchcontinue;
end showStatement;
protected function selectResultstr
"function: selectResultstr
Returns result string depending on three boolean variables
- semicolon
- verbose
- echo"
input String inString1;
input Boolean inBoolean2 "semicolon";
input Boolean inBoolean3 "verbose";
input Boolean inBoolean4 "echo";
output String outString;
algorithm
outString:=
matchcontinue (inString1,inBoolean2,inBoolean3,inBoolean4)
local String str;
case (str,_,_,false) then ""; /* echo off allways empty string */
case (str,_,true,_) then str; /* .. verbose on allways return str */
case (str,true,_,_) then ""; /* ... semicolon, no resultstr */
case (str,false,_,_) then str;
end matchcontinue;
end selectResultstr;
protected function getEcho
"function: getEcho
Return echo variable, which determines
if result should be printed or not."
output Boolean outBoolean;
algorithm
outBoolean := 0 <> Settings.getEcho();
end getEcho;
public function typeCheckFunction
"function: typeCheckFunction
Type check a function.
The function will fail iff a function has illegally typed.
Errors are handled using side effects in Error.mo
Note: This does not work for recursive functions, so don't use it!"
input Absyn.Program inProgram;
input SymbolTable inSymbolTable;
algorithm
_ := matchcontinue (inProgram,inSymbolTable)
local
Absyn.Restriction restriction;
SymbolTable st;
list<Env.Frame> env,env_1;
SCode.Element scode_class;
Absyn.Class absyn_class,cls;
Integer len;
list<Absyn.Class> class_list,morecls;
Absyn.Within w;
Absyn.TimeStamp ts;
/* Do not typecheck the following */
/* adrpo: these are not needed!
case (Absyn.BEGIN_DEFINITION(path = _),_) then ();
case (Absyn.END_DEFINITION(name = _),_) then ();
case (Absyn.COMP_DEFINITION(element = _),_) then ();
case (Absyn.IMPORT_DEFINITION(importElementFor = _),_) then ();
*/
// If it is not a function, return succeess
case (Absyn.PROGRAM(classes = {Absyn.CLASS(restriction = restriction)}),st)
equation
failure(equality(restriction = Absyn.R_FUNCTION()));
then
();
case (Absyn.PROGRAM(classes = {Absyn.CLASS(partialPrefix = true)}),st) then ();
// Type check the function
case (Absyn.PROGRAM(classes = {absyn_class}),st)
equation
env = buildEnvFromSymboltable(st);
scode_class = SCodeUtil.translateClass(absyn_class);
scode_class = SCodeFlatten.flattenClass(scode_class);
(_,env_1,_) = Inst.implicitFunctionInstantiation(Env.emptyCache(),env,InnerOuter.emptyInstHierarchy,
DAE.NOMOD(), Prefix.NOPRE(), scode_class, {});
then
();
// Recursively go through all classes
case (Absyn.PROGRAM(classes = (class_list as (cls :: morecls)), within_ = w, globalBuildTimes=ts),st)
equation
len = listLength(class_list);
false = intEq(len, 1) "avoid recurs forever";
typeCheckFunction(Absyn.PROGRAM({cls},w,ts), st);
typeCheckFunction(Absyn.PROGRAM(morecls,w,ts), st);
then
();
end matchcontinue;
end typeCheckFunction;
public function evaluate2
"function: evaluate2
Helper function to evaluate."
input Statements inStatements;
input SymbolTable inSymbolTable;
output String outString;
output SymbolTable outSymbolTable;
algorithm
(outString,outSymbolTable) := matchcontinue (inStatements,inSymbolTable)
local
String varsStr,str,str_1;
SymbolTable st,newst,st_1;
Statements stmts;
Absyn.AlgorithmItem algitem;
Boolean outres;
Absyn.Exp exp;
list<Variable> vars;
case ((stmts as ISTMTS(interactiveStmtLst = {IEXP(exp = Absyn.CALL(function_ = _))})),st)
equation
(str,newst) = evaluateGraphicalApi(stmts, st);
str_1 = stringAppend(str, "\n");
then
(str_1,newst);
case ((stmts as ISTMTS(interactiveStmtLst = {IEXP(exp = Absyn.CALL(function_ = _))})),st)
equation
(str,newst) = evaluateGraphicalApi2(stmts, st);
str_1 = stringAppend(str, "\n");
then
(str_1,newst);
case
(ISTMTS(interactiveStmtLst =
{IALG(algItem = (algitem as Absyn.ALGORITHMITEM(algorithm_ = _)))},
semicolon = outres),st) /* Evaluate algorithm statements in evaluate_alg_stmt() */
equation
(str,st_1) = evaluateAlgStmt(algitem, st);
str_1 = stringAppend(str, "\n");
then
(str_1,st_1);
case ((stmts as ISTMTS(interactiveStmtLst = {IEXP(exp = exp)})),st) /* Evaluate expressions in evaluate_expr_to_str() */
equation
(str,st_1) = evaluateExprToStr(exp, st, Absyn.dummyInfo);
str_1 = stringAppend(str, "\n");
then
(str_1,st_1);
end matchcontinue;
end evaluate2;
protected function evaluateAlgStmt "function: evaluateAlgStmt
This function takes an \'AlgorithmItem\', i.e. a statement located in an
algorithm section, and a symboltable as input arguments. The statements
are recursivly evalutated and a new interactive symbol table is returned.
"
input Absyn.AlgorithmItem inAlgorithmItem;
input SymbolTable inSymbolTable;
output String outString;
output SymbolTable outSymbolTable;
algorithm
(outString,outSymbolTable) := matchcontinue (inAlgorithmItem,inSymbolTable)
local
list<Env.Frame> env;
DAE.Exp econd,msg_1,sexp,srexp;
DAE.Properties prop,rprop;
SymbolTable st_1,st_2,st_3,st_4,st,newst;
Absyn.Exp cond,msg,exp,rexp;
Absyn.Program p;
String str,ident;
DAE.Type t;
Values.Value value;
list<DAE.Type> types;
list<String> idents;
list<Values.Value> values,valList;
list<Absyn.Exp> crefexps;
tuple<Absyn.Exp, list<Absyn.AlgorithmItem>> cond1;
list<tuple<Absyn.Exp, list<Absyn.AlgorithmItem>>> cond2,cond3,elseifexpitemlist;
list<Absyn.AlgorithmItem> algitemlist,elseitemlist;
list<Variable> vars;
String iter,estr;
list<Absyn.AlgorithmItem> algItemList;
Values.Value startv, stepv, stopv;
Absyn.Exp starte, stepe, stope;
Absyn.ComponentRef cr;
Env.Cache cache;
Absyn.Info info;
Absyn.FunctionArgs fargs;
case (Absyn.ALGORITHMITEM(info=info,
algorithm_ = Absyn.ALG_NORETCALL(functionCall = Absyn.CREF_IDENT(name = "assert"),
functionArgs = Absyn.FUNCTIONARGS(args = {cond,msg}))),
(st as SYMBOLTABLE(ast = p)))
equation
env = buildEnvFromSymboltable(st);
(cache,econd,prop,SOME(st_1)) = Static.elabExp(Env.emptyCache(),env, cond, true, SOME(st),true,Prefix.NOPRE(),info);
(_,Values.BOOL(true),SOME(st_2)) = Ceval.ceval(cache,env, econd, true,SOME(st_1),Ceval.MSG(info));
then
("",st_2);
case (Absyn.ALGORITHMITEM(info=info,algorithm_ = Absyn.ALG_NORETCALL(functionCall = Absyn.CREF_IDENT(name = "assert"),
functionArgs = Absyn.FUNCTIONARGS(args = {cond,msg}))),
(st as SYMBOLTABLE(ast = p)))
equation
env = buildEnvFromSymboltable(st);
(cache,msg_1,prop,SOME(st_1)) = Static.elabExp(Env.emptyCache(),env, msg, true, SOME(st),true,Prefix.NOPRE(),info);
(_,Values.STRING(str),SOME(st_2)) = Ceval.ceval(cache,env, msg_1, true,SOME(st_1),Ceval.MSG(info));
then
(str,st_2);
case (Absyn.ALGORITHMITEM(info=info,algorithm_ = Absyn.ALG_NORETCALL(functionCall = cr,functionArgs = fargs)),st)
equation
env = buildEnvFromSymboltable(st);
exp = Absyn.CALL(cr,fargs);
(cache,sexp,prop,SOME(st_1)) = Static.elabExp(Env.emptyCache(), env, exp, true, SOME(st),true,Prefix.NOPRE(),info);
(_,_,SOME(st_2)) = Ceval.ceval(cache, env, sexp, true,SOME(st_1),Ceval.MSG(info));
then
("",st_2);
case /* Special case to lookup fields of records.
* SimulationResult, etc are not in the environment, but it's nice to be able to script them anyway */
(Absyn.ALGORITHMITEM(info=info,algorithm_ =
Absyn.ALG_ASSIGN(assignComponent =
Absyn.CREF(Absyn.CREF_IDENT(name = ident,subscripts = {})),value = Absyn.CREF(cr))),
(st as SYMBOLTABLE(lstVarVal = vars)))
equation
value = getVariableValueLst(Absyn.pathToStringList(Absyn.crefToPath(cr)), vars);
str = ValuesUtil.valString(value);
t = Types.typeOfValue(value);
newst = addVarToSymboltable(ident, value, t, st);
then (str,newst);
case
(Absyn.ALGORITHMITEM(info=info,algorithm_ =
Absyn.ALG_ASSIGN(assignComponent =
Absyn.CREF(Absyn.CREF_IDENT(name = ident,subscripts = {})),value = exp)),
(st as SYMBOLTABLE(ast = p)))
equation
env = buildEnvFromSymboltable(st);
(cache,sexp,DAE.PROP(_,_),SOME(st_1)) = Static.elabExp(Env.emptyCache(),env, exp, true, SOME(st),true,Prefix.NOPRE(),info);
(_,value,SOME(st_2)) = Ceval.ceval(cache,env, sexp, true,SOME(st_1),Ceval.MSG(info));
t = Types.typeOfValue(value) "This type can be more specific than the elaborated type; if the dimensions are unknown...";
str = ValuesUtil.valString(value);
newst = addVarToSymboltable(ident, value, t, st_2);
then
(str,newst);
case
(Absyn.ALGORITHMITEM(info=info,algorithm_ =
Absyn.ALG_ASSIGN(assignComponent =
Absyn.TUPLE(expressions = crefexps),value = rexp)),
(st as SYMBOLTABLE(ast = p))) /* Since expressions cannot be tuples an empty string is returned */
equation
env = buildEnvFromSymboltable(st);
(cache,srexp,rprop,SOME(st_1)) = Static.elabExp(Env.emptyCache(),env, rexp, true, SOME(st),true,Prefix.NOPRE(),info);
((DAE.T_TUPLE(types),_)) = Types.getPropType(rprop);
idents = List.map(crefexps, getIdentFromTupleCrefexp);
(_,Values.TUPLE(values),SOME(st_2)) = Ceval.ceval(cache, env, srexp, true, SOME(st_1), Ceval.MSG(info));
newst = addVarsToSymboltable(idents, values, types, st_2);
then
("",newst);
case
(Absyn.ALGORITHMITEM(info=info,algorithm_ =
Absyn.ALG_IF(
ifExp = exp,
trueBranch = algitemlist,
elseIfAlgorithmBranch = elseifexpitemlist,
elseBranch = elseitemlist)),st) /* IF-statement */
equation
cond1 = (exp,algitemlist);
cond2 = (cond1 :: elseifexpitemlist);
cond3 = listAppend(cond2, {(Absyn.BOOL(true),elseitemlist)});
st_1 = evaluateIfStatementLst(cond3,st,info);
then
("",st_1);
/* while-statement */
case (Absyn.ALGORITHMITEM(info=info,algorithm_ = Absyn.ALG_WHILE(boolExpr = exp,whileBody = algitemlist)),st)
equation
(value,st_1) = evaluateExpr(exp, st,info);
st_2 = evaluateWhileStmt(value, exp, algitemlist, st_1,info);
then
("",st_2);
/* for-statement, optimized case, e.g.: for i in 1:1000 loop */
case (Absyn.ALGORITHMITEM(info=info,algorithm_ =
Absyn.ALG_FOR(iterators = {Absyn.ITERATOR(iter, NONE(), SOME(Absyn.RANGE(start=starte,step=NONE(), stop=stope)))},
forBody = algItemList)),st)
equation
(startv,st_1) = evaluateExpr(starte, st,info);
(stopv,st_2) = evaluateExpr(stope, st_1,info);
st_3 = evaluateForStmtRangeOpt(iter, startv, Values.INTEGER(1), stopv, algItemList, st_2);
then
("",st_3);
/* for-statement, optimized case, e.g.: for i in 7.3:0.4:1000.3 loop */
case (Absyn.ALGORITHMITEM(info=info,algorithm_ =
Absyn.ALG_FOR(iterators = {Absyn.ITERATOR(iter, NONE(), SOME(Absyn.RANGE(start=starte, step=SOME(stepe), stop=stope)))},
forBody = algItemList)),st)
equation
(startv,st_1) = evaluateExpr(starte, st,info);
(stepv,st_2) = evaluateExpr(stepe, st_1,info);
(stopv,st_3) = evaluateExpr(stope, st_2,info);
st_4 = evaluateForStmtRangeOpt(iter, startv, stepv, stopv, algItemList, st_3);
then
("",st_4);
/* for-statement, general case */
case (Absyn.ALGORITHMITEM(info=info,algorithm_ =
Absyn.ALG_FOR(iterators = {Absyn.ITERATOR(iter, NONE(), SOME(exp))},forBody = algItemList)),st)
equation
(Values.ARRAY(valueLst = valList),st_1) = evaluateExpr(exp, st,info);
st_2 = evaluateForStmt(iter, valList, algItemList, st_1);
then
("",st_2);
/* for-statement - not an array type */
case (Absyn.ALGORITHMITEM(info=info,algorithm_ = Absyn.ALG_FOR(iterators = {Absyn.ITERATOR(range = SOME(exp))})),st)
equation
estr = stringRepresOfExpr(exp, st);
Error.addSourceMessage(Error.NOT_ARRAY_TYPE_IN_FOR_STATEMENT, {estr}, info);
then
fail();
end matchcontinue;
end evaluateAlgStmt;
protected function evaluateForStmt
"evaluates a for-statement in an algorithm section"
input String iter "The iterator variable which will be assigned different values";
input list<Values.Value> valList "List of values that the iterator later will be assigned to";
input list<Absyn.AlgorithmItem> algItemList;
input SymbolTable inSymbolTable;
output SymbolTable outSymbolTable;
algorithm
outSymbolTable:=
match (iter,valList,algItemList, inSymbolTable)
local
Values.Value val;
list<Values.Value> vallst;
list<Absyn.AlgorithmItem> algItems;
SymbolTable st1,st2,st3,st4,st5;
String str;
case (iter, val::vallst, algItems, st1)
equation
st2 = appendVarToSymboltable(iter, val, Types.typeOfValue(val), st1);
str = Dump.unparseAlgorithmStrLst(2,algItems,"\n");
st3 = evaluateAlgStmtLst(algItems, st2);
st4 = deleteVarFromSymboltable(iter, st3);
st5 = evaluateForStmt(iter, vallst, algItems, st4);
then
st5;
case (_, {}, _, st1)
then
st1;
end match;
end evaluateForStmt;
protected function evaluateForStmtRangeOpt
"Optimized version of for statement. In this case, we do not create a large array if
a range expression is given. E.g. for i in 1:10000 loop"
input String iter "The iterator variable which will be assigned different values";
input Values.Value startVal;
input Values.Value stepVal;
input Values.Value stopVal;
input list<Absyn.AlgorithmItem> algItemList;
input SymbolTable inSymbolTable;
output SymbolTable outSymbolTable;
algorithm
outSymbolTable := matchcontinue (iter, startVal, stepVal, stopVal, algItemList, inSymbolTable)
local
Values.Value startv, stepv, stopv, nextv;
list<Absyn.AlgorithmItem> algItems;
SymbolTable st1,st2,st3,st4,st5;
case (iter, startv, stepv, stopv, algItems, st1)
equation
true = ValuesUtil.safeLessEq(startv, stopv);
st2 = appendVarToSymboltable(iter, startv, Types.typeOfValue(startv), st1);
st3 = evaluateAlgStmtLst(algItems, st2);
st4 = deleteVarFromSymboltable(iter, st3);
nextv = ValuesUtil.safeIntRealOp(startv, stepv, Values.ADDOP());
st5 = evaluateForStmtRangeOpt(iter, nextv, stepv, stopv, algItems, st4);
then
st5;
case (_,_,_,_,_,st1)
then
st1;
end matchcontinue;
end evaluateForStmtRangeOpt;
protected function evaluateWhileStmt
"function: evaluateWhileStmt
Recursively evaluates the while statement.
Note that it is tail-recursive, so we should result
in a iterative implementation."
input Values.Value inValue;
input Absyn.Exp inExp;
input list<Absyn.AlgorithmItem> inAbsynAlgorithmItemLst;
input SymbolTable inSymbolTable;
input Absyn.Info info;
output SymbolTable outSymbolTable;
algorithm
outSymbolTable:=
matchcontinue (inValue,inExp,inAbsynAlgorithmItemLst,inSymbolTable,info)
local
SymbolTable st,st_1,st_2,st_3;
Values.Value value;
Absyn.Exp exp;
list<Absyn.AlgorithmItem> algitemlst;
String estr,tstr;
DAE.Type vtype;
case (Values.BOOL(boolean = false),_,_,st,info) then st;
case (Values.BOOL(boolean = true),exp,algitemlst,st,info)
equation
st_1 = evaluateAlgStmtLst(algitemlst, st);
(value,st_2) = evaluateExpr(exp, st_1,info);
st_3 = evaluateWhileStmt(value, exp, algitemlst, st_2,info); /* Tail recursive */
then
st_3;
case (Values.BOOL(_), _,_,st,info) // An error occured when evaluating the algorithm items
then
st;
case (value,exp,_,st,info) // The condition value was not a boolean
equation
estr = stringRepresOfExpr(exp, st);
vtype = Types.typeOfValue(value);
tstr = Types.unparseType(vtype);
Error.addSourceMessage(Error.WHILE_CONDITION_TYPE_ERROR, {estr,tstr},info);
then
fail();
end matchcontinue;
end evaluateWhileStmt;
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 SymbolTable inSymbolTable;
input Absyn.Info info;
output SymbolTable outSymbolTable;
algorithm
outSymbolTable:=
matchcontinue (inValue,inExp,inAbsynAlgorithmItemLst,inTplAbsynExpAbsynAlgorithmItemLstLst,inSymbolTable,info)
local
SymbolTable st_1,st;
list<Absyn.AlgorithmItem> algitemlst;
list<tuple<Absyn.Exp, list<Absyn.AlgorithmItem>>> algrest;
String estr,tstr;
DAE.Type vtype;
Values.Value value;
Absyn.Exp exp;
case (Values.BOOL(boolean = true),_,algitemlst,_,st,info)
equation
st_1 = evaluateAlgStmtLst(algitemlst, st);
then
st_1;
case (Values.BOOL(boolean = false),_,algitemlst,algrest,st,info)
equation
st_1 = evaluateIfStatementLst(algrest, st, info);
then
st_1;
case (value,exp,_,_,st,info) /* Report type error */
equation
estr = stringRepresOfExpr(exp, st);
vtype = Types.typeOfValue(value);
tstr = Types.unparseType(vtype);
Error.addSourceMessage(Error.IF_CONDITION_TYPE_ERROR, {estr,tstr}, info);
then
fail();
end matchcontinue;
end evaluatePartOfIfStatement;
protected function evaluateIfStatementLst
"function: evaluateIfStatementLst
Evaluates all parts of a if statement
(i.e. a list of exp statements)"
input list<tuple<Absyn.Exp, list<Absyn.AlgorithmItem>>> inTplAbsynExpAbsynAlgorithmItemLstLst;
input SymbolTable inSymbolTable;
input Absyn.Info info;
output SymbolTable outSymbolTable;
algorithm
outSymbolTable:=
match (inTplAbsynExpAbsynAlgorithmItemLstLst,inSymbolTable,info)
local
SymbolTable st,st_1,st_2;
Values.Value value;
Absyn.Exp exp;
list<Absyn.AlgorithmItem> algitemlst;
list<tuple<Absyn.Exp, list<Absyn.AlgorithmItem>>> algrest;
case ({},st,info) then st;
case (((exp,algitemlst) :: algrest),st,info)
equation
(value,st_1) = evaluateExpr(exp, st, info);
st_2 = evaluatePartOfIfStatement(value, exp, algitemlst, algrest, st_1, info);
then
st_2;
end match;
end evaluateIfStatementLst;
protected function evaluateAlgStmtLst
"function: evaluateAlgStmtLst
Evaluates a list of algorithm statements"
input list<Absyn.AlgorithmItem> inAbsynAlgorithmItemLst;
input SymbolTable inSymbolTable;
output SymbolTable outSymbolTable;
algorithm
outSymbolTable:=
match (inAbsynAlgorithmItemLst,inSymbolTable)
local
SymbolTable st,st_1,st_2;
Absyn.AlgorithmItem algitem;
list<Absyn.AlgorithmItem> algrest;
case ({},st) then st;
case ((algitem :: algrest),st)
equation
(_,st_1) = evaluateAlgStmt(algitem, st);
st_2 = evaluateAlgStmtLst(algrest, st_1);
then
st_2;
end match;
end evaluateAlgStmtLst;
protected function evaluateExpr
"function: evaluateExpr
Evaluates an expression and returns its value.
We need to return the symbol table, since the command loadFile()
reads in data to the interactive environment.
Note that this function may fail.
Input: Absyn.Exp - Expression to be evaluated
SymbolTable - The symbol table
Output: Values.Value - Resulting value of the expression"
input Absyn.Exp inExp;
input SymbolTable inSymbolTable;
input Absyn.Info info;
output Values.Value outValue;
output SymbolTable outSymbolTable;
algorithm
(outValue,outSymbolTable):=
matchcontinue (inExp,inSymbolTable,info)
local
list<Env.Frame> env;
DAE.Exp sexp;
DAE.Properties prop;
SymbolTable st_1,st_2,st;
Values.Value value;
Absyn.Exp exp;
Absyn.Program p;
Env.Cache cache;
list<Variable> vars;
Absyn.ComponentRef cr;
/* Special case to lookup fields of records.
* SimulationResult, etc are not in the environment, but it's nice to be able to script them anyway */
case (Absyn.CREF(cr),(st as SYMBOLTABLE(lstVarVal = vars)),info)
equation
value = getVariableValueLst(Absyn.pathToStringList(Absyn.crefToPath(cr)), vars);
then (value,st);
case (exp,(st as SYMBOLTABLE(ast = p)),info)
equation
env = buildEnvFromSymboltable(st);
(cache,sexp,prop,SOME(st_1)) = Static.elabExp(Env.emptyCache(), env, exp, true, SOME(st),true,Prefix.NOPRE(),info);
(_,value,SOME(st_2)) = Ceval.ceval(cache,env, sexp, true,
SOME(st_1),Ceval.MSG(info));
then
(value,st_2);
end matchcontinue;
end evaluateExpr;
protected function stringRepresOfExpr
"function: stringRepresOfExpr
This function returns a string representation of an expression. For example expression
33+22 will result in \"55\" and expression: \"my\" + \"string\" will result in \"\"my\"+\"string\"\". "
input Absyn.Exp exp;
input SymbolTable st;
output String estr;
protected
list<Env.Frame> env;
DAE.Exp sexp;
DAE.Properties prop;
SymbolTable st_1;
algorithm
env := buildEnvFromSymboltable(st);
(_,sexp,prop,SOME(st_1)) := Static.elabExp(Env.emptyCache(),env, exp, true, SOME(st),true,Prefix.NOPRE(),Absyn.dummyInfo);
(_, sexp, prop) := Ceval.cevalIfConstant(Env.emptyCache(), env, sexp, prop, true, Absyn.dummyInfo);
estr := ExpressionDump.printExpStr(sexp);
end stringRepresOfExpr;
protected function evaluateExprToStr
"function: evaluateExprToStr
This function is similar to evaluateExpr, with the difference that it returns a string
and that it never fails. If the expression contain errors, an empty string will be returned
and the errors will be stated using Error.mo
Input: Absyn.Exp - Expression to be evaluated
SymbolTable - The symbol table
Output: string - The resulting value represented as a string"
input Absyn.Exp inExp;
input SymbolTable inSymbolTable;
input Absyn.Info info;
output String outString;
output SymbolTable outSymbolTable;
algorithm
(outString,outSymbolTable):=
matchcontinue (inExp,inSymbolTable,info)
local
Values.Value value;
SymbolTable st_1,st;
String str;
Absyn.Exp exp;
case (exp,st,info)
equation
(value,st_1) = evaluateExpr(exp, st, info);
str = ValuesUtil.valString(value);
then
(str,st_1);
case (_,st,_) then ("",st);
end matchcontinue;
end evaluateExprToStr;
protected function getIdentFromTupleCrefexp
"function: getIdentFromTupleCrefexp
Return the (first) identifier of a Component Reference in an expression."
input Absyn.Exp inExp;
output Absyn.Ident outIdent;
algorithm
outIdent:=
matchcontinue (inExp)
local
String id,str;
Absyn.Exp exp;
case Absyn.CREF(componentRef = Absyn.CREF_IDENT(name = id)) then id;
case exp
equation
str = Dump.printExpStr(exp);
Error.addMessage(Error.INVALID_TUPLE_CONTENT, {str});
then
fail();
end matchcontinue;
end getIdentFromTupleCrefexp;
public function getTypeOfVariable
"function: getTypeOfVariables
Return the type of an interactive variable,
given a list of variables and a variable identifier."
input Absyn.Ident inIdent;
input list<Variable> inVariableLst;
output DAE.Type outType;
algorithm
outType := matchcontinue (inIdent,inVariableLst)