Skip to content

Commit

Permalink
Fix for bug #1598
Browse files Browse the repository at this point in the history
- Implemented elaboration of matrix function.


git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@9721 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
perost committed Aug 31, 2011
1 parent 5154451 commit 956eb40
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 0 deletions.
157 changes: 157 additions & 0 deletions Compiler/FrontEnd/Static.mo
Expand Up @@ -6066,6 +6066,162 @@ algorithm
end matchcontinue;
end elabBuiltinVector2;

public function elabBuiltinMatrix
"Elaborates the builtin matrix function."
input Env.Cache inCache;
input Env.Env inEnv;
input list<Absyn.Exp> inArgs;
input list<Absyn.NamedArg> inNamedArgs;
input Boolean inImpl;
input Prefix.Prefix inPrefix;
input Absyn.Info inInfo;
output Env.Cache outCache;
output DAE.Exp outExp;
output DAE.Properties outProperties;
algorithm
(outCache, outExp, outProperties) :=
match(inCache, inEnv, inArgs, inNamedArgs, inImpl, inPrefix, inInfo)
local
Absyn.Exp arg;
Env.Cache cache;
DAE.Exp exp;
DAE.Properties props;
DAE.Type ty;

case (_, _, {arg}, _, _, _, _)
equation
(cache, exp, props, _) =
elabExp(inCache, inEnv, arg, inImpl, NONE(), true, inPrefix, inInfo);
ty = Types.getPropType(props);
(exp, props) = elabBuiltinMatrix2(inCache, inEnv, exp, props, ty, inInfo);
then
(cache, exp, props);

else
equation
Error.addSourceMessage(Error.WRONG_NO_OF_ARGS, {"matrix"}, inInfo);
then
fail();

end match;
end elabBuiltinMatrix;

protected function elabBuiltinMatrix2
"Helper function to elabBuiltinMatrix, evaluates the matrix function given the
elaborated argument."
input Env.Cache inCache;
input Env.Env inEnv;
input DAE.Exp inArg;
input DAE.Properties inProperties;
input DAE.Type inType;
input Absyn.Info inInfo;
output DAE.Exp outExp;
output DAE.Properties outProperties;
algorithm
(outExp, outProperties) :=
matchcontinue(inCache, inEnv, inArg, inProperties, inType, inInfo)
local
DAE.Type ty;
DAE.Exp exp;
DAE.Properties props;
DAE.ExpType etp;
list<DAE.Exp> expl;
DAE.ExpType ety;
DAE.Dimension dim1, dim2;
Boolean scalar;

// Scalar
case (_, _, _, _, _, _)
equation
Types.simpleType(inType);
(exp, props) = promoteExp(inArg, inProperties, 2);
then
(exp, props);

// 1-dimensional array
case (_, _, DAE.ARRAY(ty = _), _, _, _)
equation
1 = Types.numberOfDimensions(inType);
(exp, props) = promoteExp(inArg, inProperties, 1);
then
(exp, props);

// Matrix
case (_, _, DAE.MATRIX(ty = _), _, _, _)
then (inArg, inProperties);

// n-dimensional array
case (_, _, DAE.ARRAY(ty = DAE.ET_ARRAY(ety, dim1 :: dim2 :: _),
scalar = scalar, array = expl), _, _, _)
equation
expl = Util.listMap1(expl, elabBuiltinMatrix3, inInfo);
ty = Types.arrayElementType(inType);
ty = Types.liftArrayListDims(ty, {dim1, dim2});
props = Types.setTypeInProps(ty, inProperties);
then
(DAE.ARRAY(DAE.ET_ARRAY(ety, {dim1, dim2}), scalar, expl), props);

end matchcontinue;
end elabBuiltinMatrix2;

protected function elabBuiltinMatrix3
"Helper function to elabBuiltinMatrix2."
input DAE.Exp inExp;
input Absyn.Info inInfo;
output DAE.Exp outExp;
algorithm
outExp := match(inExp, inInfo)
local
DAE.ExpType ety;
Boolean scalar;
list<DAE.Exp> expl;
DAE.Dimension dim;

case (DAE.ARRAY(ty = DAE.ET_ARRAY(ety, dim :: _),
scalar = scalar, array = expl), _)
equation
expl = Util.listMap3(expl, arrayScalar, 3, "matrix", inInfo);
then
DAE.ARRAY(DAE.ET_ARRAY(ety, {dim}), scalar, expl);

end match;
end elabBuiltinMatrix3;

protected function arrayScalar
"Returns the scalar value of an array, or prints an error message and fails if
any dimension of the array isn't of size 1."
input DAE.Exp inExp;
input Integer inDim "The current dimension, used for error message.";
input String inOperator "The current operator name, used for error message.";
input Absyn.Info inInfo;
output DAE.Exp outExp;
algorithm
outExp := match(inExp, inDim, inOperator, inInfo)
local
DAE.Exp exp;
list<DAE.Exp> expl;
String dim_str;
String size_str;

// An array with one element.
case (DAE.ARRAY(array = {exp}), _, _, _)
then arrayScalar(exp, inDim + 1, inOperator, inInfo);

// Any other array.
case (DAE.ARRAY(array = expl), _, _, _)
equation
dim_str = intString(inDim);
size_str = intString(listLength(expl));
Error.addSourceMessage(Error.INVALID_ARRAY_DIM_IN_CONVERSION_OP,
{dim_str, inOperator, "1", size_str}, inInfo);
then
fail();

// Anything else is assumed to be a scalar.
else inExp;
end match;
end arrayScalar;

public function elabBuiltinHandlerGeneric "function: elabBuiltinHandlerGeneric

This function dispatches the elaboration of special builtin operators by
Expand Down Expand Up @@ -6146,6 +6302,7 @@ algorithm
case "cat" then elabBuiltinCat;
case "identity" then elabBuiltinIdentity;
case "vector" then elabBuiltinVector;
case "matrix" then elabBuiltinMatrix;
case "scalar" then elabBuiltinScalar;
case "cross" then elabBuiltinCross;
case "skew" then elabBuiltinSkew;
Expand Down
3 changes: 3 additions & 0 deletions Compiler/Util/Error.mo
Expand Up @@ -304,6 +304,7 @@ public constant ErrorID FUNCTION_UNUSED_INPUT=192;
public constant ErrorID ARRAY_TYPE_MISMATCH=193;
public constant ErrorID VECTORIZE_TWO_UNKNOWN=194;
public constant ErrorID FUNCTION_SLOT_VARIABILITY=195;
public constant ErrorID INVALID_ARRAY_DIM_IN_CONVERSION_OP=196;

public constant ErrorID UNBOUND_PARAMETER_WITH_START_VALUE_WARNING=499;
public constant ErrorID UNBOUND_PARAMETER_WARNING=500;
Expand Down Expand Up @@ -518,6 +519,8 @@ protected constant list<tuple<Integer, MessageType, Severity, String>> errorTabl
(INVALID_ARGUMENT_TYPE,TRANSLATION(),ERROR(),"Argument %s of %s must be %s"),
(INVALID_SIZE_INDEX,TRANSLATION(),ERROR(),"Invalid index %s in call to size of %s, valid index interval is [1,%s]."),
(FUNCTION_SLOT_VARIABILITY,TRANSLATION(),ERROR(),"Function argument %s is not a %sexpression"),
(INVALID_ARRAY_DIM_IN_CONVERSION_OP,TRANSLATION(),ERROR(),
"Invalid dimension %s of argument to %s, expected dimension size %s but got %s."),

/*
(CONNECT_STREAM_TO_NONSTREAM,TRANSLATION(),ERROR(),
Expand Down

0 comments on commit 956eb40

Please sign in to comment.