Skip to content

Commit

Permalink
Partial PWMap development
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalashnikovni committed Aug 17, 2023
1 parent ac843dd commit b10bf17
Show file tree
Hide file tree
Showing 81 changed files with 1,682 additions and 348 deletions.
2 changes: 1 addition & 1 deletion Makefile.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ libdir ?= $(exec_prefix)/lib

# Flags, Libraries and Includes
INCLUDES := -I.
CXXFLAGS := -std=c++17 -Wall -Werror -Wno-reorder -O3
CXXFLAGS := -std=c++17 -Wall -Werror -Wno-reorder -O3 -D BOOST_PHOENIX_STL_TUPLE_H_
ifeq ($(MODE),Debug)
CXXFLAGS += -ggdb
endif
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ In order to be able to install and compile SBG Library,
the following dependencies must be installed:

* autoconf 2.69 (avoid 2.71)
* boost1.71
* boost1.81 (or later)
* cmake
* g++
* make
Expand Down
149 changes: 138 additions & 11 deletions ast/expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,50 @@
******************************************************************************/

#include <ast/expr.hpp>
#include "ast/expr.hpp"

namespace SBG {

namespace AST {

// Arithmetic expressions ------------------------------------------------------

const char *OpNames[] = {"+", "-", "*", "^"};
std::ostream &operator<<(std::ostream &out, const UnOp &op)
{
switch (op) {
case UnOp::neg:
out << "-";
break;

default:
break;
}

return out;
}

std::ostream &operator<<(std::ostream &out, const Op &op)
{
out << OpNames[op];
switch (op) {
case Op::add:
out << "+";
break;

case Op::sub:
out << "-";
break;

case Op::mult:
out << "*";
break;

case Op::expo:
out << "^";
break;

default:
break;
}

return out;
}
Expand All @@ -42,6 +73,24 @@ std::ostream &operator<<(std::ostream &out, const ExprList &el)
return out;
}

UnaryOp::UnaryOp() : op_(), expr_() {}
UnaryOp::UnaryOp(UnOp op, Expr expr) : op_(op), expr_(expr) {}

member_imp(UnaryOp, UnOp, op);
member_imp(UnaryOp, Expr, expr);

bool UnaryOp::operator==(const UnaryOp &other) const
{
return op() == other.op() && expr() == other.expr();
}

std::ostream &operator<<(std::ostream &out, const UnaryOp &uop)
{
out << uop.op() << uop.expr();

return out;
}

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

Expand Down Expand Up @@ -83,7 +132,7 @@ std::ostream &operator<<(std::ostream &out, const Call &c)
if (sz > 0) {
unsigned int i = 0;
for (; i < sz - 1; i++)
out << c.args()[i] << ",";
out << c.args()[i] << ", ";
out << c.args()[i];
}
out << ")";
Expand All @@ -93,19 +142,47 @@ std::ostream &operator<<(std::ostream &out, const Call &c)

// SBG Structures --------------------------------------------------------------

const char *ContUOpNames[] = {"#", "\'"};
const char *ContOpNames[] = {"/\\", "\\", "<", "==", "\\/"};

std::ostream &operator<<(std::ostream &out, const ContainerUOp &op)
{
out << ContUOpNames[op];
switch (op) {
case ContainerUOp::card:
out << "#";
break;

case ContainerUOp::comp:
out << "\'";
break;
}

return out;
}

std::ostream &operator<<(std::ostream &out, const ContainerOp &op)
{
out << ContOpNames[op];
switch (op) {
case ContainerOp::eq:
out << "==";
break;

case ContainerOp::less:
out << "<";
break;

case ContainerOp::cap:
out << "/\\";
break;

case ContainerOp::diff:
out << "\\";
break;

case ContainerOp::cup:
out << "\\/";
break;

default:
break;
}

return out;
}
Expand Down Expand Up @@ -271,11 +348,33 @@ std::ostream &operator<<(std::ostream &out, const LinearExp &le)
return out;
}

std::ostream &operator<<(std::ostream &out, const ExpOp &op)
{
switch (op) {
case ExpOp::eq:
out << "==";
break;

case ExpOp::add:
out << "+";
break;

case ExpOp::sub:
out << "-";
break;

default:
break;
}

return out;
}

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

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

bool LExpBinOp::operator==(const LExpBinOp &other) const
Expand Down Expand Up @@ -310,6 +409,34 @@ std::ostream &operator<<(std::ostream &out, const LinearMap &lmap)
return out;
}

// Piecewise linear map --------------------------------------------------------

PWLMap::PWLMap() : maps_() {}
PWLMap::PWLMap(ExprList maps) : maps_(maps) {}

member_imp(PWLMap, ExprList, maps);

bool PWLMap::operator==(const PWLMap &other) const { return maps() == other.maps(); }

std::ostream &operator<<(std::ostream &out, const PWLMap &pwl)
{
PWLMap aux = pwl;
unsigned int sz = aux.maps_ref().size();

out << "<<";
if (sz > 0) {
auto it = aux.maps_ref().begin();
for (unsigned int i = 0; i < sz - 1; ++i) {
out << *it << ", ";
++it;
}
out << *it;
}
out << ">>";

return out;
}

} // namespace AST

} // namespace SBG
52 changes: 41 additions & 11 deletions ast/expr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <boost/variant/variant.hpp>
#include <boost/variant/recursive_wrapper.hpp>

#include <util/defs.hpp>
#include "util/defs.hpp"

namespace SBG {

Expand All @@ -40,19 +40,22 @@ typedef std::string Name;
typedef Util::NAT Natural;
typedef Util::RATIONAL Rational;
typedef bool Boolean;
struct UnaryOp;
struct BinOp;
struct Interval;
struct InterUnaryOp;
struct InterBinOp;
struct Set;
struct SetUnaryOp;
struct SetBinOp;
struct BinOp;
struct LinearExp;
struct LExpBinOp;
struct LinearMap;
struct PWLMap;
struct Call;

typedef boost::variant<Natural, Rational, Boolean, Util::VariableName,
boost::recursive_wrapper<UnaryOp>,
boost::recursive_wrapper<BinOp>,
boost::recursive_wrapper<Call>,
boost::recursive_wrapper<Interval>,
Expand All @@ -63,15 +66,29 @@ typedef boost::variant<Natural, Rational, Boolean, Util::VariableName,
boost::recursive_wrapper<SetBinOp>,
boost::recursive_wrapper<LinearExp>,
boost::recursive_wrapper<LExpBinOp>,
boost::recursive_wrapper<LinearMap>> Expr;
boost::recursive_wrapper<LinearMap>,
boost::recursive_wrapper<PWLMap>> Expr;
typedef std::vector<Expr> ExprList;
std::ostream &operator<<(std::ostream &out, const ExprList &el);

template <typename T>
inline bool is(Expr e) { return e.type() == typeid(T); }

typedef enum { add, sub, mult, expo } Op;
extern const char* OpNames[];
enum class UnOp { neg };
std::ostream &operator<<(std::ostream &out, const UnOp &op);

struct UnaryOp {
member_class(UnOp, op);
member_class(Expr, expr);

UnaryOp();
UnaryOp(UnOp op, Expr expr);

eq_class(UnaryOp);
};
std::ostream &operator<<(std::ostream &out, const UnaryOp &uop);

enum class Op { add, sub, mult, expo };
std::ostream &operator<<(std::ostream &out, const Op &op);

struct BinOp {
Expand Down Expand Up @@ -100,12 +117,10 @@ std::ostream &operator<<(std::ostream &out, const Call &c);

// SBG structures --------------------------------------------------------------

typedef enum { card, comp } ContainerUOp;
extern const char* ContUOpNames[];
enum class ContainerUOp { card, comp };
std::ostream &operator<<(std::ostream &out, const ContainerUOp &op);

typedef enum { cap, diff, less, eq, cup } ContainerOp;
extern const char* ContOpNames[];
enum class ContainerOp { eq, less, cap, diff, cup };
std::ostream &operator<<(std::ostream &out, const ContainerOp &op);

// Intervals -------------------------------------------------------------------
Expand Down Expand Up @@ -193,13 +208,16 @@ struct LinearExp {
};
std::ostream &operator<<(std::ostream &out, const LinearExp &le);

enum class ExpOp { eq, add, sub };
std::ostream &operator<<(std::ostream &out, const ExpOp &op);

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

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

eq_class(LExpBinOp);
};
Expand All @@ -218,6 +236,18 @@ struct LinearMap {
};
std::ostream &operator<<(std::ostream &out, const LinearMap &lm);

// Piecewise linear map --------------------------------------------------------

struct PWLMap {
member_class(ExprList, maps);

PWLMap();
PWLMap(ExprList maps);

eq_class(PWLMap);
};
std::ostream &operator<<(std::ostream &out, const PWLMap &pwl);

} // namespace AST

} // namespace SBG
Expand Down
2 changes: 1 addition & 1 deletion ast/sbg_program.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
******************************************************************************/

#include <ast/sbg_program.hpp>
#include "ast/sbg_program.hpp"

namespace SBG {

Expand Down
2 changes: 1 addition & 1 deletion ast/sbg_program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#ifndef PARSER_PROGRAM_AST_HPP
#define PARSER_PROGRAM_AST_HPP

#include <ast/statement.hpp>
#include "ast/statement.hpp"

namespace SBG {

Expand Down
2 changes: 1 addition & 1 deletion ast/statement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
******************************************************************************/

#include <ast/statement.hpp>
#include "ast/statement.hpp"

namespace SBG {

Expand Down
2 changes: 1 addition & 1 deletion ast/statement.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#ifndef PARSER_STATEMENT_AST_HPP
#define PARSER_STATEMENT_AST_HPP

#include <ast/expr.hpp>
#include "ast/expr.hpp"

namespace SBG {

Expand Down
Loading

0 comments on commit b10bf17

Please sign in to comment.