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

Commit

Permalink
[NF] Improve toString functions.
Browse files Browse the repository at this point in the history
- Implement toString for Variable, Algorithm and FlatModel.
- Improve formatting in Equation.toString and Statement.toString.

Belonging to [master]:
  - #2854
  • Loading branch information
perost authored and OpenModelica-Hudson committed Jan 9, 2019
1 parent 8311f26 commit 15e62b7
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 43 deletions.
7 changes: 7 additions & 0 deletions Compiler/NFFrontEnd/NFAlgorithm.mo
Expand Up @@ -119,5 +119,12 @@ public
end for;
end foldExpList;

function toString
input Algorithm alg;
output String str;
algorithm
str := Statement.toStringList(alg.statements);
end toString;

annotation(__OpenModelica_Interface="frontend");
end NFAlgorithm;
43 changes: 24 additions & 19 deletions Compiler/NFFrontEnd/NFEquation.mo
Expand Up @@ -60,14 +60,15 @@ public

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

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

Expand Down Expand Up @@ -643,63 +644,67 @@ public

function toString
input Equation eq;
input String indent = "";
output String str;
algorithm
str := match eq
local
String s1, s2;

case EQUALITY()
then Expression.toString(eq.lhs) + " = " + Expression.toString(eq.rhs);
then indent + Expression.toString(eq.lhs) + " = " + Expression.toString(eq.rhs) + ";";

case CREF_EQUALITY()
then ComponentRef.toString(eq.lhs) + " = " + ComponentRef.toString(eq.rhs);
then indent + ComponentRef.toString(eq.lhs) + " = " + ComponentRef.toString(eq.rhs) + ";";

case ARRAY_EQUALITY()
then Expression.toString(eq.lhs) + " = " + Expression.toString(eq.rhs);
then indent + Expression.toString(eq.lhs) + " = " + Expression.toString(eq.rhs) + ";";

case CONNECT()
then "connect(" + Expression.toString(eq.lhs) + ", " + Expression.toString(eq.rhs) + ")";
then indent + "connect(" + Expression.toString(eq.lhs) + ", " + Expression.toString(eq.rhs) + ");";

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

case IF()
then "if " + Branch.toString(listHead(eq.branches)) +
List.toString(listRest(eq.branches), Branch.toString, "", "elseif ", "\nelseif ", "", false) +
"\nend 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;";

case WHEN()
then "when " + Branch.toString(listHead(eq.branches)) +
List.toString(listRest(eq.branches), Branch.toString, "", "elsewhen ", "\nelsewhen ", "", false) +
"\nend 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;";

case ASSERT()
then "assert(" + Expression.toString(eq.condition) + ", " +
Expression.toString(eq.message) + ", " + Expression.toString(eq.level) + ")";
Expression.toString(eq.message) + ", " + Expression.toString(eq.level) + ");";

case TERMINATE()
then "terminate( " + Expression.toString(eq.message) + ")";
then "terminate( " + Expression.toString(eq.message) + ");";

case REINIT()
then "reinit(" + Expression.toString(eq.cref) + ", " + Expression.toString(eq.reinitExp) + ")";
then "reinit(" + Expression.toString(eq.cref) + ", " + Expression.toString(eq.reinitExp) + ");";

case NORETCALL()
then Expression.toString(eq.exp);
then Expression.toString(eq.exp) + ";";

else "#UNKNOWN EQUATION#";
end match;
end toString;

function toStringList
input list<Equation> eql;
input String indent = "";
output String str;
algorithm
str := List.toString(eql, toString, "", " ", "\n ", "", false) + "\n";
str := List.toString(eql, function toString(indent = indent), "", "", "\n", "", false) + "\n";
end toStringList;

annotation(__OpenModelica_Interface="frontend");
Expand Down
62 changes: 62 additions & 0 deletions Compiler/NFFrontEnd/NFFlatModel.mo
Expand Up @@ -34,7 +34,15 @@ encapsulated uniontype NFFlatModel
import Algorithm = NFAlgorithm;
import Variable = NFVariable;

protected
import Statement = NFStatement;
import IOStream;

import FlatModel = NFFlatModel;

public
record FLAT_MODEL
String name;
list<Variable> variables;
list<Equation> equations;
list<Equation> initialEquations;
Expand All @@ -43,5 +51,59 @@ encapsulated uniontype NFFlatModel
Option<SCode.Comment> comment;
end FLAT_MODEL;

function toString
input FlatModel flatModel;
output String str;
protected
IOStream.IOStream s;
algorithm
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 alg in flatModel.initialAlgorithms loop
s := toString2(alg.statements, function Statement.toString(indent = " "), "initial algorithm", s);
end for;

for alg in flatModel.algorithms loop
s := toString2(alg.statements, function Statement.toString(indent = " "), "algorithm", s);
end for;

s := IOStream.append(s, "end " + flatModel.name + ";\n");

str := IOStream.string(s);
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;
4 changes: 2 additions & 2 deletions Compiler/NFFrontEnd/NFFlatten.mo
Expand Up @@ -145,9 +145,9 @@ algorithm
alg := listReverseInPlace(sections.algorithms);
ialg := listReverseInPlace(sections.initialAlgorithms);
then
FlatModel.FLAT_MODEL(vars, eql, ieql, alg, ialg, cmt);
FlatModel.FLAT_MODEL(name, vars, eql, ieql, alg, ialg, cmt);

else FlatModel.FLAT_MODEL(vars, {}, {}, {}, {}, cmt);
else FlatModel.FLAT_MODEL(name, vars, {}, {}, {}, {}, cmt);
end match;

execStat(getInstanceName() + "(" + name + ")");
Expand Down
47 changes: 25 additions & 22 deletions Compiler/NFFrontEnd/NFStatement.mo
Expand Up @@ -420,58 +420,60 @@ public

function toString
input Statement stmt;
input String indent = "";
output String str;
algorithm
str := match stmt
local
String s1, s2;

case ASSIGNMENT()
then Expression.toString(stmt.lhs) + " := " + Expression.toString(stmt.rhs);
then indent + Expression.toString(stmt.lhs) + " := " + Expression.toString(stmt.rhs) + ";";

case FUNCTION_ARRAY_INIT()
then "array init " + stmt.name;
then indent + "array init " + stmt.name;

case FOR()
algorithm
s1 := if isSome(stmt.range) then " in " + Expression.toString(Util.getOption(stmt.range)) else "";
s2 := toStringList(stmt.body);
s2 := toStringList(stmt.body, indent + " ");
then
"for " + InstNode.name(stmt.iterator) + s1 + " loop\n" + s2 + "end for";
indent + "for " + InstNode.name(stmt.iterator) + s1 + " loop\n" + s2 + indent + "end for;";

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

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

case ASSERT()
then "assert(" + Expression.toString(stmt.condition) + ", " +
Expression.toString(stmt.message) + ", " + Expression.toString(stmt.level) + ")";
then indent + "assert(" + Expression.toString(stmt.condition) + ", " +
Expression.toString(stmt.message) + ", " + Expression.toString(stmt.level) + ");";

case TERMINATE()
then "terminate( " + Expression.toString(stmt.message) + ")";
then indent + "terminate( " + Expression.toString(stmt.message) + ");";

case NORETCALL()
then Expression.toString(stmt.exp);
then indent + Expression.toString(stmt.exp) + ";";

case WHILE()
then "while " + Expression.toString(stmt.condition) + " then\n" +
toStringList(stmt.body) + "\nend while";
then indent + "while " + Expression.toString(stmt.condition) + " then\n" +
toStringList(stmt.body, indent + " ") + "\nend while;";

case RETURN() then "return";
case BREAK() then "break";
else "#UNKNOWN STATEMENT#";
case RETURN() then indent + "return;";
case BREAK() then indent + "break;";
else indent + "#UNKNOWN STATEMENT#";
end match;
end toString;

function branchString
input tuple<Expression, list<Statement>> branch;
input String stmtType;
input String indent;
input Boolean firstBranch;
output String str;
protected
Expand All @@ -480,15 +482,16 @@ public
algorithm
(cond, body) := branch;

str := (if firstBranch then stmtType elseif Expression.isTrue(cond) then "else" else "else" + stmtType) +
" " + Expression.toString(cond) + "then\n" + toStringList(body);
str := indent + (if firstBranch then stmtType elseif Expression.isTrue(cond) then "else" else "else" + stmtType) +
" " + Expression.toString(cond) + " then\n" + toStringList(body, indent + " ");
end branchString;

function toStringList
input list<Statement> stmtl;
input String indent = "";
output String str;
algorithm
str := List.toString(stmtl, toString, "", " " , "\n ", "", false) + "\n";
str := List.toString(stmtl, function toString(indent = indent), "", "", "\n", "", false) + "\n";
end toStringList;

annotation(__OpenModelica_Interface="frontend");
Expand Down
51 changes: 51 additions & 0 deletions Compiler/NFFrontEnd/NFVariable.mo
Expand Up @@ -41,6 +41,7 @@ encapsulated uniontype NFVariable

protected
import Variable = NFVariable;
import IOStream;

public
record VARIABLE
Expand Down Expand Up @@ -89,5 +90,55 @@ public
output Boolean isEmpty = Type.isEmptyArray(variable.ty);
end isEmptyArray;

function toString
input Variable var;
input String indent = "";
output String str;
protected
IOStream.IOStream s;
Boolean first;
algorithm
s := IOStream.create(getInstanceName(), IOStream.IOStreamType.LIST());

s := IOStream.append(s, indent);

if var.visibility == Visibility.PROTECTED then
s := IOStream.append(s, "protected ");
end if;

s := IOStream.append(s, Component.Attributes.toString(var.attributes, var.ty));
s := IOStream.append(s, Type.toString(var.ty));
s := IOStream.append(s, " ");
s := IOStream.append(s, ComponentRef.toString(var.name));

if not listEmpty(var.typeAttributes) then
s := IOStream.append(s, "(");

first := true;
for a in var.typeAttributes loop
if first then
first := false;
else
s := IOStream.append(s, ", ");
end if;

s := IOStream.append(s, Util.tuple21(a));
s := IOStream.append(s, " = ");
s := IOStream.append(s, Binding.toString(Util.tuple22(a)));
end for;

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

if Binding.isBound(var.binding) then
s := IOStream.append(s, " = ");
s := IOStream.append(s, Binding.toString(var.binding));
end if;

s := IOStream.append(s, ";");
str := IOStream.string(s);
IOStream.delete(s);
end toString;

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

0 comments on commit 15e62b7

Please sign in to comment.