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

Commit

Permalink
[NF] Cleanup some toString functions.
Browse files Browse the repository at this point in the history
Belonging to [master]:
  - #2872
  • Loading branch information
perost authored and OpenModelica-Hudson committed Jan 14, 2019
1 parent d681d71 commit 973bab3
Show file tree
Hide file tree
Showing 4 changed files with 354 additions and 117 deletions.
207 changes: 170 additions & 37 deletions Compiler/NFFrontEnd/NFEquation.mo
Expand Up @@ -44,6 +44,7 @@ protected
import NFComponent.Component;
import Util;
import ElementSource;
import IOStream;

public
uniontype Branch
Expand All @@ -58,19 +59,24 @@ public
list<Error.TotalMessage> errors;
end INVALID_BRANCH;

function toString
function toStream
input Branch branch;
input String indent = "";
output String str;
input String indent;
input output IOStream.IOStream s;
algorithm
str := match branch
s := match branch
case BRANCH()
then Expression.toString(branch.condition) + " then\n" + toStringList(branch.body, indent + " ");
algorithm
s := IOStream.append(s, Expression.toString(branch.condition));
s := IOStream.append(s, " then\n");
s := toStreamList(branch.body, indent + " ", s);
then
s;

case INVALID_BRANCH()
then toString(branch.branch, indent);
then toStream(branch.branch, indent, s);
end match;
end toString;
end toStream;

function triggerErrors
input Branch branch;
Expand Down Expand Up @@ -646,66 +652,193 @@ public
input Equation eq;
input String indent = "";
output String str;
protected
IOStream.IOStream s;
algorithm
str := match eq
local
String s1, s2;
s := IOStream.create(getInstanceName(), IOStream.IOStreamType.LIST());
s := toStream(eq, indent, s);
str := IOStream.string(s);
IOStream.delete(s);
end toString;

function toStringList
input list<Equation> eql;
input String indent = "";
output String str;
protected
IOStream.IOStream s;
algorithm
s := IOStream.create(getInstanceName(), IOStream.IOStreamType.LIST());
s := toStreamList(eql, indent, s);
str := IOStream.string(s);
IOStream.delete(s);
end toStringList;

function toStream
input Equation eq;
input String indent;
input output IOStream.IOStream s;
algorithm
s := IOStream.append(s, indent);

s := match eq
case EQUALITY()
then indent + Expression.toString(eq.lhs) + " = " + Expression.toString(eq.rhs) + ";";
algorithm
s := IOStream.append(s, Expression.toString(eq.lhs));
s := IOStream.append(s, " = ");
s := IOStream.append(s, Expression.toString(eq.rhs));
then
s;

case CREF_EQUALITY()
then indent + ComponentRef.toString(eq.lhs) + " = " + ComponentRef.toString(eq.rhs) + ";";
algorithm
s := IOStream.append(s, ComponentRef.toString(eq.lhs));
s := IOStream.append(s, " = ");
s := IOStream.append(s, ComponentRef.toString(eq.rhs));
then
s;

case ARRAY_EQUALITY()
then indent + Expression.toString(eq.lhs) + " = " + Expression.toString(eq.rhs) + ";";
algorithm
s := IOStream.append(s, Expression.toString(eq.lhs));
s := IOStream.append(s, " = ");
s := IOStream.append(s, Expression.toString(eq.rhs));
then
s;

case CONNECT()
then indent + "connect(" + Expression.toString(eq.lhs) + ", " + Expression.toString(eq.rhs) + ");";
algorithm
s := IOStream.append(s, "connect(");
s := IOStream.append(s, Expression.toString(eq.lhs));
s := IOStream.append(s, " = ");
s := IOStream.append(s, Expression.toString(eq.rhs));
s := IOStream.append(s, ")");
then
s;

case FOR()
algorithm
s1 := if isSome(eq.range) then " in " + Expression.toString(Util.getOption(eq.range)) else "";
s2 := toStringList(eq.body, indent + " ");
s := IOStream.append(s, "for ");
s := IOStream.append(s, InstNode.name(eq.iterator));

if isSome(eq.range) then
s := IOStream.append(s, " in ");
s := IOStream.append(s, Expression.toString(Util.getOption(eq.range)));
end if;

s := IOStream.append(s, " loop\n");
s := toStreamList(eq.body, indent + " ", s);
s := IOStream.append(s, indent);
s := IOStream.append(s, "end for");
then
indent + "for " + InstNode.name(eq.iterator) + s1 + " loop\n" + s2 + indent + "end for;";
s;

case IF()
then indent + "if " + Branch.toString(listHead(eq.branches)) +
List.toString(listRest(eq.branches), function Branch.toString(indent = indent), "",
indent + "elseif ", "\n" + indent + "elseif ", "", false) +
indent + "end if;";
algorithm
s := IOStream.append(s, "if ");
s := Branch.toStream(listHead(eq.branches), indent, s);

for b in listRest(eq.branches) loop
s := IOStream.append(s, indent);
s := IOStream.append(s, "elseif ");
s := Branch.toStream(b, indent, s);
end for;

s := IOStream.append(s, indent);
s := IOStream.append(s, "end if");
then
s;

case WHEN()
then indent + "when " + Branch.toString(listHead(eq.branches)) +
List.toString(listRest(eq.branches), function Branch.toString(indent = indent), "",
indent + "elsewhen ", "\n" + indent + "elsewhen ", "", false) +
indent + "end when;";
algorithm
s := IOStream.append(s, "when ");
s := Branch.toStream(listHead(eq.branches), indent, s);

for b in listRest(eq.branches) loop
s := IOStream.append(s, indent);
s := IOStream.append(s, "elsewhen ");
s := Branch.toStream(b, indent, s);
end for;

s := IOStream.append(s, indent);
s := IOStream.append(s, "end when");
then
s;

case ASSERT()
then "assert(" + Expression.toString(eq.condition) + ", " +
Expression.toString(eq.message) + ", " + Expression.toString(eq.level) + ");";
algorithm
s := IOStream.append(s, "assert(");
s := IOStream.append(s, Expression.toString(eq.condition));
s := IOStream.append(s, ", ");
s := IOStream.append(s, Expression.toString(eq.message));
s := IOStream.append(s, ", ");
s := IOStream.append(s, Expression.toString(eq.level));
s := IOStream.append(s, ")");
then
s;

case TERMINATE()
then "terminate( " + Expression.toString(eq.message) + ");";
algorithm
s := IOStream.append(s, "terminate(");
s := IOStream.append(s, Expression.toString(eq.message));
s := IOStream.append(s, ")");
then
s;

case REINIT()
then "reinit(" + Expression.toString(eq.cref) + ", " + Expression.toString(eq.reinitExp) + ");";
algorithm
s := IOStream.append(s, "reinit(");
s := IOStream.append(s, Expression.toString(eq.cref));
s := IOStream.append(s, ", ");
s := IOStream.append(s, Expression.toString(eq.reinitExp));
s := IOStream.append(s, ")");
then
s;

case NORETCALL()
then Expression.toString(eq.exp) + ";";
then IOStream.append(s, Expression.toString(eq.exp));

else "#UNKNOWN EQUATION#";
else IOStream.append(s, "#UNKNOWN EQUATION#");
end match;
end toString;
end toStream;

function toStringList
function toStreamList
input list<Equation> eql;
input String indent = "";
output String str;
input String indent;
input output IOStream.IOStream s;
protected
Boolean prev_multi_line = false, multi_line;
Boolean first = true;
algorithm
str := List.toString(eql, function toString(indent = indent), "", "", "\n", "", false) + "\n";
end toStringList;
for eq in eql loop
multi_line := isMultiLine(eq);

// Improve human parsability by separating statements that spans multiple
// lines (like if-equations) with newlines.
if first then
first := false;
elseif prev_multi_line or multi_line then
s := IOStream.append(s, "\n");
end if;

prev_multi_line := multi_line;

s := toStream(eq, indent, s);
s := IOStream.append(s, ";\n");
end for;
end toStreamList;

function isMultiLine
input Equation eq;
output Boolean singleLine;
algorithm
singleLine := match eq
case FOR() then true;
case IF() then true;
case WHEN() then true;
else false;
end match;
end isMultiLine;

annotation(__OpenModelica_Interface="frontend");
end NFEquation;
55 changes: 23 additions & 32 deletions Compiler/NFFrontEnd/NFFlatModel.mo
Expand Up @@ -60,16 +60,34 @@ public
s := IOStream.create(getInstanceName(), IOStream.IOStreamType.LIST());

s := IOStream.append(s, "class " + flatModel.name + "\n");
s := toString2(flatModel.variables, function Variable.toString(indent = " "), "", s);
s := toString2(flatModel.initialEquations, function Equation.toString(indent = " "), "initial equation", s);
s := toString2(flatModel.equations, function Equation.toString(indent = " "), "equation", s);

for v in flatModel.variables loop
s := Variable.toStream(v, " ", s);
s := IOStream.append(s, ";\n");
end for;

if not listEmpty(flatModel.initialEquations) then
s := IOStream.append(s, "initial equation\n");
s := Equation.toStreamList(flatModel.initialEquations, " ", s);
end if;

if not listEmpty(flatModel.equations) then
s := IOStream.append(s, "equation\n");
s := Equation.toStreamList(flatModel.equations, " ", s);
end if;

for alg in flatModel.initialAlgorithms loop
s := toString2(alg.statements, function Statement.toString(indent = " "), "initial algorithm", s);
if not listEmpty(alg.statements) then
s := IOStream.append(s, "initial algorithm\n");
s := Statement.toStreamList(alg.statements, " ", s);
end if;
end for;

for alg in flatModel.algorithms loop
s := toString2(alg.statements, function Statement.toString(indent = " "), "algorithm", s);
if not listEmpty(alg.statements) then
s := IOStream.append(s, "algorithm\n");
s := Statement.toStreamList(alg.statements, " ", s);
end if;
end for;

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

protected
function toString2<T>
input list<T> elements;
input FuncT toStringFunc;
input String header;
input output IOStream.IOStream s;

partial function FuncT
input T element;
output String str;
end FuncT;
algorithm
if listEmpty(elements) then
return;
end if;

if not stringEmpty(header) then
s := IOStream.append(s, header);
s := IOStream.append(s, "\n");
end if;

for e in elements loop
s := IOStream.append(s, toStringFunc(e));
s := IOStream.append(s, "\n");
end for;
end toString2;

annotation(__OpenModelica_Interface="frontend");
end NFFlatModel;

0 comments on commit 973bab3

Please sign in to comment.