Skip to content

Commit

Permalink
Fixes for bug #1134:
Browse files Browse the repository at this point in the history
- Fixed constant evaluation of String(enumeration literal).
- Implemented minimumLength and leftJustified when constant evaluation String().
- Removed significantDigits from *_to_modelica_string function in runtime except
  for real, because real is the only one that supports significantDigits.
- Updated test cases that use String().


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@6286 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
perost committed Oct 5, 2010
1 parent 2f556d2 commit 78247fe
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 20 deletions.
93 changes: 88 additions & 5 deletions Compiler/Ceval.mo
Expand Up @@ -2359,7 +2359,9 @@ end cevalBuiltinPromote2;

protected function cevalBuiltinString "
author: PA
Evaluates the String operator String(r), String(i), String(b), String(e)"
Evaluates the String operator String(r), String(i), String(b), String(e).
TODO: Also evaluate String(r, significantDigits=d), and String(r, format=s)."
input Env.Cache inCache;
input Env.Env inEnv;
input list<DAE.Exp> inExpExpLst;
Expand All @@ -2376,37 +2378,118 @@ algorithm
Values.Value arr_val,res;
Integer dim_val;
list<Env.Frame> env;
DAE.Exp exp;
DAE.Exp exp, len_exp, justified_exp;
Boolean impl;
Option<Interactive.InteractiveSymbolTable> st;
Msg msg;
Env.Cache cache;
String str;
Integer i; Real r; Boolean b;
case (cache,env,{exp,_,_,_},impl,st,msg)
case (cache,env,{exp, len_exp, justified_exp},impl,st,msg)
equation
(cache,Values.INTEGER(i),_) = ceval(cache,env, exp, impl, st, NONE, msg);
str = intString(i);
(cache, str) = cevalBuiltinStringFormat(cache, env, str, len_exp, justified_exp, impl, st, msg);
then
(cache,Values.STRING(str),st);

case (cache,env,{exp,_,_,_},impl,st,msg)
case (cache,env,{exp, len_exp, justified_exp, _},impl,st,msg)
equation
(cache,Values.REAL(r),_) = ceval(cache,env, exp, impl, st, NONE, msg);
str = realString(r);
(cache, str) = cevalBuiltinStringFormat(cache, env, str, len_exp, justified_exp, impl, st, msg);
then
(cache,Values.STRING(str),st);

case (cache,env,{exp,_,_,_},impl,st,msg)
case (cache,env,{exp, len_exp, justified_exp},impl,st,msg)
equation
(cache,Values.BOOL(b),_) = ceval(cache,env, exp, impl, st, NONE, msg);
str = Util.boolString(b);
(cache, str) = cevalBuiltinStringFormat(cache, env, str, len_exp, justified_exp, impl, st, msg);
then
(cache,Values.STRING(str),st);

case (cache,env,{exp, len_exp, justified_exp},impl,st,msg)
local
Absyn.Path p;
equation
(cache,Values.ENUM_LITERAL(name = p),_) = ceval(cache,env, exp, impl, st, NONE, msg);
str = Absyn.pathLastIdent(p);
(cache, str) = cevalBuiltinStringFormat(cache, env, str, len_exp, justified_exp, impl, st, msg);
then
(cache,Values.STRING(str),st);

end matchcontinue;
end cevalBuiltinString;

protected function cevalBuiltinStringFormat
"This function formats a string by using the minimumLength and leftJustified
arguments to the String function."
input Env.Cache inCache;
input Env.Env inEnv;
input String inString;
input DAE.Exp lengthExp;
input DAE.Exp justifiedExp;
input Boolean inBoolean;
input Option<Interactive.InteractiveSymbolTable> inST;
input Msg inMsg;
output Env.Cache outCache;
output String outString;
algorithm
(outCache, outString) := matchcontinue(inCache, inEnv, inString, lengthExp,
justifiedExp, inBoolean, inST, inMsg)
local
Env.Cache cache;
Integer min_length;
Boolean left_justified;
String str;
case (cache, _, _, _, _, _, _, _)
equation
(cache, Values.INTEGER(integer = min_length), _) =
ceval(cache, inEnv, lengthExp, inBoolean, inST, NONE, inMsg);
(cache, Values.BOOL(boolean = left_justified), _) =
ceval(cache, inEnv, justifiedExp, inBoolean, inST, NONE, inMsg);
str = cevalBuiltinStringFormat2(inString, stringLength(inString), min_length, left_justified);
then
(cache, str);
end matchcontinue;
end cevalBuiltinStringFormat;

protected function cevalBuiltinStringFormat2
"Helper function to cevalBuiltinStringFormat, does the actual formatting."
input String inString;
input Integer stringLength;
input Integer minLength;
input Boolean leftJustified;
output String outString;
algorithm
outString := matchcontinue(inString, stringLength, minLength, leftJustified)
local
String str;
Integer fill_size;
// The string is longer than the minimum length, do nothing.
case (_, _, _, _)
equation
true = stringLength >= minLength;
then
inString;
// leftJustified is false, append spaces at the beginning of the string.
case (_, _, _, false)
equation
fill_size = minLength - stringLength;
str = Util.stringAppendList(Util.listFill(" ", fill_size)) +& inString;
then
str;
// leftJustified is true, append spaces at the end of the string.
case (_, _, _, true)
equation
fill_size = minLength - stringLength;
str = inString +& Util.stringAppendList(Util.listFill(" ", fill_size));
then
str;
end matchcontinue;
end cevalBuiltinStringFormat2;

protected function cevalBuiltinLinspace "
author: PA
Evaluates the linpace function"
Expand Down
39 changes: 35 additions & 4 deletions Compiler/SimCodeC.mo
Expand Up @@ -24396,17 +24396,15 @@ algorithm
then (txt, i_preExp, i_varDecls);

case ( txt,
DAE.CALL(tuple_ = false, builtin = true, path = Absyn.IDENT(name = "String"), expLst = {i_s, i_minlen, i_leftjust, i_signdig}),
DAE.CALL(tuple_ = false, builtin = true, path = Absyn.IDENT(name = "String"), expLst = {i_s, i_minlen, i_leftjust}),
i_context,
i_preExp,
i_varDecls )
local
DAE.Exp i_signdig;
DAE.Exp i_leftjust;
DAE.Exp i_minlen;
DAE.Exp i_s;
Tpl.Text i_typeStr;
Tpl.Text i_signdigExp;
Tpl.Text i_leftjustExp;
Tpl.Text i_minlenExp;
Tpl.Text i_sExp;
Expand All @@ -24416,7 +24414,6 @@ algorithm
(i_sExp, i_preExp, i_varDecls) = daeExp(emptyTxt, i_s, i_context, i_preExp, i_varDecls);
(i_minlenExp, i_preExp, i_varDecls) = daeExp(emptyTxt, i_minlen, i_context, i_preExp, i_varDecls);
(i_leftjustExp, i_preExp, i_varDecls) = daeExp(emptyTxt, i_leftjust, i_context, i_preExp, i_varDecls);
(i_signdigExp, i_preExp, i_varDecls) = daeExp(emptyTxt, i_signdig, i_context, i_preExp, i_varDecls);
i_typeStr = expTypeFromExpModelica(emptyTxt, i_s);
i_preExp = Tpl.writeText(i_preExp, i_typeStr);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_STRING("_to_modelica_string(&"));
Expand All @@ -24427,6 +24424,40 @@ algorithm
i_preExp = Tpl.writeText(i_preExp, i_minlenExp);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_STRING(", "));
i_preExp = Tpl.writeText(i_preExp, i_leftjustExp);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_STRING(");"));
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_NEW_LINE());
txt = Tpl.writeText(txt, i_tvar);
then (txt, i_preExp, i_varDecls);

case ( txt,
DAE.CALL(tuple_ = false, builtin = true, path = Absyn.IDENT(name = "String"), expLst = {i_s, i_minlen, i_leftjust, i_signdig}),
i_context,
i_preExp,
i_varDecls )
local
DAE.Exp i_signdig;
DAE.Exp i_leftjust;
DAE.Exp i_minlen;
DAE.Exp i_s;
Tpl.Text i_signdigExp;
Tpl.Text i_leftjustExp;
Tpl.Text i_minlenExp;
Tpl.Text i_sExp;
Tpl.Text i_tvar;
equation
(i_tvar, i_varDecls) = tempDecl(emptyTxt, "modelica_string", i_varDecls);
(i_sExp, i_preExp, i_varDecls) = daeExp(emptyTxt, i_s, i_context, i_preExp, i_varDecls);
(i_minlenExp, i_preExp, i_varDecls) = daeExp(emptyTxt, i_minlen, i_context, i_preExp, i_varDecls);
(i_leftjustExp, i_preExp, i_varDecls) = daeExp(emptyTxt, i_leftjust, i_context, i_preExp, i_varDecls);
(i_signdigExp, i_preExp, i_varDecls) = daeExp(emptyTxt, i_signdig, i_context, i_preExp, i_varDecls);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_STRING("modelica_real_to_modelica_string(&"));
i_preExp = Tpl.writeText(i_preExp, i_tvar);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_STRING(", "));
i_preExp = Tpl.writeText(i_preExp, i_sExp);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_STRING(", "));
i_preExp = Tpl.writeText(i_preExp, i_minlenExp);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_STRING(", "));
i_preExp = Tpl.writeText(i_preExp, i_leftjustExp);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_STRING(", "));
i_preExp = Tpl.writeText(i_preExp, i_signdigExp);
i_preExp = Tpl.writeTok(i_preExp, Tpl.ST_STRING(");"));
Expand Down
9 changes: 6 additions & 3 deletions Compiler/Static.mo
Expand Up @@ -6406,11 +6406,14 @@ algorithm
case (cache,env,args as e::_,nargs,impl,pre)
equation
(cache,exp,DAE.PROP(tp,c),_,dae1) = elabExp(cache,env, e, impl, NONE,true,pre);
/* Create argument slots for String function */
// Create argument slots for String function.
slots = {SLOT(("x",tp),false,NONE,{}),
SLOT(("minimumLength",DAE.T_INTEGER_DEFAULT),false,SOME(DAE.ICONST(0)),{}),
SLOT(("leftJustified",DAE.T_BOOL_DEFAULT),false,SOME(DAE.BCONST(true)),{}),
SLOT(("significantDigits",DAE.T_INTEGER_DEFAULT),false,SOME(DAE.ICONST(6)),{})};
SLOT(("leftJustified",DAE.T_BOOL_DEFAULT),false,SOME(DAE.BCONST(true)),{})};
// Only String(Real) has the significantDigits option.
slots = Util.if_(Types.isRealOrSubTypeReal(tp),
listAppend(slots, {SLOT(("significantDigits",DAE.T_INTEGER_DEFAULT),false,SOME(DAE.ICONST(6)),{})}),
slots);
(cache,args_1,newslots,constlist,_,dae2) = elabInputArgs(cache,env, args, nargs, slots, true/*checkTypes*/ ,impl, {}, pre);
c = Util.listReduce(constlist, Types.constAnd);
dae = DAEUtil.joinDaes(dae1,dae2);
Expand Down
13 changes: 11 additions & 2 deletions Compiler/susan_codegen/SimCode/SimCodeC.tpl
Expand Up @@ -4381,6 +4381,16 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/,
let tvar = tempDecl(arr_tp_str, &varDecls /*BUFC*/)
let &preExp += 'identity_alloc_<%arr_tp_str%>(<%var1%>, &<%tvar%>);<%\n%>'
tvar
case CALL(tuple_=false, builtin=true,
path=IDENT(name="String"),
expLst={s, minlen, leftjust}) then
let tvar = tempDecl("modelica_string", &varDecls /*BUFC*/)
let sExp = daeExp(s, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
let minlenExp = daeExp(minlen, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
let leftjustExp = daeExp(leftjust, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
let typeStr = expTypeFromExpModelica(s)
let &preExp += '<%typeStr%>_to_modelica_string(&<%tvar%>, <%sExp%>, <%minlenExp%>, <%leftjustExp%>);<%\n%>'
tvar
case CALL(tuple_=false, builtin=true,
path=IDENT(name="String"),
expLst={s, minlen, leftjust, signdig}) then
Expand All @@ -4389,8 +4399,7 @@ template daeExpCall(Exp call, Context context, Text &preExp /*BUFP*/,
let minlenExp = daeExp(minlen, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
let leftjustExp = daeExp(leftjust, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
let signdigExp = daeExp(signdig, context, &preExp /*BUFC*/, &varDecls /*BUFC*/)
let typeStr = expTypeFromExpModelica(s)
let &preExp += '<%typeStr%>_to_modelica_string(&<%tvar%>, <%sExp%>, <%minlenExp%>, <%leftjustExp%>, <%signdigExp%>);<%\n%>'
let &preExp += 'modelica_real_to_modelica_string(&<%tvar%>, <%sExp%>, <%minlenExp%>, <%leftjustExp%>, <%signdigExp%>);<%\n%>'
tvar
case CALL(tuple_=false, builtin=true,
path=IDENT(name="delay"),
Expand Down
6 changes: 3 additions & 3 deletions c_runtime/modelica_string.c
Expand Up @@ -49,7 +49,7 @@ int modelica_string_length(modelica_string_const a)

/* Convert a modelica_integer to a modelica_string, used in String(i) */

void modelica_integer_to_modelica_string(modelica_string_t* dest,modelica_integer i, modelica_integer minLen,modelica_boolean leftJustified,modelica_integer signDigits)
void modelica_integer_to_modelica_string(modelica_string_t* dest,modelica_integer i, modelica_integer minLen,modelica_boolean leftJustified)
{
char formatStr[40];
char buf[400];
Expand Down Expand Up @@ -81,7 +81,7 @@ void modelica_real_to_modelica_string(modelica_string_t* dest,modelica_real r,mo

/* Convert a modelica_boolean to a modelica_string, used in String(b) */

void modelica_boolean_to_modelica_string(modelica_string_t* dest,modelica_boolean b, modelica_integer minLen, modelica_boolean leftJustified, modelica_integer signDigits)
void modelica_boolean_to_modelica_string(modelica_string_t* dest,modelica_boolean b, modelica_integer minLen, modelica_boolean leftJustified)
{
if (b) {
init_modelica_string(dest,"true");
Expand All @@ -92,7 +92,7 @@ void modelica_boolean_to_modelica_string(modelica_string_t* dest,modelica_boolea

/* Convert a modelica_enumeration to a modelica_string, used in String(b) */

void modelica_enumeration_to_modelica_string(modelica_string_t* dest,modelica_integer nr,modelica_string_t e[],modelica_integer minLen, modelica_boolean leftJustified, modelica_integer signDigits)
void modelica_enumeration_to_modelica_string(modelica_string_t* dest,modelica_integer nr,modelica_string_t e[],modelica_integer minLen, modelica_boolean leftJustified)
{
int i;
int length = strlen(e[nr-1]);
Expand Down
6 changes: 3 additions & 3 deletions c_runtime/modelica_string.h
Expand Up @@ -51,13 +51,13 @@ void modelica_real_to_modelica_string(modelica_string_t* dest,modelica_real r,mo
modelica_boolean leftJustified,modelica_integer signDigits);

void modelica_integer_to_modelica_string(modelica_string_t* dest,modelica_integer i,
modelica_integer minLen,modelica_boolean leftJustified,modelica_integer signDigits);
modelica_integer minLen,modelica_boolean leftJustified);

void modelica_boolean_to_modelica_string(modelica_string_t* dest,modelica_boolean b,
modelica_integer minLen, modelica_boolean leftJustified, modelica_integer signDigits);
modelica_integer minLen, modelica_boolean leftJustified);

void modelica_enumeration_to_modelica_string(modelica_string_t* dest,modelica_integer nr, modelica_string_t e[],
modelica_integer minLen, modelica_boolean leftJustified, modelica_integer signDigits);
modelica_integer minLen, modelica_boolean leftJustified);


/* Frees memory*/
Expand Down

0 comments on commit 78247fe

Please sign in to comment.