Skip to content

Commit 67cd8b9

Browse files
authored
Base Modelica improvements (#13175)
- Fix formatting of arguments for the `String` operator. - Don't quote `StateSelect` and `AssertionLevel`.
1 parent 78d9a41 commit 67cd8b9

File tree

7 files changed

+161
-3
lines changed

7 files changed

+161
-3
lines changed

OMCompiler/Compiler/NFFrontEnd/NFCall.mo

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ public
810810
case TYPED_CALL()
811811
algorithm
812812
name := AbsynUtil.pathString(Function.nameConsiderBuiltin(call.fn));
813-
arg_str := stringDelimitList(list(Expression.toFlatString(arg, format) for arg in call.arguments), ", ");
813+
arg_str := toFlatStringArgs(call.arguments, name, format);
814814
then
815815
if Function.isBuiltin(call.fn) then
816816
stringAppendList({name, "(", arg_str, ")"})
@@ -851,6 +851,50 @@ public
851851
end match;
852852
end toFlatString;
853853

854+
function toFlatStringArgs
855+
input list<Expression> args;
856+
input String fnName;
857+
input BaseModelica.OutputFormat format;
858+
output String argsString;
859+
protected
860+
Expression arg1, arg2;
861+
list<Expression> rest_args;
862+
algorithm
863+
argsString := match fnName
864+
case "String"
865+
then match args
866+
case {arg1, arg2}
867+
then Expression.toFlatString(arg1, format) + ", format = " + Expression.toFlatString(arg2, format);
868+
869+
else
870+
algorithm
871+
arg1 :: rest_args := args;
872+
argsString := Expression.toFlatString(arg1, format);
873+
874+
if listLength(rest_args) == 3 then
875+
arg1 :: rest_args := rest_args;
876+
if not Expression.isIntegerValue(arg1, 6) then
877+
argsString := argsString + ", significantDigits = " + Expression.toFlatString(arg1, format);
878+
end if;
879+
end if;
880+
881+
arg1 :: rest_args := rest_args;
882+
if not Expression.isZero(arg1) then
883+
argsString := argsString + ", minimumLength = " + Expression.toFlatString(arg1, format);
884+
end if;
885+
886+
arg1 :: rest_args := rest_args;
887+
if not Expression.isTrue(arg1) then
888+
argsString := argsString + ", leftJustified = " + Expression.toFlatString(arg1, format);
889+
end if;
890+
then
891+
argsString;
892+
end match;
893+
894+
else stringDelimitList(list(Expression.toFlatString(arg, format) for arg in args), ", ");
895+
end match;
896+
end toFlatStringArgs;
897+
854898
function typedString
855899
"Like toString, but prefixes each argument with its type as a comment."
856900
input NFCall call;

OMCompiler/Compiler/NFFrontEnd/NFExpression.mo

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1800,6 +1800,19 @@ public
18001800
threaded for l in lits, i in 1:listLength(lits));
18011801
end makeEnumLiterals;
18021802

1803+
function isIntegerValue
1804+
"Returns true if the expression is an Integer expression with the given
1805+
value, otherwise false."
1806+
input Expression exp;
1807+
input Integer value;
1808+
output Boolean result;
1809+
algorithm
1810+
result := match exp
1811+
case INTEGER() then exp.value == value;
1812+
else false;
1813+
end match;
1814+
end isIntegerValue;
1815+
18031816
function toInteger
18041817
input Expression exp;
18051818
output Integer i;
@@ -1930,7 +1943,10 @@ public
19301943
case BOOLEAN() then boolString(exp.value);
19311944

19321945
case ENUM_LITERAL(ty = t as Type.ENUMERATION())
1933-
then "'" + AbsynUtil.pathString(t.typePath) + "'." + exp.name;
1946+
then if Type.isBuiltinEnumeration(t) then
1947+
AbsynUtil.pathString(t.typePath) + "." + exp.name
1948+
else
1949+
"'" + AbsynUtil.pathString(t.typePath) + "'." + exp.name;
19341950

19351951
case CLKCONST(clk) then ClockKind.toFlatString(clk, format);
19361952

OMCompiler/Compiler/NFFrontEnd/NFFlatModel.mo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ public
352352
algorithm
353353
() := match ty
354354
case Type.ENUMERATION()
355+
guard not Type.isBuiltinEnumeration(ty)
355356
algorithm
356357
UnorderedMap.tryAdd(ty.typePath, ty, types);
357358
then

OMCompiler/Compiler/NFFrontEnd/NFType.mo

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,24 @@ public
465465
end match;
466466
end isEnumeration;
467467

468+
function isBuiltinEnumeration
469+
input Type ty;
470+
output Boolean isBuiltin;
471+
protected
472+
String name;
473+
algorithm
474+
isBuiltin := match ty
475+
case ENUMERATION(typePath = Absyn.Path.IDENT(name))
476+
then match name
477+
case "StateSelect" then true;
478+
case "AssertionLevel" then true;
479+
else false;
480+
end match;
481+
482+
else false;
483+
end match;
484+
end isBuiltinEnumeration;
485+
468486
function isUnspecifiedEnumeration
469487
input Type ty;
470488
output Boolean res;
@@ -942,7 +960,10 @@ public
942960
case Type.STRING() then "String";
943961
case Type.BOOLEAN() then "Boolean";
944962
case Type.CLOCK() then "Clock";
945-
case Type.ENUMERATION() then if listEmpty(ty.literals) then "enumeration(:)" else Util.makeQuotedIdentifier(AbsynUtil.pathString(ty.typePath));
963+
case Type.ENUMERATION()
964+
then if listEmpty(ty.literals) then "enumeration(:)"
965+
elseif Type.isBuiltinEnumeration(ty) then AbsynUtil.pathString(ty.typePath)
966+
else Util.makeQuotedIdentifier(AbsynUtil.pathString(ty.typePath));
946967
case Type.ARRAY() then List.toString(ty.dimensions, function Dimension.toFlatString(format = format), toFlatString(ty.elementType, format), "[", ", ", "]", false);
947968
case Type.TUPLE() then "(" + stringDelimitList(List.map(ty.types, function toFlatString(format = format)), ", ") + ")";
948969
case Type.NORETCALL() then "()";

testsuite/openmodelica/basemodelica/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ TESTFILES = \
3535
ScalarizedWithoutRecords1.mo \
3636
SD.mo \
3737
SimpleCoolingCycle.mo \
38+
StateSelect1.mo \
39+
String1.mo \
3840
Tables.mos \
3941
Tuple1.mo \
4042
Tuple2.mo \
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// name: StateSelect1
2+
// status: correct
3+
4+
model StateSelect1
5+
Real x(stateSelect = StateSelect.never);
6+
parameter StateSelect s = StateSelect.always;
7+
Real y(stateSelect = s);
8+
annotation(__OpenModelica_commandLineOptions="-f");
9+
end StateSelect1;
10+
11+
// Result:
12+
// //! base 0.1.0
13+
// package 'StateSelect1'
14+
// model 'StateSelect1'
15+
// Real 'x'(stateSelect = StateSelect.never);
16+
// parameter StateSelect 's' = StateSelect.always;
17+
// Real 'y'(stateSelect = StateSelect.always);
18+
// end 'StateSelect1';
19+
// end 'StateSelect1';
20+
// endResult
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// name: String1
2+
// status: correct
3+
4+
model String1
5+
Real r;
6+
Integer i;
7+
Boolean b;
8+
type E = enumeration(one, two, three);
9+
E e;
10+
11+
String s_r1 = String(r);
12+
String s_r2 = String(r, format = "g");
13+
String s_r3 = String(r, minimumLength = 2, leftJustified = false, significantDigits = 4);
14+
String s_r4 = String(r, minimumLength = 0, leftJustified = true, significantDigits = 6);
15+
String s_r5 = String(r, minimumLength = 2, significantDigits = 6);
16+
17+
String s_i1 = String(i);
18+
String s_i2 = String(i, format = "d");
19+
String s_i3 = String(i, minimumLength = 8, leftJustified = false);
20+
21+
String s_b1 = String(b);
22+
String s_b2 = String(b, minimumLength = 4, leftJustified = true);
23+
24+
String s_e1 = String(e);
25+
String s_e2 = String(e, minimumLength = 12, leftJustified = false);
26+
27+
annotation(__OpenModelica_commandLineOptions="-f");
28+
end String1;
29+
30+
// Result:
31+
// //! base 0.1.0
32+
// package 'String1'
33+
// type 'E' = enumeration(one, two, three);
34+
//
35+
// model 'String1'
36+
// Real 'r';
37+
// Integer 'i';
38+
// Boolean 'b';
39+
// 'E' 'e';
40+
// String 's_r1' = String('r');
41+
// String 's_r2' = String('r', format = "g");
42+
// String 's_r3' = String('r', significantDigits = 4, minimumLength = 2, leftJustified = false);
43+
// String 's_r4' = String('r');
44+
// String 's_r5' = String('r', minimumLength = 2);
45+
// String 's_i1' = String('i');
46+
// String 's_i2' = String('i', format = "d");
47+
// String 's_i3' = String('i', minimumLength = 8, leftJustified = false);
48+
// String 's_b1' = String('b');
49+
// String 's_b2' = String('b', minimumLength = 4);
50+
// String 's_e1' = String('e');
51+
// String 's_e2' = String('e', minimumLength = 12, leftJustified = false);
52+
// end 'String1';
53+
// end 'String1';
54+
// endResult

0 commit comments

Comments
 (0)