diff options
author | Tuowen Zhao <ztuowen@gmail.com> | 2016-09-24 10:39:43 -0600 |
---|---|---|
committer | Tuowen Zhao <ztuowen@gmail.com> | 2016-09-24 10:39:43 -0600 |
commit | 47ba6bf1100fe1da8d80607053d88cfd2abe25ff (patch) | |
tree | 6b5bc16c5f69cd870457062990712464588ba7a9 /src/chillASTs.cc | |
parent | f939f4bacb23cfe0c4fb96c5d71f30412fa828d7 (diff) | |
download | chill-47ba6bf1100fe1da8d80607053d88cfd2abe25ff.tar.gz chill-47ba6bf1100fe1da8d80607053d88cfd2abe25ff.tar.bz2 chill-47ba6bf1100fe1da8d80607053d88cfd2abe25ff.zip |
added precedence to operators
Diffstat (limited to 'src/chillASTs.cc')
-rw-r--r-- | src/chillASTs.cc | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/src/chillASTs.cc b/src/chillASTs.cc index 170e481..3665571 100644 --- a/src/chillASTs.cc +++ b/src/chillASTs.cc @@ -3660,13 +3660,11 @@ void chillAST_CStyleCastExpr::print(int indent, FILE *fp) { subexpr->print(0, fp); fprintf(fp, "f"); } else { // general case - fprintf(fp, "((%s) ", towhat); - //fprintf(fp, "\ntowhat '%s'\n", towhat ); - + fprintf(fp, "(%s) ", towhat); + if (subexpr->getPrec()<getPrec()) fprintf(fp,"("); if (subexpr->isVarDecl()) fprintf(fp, "%s", ((chillAST_VarDecl *) subexpr)->varname); else subexpr->print(indent, fp); - //fprintf(fp, "subexpr '%s' ", subexpr->getTypeString()); - fprintf(fp, ")"); + if (subexpr->getPrec()<getPrec()) fprintf(fp,")"); } fflush(fp); }; @@ -5653,9 +5651,23 @@ bool opInSet(const char* set,char* op) { } int chillAST_BinaryOperator::getPrec() { - for (int i = 0; i< 15;++i) - if (opInSet(binaryPrec[i],op)) return INT8_MAX+i; + for (int i = 0; i< 16;++i) + if (opInSet(binaryPrec[i],op)) return INT8_MAX+i+1; return INT8_MAX; } -// TODO add unary
\ No newline at end of file +const char* unaryPrec[] = { + "", + " -- ++ ", + " -- ++ + - ! ~ * & ", +}; + +int chillAST_UnaryOperator::getPrec() { + if (prefix) + for (int i = 2;i>=0;--i) + if (opInSet(unaryPrec[i],op)) return INT8_MAX+i+1; + else + for (int i = 0;i<3;--i) + if (opInSet(unaryPrec[i],op)) return INT8_MAX+i+1; + return INT8_MAX; +} |