Skip to content

Commit

Permalink
add abstract interpreter framework
Browse files Browse the repository at this point in the history
  • Loading branch information
ytsao committed Dec 6, 2024
1 parent 26b066b commit ec6ed89
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 0 deletions.
8 changes: 8 additions & 0 deletions abstract_interpreter/include/abstract_interpreter.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "ast.hpp"

class AbstractInterpreter {
public:
void eval(const ASTNode& ast){

}
};
41 changes: 41 additions & 0 deletions abstract_interpreter/include/interval.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "ast.hpp"

class Interval {
public:
int lower;
int upper;

Interval() : lower(0), upper(0) {}
Interval(int lower, int upper) : lower(lower), upper(upper) {}

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

void le(){return;}
void leq(){return;}
void ge(){return;}
void geq(){return;}
void eq(){return;}
void neq(){return;}

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

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

void widen(const Interval& other){
if (lower > other.lower){
lower = std::numeric_limits<int>::min();
}
if (upper < other.upper){
upper = std::numeric_limits<int>::max();
}
}
};
67 changes: 67 additions & 0 deletions abstract_interpreter/include/store.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include "ast.hpp"
#include "interval.hpp"

class InterValStore {
public:
std::map<std::string, Interval> store;

void set(const std::string& name, const Interval& value){
store[name] = value;
}

Interval get(const std::string& name){
return store[name];
}

void add(const std::string& name, const Interval& value){
store[name].add();
}

void sub(const std::string& name, const Interval& value){
store[name].sub();
}

void mul(const std::string& name, const Interval& value){
store[name].mul();
}

void div(const std::string& name, const Interval& value){
store[name].div();
}

void le(const std::string& name, const Interval& value){
store[name].le();
}

void leq(const std::string& name, const Interval& value){
store[name].leq();
}

void ge(const std::string& name, const Interval& value){
store[name].ge();
}

void geq(const std::string& name, const Interval& value){
store[name].geq();
}

void eq(const std::string& name, const Interval& value){
store[name].eq();
}

void neq(const std::string& name, const Interval& value){
store[name].neq();
}

void join(const std::string& name, const Interval& value){
store[name].join(value);
}

void meet(const std::string& name, const Interval& value){
store[name].meet(value);
}

void widen(const std::string& name, const Interval& value){
store[name].widen(value);
}
};

0 comments on commit ec6ed89

Please sign in to comment.