@@ -44,6 +44,7 @@ protected
4444 import NFComponent.Component ;
4545 import Util ;
4646 import ElementSource ;
47+ import IOStream ;
4748
4849public
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
710843annotation(__OpenModelica_Interface= "frontend" );
711844end NFEquation ;
0 commit comments