Changeset d7aa12c for src/Common
- Timestamp:
- May 23, 2019, 11:55:32 AM (6 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- b5fed34
- Parents:
- 9d6e7fa9
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
TabularUnified src/Common/Eval.cc ¶
r9d6e7fa9 rd7aa12c 17 17 18 18 #include "Common/PassVisitor.h" 19 #include "AST/Pass.hpp" 19 20 #include "InitTweak/InitTweak.h" 20 21 #include "SynTree/Expression.h" 21 22 22 struct Eval : public WithShortCircuiting { 23 //------------------------------------------------------------- 24 // Old AST 25 struct EvalOld : public WithShortCircuiting { 23 26 long long int value = 0; 24 27 bool valid = true; … … 80 83 }; 81 84 85 //------------------------------------------------------------- 86 // New AST 87 struct EvalNew : public ast::WithShortCircuiting { 88 long long int value = 0; 89 bool valid = true; 90 91 void previsit( const ast::Node * ) { visit_children = false; } 92 void postvisit( const ast::Node * ) { valid = false; } 93 94 void postvisit( const ast::ConstantExpr * expr ) { 95 value = expr->intValue(); 96 } 97 98 void postvisit( const ast::CastExpr * expr ) { 99 auto arg = eval(expr->arg); 100 valid = arg.second; 101 value = arg.first; 102 // TODO: perform type conversion on value if valid 103 } 104 105 void postvisit( const ast::VariableExpr * expr ) { 106 if ( const ast::EnumInstType * inst = dynamic_cast<const ast::EnumInstType *>(expr->result.get()) ) { 107 if ( const ast::EnumDecl * decl = inst->base ) { 108 if ( decl->valueOf( expr->var, value ) ) { // value filled by valueOf 109 return; 110 } 111 } 112 } 113 valid = false; 114 } 115 116 void postvisit( const ast::ApplicationExpr * expr ) { 117 const ast::DeclWithType * function = InitTweak::getFunction(expr); 118 if ( ! function || function->linkage != ast::Linkage::Intrinsic ) { valid = false; return; } 119 const std::string & fname = function->name; 120 assertf( expr->args.size() == 1 || expr->args.size() == 2, "Intrinsic function with %zd arguments: %s", expr->args.size(), fname.c_str() ); 121 std::pair<long long int, bool> arg1, arg2; 122 arg1 = eval(expr->args.front()); 123 valid = valid && arg1.second; 124 if ( ! valid ) return; 125 if ( expr->args.size() == 2 ) { 126 arg2 = eval(expr->args.back()); 127 valid = valid && arg2.second; 128 if ( ! valid ) return; 129 } 130 if (fname == "?+?") { 131 value = arg1.first + arg2.first; 132 } else if (fname == "?-?") { 133 value = arg1.first - arg2.first; 134 } else if (fname == "?*?") { 135 value = arg1.first * arg2.first; 136 } else if (fname == "?/?") { 137 value = arg1.first / arg2.first; 138 } else if (fname == "?%?") { 139 value = arg1.first % arg2.first; 140 } else { 141 valid = false; 142 } 143 // TODO: implement other intrinsic functions 144 } 145 }; 146 82 147 std::pair<long long int, bool> eval(Expression * expr) { 83 PassVisitor<Eval > ev;148 PassVisitor<EvalOld> ev; 84 149 if (expr) { 85 150 expr->accept(ev); … … 91 156 92 157 std::pair<long long int, bool> eval(const ast::Expr * expr) { 93 #warning not implemented 94 return { 0, false }; 158 ast::Pass<EvalNew> ev; 159 if (expr) { 160 expr->accept(ev); 161 return std::make_pair(ev.pass.value, ev.pass.valid); 162 } else { 163 return std::make_pair(0, false); 164 } 95 165 } 96 166
Note: See TracChangeset
for help on using the changeset viewer.