summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTuowen Zhao <ztuowen@gmail.com>2016-09-24 00:04:39 -0600
committerTuowen Zhao <ztuowen@gmail.com>2016-09-24 00:04:39 -0600
commitf939f4bacb23cfe0c4fb96c5d71f30412fa828d7 (patch)
treebaa140cc0ca283d08bda6c433610c09fc741b8da
parentddf5a43a66a91009e7fa33a689aac45e73c4dc97 (diff)
downloadchill-f939f4bacb23cfe0c4fb96c5d71f30412fa828d7.tar.gz
chill-f939f4bacb23cfe0c4fb96c5d71f30412fa828d7.tar.bz2
chill-f939f4bacb23cfe0c4fb96c5d71f30412fa828d7.zip
binary op precedence
-rw-r--r--include/chillAST/chillAST_node.hh2
-rw-r--r--include/chillAST/chillASTs.hh2
-rw-r--r--src/chillASTs.cc41
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()<lhs->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