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