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

Commit 973bab3

Browse files
perostOpenModelica-Hudson
authored andcommitted
[NF] Cleanup some toString functions.
Belonging to [master]: - #2872
1 parent d681d71 commit 973bab3

4 files changed

Lines changed: 354 additions & 117 deletions

File tree

Compiler/NFFrontEnd/NFEquation.mo

Lines changed: 170 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ protected
4444
import NFComponent.Component;
4545
import Util;
4646
import ElementSource;
47+
import IOStream;
4748

4849
public
4950
uniontype Branch
@@ -58,19 +59,24 @@ public
5859
list<Error.TotalMessage> errors;
5960
end INVALID_BRANCH;
6061

61-
function toString
62+
function toStream
6263
input Branch branch;
63-
input String indent = "";
64-
output String str;
64+
input String indent;
65+
input output IOStream.IOStream s;
6566
algorithm
66-
str := match branch
67+
s := match branch
6768
case BRANCH()
68-
then Expression.toString(branch.condition) + " then\n" + toStringList(branch.body, indent + " ");
69+
algorithm
70+
s := IOStream.append(s, Expression.toString(branch.condition));
71+
s := IOStream.append(s, " then\n");
72+
s := toStreamList(branch.body, indent + " ", s);
73+
then
74+
s;
6975

7076
case INVALID_BRANCH()
71-
then toString(branch.branch, indent);
77+
then toStream(branch.branch, indent, s);
7278
end match;
73-
end toString;
79+
end toStream;
7480

7581
function triggerErrors
7682
input Branch branch;
@@ -646,66 +652,193 @@ public
646652
input Equation eq;
647653
input String indent = "";
648654
output String str;
655+
protected
656+
IOStream.IOStream s;
649657
algorithm
650-
str := match eq
651-
local
652-
String s1, s2;
658+
s := IOStream.create(getInstanceName(), IOStream.IOStreamType.LIST());
659+
s := toStream(eq, indent, s);
660+
str := IOStream.string(s);
661+
IOStream.delete(s);
662+
end toString;
663+
664+
function toStringList
665+
input list<Equation> eql;
666+
input String indent = "";
667+
output String str;
668+
protected
669+
IOStream.IOStream s;
670+
algorithm
671+
s := IOStream.create(getInstanceName(), IOStream.IOStreamType.LIST());
672+
s := toStreamList(eql, indent, s);
673+
str := IOStream.string(s);
674+
IOStream.delete(s);
675+
end toStringList;
676+
677+
function toStream
678+
input Equation eq;
679+
input String indent;
680+
input output IOStream.IOStream s;
681+
algorithm
682+
s := IOStream.append(s, indent);
653683

684+
s := match eq
654685
case EQUALITY()
655-
then indent + Expression.toString(eq.lhs) + " = " + Expression.toString(eq.rhs) + ";";
686+
algorithm
687+
s := IOStream.append(s, Expression.toString(eq.lhs));
688+
s := IOStream.append(s, " = ");
689+
s := IOStream.append(s, Expression.toString(eq.rhs));
690+
then
691+
s;
656692

657693
case CREF_EQUALITY()
658-
then indent + ComponentRef.toString(eq.lhs) + " = " + ComponentRef.toString(eq.rhs) + ";";
694+
algorithm
695+
s := IOStream.append(s, ComponentRef.toString(eq.lhs));
696+
s := IOStream.append(s, " = ");
697+
s := IOStream.append(s, ComponentRef.toString(eq.rhs));
698+
then
699+
s;
659700

660701
case ARRAY_EQUALITY()
661-
then indent + Expression.toString(eq.lhs) + " = " + Expression.toString(eq.rhs) + ";";
702+
algorithm
703+
s := IOStream.append(s, Expression.toString(eq.lhs));
704+
s := IOStream.append(s, " = ");
705+
s := IOStream.append(s, Expression.toString(eq.rhs));
706+
then
707+
s;
662708

663709
case CONNECT()
664-
then indent + "connect(" + Expression.toString(eq.lhs) + ", " + Expression.toString(eq.rhs) + ");";
710+
algorithm
711+
s := IOStream.append(s, "connect(");
712+
s := IOStream.append(s, Expression.toString(eq.lhs));
713+
s := IOStream.append(s, " = ");
714+
s := IOStream.append(s, Expression.toString(eq.rhs));
715+
s := IOStream.append(s, ")");
716+
then
717+
s;
665718

666719
case FOR()
667720
algorithm
668-
s1 := if isSome(eq.range) then " in " + Expression.toString(Util.getOption(eq.range)) else "";
669-
s2 := toStringList(eq.body, indent + " ");
721+
s := IOStream.append(s, "for ");
722+
s := IOStream.append(s, InstNode.name(eq.iterator));
723+
724+
if isSome(eq.range) then
725+
s := IOStream.append(s, " in ");
726+
s := IOStream.append(s, Expression.toString(Util.getOption(eq.range)));
727+
end if;
728+
729+
s := IOStream.append(s, " loop\n");
730+
s := toStreamList(eq.body, indent + " ", s);
731+
s := IOStream.append(s, indent);
732+
s := IOStream.append(s, "end for");
670733
then
671-
indent + "for " + InstNode.name(eq.iterator) + s1 + " loop\n" + s2 + indent + "end for;";
734+
s;
672735

673736
case 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;";
737+
algorithm
738+
s := IOStream.append(s, "if ");
739+
s := Branch.toStream(listHead(eq.branches), indent, s);
740+
741+
for b in listRest(eq.branches) loop
742+
s := IOStream.append(s, indent);
743+
s := IOStream.append(s, "elseif ");
744+
s := Branch.toStream(b, indent, s);
745+
end for;
746+
747+
s := IOStream.append(s, indent);
748+
s := IOStream.append(s, "end if");
749+
then
750+
s;
678751

679752
case 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;";
753+
algorithm
754+
s := IOStream.append(s, "when ");
755+
s := Branch.toStream(listHead(eq.branches), indent, s);
756+
757+
for b in listRest(eq.branches) loop
758+
s := IOStream.append(s, indent);
759+
s := IOStream.append(s, "elsewhen ");
760+
s := Branch.toStream(b, indent, s);
761+
end for;
762+
763+
s := IOStream.append(s, indent);
764+
s := IOStream.append(s, "end when");
765+
then
766+
s;
684767

685768
case ASSERT()
686-
then "assert(" + Expression.toString(eq.condition) + ", " +
687-
Expression.toString(eq.message) + ", " + Expression.toString(eq.level) + ");";
769+
algorithm
770+
s := IOStream.append(s, "assert(");
771+
s := IOStream.append(s, Expression.toString(eq.condition));
772+
s := IOStream.append(s, ", ");
773+
s := IOStream.append(s, Expression.toString(eq.message));
774+
s := IOStream.append(s, ", ");
775+
s := IOStream.append(s, Expression.toString(eq.level));
776+
s := IOStream.append(s, ")");
777+
then
778+
s;
688779

689780
case TERMINATE()
690-
then "terminate( " + Expression.toString(eq.message) + ");";
781+
algorithm
782+
s := IOStream.append(s, "terminate(");
783+
s := IOStream.append(s, Expression.toString(eq.message));
784+
s := IOStream.append(s, ")");
785+
then
786+
s;
691787

692788
case REINIT()
693-
then "reinit(" + Expression.toString(eq.cref) + ", " + Expression.toString(eq.reinitExp) + ");";
789+
algorithm
790+
s := IOStream.append(s, "reinit(");
791+
s := IOStream.append(s, Expression.toString(eq.cref));
792+
s := IOStream.append(s, ", ");
793+
s := IOStream.append(s, Expression.toString(eq.reinitExp));
794+
s := IOStream.append(s, ")");
795+
then
796+
s;
694797

695798
case NORETCALL()
696-
then Expression.toString(eq.exp) + ";";
799+
then IOStream.append(s, Expression.toString(eq.exp));
697800

698-
else "#UNKNOWN EQUATION#";
801+
else IOStream.append(s, "#UNKNOWN EQUATION#");
699802
end match;
700-
end toString;
803+
end toStream;
701804

702-
function toStringList
805+
function toStreamList
703806
input list<Equation> eql;
704-
input String indent = "";
705-
output String str;
807+
input String indent;
808+
input output IOStream.IOStream s;
809+
protected
810+
Boolean prev_multi_line = false, multi_line;
811+
Boolean first = true;
706812
algorithm
707-
str := List.toString(eql, function toString(indent = indent), "", "", "\n", "", false) + "\n";
708-
end toStringList;
813+
for eq in eql loop
814+
multi_line := isMultiLine(eq);
815+
816+
// Improve human parsability by separating statements that spans multiple
817+
// lines (like if-equations) with newlines.
818+
if first then
819+
first := false;
820+
elseif prev_multi_line or multi_line then
821+
s := IOStream.append(s, "\n");
822+
end if;
823+
824+
prev_multi_line := multi_line;
825+
826+
s := toStream(eq, indent, s);
827+
s := IOStream.append(s, ";\n");
828+
end for;
829+
end toStreamList;
830+
831+
function isMultiLine
832+
input Equation eq;
833+
output Boolean singleLine;
834+
algorithm
835+
singleLine := match eq
836+
case FOR() then true;
837+
case IF() then true;
838+
case WHEN() then true;
839+
else false;
840+
end match;
841+
end isMultiLine;
709842

710843
annotation(__OpenModelica_Interface="frontend");
711844
end NFEquation;

Compiler/NFFrontEnd/NFFlatModel.mo

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,34 @@ public
6060
s := IOStream.create(getInstanceName(), IOStream.IOStreamType.LIST());
6161

6262
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);
63+
64+
for v in flatModel.variables loop
65+
s := Variable.toStream(v, " ", s);
66+
s := IOStream.append(s, ";\n");
67+
end for;
68+
69+
if not listEmpty(flatModel.initialEquations) then
70+
s := IOStream.append(s, "initial equation\n");
71+
s := Equation.toStreamList(flatModel.initialEquations, " ", s);
72+
end if;
73+
74+
if not listEmpty(flatModel.equations) then
75+
s := IOStream.append(s, "equation\n");
76+
s := Equation.toStreamList(flatModel.equations, " ", s);
77+
end if;
6678

6779
for alg in flatModel.initialAlgorithms loop
68-
s := toString2(alg.statements, function Statement.toString(indent = " "), "initial algorithm", s);
80+
if not listEmpty(alg.statements) then
81+
s := IOStream.append(s, "initial algorithm\n");
82+
s := Statement.toStreamList(alg.statements, " ", s);
83+
end if;
6984
end for;
7085

7186
for alg in flatModel.algorithms loop
72-
s := toString2(alg.statements, function Statement.toString(indent = " "), "algorithm", s);
87+
if not listEmpty(alg.statements) then
88+
s := IOStream.append(s, "algorithm\n");
89+
s := Statement.toStreamList(alg.statements, " ", s);
90+
end if;
7391
end for;
7492

7593
s := IOStream.append(s, "end " + flatModel.name + ";\n");
@@ -78,32 +96,5 @@ public
7896
IOStream.delete(s);
7997
end toString;
8098

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-
10899
annotation(__OpenModelica_Interface="frontend");
109100
end NFFlatModel;

0 commit comments

Comments
 (0)