Skip to content

Commit

Permalink
[iss-195]
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 5399d83
Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com>
Date:   Thu Apr 20 11:41:02 2023 -0300

    Detect bin ops in expression visitors.

commit 7299075
Author: joaquin.f.fernandez <joaquin.f.fernandez@gmail.com>
Date:   Thu Apr 20 11:13:52 2023 -0300

    Fix PWL map visitor constant assignments.
  • Loading branch information
joaquinffernandez committed Apr 20, 2023
1 parent 3927fd4 commit 5c74c62
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
16 changes: 14 additions & 2 deletions src/mmoc/util/ast_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,21 @@ class AST_Expression_Traverse {
template <class R>
class AST_Expression_Visitor {
public:
AST_Expression_Visitor() : _left(nullptr), _right(nullptr), _in_bin_op(false){};
virtual ~AST_Expression_Visitor() = default;
R apply(AST_Expression e)
{
switch (e->expressionType()) {
case EXPBINOP: {
AST_Expression_BinOp b = e->getAsBinOp();
AST_Expression left = b->left(), right = b->right();
return (foldTraverseElement(apply(left), apply(right), b->binopType()));
if (!_in_bin_op) {
_in_bin_op = true;
_left = b->left();
_right = b->right();
}
R ret = foldTraverseElement(apply(b->left()), apply(b->right()), b->binopType());
_in_bin_op = false;
return ret;
}
case EXPUMINUS:
return foldTraverseElementUMinus(e);
Expand All @@ -98,6 +105,11 @@ class AST_Expression_Visitor {
}
};

protected:
AST_Expression _left;
AST_Expression _right;
bool _in_bin_op;

private:
virtual R foldTraverseElement(AST_Expression) = 0;
virtual R foldTraverseElementUMinus(AST_Expression) = 0;
Expand Down
18 changes: 10 additions & 8 deletions src/mmoc/util/visitors/pwl_map_values.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,10 @@ AST_Expression PWLMapValues::foldTraverseElement(AST_Expression exp)
break;
}
case EXPINTEGER:
_constant = exp->getAsInteger()->val();
_slope = 0;
if (!_in_bin_op) {
_constant = exp->getAsInteger()->val();
_slope = 0;
}
break;
default:
return exp;
Expand All @@ -57,7 +59,7 @@ AST_Expression PWLMapValues::foldTraverseElement(AST_Expression exp)
}

AST_Expression PWLMapValues::foldTraverseElement(AST_Expression left, AST_Expression right, BinOpType type)
{
{
EvalInitExp eval_exp;
static const bool EVAL_INT = true;
IsConstantExpression constant_exp(EVAL_INT);
Expand Down Expand Up @@ -101,21 +103,21 @@ void PWLMapValues::assign(AST_Expression left, AST_Expression right, bool cte_le
{
EvalInitExp eval_exp;
if (cte_left && cte_right) {
_constant = sign * (eval_exp.apply(left) + eval_exp.apply(right));
_constant = eval_exp.apply(left) + sign * eval_exp.apply(right);
} else if (cte_right) {
_constant = sign * eval_exp.apply(right);
} else {
_constant = sign * eval_exp.apply(left);
}
}

int PWLMapValues::constant() const { return _constant; }
int PWLMapValues::constant() const { return _constant; }

int PWLMapValues::slope() const { return _slope; }
int PWLMapValues::slope() const { return _slope; }

std::string PWLMapValues::variable() const { return _variable; }
std::string PWLMapValues::variable() const { return _variable; }

bool PWLMapValues::isScalar() const { return _slope == 0;}
bool PWLMapValues::isScalar() const { return _slope == 0; }

} // namespace Util
} // namespace MicroModelica

0 comments on commit 5c74c62

Please sign in to comment.