From f939f4bacb23cfe0c4fb96c5d71f30412fa828d7 Mon Sep 17 00:00:00 2001 From: Tuowen Zhao Date: Sat, 24 Sep 2016 00:04:39 -0600 Subject: binary op precedence --- include/chillAST/chillAST_node.hh | 2 +- include/chillAST/chillASTs.hh | 2 ++ src/chillASTs.cc | 41 ++++++++++++++++++++++++++++++++++----- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/include/chillAST/chillAST_node.hh b/include/chillAST/chillAST_node.hh index ed155b2..808a62c 100644 --- a/include/chillAST/chillAST_node.hh +++ b/include/chillAST/chillAST_node.hh @@ -38,7 +38,7 @@ public: //! the type of this current node virtual CHILLAST_NODE_TYPE getType() {return CHILLAST_NODE_UNKNOWN;}; //! the precedence of the current node, 0 being the highest - virtual int getPrec() {return INT16_MAX;} + virtual int getPrec() {return INT8_MAX;} bool isSourceFile() { return (getType() == CHILLAST_NODE_SOURCEFILE); }; diff --git a/include/chillAST/chillASTs.hh b/include/chillAST/chillASTs.hh index fb27373..2c7006d 100644 --- a/include/chillAST/chillASTs.hh +++ b/include/chillAST/chillASTs.hh @@ -874,6 +874,7 @@ public: class chillAST_TernaryOperator : public chillAST_Node { public: + virtual int getPrec() {return INT8_MAX+15;} virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_TERNARYOPERATOR;} // variables that are special for this type of node char *op; // TODO need enum so far, only "?" conditional operator @@ -955,6 +956,7 @@ public: class chillAST_BinaryOperator : public chillAST_Node { public: + virtual int getPrec(); virtual CHILLAST_NODE_TYPE getType(){return CHILLAST_NODE_BINARYOPERATOR;} // variables that are special for this type of node char *op; // TODO need enum diff --git a/src/chillASTs.cc b/src/chillASTs.cc index 29bc59a..170e481 100644 --- a/src/chillASTs.cc +++ b/src/chillASTs.cc @@ -1923,11 +1923,11 @@ void chillAST_BinaryOperator::dump(int indent, FILE *fp) { fflush(fp); } -void chillAST_BinaryOperator::print(int indent, FILE *fp) { // TODO this needparens logic is wrong +void chillAST_BinaryOperator::print(int indent, FILE *fp) { printPreprocBEFORE(indent, fp); chillindent(indent, fp); - bool needparens = getPrec()<=lhs->getPrec(); + bool needparens = getPrec()getPrec(); if (needparens) fprintf(fp, "("); if (lhs) lhs->print(0, fp); @@ -5620,11 +5620,42 @@ void chillAST_Preprocessing::print(int indent, FILE *fp) { // probably very wro if (position == CHILLAST_PREPROCESSING_LINEBEFORE) { - //fprintf(fp, "\n"); // comment seems to have \n at the end already - //chillindent(indent, fp); } - //if (pptype != CHILLAST_PREPROCESSING_IMMEDIATELYBEFORE && pptype != CHILLAST_PREPROCESSING_UNKNOWN) fprint(fp, "\n"); } + +//! I'm just a bit lazy to write ifs ... +const char* binaryPrec[] = { + " :: ", + " . -> ", + "", + " .* ->* ", + " * / % ", + " + - ", + " << >> ", + " < <= > >=", + " == != ", + " & ", + " ^ ", + " | ", + " && ", + " || ", + " = += -= *= /= %= <<= >>= &= ^= |= ", + " , " +}; + +bool opInSet(const char* set,char* op) { + string tmp = op; + tmp=" "+tmp+" "; + return strstr(set, tmp.c_str()) != NULL; +} + +int chillAST_BinaryOperator::getPrec() { + for (int i = 0; i< 15;++i) + if (opInSet(binaryPrec[i],op)) return INT8_MAX+i; + return INT8_MAX; +} + +// TODO add unary \ No newline at end of file -- cgit v1.2.3-70-g09d2