Skip to content

Commit

Permalink
- Added ceval support for String(real,significantDigits=xxx)
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@11081 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
sjoelund committed Feb 10, 2012
1 parent 796ae91 commit 6440acd
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 7 deletions.
15 changes: 9 additions & 6 deletions Compiler/FrontEnd/Ceval.mo
Expand Up @@ -2418,13 +2418,13 @@ algorithm
matchcontinue (inCache,inEnv,inExpExpLst,inBoolean,inInteractiveInteractiveSymbolTableOption,inMsg)
local
list<Env.Frame> env;
DAE.Exp exp, len_exp, justified_exp;
DAE.Exp exp, len_exp, justified_exp, sig_dig;
Boolean impl;
Option<Interactive.SymbolTable> st;
Msg msg;
Env.Cache cache;
String str;
Integer i; Real r; Boolean b;
String str,format;
Integer i,len,sig; Real r; Boolean b, left_just;
Absyn.Path p;

case (cache,env,{exp, len_exp, justified_exp},impl,st,msg)
Expand All @@ -2435,11 +2435,14 @@ algorithm
then
(cache,Values.STRING(str),st);

case (cache,env,{exp, len_exp, justified_exp, _},impl,st,msg)
case (cache,env,{exp, len_exp, justified_exp, sig_dig},impl,st,msg)
equation
(cache,Values.REAL(r),_) = ceval(cache,env, exp, impl, st,msg);
str = realString(r);
(cache, str) = cevalBuiltinStringFormat(cache, env, str, len_exp, justified_exp, impl, st, msg);
(cache,Values.INTEGER(len),_) = ceval(cache,env, len_exp, impl, st,msg);
(cache,Values.BOOL(left_just),_) = ceval(cache,env, justified_exp, impl, st,msg);
(cache,Values.INTEGER(sig),_) = ceval(cache,env, sig_dig, impl, st,msg);
format = "%"+&Util.if_(left_just,"-","") +& intString(len) +& "." +& intString(sig) +& "g";
str = System.snprintff(format,len+20,r);
then
(cache,Values.STRING(str),st);

Expand Down
2 changes: 1 addition & 1 deletion Compiler/Script/CevalScript.mo
Expand Up @@ -117,7 +117,7 @@ public constant Integer RT_CLOCK_SIMCODE = 15;
public constant Integer RT_CLOCK_LINEARIZE = 16;
public constant Integer RT_CLOCK_TEMPLATES = 17;
public constant Integer RT_CLOCK_UNCERTAINTIES = 18;
public constant list<Integer> buildModelClocks = {RT_CLOCK_SIMULATE_TOTAL,RT_CLOCK_BUILD_MODEL,RT_CLOCK_TEMPLATES,RT_CLOCK_LINEARIZE,RT_CLOCK_SIMCODE,RT_CLOCK_BACKEND,RT_CLOCK_FRONTEND};
public constant list<Integer> buildModelClocks = {RT_CLOCK_BUILD_MODEL,RT_CLOCK_SIMULATE_TOTAL,RT_CLOCK_TEMPLATES,RT_CLOCK_LINEARIZE,RT_CLOCK_SIMCODE,RT_CLOCK_BACKEND,RT_CLOCK_FRONTEND};

protected constant DAE.Type simulationResultType_rtest = DAE.T_COMPLEX(ClassInf.RECORD(Absyn.IDENT("SimulationResult")),{
DAE.TYPES_VAR("resultFile",DAE.ATTR(SCode.NOT_FLOW(),SCode.NOT_STREAM(),SCode.VAR(),Absyn.BIDIR(),Absyn.NOT_INNER_OUTER()),SCode.PUBLIC(),DAE.T_STRING_DEFAULT,DAE.UNBOUND(),NONE()),
Expand Down
8 changes: 8 additions & 0 deletions Compiler/Util/System.mo
Expand Up @@ -883,4 +883,12 @@ function iconv "The iconv() function converts one multibyte characters from one
external "C" result=SystemImpl__iconv(string,from,to) annotation(Library = {"omcruntime"});
end iconv;

function snprintff "sprintf format string that takes one double as argument"
input String format;
input Integer maxlen;
input Real val;
output String str;
external "C" str=System_snprintff(format,maxlen,val) annotation(Library = {"omcruntime"});
end snprintff;

end System;
8 changes: 8 additions & 0 deletions Compiler/runtime/System_omc.cpp
Expand Up @@ -663,4 +663,12 @@ extern const char* System_getMakeCommand()
return DEFAULT_MAKE;
}

extern const char* System_snprintff(const char *fmt, int len, double d)
{
static char buf[1024];
assert(1024>len);
snprintf(buf,len,fmt,d);
buf[1024] = 0;
}

}
13 changes: 13 additions & 0 deletions Compiler/runtime/System_rml.c
Expand Up @@ -2042,3 +2042,16 @@ RML_BEGIN_LABEL(System__iconv)
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(System__snprintff)
{
const char *fmt = RML_STRINGDATA(rmlA0);
long len = RML_UNTAGFIXNUM(rmlA1);
double d = rml_prim_get_real(rmlA2);
char buf[len];
snprintf(buf,len,fmt,d);
buf[len-1] = 0;
rmlA0 = mk_scon(buf);
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

0 comments on commit 6440acd

Please sign in to comment.