Skip to content

Commit

Permalink
Implemented the builtin function: product. Works on a testcase with v…
Browse files Browse the repository at this point in the history
…ectors. Has not yet been tested on matrices.

git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@2677 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
petfr committed Jan 31, 2007
1 parent 76ccbca commit abd5bd5
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
5 changes: 4 additions & 1 deletion Compiler/Error.mo
Expand Up @@ -49,7 +49,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
"

public
public
uniontype Severity "severity of message"
record ERROR "Error when tool can not succed in translation" end ERROR;

Expand Down Expand Up @@ -190,6 +190,7 @@ public constant ErrorID RECURSIVE_DEFINITION=92;
public constant ErrorID NOT_ARRAY_TYPE_IN_FOR_STATEMENT= 93;
public constant ErrorID UNBOUND_PARAMETER_WARNING=500;
public constant ErrorID BUILTIN_FUNCTION_SUM_HAS_SCALAR_PARAMETER=501;
public constant ErrorID BUILTIN_FUNCTION_PRODUCT_HAS_SCALAR_PARAMETER=502;
public constant ErrorID INDEX_REDUCTION_NOTIFICATION=1000;
protected constant list<tuple<Integer, MessageType, Severity, String>> errorTable={(SYNTAX_ERROR,SYNTAX(),ERROR(),"Syntax error near: %s"),
(GRAMMATIC_ERROR,GRAMMAR(),ERROR(),"error: %s"),
Expand Down Expand Up @@ -375,6 +376,8 @@ protected constant list<tuple<Integer, MessageType, Severity, String>> errorTabl
"Warning, parameter %s has no value"),
(BUILTIN_FUNCTION_SUM_HAS_SCALAR_PARAMETER,TRANSLATION(),WARNING(),
"Warning, function \"sum\" has scalar as argument in sum(%s)"),
(BUILTIN_FUNCTION_PRODUCT_HAS_SCALAR_PARAMETER,TRANSLATION(),WARNING(),
"Warning, function \"product\" has scalar as argument in sum(%s)"),
(INDEX_REDUCTION_NOTIFICATION,SYMBOLIC(),NOTIFICATION(),
"Notification, differentiated equation %s to %s for index reduction")};

Expand Down
85 changes: 83 additions & 2 deletions Compiler/Static.mo
Expand Up @@ -2364,15 +2364,15 @@ algorithm
equation
(cache,exp_1,Types.PROP((Types.T_INTEGER({}),_),c),_) = elabExp(cache,env, arrexp, impl, NONE,true);
str = Dump.printExpStr(arrexp);
Error.addMessage(Error.BUILTIN_FUNCTION_SUM_HAS_SCALAR_PARAMETER, {str});
Error.addMessage(Error.BUILTIN_FUNCTION_PRODUCT_HAS_SCALAR_PARAMETER, {str});
then
(cache,exp_1,Types.PROP((Types.T_INTEGER({}),NONE),c));
case (cache,env,{arrexp},impl) /* impl */
local String str;
equation
(cache,exp_1,Types.PROP((Types.T_REAL({}),_),c),_) = elabExp(cache,env, arrexp, impl, NONE,true);
str = Dump.printExpStr(arrexp);
Error.addMessage(Error.BUILTIN_FUNCTION_SUM_HAS_SCALAR_PARAMETER, {str});
Error.addMessage(Error.BUILTIN_FUNCTION_PRODUCT_HAS_SCALAR_PARAMETER, {str});
then
(cache,exp_1,Types.PROP((Types.T_REAL({}),NONE),c));
case (cache,env,{arrexp},impl) /* impl */
Expand Down Expand Up @@ -2413,6 +2413,85 @@ algorithm
end matchcontinue;
end elabBuiltinSum2;

protected function elabBuiltinProduct "function: elabBuiltinProduct
This function elaborates the builtin operator product.
The input is the arguments to fill as Absyn.Exp expressions and the environment Env.Env
"
input Env.Cache inCache;
input Env.Env inEnv;
input list<Absyn.Exp> inAbsynExpLst;
input Boolean inBoolean;
output Env.Cache outCache;
output Exp.Exp outExp;
output Types.Properties outProperties;
algorithm
(outCache,outExp,outProperties):=
matchcontinue (inCache,inEnv,inAbsynExpLst,inBoolean)
local
Exp.Exp exp_1,exp_2;
Types.ArrayDim dim;
tuple<Types.TType, Option<Absyn.Path>> tp;
Types.Const c;
list<Env.Frame> env;
Absyn.Exp arrexp;
Boolean impl;
Env.Cache cache;
case (cache,env,{arrexp},impl) /* impl */
local String str;
equation
(cache,exp_1,Types.PROP((Types.T_INTEGER({}),_),c),_) = elabExp(cache,env, arrexp, impl, NONE,true);
str = Dump.printExpStr(arrexp);
Error.addMessage(Error.BUILTIN_FUNCTION_PRODUCT_HAS_SCALAR_PARAMETER, {str});
then
(cache,exp_1,Types.PROP((Types.T_INTEGER({}),NONE),c));
case (cache,env,{arrexp},impl) /* impl */
local String str;
equation
(cache,exp_1,Types.PROP((Types.T_REAL({}),_),c),_) = elabExp(cache,env, arrexp, impl, NONE,true);
str = Dump.printExpStr(arrexp);
Error.addMessage(Error.BUILTIN_FUNCTION_PRODUCT_HAS_SCALAR_PARAMETER, {str});
then
(cache,exp_1,Types.PROP((Types.T_REAL({}),NONE),c));
case (cache,env,{arrexp},impl) /* impl */
local Exp.Type etp; Types.Type t;
equation
(cache,exp_1,Types.PROP(t as (Types.T_ARRAY(dim,tp),_),c),_) = elabExp(cache,env, arrexp, impl, NONE,true);
tp = Types.arrayElementType(t);
etp = Types.elabType(tp);
exp_2 = elabBuiltinProduct2(Exp.CALL(Absyn.IDENT("product"),{exp_1},false,true,etp));
then
(cache,exp_2,Types.PROP(tp,c));
end matchcontinue;
end elabBuiltinProduct;

protected function elabBuiltinProduct2 " replaces product({a1,a2,...an})
with a1*a2*...*an} and
product([a11,a12,...,a1n;...,am1,am2,..amn]) with a11*a12*...*amn
"
input Exp.Exp inExp;
output Exp.Exp outExp;
algorithm
outExp := matchcontinue(inExp)
local
Exp.Type ty;
Boolean sc;
list<Exp.Exp> expl;
Exp.Exp e;
list<list<tuple<Exp.Exp, Boolean>>> mexpl;
Integer dim;
case(Exp.CALL(_,{Exp.ARRAY(ty,sc,expl)},_,_,_)) equation
e = Exp.makeProductLst(expl);
then e;
case(Exp.CALL(_,{Exp.MATRIX(ty,dim,mexpl)},_,_,_)) equation
expl = Util.listMap(Util.listFlatten(mexpl), Util.tuple21);
e = Exp.makeProductLst(expl);
then e;

case (e) then e;
end matchcontinue;
end elabBuiltinProduct2;

protected function elabBuiltinPre "function: elabBuiltinPre
This function elaborates the builtin operator pre.
Expand Down Expand Up @@ -4179,6 +4258,8 @@ algorithm

case "sum" then elabBuiltinSum;

case "product" then elabBuiltinProduct;

case "pre" then elabBuiltinPre;

case "initial" then elabBuiltinInitial;
Expand Down

0 comments on commit abd5bd5

Please sign in to comment.