Skip to content

Commit

Permalink
- constant evaluate Modelica.Utilities.Strings.substring
Browse files Browse the repository at this point in the history
- update tests due to changes of SimulationResult record

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@7217 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
adrpo committed Nov 27, 2010
1 parent f45d17e commit 85f4086
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 5 deletions.
24 changes: 24 additions & 0 deletions Compiler/Builtin.mo
Expand Up @@ -267,6 +267,18 @@ protected constant DAE.Type int2string =(
protected constant DAE.Type bool2string =(
DAE.T_FUNCTION({("x",DAE.T_BOOL_DEFAULT)},DAE.T_STRING_DEFAULT,DAE.NO_INLINE()),NONE());

protected constant DAE.Type stringIntInt2string=(
DAE.T_FUNCTION(
{
("x",DAE.T_STRING_DEFAULT),
("y",DAE.T_INTEGER_DEFAULT),
("z",DAE.T_INTEGER_DEFAULT)
},
DAE.T_STRING_DEFAULT,
DAE.NO_INLINE()),
NONE());


/* type for builtin operator der has unit type parameter to be able to express that derivative of expression
means an addition of 1/s on the unit dimension */
protected constant DAE.Type derType=(
Expand Down Expand Up @@ -2203,6 +2215,17 @@ algorithm
end matchcontinue;
end isATan2;

public function isSubstring
input Absyn.Path inPath;
algorithm
_:=
matchcontinue (inPath)
case (Absyn.IDENT(name = "substring")) then ();
case (Absyn.QUALIFIED(name = "Modelica",path = Absyn.QUALIFIED(name = "Utilities", path = Absyn.QUALIFIED(name = "Strings",path = Absyn.IDENT(name = "substring"))))) then ();
case (Absyn.FULLYQUALIFIED(inPath)) equation isSubstring(inPath); then ();
end matchcontinue;
end isSubstring;

public function isSinh
input Absyn.Path inPath;
algorithm
Expand Down Expand Up @@ -2489,6 +2512,7 @@ algorithm
env = Env.extendFrameT(env, "acos", real2real);
env = Env.extendFrameT(env, "atan", real2real);
env = Env.extendFrameT(env, "atan2", realReal2real);
env = Env.extendFrameT(env, "substring", stringIntInt2string);
env = Env.extendFrameT(env, "exp", dimesionlessReal2DimensionlessReal);
env = Env.extendFrameT(env, "log", dimesionlessReal2DimensionlessReal);
env = Env.extendFrameT(env, "ln", dimesionlessReal2DimensionlessReal);
Expand Down
63 changes: 60 additions & 3 deletions Compiler/Ceval.mo
Expand Up @@ -1049,7 +1049,7 @@ algorithm
case "log10" then cevalBuiltinLog10;
case "arcsin" then cevalBuiltinAsin;
case "arccos" then cevalBuiltinAcos;
case "arctan" then cevalBuiltinAtan;
case "arctan" then cevalBuiltinAtan;
case "integer" then cevalBuiltinInteger;
case "boolean" then cevalBuiltinBoolean;
case "mod" then cevalBuiltinMod;
Expand All @@ -1073,7 +1073,8 @@ algorithm
case "rooted" then cevalBuiltinRooted; //
case "cross" then cevalBuiltinCross;
case "fill" then cevalBuiltinFill;
case "print" equation true = RTOpts.acceptMetaModelicaGrammar(); then cevalBuiltinPrint;
case "Modelica.Utilities.Strings.substring" then cevalBuiltinSubstring;
case "print" equation true = RTOpts.acceptMetaModelicaGrammar(); then cevalBuiltinPrint;
// MetaModelica type conversions
case "intReal" equation true = RTOpts.acceptMetaModelicaGrammar(); then cevalIntReal;
case "intString" equation true = RTOpts.acceptMetaModelicaGrammar(); then cevalIntString;
Expand Down Expand Up @@ -1423,6 +1424,7 @@ algorithm
case ("sinh",SOME("sinh")) then ();
case ("tan",SOME("tan")) then ();
case ("tanh",SOME("tanh")) then ();
case ("substring",SOME("substring")) then ();
end matchcontinue;
end isKnownExternalFunc;

Expand All @@ -1436,7 +1438,11 @@ protected function cevalKnownExternalFuncs2 "function: cevalKnownExternalFuncs2
output Values.Value outValue;
algorithm
outValue := matchcontinue (inIdent,inAbsynIdentOption,inValuesValueLst,inMsg)
local Real rv_1,rv,rv1,rv2,sv,cv;
local
Real rv_1,rv,rv1,rv2,sv,cv;
String str;
Integer start, stop;

case ("acos",SOME("acos"),{Values.REAL(real = rv)},_)
equation
rv_1 = realAcos(rv);
Expand Down Expand Up @@ -1504,6 +1510,17 @@ algorithm
rv_1 = realTanh(rv);
then
Values.REAL(rv_1);

case ("substring",SOME("substring"),
{
Values.STRING(string = str),
Values.INTEGER(integer = start),
Values.INTEGER(integer = stop)
},_)
equation
str = System.substring(str, start, stop);
then
Values.STRING(str);
end matchcontinue;
end cevalKnownExternalFuncs2;

Expand Down Expand Up @@ -2240,6 +2257,46 @@ algorithm
end matchcontinue;
end cevalBuiltinPromote2;

protected function cevalBuiltinSubstring "
author: PA
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;
input Boolean inBoolean;
input Option<Interactive.InteractiveSymbolTable> inInteractiveInteractiveSymbolTableOption;
input Msg inMsg;
output Env.Cache outCache;
output Values.Value outValue;
output Option<Interactive.InteractiveSymbolTable> outInteractiveInteractiveSymbolTableOption;
algorithm
(outCache,outValue,outInteractiveInteractiveSymbolTableOption):=
matchcontinue (inCache,inEnv,inExpExpLst,inBoolean,inInteractiveInteractiveSymbolTableOption,inMsg)
local
Values.Value arr_val,res;
Integer dim_val;
list<Env.Frame> env;
DAE.Exp str_exp, start_exp, stop_exp;
Boolean impl;
Option<Interactive.InteractiveSymbolTable> st;
Msg msg;
Env.Cache cache;
String str;
Integer start, stop;
Absyn.Path p;

case (cache,env,{str_exp, start_exp, stop_exp},impl,st,msg)
equation
(cache,Values.STRING(str),_) = ceval(cache,env, str_exp, impl, st, NONE(), msg);
(cache,Values.INTEGER(start),_) = ceval(cache,env, start_exp, impl, st, NONE(), msg);
(cache,Values.INTEGER(stop),_) = ceval(cache,env, stop_exp, impl, st, NONE(), msg);
str = System.substring(str, start, stop);
then
(cache,Values.STRING(str),st);
end matchcontinue;
end cevalBuiltinSubstring;

protected function cevalBuiltinString "
author: PA
Evaluates the String operator String(r), String(i), String(b), String(e).
Expand Down
4 changes: 2 additions & 2 deletions Compiler/SCode.mo
Expand Up @@ -2813,7 +2813,7 @@ public function traverseEEquationExps
output tuple<Absyn.Exp, Argument> outTuple;
end TraverseFunc;
algorithm
outTuple := match(inEEquation, inTuple)
(outEEquation, outTuple) := match(inEEquation, inTuple)
local
TraverseFunc traverser;
Argument arg;
Expand Down Expand Up @@ -2862,7 +2862,7 @@ public function traverseStatementExps
output tuple<Absyn.Exp, Argument> outTuple;
end TraverseFunc;
algorithm
outTuple := match(inStatement, inTuple)
(outStatement, outTuple) := match(inStatement, inTuple)
local
TraverseFunc traverser;
Argument arg;
Expand Down
9 changes: 9 additions & 0 deletions Compiler/System.mo
Expand Up @@ -136,6 +136,15 @@ public function strtok
external "C" outStringLst=System_strtok(inString1,inString2) annotation(Library = "omcruntime");
end strtok;

public function substring
input String inString;
input Integer start;
input Integer stop;
output String outString;

external "C" outString = System_substring(inString,start,stop) annotation(Library = "omcruntime");
end substring;

public function setCCompiler
input String inString;

Expand Down
39 changes: 39 additions & 0 deletions Compiler/runtime/System_omc.cpp
Expand Up @@ -227,6 +227,45 @@ extern void* System_strtok(const char *str0, const char *delimit)
return listReverse(res);
}

extern void* System_substring(const char *inStr, int start, int stop)
{
char* substring = NULL;
char* str = strdup(inStr);
int startIndex = start;
int stopIndex = stop;
int len1 = strlen(str);
int len2 = 0;
void *res = NULL;

/* Check arguments */
if ( startIndex < 1 )
{
free(str);
MMC_THROW();
}
if ( stopIndex == -999 )
{
stopIndex = startIndex;
} else if ( stopIndex < startIndex ) {
free(str);
MMC_THROW();
} else if ( stopIndex > len1 ) {
free(str);
MMC_THROW();
}

/* Allocate memory and copy string */
len2 = stopIndex - startIndex + 1;
substring = (char*)malloc(len2);
strncpy(substring, &str[startIndex-1], len2);
substring[len2] = '\0';

res = mmc_mk_scon(substring);

free(substring);
return res;
}

const char* System_getClassnamesForSimulation()
{
if(class_names_for_simulation)
Expand Down
36 changes: 36 additions & 0 deletions Compiler/runtime/System_rml.c
Expand Up @@ -125,6 +125,42 @@ RML_BEGIN_LABEL(System__strtok)
}
RML_END_LABEL

RML_BEGIN_LABEL(System__substring)
{
char* substring = NULL;
char* str = RML_STRINGDATA(rmlA0);
int startIndex = RML_UNTAGFIXNUM(rmlA1);
int stopIndex = RML_UNTAGFIXNUM(rmlA2);
int len1 = strlen(str);
int len2 = 0;

/* Check arguments */
if ( startIndex < 1 )
{
RML_TAILCALLK(rmlFC);
}
if ( stopIndex == -999 )
{
stopIndex = startIndex;
} else if ( stopIndex< startIndex ) {
RML_TAILCALLK(rmlFC);
} else if ( stopIndex > len1 ) {
RML_TAILCALLK(rmlFC);
}

/* Allocate memory and copy string */
len2 = stopIndex - startIndex + 1;
substring = (char*)malloc(len2);
strncpy(substring, &str[startIndex-1], len2);
substring[len2] = '\0';

rmlA0 = mk_scon(substring);

free(substring);
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(System__toupper)
{
char *base = RML_STRINGDATA(rmlA0);
Expand Down

0 comments on commit 85f4086

Please sign in to comment.