Skip to content

Commit

Permalink
Added SBGMap to /sbg
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalashnikovni committed Jul 24, 2023
1 parent c504f70 commit 575d97b
Show file tree
Hide file tree
Showing 34 changed files with 752 additions and 286 deletions.
19 changes: 19 additions & 0 deletions ast/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,25 @@ std::ostream &operator<<(std::ostream &out, const LinearExp &le)
return out;
}

LExpBinOp::LExpBinOp() : left_(), op_(), right_() {}
LExpBinOp::LExpBinOp(Expr left, Op op, Expr right) : left_(left), op_(op), right_(right) {}

member_imp(LExpBinOp, Expr, left);
member_imp(LExpBinOp, Op, op);
member_imp(LExpBinOp, Expr, right);

bool LExpBinOp::operator==(const LExpBinOp &other) const
{
return left() == other.left() && op() == other.op() && right() == other.right();
}

std::ostream &operator<<(std::ostream &out, const LExpBinOp &lbop)
{
out << "(" << lbop.left() << ")" << lbop.op() << "(" << lbop.right() << ")";

return out;
}

} // namespace AST

} // namespace SBG
20 changes: 17 additions & 3 deletions ast/expr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace AST {
// Arithmetic and call structures ----------------------------------------------

typedef std::string Name;
typedef Util::INT Integer;
typedef Util::NAT Natural;
typedef Util::RATIONAL Rational;
typedef bool Boolean;
struct Interval;
Expand All @@ -48,9 +48,10 @@ struct SetUnaryOp;
struct SetBinOp;
struct BinOp;
struct LinearExp;
struct LExpBinOp;
struct Call;

typedef boost::variant<Integer, Rational, Boolean, Util::VariableName,
typedef boost::variant<Natural, Rational, Boolean, Util::VariableName,
boost::recursive_wrapper<BinOp>,
boost::recursive_wrapper<Call>,
boost::recursive_wrapper<Interval>,
Expand All @@ -59,7 +60,8 @@ typedef boost::variant<Integer, Rational, Boolean, Util::VariableName,
boost::recursive_wrapper<Set>,
boost::recursive_wrapper<SetUnaryOp>,
boost::recursive_wrapper<SetBinOp>,
boost::recursive_wrapper<LinearExp>> Expr;
boost::recursive_wrapper<LinearExp>,
boost::recursive_wrapper<LExpBinOp>> Expr;
typedef std::vector<Expr> ExprList;
std::ostream &operator<<(std::ostream &out, const ExprList &el);

Expand Down Expand Up @@ -189,6 +191,18 @@ struct LinearExp {
};
std::ostream &operator<<(std::ostream &out, const LinearExp &le);

struct LExpBinOp {
member_class(Expr, left);
member_class(Op, op);
member_class(Expr, right);

LExpBinOp();
LExpBinOp(Expr left, Op op, Expr right);

eq_class(LExpBinOp);
};
std::ostream &operator<<(std::ostream &out, const LExpBinOp &lbop);

} // namespace AST

} // namespace SBG
Expand Down
3 changes: 2 additions & 1 deletion eval/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ EVAL_SRC := \
$(PARSER_DIR)/expr.cpp \
$(PARSER_DIR)/statement.cpp \
$(PARSER_DIR)/sbg_program.cpp \
$(VISIT_DIR)/eval_int.cpp \
$(VISIT_DIR)/eval_nat.cpp \
$(VISIT_DIR)/eval_rat.cpp \
$(VISIT_DIR)/eval_interval.cpp \
$(VISIT_DIR)/eval_set.cpp \
$(VISIT_DIR)/eval_le.cpp \
$(VISIT_DIR)/eval_expr.cpp \
$(VISIT_DIR)/stm_visitor.cpp \
$(VISIT_DIR)/program_visitor.cpp \
Expand Down
13 changes: 7 additions & 6 deletions eval/defs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ namespace Eval {

// Type definitions ------------------------------------------------------------

Util::INT toInt(ExprBaseType v)
Util::NAT toNat(ExprBaseType v)
{
if (std::holds_alternative<Util::INT>(v))
return std::get<Util::INT>(v);
if (std::holds_alternative<Util::NAT>(v))
return std::get<Util::NAT>(v);

if (std::holds_alternative<Util::RATIONAL>(v)) {
Util::RATIONAL v_rat = std::get<Util::RATIONAL>(v);
if (v_rat.value().denominator() == 1)
return v_rat.value().numerator();
if (v_rat.denominator() == 1 && v_rat.numerator() >= 0)
return v_rat.numerator();
}

Util::ERROR("toInt: expression is not integer");
Expand All @@ -57,7 +57,8 @@ MaybeVValue VarEnv::operator[](VKey k) const
}

FuncEnv::FuncEnv() {}
FuncEnvType FuncEnv::mapping_ = {{"isEmpty", 0}, {"isMember", 1}, {"minElem", 2}, {"maxElem", 3}, {"lt", 4}};
FuncEnvType FuncEnv::mapping_ = {{"isEmpty", 0}, {"isMember", 1}, {"minElem", 2}, {"maxElem", 3}, {"lt", 4},
{"compose", 5}, {"inv", 6}};

MaybeFValue FuncEnv::operator[](FKey k) const
{
Expand Down
10 changes: 5 additions & 5 deletions eval/defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace Eval {

// Type definitions ------------------------------------------------------------

typedef std::variant<Util::INT, Util::RATIONAL, SBG::Interval, SBG::Set, SBG::LExp> ExprBaseType;
typedef std::variant<Util::NAT, Util::RATIONAL, SBG::Interval, SBG::Set, SBG::LExp> ExprBaseType;
typedef std::optional<ExprBaseType> MaybeEBT;

template <class T>
Expand All @@ -68,11 +68,11 @@ std::ostream &operator<<(std::ostream &out, streamer<std::variant<Ts...>> sv)
return out;
}

/** @function toInt
/** @function toNat
*
* @brief Converts a rational n/1 to an integer n (if needed).
* @brief Converts a positive rational n/1 to n (if possible).
*/
Util::INT toInt(ExprBaseType t);
Util::NAT toNat(ExprBaseType t);

// Environments ----------------------------------------------------------------

Expand Down Expand Up @@ -116,7 +116,7 @@ struct FuncEnv{
static FuncEnvType mapping_;
};

typedef enum { empty, member, min, max, lt } Func;
typedef enum { empty, member, min, max, lt, comp, inv } Func;

/** @struct Overload
*
Expand Down
87 changes: 73 additions & 14 deletions eval/visitors/eval_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,68 @@ namespace Eval {
auto empty_visitor_ = Overload{
[](SBG::Interval a) { return isEmpty(a); },
[](SBG::Set a) { return isEmpty(a); },
[](auto a) { return true; } // << default!
[](auto a) {
Util::ERROR("Wrong arguments for isEmpty");
return true;
}
};

auto member_visitor_ = Overload{
[](Util::INT a, SBG::Interval b) { return isMember(a, b); },
[](Util::INT a, SBG::Set b) { return isMember(a, b); },
[](auto a, auto b) { return false; } // << default!
[](Util::NAT a, SBG::Interval b) { return isMember(a, b); },
[](Util::NAT a, SBG::Set b) { return isMember(a, b); },
[](auto a, auto b) {
Util::ERROR("Wrong arguments for isMember");
return false;
}
};

auto min_visitor_ = Overload{
[](SBG::Interval a) { return minElem(a); },
[](SBG::Set a) { return minElem(a); },
[](auto a) { return (Util::INT) 0; }
[](auto a) {
Util::ERROR("Wrong arguments for minElem");
return (Util::NAT) 0;
}
};

auto max_visitor_ = Overload{
[](SBG::Interval a) { return maxElem(a); },
[](SBG::Set a) { return maxElem(a); },
[](auto a) { return (Util::INT) 0; }
[](auto a) {
Util::ERROR("Wrong arguments for maxElem");
return (Util::NAT) 0;
}
};

auto lt_visitor_ = Overload{
[](SBG::Interval a, SBG::Interval b) { return least(a, b); },
[](auto a, auto b) { return SBG::Interval(1, 0, 0); }
[](auto a, auto b) {
Util::ERROR("Wrong arguments for lt");
return SBG::Interval();
}
};

auto compose_visitor_ = Overload{
[](SBG::LExp a, SBG::LExp b) { return composition(a, b); },
[](auto a, auto b) {
Util::ERROR("Wrong arguments for composition");
return SBG::LExp(); }
};

auto inverse_visitor_ = Overload{
[](SBG::LExp a) { return inverse(a); },
[](auto a) {
Util::ERROR("Wrong arguments for inversion");
return SBG::LExp();
}
};

// Expression evaluator --------------------------------------------------------

EvalExpression::EvalExpression() : env_() {}
EvalExpression::EvalExpression(VarEnv env) : env_(env) {}

ExprBaseType EvalExpression::operator()(AST::Integer v) const { return v; }
ExprBaseType EvalExpression::operator()(AST::Natural v) const { return v; }

ExprBaseType EvalExpression::operator()(AST::Rational v) const { return v; }

Expand Down Expand Up @@ -125,13 +155,13 @@ ExprBaseType EvalExpression::operator()(AST::Call v) const

case Eval::Func::min: {
ExprBaseType container = eval_args[0];
Util::INT result = std::visit(min_visitor_, container);
Util::NAT result = std::visit(min_visitor_, container);
return result;
}

case Eval::Func::max: {
ExprBaseType container = eval_args[0];
Util::INT result = std::visit(max_visitor_, container);
Util::NAT result = std::visit(max_visitor_, container);
return result;
}

Expand All @@ -141,6 +171,18 @@ ExprBaseType EvalExpression::operator()(AST::Call v) const
return result;
}

case Eval::Func::comp: {
ExprBaseType lexp1 = eval_args[0], lexp2 = eval_args[1];
SBG::LExp result = std::visit(compose_visitor_, lexp1, lexp2);
return result;
}

case Eval::Func::inv: {
ExprBaseType lexp = eval_args[0];
SBG::LExp result = std::visit(inverse_visitor_, lexp);
return result;
}

default:
Util::ERROR("EvalExpression: function %s not implemented", vname.c_str());
return 0;
Expand All @@ -153,10 +195,10 @@ ExprBaseType EvalExpression::operator()(AST::Call v) const

ExprBaseType EvalExpression::operator()(AST::Interval v) const
{
EvalInt int_visit(env_);
Util::INT b = Apply(int_visit, v.begin());
Util::INT s = Apply(int_visit, v.step());
Util::INT e = Apply(int_visit, v.end());
EvalNat nat_visit(env_);
Util::NAT b = Apply(nat_visit, v.begin());
Util::NAT s = Apply(nat_visit, v.step());
Util::NAT e = Apply(nat_visit, v.end());

return Interval(b, s, e);
}
Expand Down Expand Up @@ -253,6 +295,23 @@ ExprBaseType EvalExpression::operator()(AST::LinearExp v) const
return SBG::LExp(Apply(visit_rat, v.slope()), Apply(visit_rat, v.offset()));
}

ExprBaseType EvalExpression::operator()(AST::LExpBinOp v) const
{
AST::Expr l = v.left(), r = v.right();
EvalLE visit_le(env_);
switch (v.op()) {
case AST::Op::add:
return Apply(visit_le, l) + Apply(visit_le, r);

case AST::Op::sub:
return Apply(visit_le, l) - Apply(visit_le, r);

default:
Util::ERROR("EvalExpression: LExpBinOp %s not supported.", AST::OpNames[v.op()]);
return 0;
}
}

} // namespace Eval

} // namespace SBG
4 changes: 3 additions & 1 deletion eval/visitors/eval_expr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#ifndef AST_VISITOR_EVALEXP
#define AST_VISITOR_EVALEXP

#include <eval/visitors/eval_le.hpp>
#include <eval/visitors/eval_rat.hpp>
#include <eval/visitors/eval_set.hpp>

Expand All @@ -36,7 +37,7 @@ struct EvalExpression : public boost::static_visitor<ExprBaseType> {
EvalExpression();
EvalExpression(VarEnv env);

ExprBaseType operator()(AST::Integer v) const;
ExprBaseType operator()(AST::Natural v) const;
ExprBaseType operator()(AST::Rational v) const;
ExprBaseType operator()(AST::Boolean v) const;
ExprBaseType operator()(Util::VariableName v) const;
Expand All @@ -49,6 +50,7 @@ struct EvalExpression : public boost::static_visitor<ExprBaseType> {
ExprBaseType operator()(AST::SetUnaryOp v) const;
ExprBaseType operator()(AST::SetBinOp v) const;
ExprBaseType operator()(AST::LinearExp v) const;
ExprBaseType operator()(AST::LExpBinOp v) const;

private:
mutable VarEnv env_;
Expand Down
Loading

0 comments on commit 575d97b

Please sign in to comment.