Skip to content

Commit

Permalink
arithemetic operators in interval domain
Browse files Browse the repository at this point in the history
  • Loading branch information
ytsao committed Dec 6, 2024
1 parent ec6ed89 commit 1f44a7b
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 23 deletions.
17 changes: 17 additions & 0 deletions abstract_interpreter/include/abstract_interpreter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,23 @@
class AbstractInterpreter {
public:
void eval(const ASTNode& ast){
// traverse the AST
dfs(ast);
}

void dfs(const ASTNode& ast){
if (ast.type == NodeType::DECLARATION){
return;
}

if (ast.children.size() > 0){
for (const auto& children: ast.children){

}
}
else{

}

}
};
2 changes: 1 addition & 1 deletion abstract_interpreter/include/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct ASTNode {
ASTNode(NodeType t, const std::string& name): type(t), value(name){}
ASTNode(NodeType t, const VType& value): type(t), value(value) {}

static void printVariant(const std::variant<std::string, int, BinOp, LogicOp>& value) {
static void printVariant(const VType& value) {
std::visit([](const auto& v) {
std::cout << v << std::endl;
}, value);
Expand Down
102 changes: 80 additions & 22 deletions abstract_interpreter/include/interval.hpp
Original file line number Diff line number Diff line change
@@ -1,41 +1,99 @@
#include <algorithm>

#include "ast.hpp"

class Interval {
public:
int lower;
int upper;
using long long ll;

ll lb;
ll ub;

Interval() : lb(0), ub(0) {}
Interval(int lb, int ub) : lb(lb), ub(ub) {}

void add(const Interval& other){
lb += other.lb;
ub += other.ub;
return;
}

void sub(const Interval& other){
lb -= other.lb;
ub -= other.ub;
return;
}

void mul(const Interval& other){
ll B[4];
B[0] = lb * other.lb;
B[1] = lb * other.ub;
B[2] = ub * other.lb;
B[3] = ub * other.ub;

lb = std::min_element(B, B+4);
ub = std::max_element(B, B+4);

return;
}

Interval() : lower(0), upper(0) {}
Interval(int lower, int upper) : lower(lower), upper(upper) {}
void div(const Interval& other){
if (0 < other.lb || other.ub < 0){
std::cout << "[ERORR]: The division contains zero." << std::endl;
}

ll B[4];
B[0] = lb * (1 / other.lb);
B[1] = lb * (1 / other.ub);
B[2] = ub * (1 / other.lb);
B[3] = ub * (1 / other.ub);

lb = std::min_element(B, B+4);
ub = std::max_element(B, B+4);

void add(){return;}
void sub(){return;}
void mul(){return;}
void div(){return;}
return;
}

bool le(const Interval& other){
return false;
}

void le(){return;}
void leq(){return;}
void ge(){return;}
void geq(){return;}
void eq(){return;}
void neq(){return;}
bool leq(const Interval& other){
return false;
}

bool ge(const Interval& other){
return false;
}

bool geq(const Interval& other){
return false;
}

bool eq(const Interval& other){
return false;
}

bool neq(const Interval& other){
return false;
}

void meet(const Interval& other){
lower = std::max(lower, other.lower);
upper = std::min(upper, other.upper);
lb = std::max(lb, other.lb);
ub = std::min(ub, other.ub);
}

void join(const Interval& other){
lower = std::min(lower, other.lower);
upper = std::max(upper, other.upper);
lb = std::min(lb, other.lb);
ub = std::max(ub, other.ub);
}

void widen(const Interval& other){
if (lower > other.lower){
lower = std::numeric_limits<int>::min();
if (lb > other.lb){
lb = std::numeric_limits<int>::min();
}
if (upper < other.upper){
upper = std::numeric_limits<int>::max();
if (ub < other.ub){
ub = std::numeric_limits<int>::max();
}
}
};

0 comments on commit 1f44a7b

Please sign in to comment.