Skip to content

Commit

Permalink
Added SBGMap to ast/, parser/, eval/ (parser needs debugging)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalashnikovni committed Jul 24, 2023
1 parent 575d97b commit 0db678a
Show file tree
Hide file tree
Showing 28 changed files with 384 additions and 100 deletions.
20 changes: 20 additions & 0 deletions ast/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,26 @@ std::ostream &operator<<(std::ostream &out, const LExpBinOp &lbop)
return out;
}

// SBG map ---------------------------------------------------------------------

LinearMap::LinearMap() : dom_(), lexp_() {}
LinearMap::LinearMap(Expr dom, Expr lexp) : dom_(dom), lexp_(lexp) {}

member_imp(LinearMap, Expr, dom);
member_imp(LinearMap, Expr, lexp);

bool LinearMap::operator==(const LinearMap &other) const
{
return dom() == other.dom() && lexp() == other.lexp();
}

std::ostream &operator<<(std::ostream &out, const LinearMap &lmap)
{
out << lmap.dom() << "" << lmap.lexp();

return out;
}

} // namespace AST

} // namespace SBG
17 changes: 16 additions & 1 deletion ast/expr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ struct SetBinOp;
struct BinOp;
struct LinearExp;
struct LExpBinOp;
struct LinearMap;
struct Call;

typedef boost::variant<Natural, Rational, Boolean, Util::VariableName,
Expand All @@ -61,7 +62,8 @@ typedef boost::variant<Natural, Rational, Boolean, Util::VariableName,
boost::recursive_wrapper<SetUnaryOp>,
boost::recursive_wrapper<SetBinOp>,
boost::recursive_wrapper<LinearExp>,
boost::recursive_wrapper<LExpBinOp>> Expr;
boost::recursive_wrapper<LExpBinOp>,
boost::recursive_wrapper<LinearMap>> Expr;
typedef std::vector<Expr> ExprList;
std::ostream &operator<<(std::ostream &out, const ExprList &el);

Expand Down Expand Up @@ -203,6 +205,19 @@ struct LExpBinOp {
};
std::ostream &operator<<(std::ostream &out, const LExpBinOp &lbop);

// SBG map ---------------------------------------------------------------------

struct LinearMap {
member_class(Expr, dom);
member_class(Expr, lexp);

LinearMap();
LinearMap(Expr dom, Expr lexp);

eq_class(LinearMap);
};
std::ostream &operator<<(std::ostream &out, const LinearMap &lm);

} // namespace AST

} // namespace SBG
Expand Down
4 changes: 3 additions & 1 deletion eval/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ EVAL_SRC := \
$(VISIT_DIR)/eval_interval.cpp \
$(VISIT_DIR)/eval_set.cpp \
$(VISIT_DIR)/eval_le.cpp \
$(VISIT_DIR)/eval_map.cpp \
$(VISIT_DIR)/eval_expr.cpp \
$(VISIT_DIR)/stm_visitor.cpp \
$(VISIT_DIR)/program_visitor.cpp \
$(SBG_DIR)/interval.cpp \
$(SBG_DIR)/pw_inter.cpp \
$(SBG_DIR)/lexp.cpp
$(SBG_DIR)/lexp.cpp \
$(SBG_DIR)/map.cpp

# Objects
EVAL_OBJ=$(addprefix $(BUILD_DIR)/, $(EVAL_SRC:.cpp=.o))
Expand Down
4 changes: 2 additions & 2 deletions eval/defs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Util::NAT toNat(ExprBaseType v)
return v_rat.numerator();
}

Util::ERROR("toInt: expression is not integer");
Util::ERROR("toNat: expression is not integer");
return 0;
}

Expand All @@ -58,7 +58,7 @@ MaybeVValue VarEnv::operator[](VKey k) const

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

MaybeFValue FuncEnv::operator[](FKey k) const
{
Expand Down
7 changes: 3 additions & 4 deletions eval/defs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@

#include <ast/expr.hpp>
#include <ast/statement.hpp>
#include <sbg/pw_inter.hpp>
#include <sbg/lexp.hpp>
#include <sbg/map.hpp>
#include <util/debug.hpp>
#include <util/defs.hpp>

Expand All @@ -43,7 +42,7 @@ namespace Eval {

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

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

template <class T>
Expand Down Expand Up @@ -116,7 +115,7 @@ struct FuncEnv{
static FuncEnvType mapping_;
};

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

/** @struct Overload
*
Expand Down
90 changes: 65 additions & 25 deletions eval/visitors/eval_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,38 @@ auto inverse_visitor_ = Overload{
}
};

auto image_visitor1_ = Overload{
[](SBG::SBGMap a) { return image(a); },
[](auto a) {
Util::ERROR("Wrong arguments for image 1");
return SBG::SetPiece();
}
};

auto image_visitor2_ = Overload{
[](SetPiece a, SBGMap b) { return image(a, b); },
[](auto a, auto b) {
Util::ERROR("Wrong arguments for image 2");
return SBG::SetPiece();
}
};

auto pre_image_visitor1_ = Overload{
[](SBG::SBGMap a) { return preImage(a); },
[](auto a) {
Util::ERROR("Wrong arguments for pre image 1");
return SBG::SetPiece();
}
};

auto pre_image_visitor2_ = Overload{
[](SetPiece a, SBGMap b) { return preImage(a, b); },
[](auto a, auto b) {
Util::ERROR("Wrong arguments for pre image 2");
return SBG::SetPiece();
}
};

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

EvalExpression::EvalExpression() : env_() {}
Expand Down Expand Up @@ -183,6 +215,34 @@ ExprBaseType EvalExpression::operator()(AST::Call v) const
return result;
}

case Eval::Func::im: {
if (eval_args.size() == 1) {
ExprBaseType sbgmap = eval_args[0];
SetPiece result = std::visit(image_visitor1_, sbgmap);
return result;
}

else if (eval_args.size() == 2) {
ExprBaseType subdom = eval_args[0], sbgmap = eval_args[1];
SetPiece result = std::visit(image_visitor2_, subdom, sbgmap);
return result;
}
}

case Eval::Func::preim: {
if (eval_args.size() == 1) {
ExprBaseType sbgmap = eval_args[0];
SetPiece result = std::visit(pre_image_visitor1_, sbgmap);
return result;
}

else if (eval_args.size() == 2) {
ExprBaseType subdom = eval_args[0], sbgmap = eval_args[1];
SetPiece result = std::visit(pre_image_visitor2_, subdom, sbgmap);
return result;
}
}

default:
Util::ERROR("EvalExpression: function %s not implemented", vname.c_str());
return 0;
Expand All @@ -193,15 +253,7 @@ ExprBaseType EvalExpression::operator()(AST::Call v) const
return 0;
}

ExprBaseType EvalExpression::operator()(AST::Interval v) const
{
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);
}
ExprBaseType EvalExpression::operator()(AST::Interval v) const { return Apply(EvalInterval(env_), AST::Expr(v)); }

ExprBaseType EvalExpression::operator()(AST::InterUnaryOp v) const
{
Expand Down Expand Up @@ -237,16 +289,7 @@ ExprBaseType EvalExpression::operator()(AST::InterBinOp v) const
}
}

ExprBaseType EvalExpression::operator()(AST::Set v) const
{
SBG::InterSet res;
EvalInterval inter_visit(env_);

BOOST_FOREACH (auto i, v.pieces())
res.emplace(Apply(inter_visit, i));

return SBG::Set(res);
}
ExprBaseType EvalExpression::operator()(AST::Set v) const { return Apply(EvalSet(env_), AST::Expr(v)); }

ExprBaseType EvalExpression::operator()(AST::SetUnaryOp v) const
{
Expand Down Expand Up @@ -288,12 +331,7 @@ ExprBaseType EvalExpression::operator()(AST::SetBinOp v) const
}
}

ExprBaseType EvalExpression::operator()(AST::LinearExp v) const
{
EvalRat visit_rat(env_);

return SBG::LExp(Apply(visit_rat, v.slope()), Apply(visit_rat, v.offset()));
}
ExprBaseType EvalExpression::operator()(AST::LinearExp v) const { return Apply(EvalLE(env_), AST::Expr(v)); }

ExprBaseType EvalExpression::operator()(AST::LExpBinOp v) const
{
Expand All @@ -312,6 +350,8 @@ ExprBaseType EvalExpression::operator()(AST::LExpBinOp v) const
}
}

ExprBaseType EvalExpression::operator()(AST::LinearMap v) const { return Apply(EvalMap(env_), AST::Expr(v)); }

} // namespace Eval

} // namespace SBG
4 changes: 2 additions & 2 deletions eval/visitors/eval_expr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +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_map.hpp>
#include <eval/visitors/eval_set.hpp>

namespace SBG {
Expand All @@ -51,6 +50,7 @@ struct EvalExpression : public boost::static_visitor<ExprBaseType> {
ExprBaseType operator()(AST::SetBinOp v) const;
ExprBaseType operator()(AST::LinearExp v) const;
ExprBaseType operator()(AST::LExpBinOp v) const;
ExprBaseType operator()(AST::LinearMap v) const;

private:
mutable VarEnv env_;
Expand Down
13 changes: 11 additions & 2 deletions eval/visitors/eval_interval.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ SBG::Interval EvalInterval::operator()(AST::Call v) const

SBG::Interval EvalInterval::operator()(AST::Interval v) const
{
EvalNat eval_nat(env_);
EvalNat visit_nat(env_);
Util::NAT b = Apply(visit_nat, v.begin());
Util::NAT s = Apply(visit_nat, v.step());
Util::NAT e = Apply(visit_nat, v.end());

return SBG::Interval(Apply(eval_nat, v.begin()), Apply(eval_nat, v.step()), Apply(eval_nat, v.end()));
return Interval(b, s, e);
}

SBG::Interval EvalInterval::operator()(AST::InterUnaryOp v) const
Expand Down Expand Up @@ -134,6 +137,12 @@ SBG::Interval EvalInterval::operator()(AST::LExpBinOp v) const
return SBG::Interval();
}

SBG::Interval EvalInterval::operator()(AST::LinearMap v) const
{
Util::ERROR("EvalInterval: trying to evaluate a LinearMap");
return SBG::Interval();
}

} // namespace Eval

} // namespace SBG
1 change: 1 addition & 0 deletions eval/visitors/eval_interval.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct EvalInterval : public boost::static_visitor<SBG::Interval> {
SBG::Interval operator()(AST::SetBinOp v) const;
SBG::Interval operator()(AST::LinearExp v) const;
SBG::Interval operator()(AST::LExpBinOp v) const;
SBG::Interval operator()(AST::LinearMap v) const;

private:
mutable VarEnv env_;
Expand Down
10 changes: 9 additions & 1 deletion eval/visitors/eval_le.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ LExp EvalLE::operator()(AST::SetBinOp v) const
LExp EvalLE::operator()(AST::LinearExp le) const
{
EvalRat visit_rat(env_);
AST::Expr m = le.slope();
AST::Expr h = le.offset();

return LExp(Apply(visit_rat, le.slope()), Apply(visit_rat, le.offset()));
return LExp(Apply(visit_rat, m), Apply(visit_rat, h));
}

LExp EvalLE::operator()(AST::LExpBinOp v) const
Expand All @@ -129,6 +131,12 @@ LExp EvalLE::operator()(AST::LExpBinOp v) const
}
}

LExp EvalLE::operator()(AST::LinearMap v) const
{
Util::ERROR("EvalLE: trying to evaluate a LinearMap");
return SBG::LExp();
}

} // namespace Eval

} // namespace SBG
1 change: 1 addition & 0 deletions eval/visitors/eval_le.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct EvalLE : public boost::static_visitor<SBG::LExp> {
SBG::LExp operator()(AST::SetBinOp v) const;
SBG::LExp operator()(AST::LinearExp v) const;
SBG::LExp operator()(AST::LExpBinOp v) const;
SBG::LExp operator()(AST::LinearMap v) const;

private:
mutable VarEnv env_;
Expand Down
Loading

0 comments on commit 0db678a

Please sign in to comment.