Skip to content

Commit

Permalink
Update variable interface, add cast method for discrete integer varia…
Browse files Browse the repository at this point in the history
…bles.
  • Loading branch information
joaquinffernandez committed Aug 27, 2024
1 parent 1777b45 commit b982ea0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 35 deletions.
26 changes: 16 additions & 10 deletions src/mmoc/util/symbol_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ namespace Util {

Variable::Variable()
: _unknown(false),
_discrete(false),
_t(nullptr),
_tp(TP_CONSTANT),
_m(nullptr),
Expand All @@ -59,7 +58,6 @@ Variable::Variable()

Variable::Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c)
: _unknown(false),
_discrete(false),
_t(t),
_tp(tp),
_m(m),
Expand All @@ -80,9 +78,8 @@ Variable::Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c)
processModification();
}

Variable::Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c, vector<int> s, bool array)
Variable::Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c, const vector<int> &s, bool array)
: _unknown(false),
_discrete(false),
_t(t),
_tp(tp),
_m(m),
Expand All @@ -105,7 +102,6 @@ Variable::Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c,

Variable &Variable::operator=(const Variable &other)
{
_discrete = other._discrete;
_t = other._t;
_tp = other._tp;
_m = other._m;
Expand Down Expand Up @@ -178,9 +174,8 @@ void Variable::setName(string name) { _name = name; }

unsigned int Variable::size()
{
vector<int>::const_iterator it;
unsigned int total = 1;
for (it = _size.begin(); it != _size.end(); it++) {
for (vector<int>::const_iterator it = _size.begin(); it != _size.end(); it++) {
total *= *it;
}
return total;
Expand Down Expand Up @@ -208,6 +203,17 @@ string Variable::print() const
return buffer.str();
}

bool Variable::isDiscreteInteger() const { return (_t->getType() == SymbolType::TYINTEGER) && isDiscrete(); }

std::string Variable::castOperator() const
{
string cast_operator;
if (isDiscreteInteger()) {
cast_operator = "(int)";
}
return cast_operator;
}

ostream &operator<<(ostream &out, const Variable &v)
{
out << v.print();
Expand All @@ -234,7 +240,7 @@ string Variable::initialization()
{
stringstream buffer;
if (hasAssignment() || hasStartModifier() || hasEachModifier()) {
Range range = Range(*this);
auto range = Range(*this);
Expression ex(exp());
Expression var = Utils::instance().variableExpression(name(), range);
if (hasEachModifier()) {
Expand Down Expand Up @@ -299,7 +305,7 @@ void VarSymbolTable::insert(VarName name, Variable variable)
}
}

Option<Variable> VarSymbolTable::lookup(string name)
Option<Variable> VarSymbolTable::lookup(const string &name) const
{
std::map<string, Variable> table = map();
Option<Variable> var = table[name];
Expand All @@ -309,7 +315,7 @@ Option<Variable> VarSymbolTable::lookup(string name)
return Option<Variable>();
}

unsigned int VarSymbolTable::maxDim() const { return _max_dims; }
unsigned long VarSymbolTable::maxDim() const { return _max_dims; }

} // namespace Util
} // namespace MicroModelica
46 changes: 21 additions & 25 deletions src/mmoc/util/symbol_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ class Variable {
public:
Variable();
Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c);
Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c, vector<int> s, bool array);
Variable(Type t, AST_TypePrefix tp, AST_Modification m, AST_Comment c, const vector<int>& s, bool array);
Variable& operator=(const Variable& other);
bool operator==(const Variable& other);
bool operator!=(const Variable& other);

typedef enum { State, Algebraic, NotAssigned } RealType;

inline void setRealType(RealType type) { _realType = type; };
inline AST_TypePrefix typePrefix() { return _tp; };
inline AST_TypePrefix typePrefix() const { return _tp; };
inline AST_Comment comment() { return _comm; };
inline void setComment(AST_Comment c) { _comm = c; };
inline AST_Modification modification() { return _m; };
Expand All @@ -67,13 +67,8 @@ class Variable {
unsetStartEach();
};
inline bool isParameter() const { return _tp & TP_PARAMETER; };
inline bool isDiscrete() const { return (_tp & TP_DISCRETE) || _discrete; };
inline bool isDiscrete() const { return (_tp & TP_DISCRETE); };
inline bool builtIn() const { return _builtin; };
inline void setDiscrete()
{
_discrete = true;
unsetAssignment();
};
inline void setBuiltIn() { _builtin = true; };
inline bool isConstant() const { return _tp & TP_CONSTANT; };
inline bool isInput() const { return _tp & TP_INPUT; };
Expand All @@ -82,31 +77,31 @@ class Variable {
inline bool isEqType() const { return _tp & TP_EQ; };
inline bool isLocal() const { return _tp & TP_LOCAL; };
inline bool isState() const { return _realType == State; };
inline bool isString() const { return _t->getType() == TYSTRING; };
inline bool isString() const { return _t->getType() == SymbolType::TYSTRING; };
inline void setState() { unsetAssignment(); };
inline bool isUnknown() { return _unknown; };
inline bool isUnknown() const { return _unknown; };
inline void setUnknown() { _unknown = true; };
inline bool isTime() { return _name.compare("time") == 0; };
inline bool isTime() const { return _name.compare("time") == 0; };
inline bool isAlgebraic() const { return _realType == Algebraic; };
inline void setValue(int val) { _value = val; };
inline int value() { return _value; };
inline int value() const { return _value; };
unsigned int size();
inline bool hasAssignment() { return _hasAssigment; };
inline bool hasStartModifier() { return _hasStart; };
inline bool hasEachModifier() { return _hasEach; };
inline bool hasAssignment() const { return _hasAssigment; };
inline bool hasStartModifier() const { return _hasStart; };
inline bool hasEachModifier() const { return _hasEach; };
inline void setEachModifier(bool each) { _hasEach = each; };
inline string name() const { return _name; };
void setName(string name);
inline AST_Expression exp() { return _exp; };
inline bool isArray() const { return _isArray; };
inline bool isScalar() { return !isArray(); };
inline bool isScalar() const { return !isArray(); };
friend ostream& operator<<(ostream& os, const Variable& e);
inline unsigned int size(int dim) const { return _size[dim]; };
unsigned int rowSize(unsigned int dim) const;
inline unsigned int dimensions() const { return _size.size(); };
inline unsigned long dimensions() const { return _size.size(); };
std::string declaration(std::string prefix = "");
std::string initialization();
inline bool hasOffset() { return _hasOffset; };
inline bool hasOffset() const { return _hasOffset; };
inline void setOffset(int offset)
{
_offset = offset;
Expand All @@ -115,19 +110,20 @@ class Variable {
inline int offset() const { return _offset; };
inline bool isModelVar() const { return isState() || isDiscrete() || isAlgebraic() || isParameter() || isEqType() || isOutput(); };
std::string print() const;
std::string castOperator() const;
friend std::ostream& operator<<(std::ostream& out, const Variable& v);

private:
protected:
void processModification();
void unsetAssignment() { _hasAssigment = false; };
inline void unsetStartEach()
{
_hasEach = false;
_hasStart = false;
};
bool isDiscreteInteger() const;

bool _unknown;
bool _discrete;
Type _t;
AST_TypePrefix _tp;
AST_Modification _m;
Expand Down Expand Up @@ -157,16 +153,16 @@ class VarSymbolTable : public ModelTable<VarName, Variable> {
~VarSymbolTable() = default;
void initialize(TypeSymbolTable tst);
void insert(VarName name, Variable variable);
inline bool parameters() { return _parameters; };
Option<Variable> lookup(std::string name);
unsigned int maxDim() const;
inline bool parameters() const { return _parameters; };
Option<Variable> lookup(const std::string& name) const;
unsigned long maxDim() const;

private:
bool _parameters;
unsigned int _max_dims;
unsigned long _max_dims;
};

typedef std::list<Variable> VariableList;
using VariableList = std::list<Variable>;

} // namespace Util
} // namespace MicroModelica

0 comments on commit b982ea0

Please sign in to comment.