Skip to content

Commit

Permalink
Added two new builtin functions. stringCmp compares two strings and r…
Browse files Browse the repository at this point in the history
…eturns 0 if unequal. getTag fetches the type name of an ojbect reference, using the c++ typeid(ref).name().

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@2808 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Kristian Stavåker committed May 30, 2007
1 parent 3f087a3 commit 510421b
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Compiler/Builtin.mo
Expand Up @@ -176,6 +176,10 @@ constant tuple<Types.TType, Option<Type_a>> nil2real=(Types.T_FUNCTION({},(Types

protected constant tuple<Types.TType, Option<Type_a>> nil2bool=(Types.T_FUNCTION({},(Types.T_REAL({}),NONE)),NONE);

protected constant tuple<Types.TType, Option<Type_a>> record2str=(Types.T_FUNCTION({("x",(Types.T_COMPLEX(ClassInf.UNKNOWN(""),{},NONE()),NONE))},(Types.T_STRING({}),NONE)),NONE);

protected constant tuple<Types.TType, Option<Type_a>> strStr2bool=(Types.T_FUNCTION({("x",(Types.T_STRING({}),NONE)),("y",(Types.T_STRING({}),NONE))},(Types.T_BOOL({}),NONE)),NONE);

protected constant tuple<Types.TType, Option<Type_a>> real2string=(
Types.T_FUNCTION({("x",(Types.T_REAL({}),NONE))},(Types.T_STRING({}),NONE)),NONE);

Expand Down Expand Up @@ -2547,6 +2551,8 @@ algorithm
env = Env.extendFrameC(env, booleanType);
env = Env.extendFrameC(env, stateSelectType);
env = Env.extendFrameV(env, timeVar, NONE, Env.VAR_UNTYPED(), {});
env = Env.extendFrameT(env, "getTag", record2str);
env = Env.extendFrameT(env, "stringCmp", strStr2bool);
env = Env.extendFrameT(env, "initial", nil2real) "non-functions" ;
env = Env.extendFrameT(env, "terminal", nil2real);
env = Env.extendFrameT(env, "event", bool2bool);
Expand Down
71 changes: 71 additions & 0 deletions Compiler/Ceval.mo
Expand Up @@ -233,6 +233,8 @@ algorithm
case "identity" then cevalBuiltinIdentity;
case "promote" then cevalBuiltinPromote;
case "String" then cevalBuiltinString;
case "stringCmp" then cevalBuiltinStringCmp;
case "getTag" then cevalBuiltinGetTag;
case id
equation
Debug.fprint("ceval", "No Ceval.cevalBuiltinHandler found for: ");
Expand Down Expand Up @@ -4044,6 +4046,75 @@ algorithm
end matchcontinue;
end cevalBuiltinString;

protected function cevalBuiltinStringCmp "
author: KS
Evaluates the comparsion between two strings"
input Env.Cache inCache;
input Env.Env inEnv;
input list<Exp.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;
Exp.Exp exp1,exp2;
Boolean impl;
Option<Interactive.InteractiveSymbolTable> st;
Msg msg;
Env.Cache cache;
String s1,s2;
Integer i; Real r; Boolean b;
case (cache,env,{exp1,exp2},impl,st,msg)
equation
(cache,Values.STRING(s1),_) = ceval(cache,env, exp1, impl, st, NONE, msg);
(cache,Values.STRING(s2),_) = ceval(cache,env, exp2, impl, st, NONE, msg);
b = stringEqual(s1,s2);
then
(cache,Values.BOOL(b),st);
end matchcontinue;
end cevalBuiltinStringCmp;

protected function cevalBuiltinGetTag "
author: KS
Evaluates the getTag function. Never used?"
input Env.Cache inCache;
input Env.Env inEnv;
input list<Exp.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;
Exp.Exp exp1,exp2;
Boolean impl;
Option<Interactive.InteractiveSymbolTable> st;
Msg msg;
Env.Cache cache;
String s1,s2;
Integer i; Real r; Boolean b;
case (cache,env,{_},impl,st,msg)
equation
then
(cache,Values.STRING("REC"),st);
end matchcontinue;
end cevalBuiltinGetTag;

protected function cevalCat "function: cevalCat
evaluates the cat operator given a list of
array values and a concatenation dimension."
Expand Down
26 changes: 26 additions & 0 deletions Compiler/Codegen.mo
Expand Up @@ -3917,6 +3917,32 @@ algorithm
cfn = cMergeFns({cfn1,cfn2,cfn3,cfn4,cfn});
then
(cfn,tvar,tnr1);

case (Exp.CALL(path = Absyn.IDENT(name = "stringCmp"),expLst = {s1,s2},tuple_ = false,builtin = true),tnr,context) /* max(v), v is vector */
local String cref_str; Exp.Exp s,minlen,leftjust,signdig; Boolean needCast; String var1,var2,var3,var4;
CFunction cfn3,cfn4;
equation
(tdecl,tvar,tnr1) = generateTempDecl("modelica_integer", tnr);
(cfn1,var1,tnr1) = generateExpression(s1, tnr1, context);
(cfn2,var2,tnr1) = generateExpression(s2, tnr1, context);
cfn = cAddVariables(cEmptyFunction, {tdecl});
stmt = Util.stringAppendList({tvar," = strcmp(",var1,",",var2,");"});
cfn = cAddStatements(cfn, {stmt});
cfn = cMergeFns({cfn1,cfn2,cfn});
then
(cfn,tvar,tnr1);

case (Exp.CALL(path = Absyn.IDENT(name = "getTag"),expLst = {s1},tuple_ = false,builtin = true),tnr,context) /* max(v), v is vector */
local String cref_str; Exp.Exp s,minlen,leftjust,signdig; Boolean needCast; String var1,var2,var3,var4;
equation
(tdecl,tvar,tnr1) = generateTempDecl("modelica_string", tnr);
(cfn1,var1,tnr1) = generateExpression(s1, tnr1, context);
cfn = cAddVariables(cEmptyFunction, {tdecl});
stmt = Util.stringAppendList({tvar," = (modelica_string)typeid(",var1,").name();"});
cfn = cAddStatements(cfn, {stmt});
cfn = cMergeFns({cfn1,cfn});
then
(cfn,tvar,tnr1);

end matchcontinue;
end generateBuiltinFunction;
Expand Down

0 comments on commit 510421b

Please sign in to comment.