libsesstype  2.0.0
Library for Session Types programming.
expr_eval.h
00001 #ifndef SESSTYPE__PARAMETERISED__UTIL__EXPR_EVAL_H__
00002 #define SESSTYPE__PARAMETERISED__UTIL__EXPR_EVAL_H__
00003 
00004 #ifdef __cplusplus
00005 #include <stack>
00006 #endif
00007 
00008 #include "sesstype/parameterised/util/expr_visitor.h"
00009 
00010 #ifdef __cplusplus
00011 namespace sesstype {
00012 namespace parameterised {
00013 namespace util {
00014 #endif
00015 
00016 #ifdef __cplusplus
00017 class ExprEval : public ExprVisitor {
00018     std::stack<Expr *> stack_;
00019     bool invalid_;
00020 
00021   public:
00022     ExprEval() : stack_(), invalid_(false) { }
00023 
00024     Expr *eval()
00025     {
00026         return stack_.top();
00027     }
00028 
00029     bool is_valid()
00030     {
00031         return invalid_;
00032     }
00033 
00034     virtual void visit(Expr *expr) override
00035     {
00036         // Nothing.
00037     }
00038 
00039     virtual void visit(VarExpr *expr) override
00040     {
00041         stack_.push(expr->clone());
00042     }
00043 
00044     virtual void visit(ValExpr *expr) override
00045     {
00046         stack_.push(expr->clone());
00047     }
00048 
00049     virtual void visit(AddExpr *expr) override
00050     {
00051         expr->lhs()->accept(*this);
00052         Expr *lhs = stack_.top();
00053         stack_.pop();
00054 
00055         expr->rhs()->accept(*this);
00056         Expr *rhs = stack_.top();
00057         stack_.pop();
00058 
00059         if (auto lhs_ve = dynamic_cast<ValExpr *>(lhs)) {
00060             if (auto rhs_ve = dynamic_cast<ValExpr *>(rhs)) {
00061                 stack_.push(new ValExpr(lhs_ve->num() + rhs_ve->num()));
00062                 return;
00063             }
00064         }
00065         stack_.push(new AddExpr(lhs, rhs));
00066     }
00067 
00068     virtual void visit(SubExpr *expr) override
00069     {
00070         expr->lhs()->accept(*this);
00071         Expr *lhs = stack_.top();
00072         stack_.pop();
00073 
00074         expr->rhs()->accept(*this);
00075         Expr *rhs = stack_.top();
00076         stack_.pop();
00077 
00078         if (auto lhs_ve = dynamic_cast<ValExpr *>(lhs)) {
00079             if (auto rhs_ve = dynamic_cast<ValExpr *>(rhs)) {
00080                 stack_.push(new ValExpr(lhs_ve->num() - rhs_ve->num()));
00081                 return;
00082             }
00083         }
00084         stack_.push(new SubExpr(lhs, rhs));
00085     }
00086 
00087     virtual void visit(MulExpr *expr) override
00088     {
00089         expr->lhs()->accept(*this);
00090         Expr *lhs = stack_.top();
00091         stack_.pop();
00092 
00093         expr->rhs()->accept(*this);
00094         Expr *rhs = stack_.top();
00095         stack_.pop();
00096 
00097         if (auto lhs_ve = dynamic_cast<ValExpr *>(lhs)) {
00098             if (auto rhs_ve = dynamic_cast<ValExpr *>(rhs)) {
00099                 stack_.push(new ValExpr(lhs_ve->num() * rhs_ve->num()));
00100                 return;
00101             }
00102         }
00103         stack_.push(new MulExpr(lhs, rhs));
00104     }
00105 
00106     virtual void visit(DivExpr *expr) override
00107     {
00108         expr->lhs()->accept(*this);
00109         Expr *lhs = stack_.top();
00110         stack_.pop();
00111 
00112         expr->rhs()->accept(*this);
00113         Expr *rhs = stack_.top();
00114         stack_.pop();
00115 
00116         if (auto lhs_ve = dynamic_cast<ValExpr *>(lhs)) {
00117             if (auto rhs_ve = dynamic_cast<ValExpr *>(rhs)) {
00118                 stack_.push(new ValExpr(lhs_ve->num() / rhs_ve->num()));
00119                 return;
00120             }
00121         }
00122         stack_.push(new DivExpr(lhs, rhs));
00123     }
00124 
00125     virtual void visit(ModExpr *expr) override
00126     {
00127         expr->lhs()->accept(*this);
00128         Expr *lhs = stack_.top();
00129         stack_.pop();
00130 
00131         expr->rhs()->accept(*this);
00132         Expr *rhs = stack_.top();
00133         stack_.pop();
00134 
00135         if (auto lhs_ve = dynamic_cast<ValExpr *>(lhs)) {
00136             if (auto rhs_ve = dynamic_cast<ValExpr *>(rhs)) {
00137                 stack_.push(new ValExpr(lhs_ve->num() % rhs_ve->num()));
00138                 return;
00139             }
00140         }
00141         stack_.push(new ModExpr(lhs, rhs));
00142     }
00143 
00144     virtual void visit(ShlExpr *expr) override
00145     {
00146         expr->lhs()->accept(*this);
00147         Expr *lhs = stack_.top();
00148         stack_.pop();
00149 
00150         expr->rhs()->accept(*this);
00151         Expr *rhs = stack_.top();
00152         stack_.pop();
00153 
00154         if (auto lhs_ve = dynamic_cast<ValExpr *>(lhs)) {
00155             if (auto rhs_ve = dynamic_cast<ValExpr *>(rhs)) {
00156                 stack_.push(new ValExpr(lhs_ve->num() << rhs_ve->num()));
00157                 return;
00158             }
00159         }
00160         stack_.push(new ShlExpr(lhs, rhs));
00161     }
00162 
00163     virtual void visit(ShrExpr *expr) override
00164     {
00165         expr->lhs()->accept(*this);
00166         Expr *lhs = stack_.top();
00167         stack_.pop();
00168 
00169         expr->rhs()->accept(*this);
00170         Expr *rhs = stack_.top();
00171         stack_.pop();
00172 
00173         if (auto lhs_ve = dynamic_cast<ValExpr *>(lhs)) {
00174             if (auto rhs_ve = dynamic_cast<ValExpr *>(rhs)) {
00175                 stack_.push(new ValExpr(lhs_ve->num() >> rhs_ve->num()));
00176                 return;
00177             }
00178         }
00179         stack_.push(new ShrExpr(lhs, rhs));
00180     }
00181 
00182     virtual void visit(SeqExpr *expr) override
00183     {
00184         stack_.push(expr->clone());
00185         invalid_ = true;
00186     }
00187 
00188     virtual void visit(RngExpr *expr) override
00189     {
00190         expr->from()->accept(*this);
00191         Expr *from = stack_.top();
00192         stack_.pop();
00193 
00194         expr->to()->accept(*this);
00195         Expr *to = stack_.top();
00196         stack_.pop();
00197 
00198         stack_.push(new RngExpr(std::string(expr->bindvar()), from, to));
00199     }
00200 
00201     virtual void visit(LogExpr *expr) override
00202     {
00203         expr->value()->accept(*this);
00204         Expr *e = stack_.top();
00205         stack_.pop();
00206 
00207         expr->base()->accept(*this);
00208         Expr *base = stack_.top();
00209         stack_.pop();
00210 
00211         stack_.push(new LogExpr(e, base));
00212     }
00213 };
00214 #endif // __cplusplus
00215 #ifdef __cplusplus
00216 } // namespace util
00217 #endif
00218 
00219 #ifdef __cplusplus
00220 } // namespace parameterised
00221 } // namespace sesstype
00222 #endif
00223 
00224 #endif//SENDTYPE__PARAMETERISED__UTIL__EXPR_EVAL_H__
 All Classes Namespaces Files Functions