Skip to content

Commit 6f37ffb

Browse files
committed
- Added pretty-printing of the cases in match-expressions (which used to be ignored)
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7631 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
1 parent f93f6ba commit 6f37ffb

File tree

4 files changed

+91
-40
lines changed

4 files changed

+91
-40
lines changed

Compiler/BackEnd/BackendDAEUtil.mo

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2598,17 +2598,16 @@ protected
25982598
String keystr;
25992599
algorithm
26002600
keystr := ComponentReference.printComponentRefStr(key);
2601-
v := treeGet2(bt, keystr);
2601+
v := treeGet3(bt, keystr, treeGet2(bt, keystr));
26022602
end treeGet;
26032603

2604-
protected function treeGet2 "function: treeGet2
2605-
author: PA
2606-
Helper function to treeGet"
2604+
protected function treeGet2
2605+
"Helper function to treeGet"
26072606
input BackendDAE.BinTree inBinTree;
26082607
input String inString;
2609-
output BackendDAE.Value outValue;
2608+
output Integer compResult;
26102609
algorithm
2611-
outValue := match (inBinTree,inString)
2610+
compResult := match (inBinTree,inString)
26122611
local
26132612
String rkeystr,keystr;
26142613
DAE.ComponentRef rkey;
@@ -2620,7 +2619,7 @@ algorithm
26202619
case (BackendDAE.TREENODE(value = SOME(BackendDAE.TREEVALUE(key=rkey))),keystr)
26212620
equation
26222621
rkeystr = ComponentReference.printComponentRefStr(rkey);
2623-
then treeGet3(inBinTree, keystr, stringCompare(rkeystr, keystr));
2622+
then stringCompare(rkeystr, keystr);
26242623
end match;
26252624
end treeGet2;
26262625

@@ -2643,10 +2642,14 @@ algorithm
26432642
case (BackendDAE.TREENODE(value = SOME(BackendDAE.TREEVALUE(value=rval))),keystr,0) then rval;
26442643
// search right
26452644
case (BackendDAE.TREENODE(rightSubTree = SOME(right)),keystr,1)
2646-
then treeGet2(right, keystr);
2645+
equation
2646+
compResult = treeGet2(right, keystr);
2647+
then treeGet3(right, keystr, compResult);
26472648
// search left
26482649
case (BackendDAE.TREENODE(leftSubTree = SOME(left)),keystr,-1)
2649-
then treeGet2(left, keystr);
2650+
equation
2651+
compResult = treeGet2(left, keystr);
2652+
then treeGet3(left, keystr, compResult);
26502653
end match;
26512654
end treeGet3;
26522655

Compiler/FrontEnd/DAEDump.mo

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1718,8 +1718,8 @@ algorithm
17181718
end matchcontinue;
17191719
end ppWhenStmtStr;
17201720

1721-
protected function ppStmtStr "function: ppStmtStr
1722-
Helper function to ppStatementStr"
1721+
public function ppStmtStr
1722+
"Helper function to ppStatementStr"
17231723
input DAE.Statement inStatement;
17241724
input Integer inInteger;
17251725
output String outString;
@@ -1735,52 +1735,41 @@ algorithm
17351735
list<DAE.Statement> then_,stmts;
17361736
DAE.Statement stmt;
17371737
Algorithm.Else else_;
1738+
DAE.Pattern pat;
17381739

17391740
case (DAE.STMT_ASSIGN(exp1 = e2 as DAE.CREF(c,_),exp = e),i)
17401741
equation
17411742
s1 = indentStr(i);
17421743
s2 = ComponentReference.printComponentRefStr(c);
1743-
s3 = stringAppend(s1, s2);
1744-
s4 = stringAppend(s3, " := ");
1745-
s5 = ExpressionDump.printExpStr(e);
1746-
s6 = stringAppend(s4, s5);
1747-
str = stringAppend(s6, ";\n");
1744+
s3 = ExpressionDump.printExpStr(e);
1745+
str = stringAppendList({s1,s2," := ",s3,";\n"});
17481746
then
17491747
str;
17501748

17511749
case (DAE.STMT_ASSIGN(exp1 = e2 as DAE.ARRAY(array=_),exp = e),i)
17521750
equation
17531751
s1 = indentStr(i);
17541752
s2 = ExpressionDump.printExpStr(e2);
1755-
s3 = stringAppend(s1, s2);
1756-
s4 = stringAppend(s3, " := ");
1757-
s5 = ExpressionDump.printExpStr(e);
1758-
s6 = stringAppend(s4, s5);
1759-
str = stringAppend(s6, ";\n");
1753+
s3 = ExpressionDump.printExpStr(e);
1754+
str = stringAppendList({s1,s2," := ",s3,";\n"});
17601755
then
17611756
str;
17621757

17631758
case (DAE.STMT_ASSIGN(exp1 = e2 as DAE.ASUB(_,_),exp = e),i)
17641759
equation
17651760
s1 = indentStr(i);
17661761
s2 = ExpressionDump.printExpStr(e2);
1767-
s3 = stringAppend(s1, s2);
1768-
s4 = stringAppend(s3, " := ");
1769-
s5 = ExpressionDump.printExpStr(e);
1770-
s6 = stringAppend(s4, s5);
1771-
str = stringAppend(s6, ";\n");
1762+
s3 = ExpressionDump.printExpStr(e);
1763+
str = stringAppendList({s1,s2," := ",s3,";\n"});
17721764
then
17731765
str;
17741766

17751767
case (DAE.STMT_ASSIGN_ARR(componentRef = c,exp = e),i)
17761768
equation
17771769
s1 = indentStr(i);
17781770
s2 = ComponentReference.printComponentRefStr(c);
1779-
s3 = stringAppend(s1, s2);
1780-
s4 = stringAppend(s3, " := ");
1781-
s5 = ExpressionDump.printExpStr(e);
1782-
s6 = stringAppend(s4, s5);
1783-
str = stringAppend(s6, ";\n");
1771+
s3 = ExpressionDump.printExpStr(e);
1772+
str = stringAppendList({s1,s2," := ",s3,";\n"});
17841773
then
17851774
str;
17861775

@@ -1893,7 +1882,22 @@ algorithm
18931882
e2_str = ExpressionDump.printExpStr(e2);
18941883
str = stringAppendList({s1,"reinit(",e1_str,", ",e2_str,");\n"});
18951884
then str;
1896-
1885+
1886+
case (DAE.STMT_FAILURE(body=stmts),i)
1887+
equation
1888+
s1 = indentStr(i);
1889+
s2 = ppStmtListStr(stmts, i+2);
1890+
str = stringAppendList({s1,"failure(\n",s2,s1,");\n"});
1891+
then str;
1892+
1893+
case (DAE.STMT_ASSIGN_PATTERN(lhs=pat, rhs=e),i)
1894+
equation
1895+
s1 = indentStr(i);
1896+
s2 = Patternm.patternStr(pat);
1897+
s3 = ExpressionDump.printExpStr(e);
1898+
str = stringAppendList({s1,s2," := ",s3,";\n"});
1899+
then str;
1900+
18971901
case (_,i)
18981902
equation
18991903
s1 = indentStr(i);

Compiler/FrontEnd/ExpressionDump.mo

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ public type Var = DAE.ExpVar;
5555

5656
// protected imports
5757
protected import ComponentReference;
58+
protected import DAEDump;
5859
protected import Dump;
5960
protected import Expression;
61+
protected import Patternm;
6062
protected import RTOpts;
6163
protected import Util;
6264
protected import Print;
@@ -566,6 +568,7 @@ algorithm
566568
list<list<tuple<DAE.Exp, Boolean>>> lstes;
567569
Absyn.MatchType matchTy;
568570
DAE.ExpType et;
571+
list<DAE.MatchCase> cases;
569572

570573
case (DAE.END(), _, _, _) then "end";
571574

@@ -890,11 +893,12 @@ algorithm
890893
then
891894
s;
892895

893-
case (DAE.MATCHEXPRESSION(matchType=matchTy,inputs=es), _, _, _)
896+
case (DAE.MATCHEXPRESSION(matchType=matchTy,inputs=es,cases=cases), _, _, _)
894897
equation
895898
s1 = Dump.printMatchType(matchTy);
896899
s2 = printExp2Str(DAE.TUPLE(es), stringDelimiter, opcreffunc, opcallfunc);
897-
s = stringAppendList({s1,s2,"\n","#cases#\n","end ",s1});
900+
s3 = stringAppendList(Util.listMap(cases,printCase2Str));
901+
s = stringAppendList({s1,s2,s3,"end ",s1});
898902
then s;
899903

900904
case (DAE.SHARED_LITERAL(index=i,ty=et), _, _, _)
@@ -912,6 +916,31 @@ algorithm
912916
end matchcontinue;
913917
end printExp2Str;
914918

919+
protected function printCase2Str
920+
"Prints a matchcase as string"
921+
input DAE.MatchCase matchCase;
922+
output String str;
923+
algorithm
924+
str := match matchCase
925+
local
926+
list<DAE.Pattern> patterns;
927+
list<DAE.Statement> body;
928+
Option<DAE.Exp> result;
929+
String resultStr,patternsStr,bodyStr;
930+
case DAE.CASE(patterns=patterns, body={}, result=result)
931+
equation
932+
patternsStr = Patternm.patternStr(DAE.PAT_META_TUPLE(patterns));
933+
resultStr = Util.getOptionOrDefault(Util.applyOption(result, printExpStr), "fail()");
934+
then stringAppendList({" case ",patternsStr," then ",resultStr,";\n"});
935+
case DAE.CASE(patterns=patterns, body=body, result=result)
936+
equation
937+
patternsStr = Patternm.patternStr(DAE.PAT_META_TUPLE(patterns));
938+
resultStr = Util.getOptionOrDefault(Util.applyOption(result, printExpStr), "fail()");
939+
bodyStr = stringAppendList(Util.listMap1(body, DAEDump.ppStmtStr, 8));
940+
then stringAppendList({" case ",patternsStr,"\n algorithm\n",bodyStr," then ",resultStr,";\n"});
941+
end match;
942+
end printCase2Str;
943+
915944
public function expPriority
916945
"function: expPriority
917946
Returns a priority number for an expression.
@@ -1750,6 +1779,7 @@ algorithm
17501779
DAE.Exp ae1;
17511780
Boolean b;
17521781
Absyn.MatchType matchTy;
1782+
list<DAE.MatchCase> cases;
17531783

17541784
case (DAE.ICONST(integer = i),_)
17551785
equation
@@ -2069,13 +2099,13 @@ algorithm
20692099
then
20702100
();
20712101

2072-
case (DAE.MATCHEXPRESSION(matchType=matchTy,inputs=es), _)
2102+
case (DAE.MATCHEXPRESSION(matchType=matchTy,inputs=es,cases=cases), _)
20732103
equation
20742104
Print.printBuf(Dump.printMatchType(matchTy));
20752105
Print.printBuf(" (");
20762106
printList(es, printExp, ",");
20772107
Print.printBuf(") \n");
2078-
Print.printBuf(" #cases#\n");
2108+
Print.printBuf(stringAppendList(Util.listMap(cases,printCase2Str)));
20792109
Print.printBuf(" end ");
20802110
Print.printBuf(Dump.printMatchType(matchTy));
20812111
then ();

Compiler/FrontEnd/Patternm.mo

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,22 +460,36 @@ algorithm
460460
DAE.Pattern pat,head,tail;
461461
String id;
462462
DAE.ExpType et;
463+
Absyn.Path name;
463464
case DAE.PAT_WILD() then "_";
464-
case DAE.PAT_AS(id,_,DAE.PAT_WILD()) then id;
465+
case DAE.PAT_AS(id=id,pat=DAE.PAT_WILD()) then id;
466+
case DAE.PAT_AS_FUNC_PTR(id,DAE.PAT_WILD()) then id;
465467
case DAE.PAT_SOME(pat)
466468
equation
467469
str = patternStr(pat);
468470
then "SOME(" +& str +& ")";
469471
case DAE.PAT_META_TUPLE(pats)
470472
equation
471473
str = Util.stringDelimitList(Util.listMap(pats,patternStr),",");
472-
then "Tuple(" +& str +& ")";
474+
then "(" +& str +& ")";
475+
476+
case DAE.PAT_CALL_TUPLE(pats)
477+
equation
478+
str = Util.stringDelimitList(Util.listMap(pats,patternStr),",");
479+
then "(" +& str +& ")";
480+
481+
case DAE.PAT_CALL(name=name, patterns=pats)
482+
equation
483+
id = Absyn.pathString(name);
484+
str = Util.stringDelimitList(Util.listMap(pats,patternStr),",");
485+
then stringAppendList({id,"(",str,")"});
473486

474487
case DAE.PAT_CONS(head,tail) then patternStr(head) +& "::" +& patternStr(tail);
475488

476-
case DAE.PAT_CONSTANT(_,exp) then ExpressionDump.printExpStr(exp);
489+
case DAE.PAT_CONSTANT(exp=exp) then ExpressionDump.printExpStr(exp);
477490
// case DAE.PAT_CONSTANT(SOME(et),exp) then "(" +& ExpressionDump.typeString(et) +& ")" +& ExpressionDump.printExpStr(exp);
478-
case DAE.PAT_AS(id,_,pat) then id +& " as " +& patternStr(pat);
491+
case DAE.PAT_AS(id=id,pat=pat) then id +& " as " +& patternStr(pat);
492+
case DAE.PAT_AS_FUNC_PTR(id, pat) then id +& " as " +& patternStr(pat);
479493
else
480494
equation
481495
Error.addMessage(Error.INTERNAL_ERROR, {"Patternm.patternStr not implemented correctly"});

0 commit comments

Comments
 (0)