Skip to content
This repository was archived by the owner on May 18, 2019. It is now read-only.

Commit 15e62b7

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Improve toString functions.
- Implement toString for Variable, Algorithm and FlatModel. - Improve formatting in Equation.toString and Statement.toString. Belonging to [master]: - #2854
1 parent 8311f26 commit 15e62b7

6 files changed

Lines changed: 171 additions & 43 deletions

File tree

Compiler/NFFrontEnd/NFAlgorithm.mo

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,12 @@ public
119119
end for;
120120
end foldExpList;
121121

122+
function toString
123+
input Algorithm alg;
124+
output String str;
125+
algorithm
126+
str := Statement.toStringList(alg.statements);
127+
end toString;
128+
122129
annotation(__OpenModelica_Interface="frontend");
123130
end NFAlgorithm;

Compiler/NFFrontEnd/NFEquation.mo

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ public
6060

6161
function toString
6262
input Branch branch;
63+
input String indent = "";
6364
output String str;
6465
algorithm
6566
str := match branch
6667
case BRANCH()
67-
then Expression.toString(branch.condition) + " then\n" + toStringList(branch.body);
68+
then Expression.toString(branch.condition) + " then\n" + toStringList(branch.body, indent + " ");
6869

6970
case INVALID_BRANCH()
70-
then toString(branch.branch);
71+
then toString(branch.branch, indent);
7172
end match;
7273
end toString;
7374

@@ -643,63 +644,67 @@ public
643644

644645
function toString
645646
input Equation eq;
647+
input String indent = "";
646648
output String str;
647649
algorithm
648650
str := match eq
649651
local
650652
String s1, s2;
651653

652654
case EQUALITY()
653-
then Expression.toString(eq.lhs) + " = " + Expression.toString(eq.rhs);
655+
then indent + Expression.toString(eq.lhs) + " = " + Expression.toString(eq.rhs) + ";";
654656

655657
case CREF_EQUALITY()
656-
then ComponentRef.toString(eq.lhs) + " = " + ComponentRef.toString(eq.rhs);
658+
then indent + ComponentRef.toString(eq.lhs) + " = " + ComponentRef.toString(eq.rhs) + ";";
657659

658660
case ARRAY_EQUALITY()
659-
then Expression.toString(eq.lhs) + " = " + Expression.toString(eq.rhs);
661+
then indent + Expression.toString(eq.lhs) + " = " + Expression.toString(eq.rhs) + ";";
660662

661663
case CONNECT()
662-
then "connect(" + Expression.toString(eq.lhs) + ", " + Expression.toString(eq.rhs) + ")";
664+
then indent + "connect(" + Expression.toString(eq.lhs) + ", " + Expression.toString(eq.rhs) + ");";
663665

664666
case FOR()
665667
algorithm
666668
s1 := if isSome(eq.range) then " in " + Expression.toString(Util.getOption(eq.range)) else "";
667-
s2 := toStringList(eq.body);
669+
s2 := toStringList(eq.body, indent + " ");
668670
then
669-
"for " + InstNode.name(eq.iterator) + s1 + " loop\n" + s2 + "end for";
671+
indent + "for " + InstNode.name(eq.iterator) + s1 + " loop\n" + s2 + indent + "end for;";
670672

671673
case IF()
672-
then "if " + Branch.toString(listHead(eq.branches)) +
673-
List.toString(listRest(eq.branches), Branch.toString, "", "elseif ", "\nelseif ", "", false) +
674-
"\nend if";
674+
then indent + "if " + Branch.toString(listHead(eq.branches)) +
675+
List.toString(listRest(eq.branches), function Branch.toString(indent = indent), "",
676+
indent + "elseif ", "\n" + indent + "elseif ", "", false) +
677+
indent + "end if;";
675678

676679
case WHEN()
677-
then "when " + Branch.toString(listHead(eq.branches)) +
678-
List.toString(listRest(eq.branches), Branch.toString, "", "elsewhen ", "\nelsewhen ", "", false) +
679-
"\nend when";
680+
then indent + "when " + Branch.toString(listHead(eq.branches)) +
681+
List.toString(listRest(eq.branches), function Branch.toString(indent = indent), "",
682+
indent + "elsewhen ", "\n" + indent + "elsewhen ", "", false) +
683+
indent + "end when;";
680684

681685
case ASSERT()
682686
then "assert(" + Expression.toString(eq.condition) + ", " +
683-
Expression.toString(eq.message) + ", " + Expression.toString(eq.level) + ")";
687+
Expression.toString(eq.message) + ", " + Expression.toString(eq.level) + ");";
684688

685689
case TERMINATE()
686-
then "terminate( " + Expression.toString(eq.message) + ")";
690+
then "terminate( " + Expression.toString(eq.message) + ");";
687691

688692
case REINIT()
689-
then "reinit(" + Expression.toString(eq.cref) + ", " + Expression.toString(eq.reinitExp) + ")";
693+
then "reinit(" + Expression.toString(eq.cref) + ", " + Expression.toString(eq.reinitExp) + ");";
690694

691695
case NORETCALL()
692-
then Expression.toString(eq.exp);
696+
then Expression.toString(eq.exp) + ";";
693697

694698
else "#UNKNOWN EQUATION#";
695699
end match;
696700
end toString;
697701

698702
function toStringList
699703
input list<Equation> eql;
704+
input String indent = "";
700705
output String str;
701706
algorithm
702-
str := List.toString(eql, toString, "", " ", "\n ", "", false) + "\n";
707+
str := List.toString(eql, function toString(indent = indent), "", "", "\n", "", false) + "\n";
703708
end toStringList;
704709

705710
annotation(__OpenModelica_Interface="frontend");

Compiler/NFFrontEnd/NFFlatModel.mo

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@ encapsulated uniontype NFFlatModel
3434
import Algorithm = NFAlgorithm;
3535
import Variable = NFVariable;
3636

37+
protected
38+
import Statement = NFStatement;
39+
import IOStream;
40+
41+
import FlatModel = NFFlatModel;
42+
43+
public
3744
record FLAT_MODEL
45+
String name;
3846
list<Variable> variables;
3947
list<Equation> equations;
4048
list<Equation> initialEquations;
@@ -43,5 +51,59 @@ encapsulated uniontype NFFlatModel
4351
Option<SCode.Comment> comment;
4452
end FLAT_MODEL;
4553

54+
function toString
55+
input FlatModel flatModel;
56+
output String str;
57+
protected
58+
IOStream.IOStream s;
59+
algorithm
60+
s := IOStream.create(getInstanceName(), IOStream.IOStreamType.LIST());
61+
62+
s := IOStream.append(s, "class " + flatModel.name + "\n");
63+
s := toString2(flatModel.variables, function Variable.toString(indent = " "), "", s);
64+
s := toString2(flatModel.initialEquations, function Equation.toString(indent = " "), "initial equation", s);
65+
s := toString2(flatModel.equations, function Equation.toString(indent = " "), "equation", s);
66+
67+
for alg in flatModel.initialAlgorithms loop
68+
s := toString2(alg.statements, function Statement.toString(indent = " "), "initial algorithm", s);
69+
end for;
70+
71+
for alg in flatModel.algorithms loop
72+
s := toString2(alg.statements, function Statement.toString(indent = " "), "algorithm", s);
73+
end for;
74+
75+
s := IOStream.append(s, "end " + flatModel.name + ";\n");
76+
77+
str := IOStream.string(s);
78+
IOStream.delete(s);
79+
end toString;
80+
81+
protected
82+
function toString2<T>
83+
input list<T> elements;
84+
input FuncT toStringFunc;
85+
input String header;
86+
input output IOStream.IOStream s;
87+
88+
partial function FuncT
89+
input T element;
90+
output String str;
91+
end FuncT;
92+
algorithm
93+
if listEmpty(elements) then
94+
return;
95+
end if;
96+
97+
if not stringEmpty(header) then
98+
s := IOStream.append(s, header);
99+
s := IOStream.append(s, "\n");
100+
end if;
101+
102+
for e in elements loop
103+
s := IOStream.append(s, toStringFunc(e));
104+
s := IOStream.append(s, "\n");
105+
end for;
106+
end toString2;
107+
46108
annotation(__OpenModelica_Interface="frontend");
47109
end NFFlatModel;

Compiler/NFFrontEnd/NFFlatten.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ algorithm
145145
alg := listReverseInPlace(sections.algorithms);
146146
ialg := listReverseInPlace(sections.initialAlgorithms);
147147
then
148-
FlatModel.FLAT_MODEL(vars, eql, ieql, alg, ialg, cmt);
148+
FlatModel.FLAT_MODEL(name, vars, eql, ieql, alg, ialg, cmt);
149149

150-
else FlatModel.FLAT_MODEL(vars, {}, {}, {}, {}, cmt);
150+
else FlatModel.FLAT_MODEL(name, vars, {}, {}, {}, {}, cmt);
151151
end match;
152152

153153
execStat(getInstanceName() + "(" + name + ")");

Compiler/NFFrontEnd/NFStatement.mo

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -420,58 +420,60 @@ public
420420

421421
function toString
422422
input Statement stmt;
423+
input String indent = "";
423424
output String str;
424425
algorithm
425426
str := match stmt
426427
local
427428
String s1, s2;
428429

429430
case ASSIGNMENT()
430-
then Expression.toString(stmt.lhs) + " := " + Expression.toString(stmt.rhs);
431+
then indent + Expression.toString(stmt.lhs) + " := " + Expression.toString(stmt.rhs) + ";";
431432

432433
case FUNCTION_ARRAY_INIT()
433-
then "array init " + stmt.name;
434+
then indent + "array init " + stmt.name;
434435

435436
case FOR()
436437
algorithm
437438
s1 := if isSome(stmt.range) then " in " + Expression.toString(Util.getOption(stmt.range)) else "";
438-
s2 := toStringList(stmt.body);
439+
s2 := toStringList(stmt.body, indent + " ");
439440
then
440-
"for " + InstNode.name(stmt.iterator) + s1 + " loop\n" + s2 + "end for";
441+
indent + "for " + InstNode.name(stmt.iterator) + s1 + " loop\n" + s2 + indent + "end for;";
441442

442443
case IF()
443-
then branchString(listHead(stmt.branches), "if", true) +
444-
stringDelimitList(list(branchString(b, "if", false) for b in listRest(stmt.branches)), "\n") +
445-
"\nend if";
444+
then branchString(listHead(stmt.branches), "if", indent, true) +
445+
stringDelimitList(list(branchString(b, "if", indent, false) for b in listRest(stmt.branches)), "\n") +
446+
indent + "end if;";
446447

447448
case WHEN()
448-
then branchString(listHead(stmt.branches), "when", true) +
449-
stringDelimitList(list(branchString(b, "when", false) for b in listRest(stmt.branches)), "\n") +
450-
"\nwhen if";
449+
then branchString(listHead(stmt.branches), "when", indent, true) +
450+
stringDelimitList(list(branchString(b, "when", indent, false) for b in listRest(stmt.branches)), "\n") +
451+
indent + "when if;";
451452

452453
case ASSERT()
453-
then "assert(" + Expression.toString(stmt.condition) + ", " +
454-
Expression.toString(stmt.message) + ", " + Expression.toString(stmt.level) + ")";
454+
then indent + "assert(" + Expression.toString(stmt.condition) + ", " +
455+
Expression.toString(stmt.message) + ", " + Expression.toString(stmt.level) + ");";
455456

456457
case TERMINATE()
457-
then "terminate( " + Expression.toString(stmt.message) + ")";
458+
then indent + "terminate( " + Expression.toString(stmt.message) + ");";
458459

459460
case NORETCALL()
460-
then Expression.toString(stmt.exp);
461+
then indent + Expression.toString(stmt.exp) + ";";
461462

462463
case WHILE()
463-
then "while " + Expression.toString(stmt.condition) + " then\n" +
464-
toStringList(stmt.body) + "\nend while";
464+
then indent + "while " + Expression.toString(stmt.condition) + " then\n" +
465+
toStringList(stmt.body, indent + " ") + "\nend while;";
465466

466-
case RETURN() then "return";
467-
case BREAK() then "break";
468-
else "#UNKNOWN STATEMENT#";
467+
case RETURN() then indent + "return;";
468+
case BREAK() then indent + "break;";
469+
else indent + "#UNKNOWN STATEMENT#";
469470
end match;
470471
end toString;
471472

472473
function branchString
473474
input tuple<Expression, list<Statement>> branch;
474475
input String stmtType;
476+
input String indent;
475477
input Boolean firstBranch;
476478
output String str;
477479
protected
@@ -480,15 +482,16 @@ public
480482
algorithm
481483
(cond, body) := branch;
482484

483-
str := (if firstBranch then stmtType elseif Expression.isTrue(cond) then "else" else "else" + stmtType) +
484-
" " + Expression.toString(cond) + "then\n" + toStringList(body);
485+
str := indent + (if firstBranch then stmtType elseif Expression.isTrue(cond) then "else" else "else" + stmtType) +
486+
" " + Expression.toString(cond) + " then\n" + toStringList(body, indent + " ");
485487
end branchString;
486488

487489
function toStringList
488490
input list<Statement> stmtl;
491+
input String indent = "";
489492
output String str;
490493
algorithm
491-
str := List.toString(stmtl, toString, "", " " , "\n ", "", false) + "\n";
494+
str := List.toString(stmtl, function toString(indent = indent), "", "", "\n", "", false) + "\n";
492495
end toStringList;
493496

494497
annotation(__OpenModelica_Interface="frontend");

Compiler/NFFrontEnd/NFVariable.mo

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ encapsulated uniontype NFVariable
4141

4242
protected
4343
import Variable = NFVariable;
44+
import IOStream;
4445

4546
public
4647
record VARIABLE
@@ -89,5 +90,55 @@ public
8990
output Boolean isEmpty = Type.isEmptyArray(variable.ty);
9091
end isEmptyArray;
9192

93+
function toString
94+
input Variable var;
95+
input String indent = "";
96+
output String str;
97+
protected
98+
IOStream.IOStream s;
99+
Boolean first;
100+
algorithm
101+
s := IOStream.create(getInstanceName(), IOStream.IOStreamType.LIST());
102+
103+
s := IOStream.append(s, indent);
104+
105+
if var.visibility == Visibility.PROTECTED then
106+
s := IOStream.append(s, "protected ");
107+
end if;
108+
109+
s := IOStream.append(s, Component.Attributes.toString(var.attributes, var.ty));
110+
s := IOStream.append(s, Type.toString(var.ty));
111+
s := IOStream.append(s, " ");
112+
s := IOStream.append(s, ComponentRef.toString(var.name));
113+
114+
if not listEmpty(var.typeAttributes) then
115+
s := IOStream.append(s, "(");
116+
117+
first := true;
118+
for a in var.typeAttributes loop
119+
if first then
120+
first := false;
121+
else
122+
s := IOStream.append(s, ", ");
123+
end if;
124+
125+
s := IOStream.append(s, Util.tuple21(a));
126+
s := IOStream.append(s, " = ");
127+
s := IOStream.append(s, Binding.toString(Util.tuple22(a)));
128+
end for;
129+
130+
s := IOStream.append(s, ")");
131+
end if;
132+
133+
if Binding.isBound(var.binding) then
134+
s := IOStream.append(s, " = ");
135+
s := IOStream.append(s, Binding.toString(var.binding));
136+
end if;
137+
138+
s := IOStream.append(s, ";");
139+
str := IOStream.string(s);
140+
IOStream.delete(s);
141+
end toString;
142+
92143
annotation(__OpenModelica_Interface="frontend");
93144
end NFVariable;

0 commit comments

Comments
 (0)