From 84d99790b9d82478dc9d17a4b9d02e0ac7370b6a Mon Sep 17 00:00:00 2001 From: Jason Orendorff Date: Thu, 3 Mar 2011 15:08:01 -0600 Subject: [PATCH] Bug 630213 - ASTSerializer::leftAssociate right-associates. r=dherman. --HG-- extra : rebase_source : 18bbf8f963f4c76b1fe6d4e7acc9aa6b33381b73 --- js/src/jsreflect.cpp | 41 +++++-------------- .../tests/js1_8_5/extensions/reflect-parse.js | 5 ++- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/js/src/jsreflect.cpp b/js/src/jsreflect.cpp index ec2a154df10..d3db7da792c 100644 --- a/js/src/jsreflect.cpp +++ b/js/src/jsreflect.cpp @@ -2320,53 +2320,34 @@ ASTSerializer::leftAssociate(JSParseNode *pn, Value *dst) const size_t len = pn->pn_count; JS_ASSERT(len >= 1); - if (len == 1) - return expression(pn->pn_head, dst); - - JS_ASSERT(len >= 2); - - Vector list(cx); - if (!list.reserve(len)) - return false; - - for (JSParseNode *next = pn->pn_head; next; next = next->pn_next) { - JS_ALWAYS_TRUE(list.append(next)); /* space check above */ - } - TokenKind tk = PN_TYPE(pn); - bool lor = tk == TOK_OR; bool logop = lor || (tk == TOK_AND); - Value right; - - if (!expression(list[len - 1], &right)) + JSParseNode *head = pn->pn_head; + Value left; + if (!expression(head, &left)) return false; - - size_t i = len - 2; - - do { - JSParseNode *next = list[i]; - - Value left; - if (!expression(next, &left)) + for (JSParseNode *next = head->pn_next; next; next = next->pn_next) { + Value right; + if (!expression(next, &right)) return false; - TokenPos subpos = { next->pn_pos.begin, pn->pn_pos.end }; + TokenPos subpos = { pn->pn_pos.begin, next->pn_pos.end }; if (logop) { - if (!builder.logicalExpression(lor, left, right, &subpos, &right)) + if (!builder.logicalExpression(lor, left, right, &subpos, &left)) return false; } else { BinaryOperator op = binop(PN_TYPE(pn), PN_OP(pn)); LOCAL_ASSERT(op > BINOP_ERR && op < BINOP_LIMIT); - if (!builder.binaryExpression(op, left, right, &subpos, &right)) + if (!builder.binaryExpression(op, left, right, &subpos, &left)) return false; } - } while (i-- != 0); + } - *dst = right; + *dst = left; return true; } diff --git a/js/src/tests/js1_8_5/extensions/reflect-parse.js b/js/src/tests/js1_8_5/extensions/reflect-parse.js index 97bbc76e946..9133657c3e4 100644 --- a/js/src/tests/js1_8_5/extensions/reflect-parse.js +++ b/js/src/tests/js1_8_5/extensions/reflect-parse.js @@ -251,8 +251,9 @@ assertExpr("(x << y)", binExpr("<<", ident("x"), ident("y"))); assertExpr("(x >> y)", binExpr(">>", ident("x"), ident("y"))); assertExpr("(x >>> y)", binExpr(">>>", ident("x"), ident("y"))); assertExpr("(x + y)", binExpr("+", ident("x"), ident("y"))); -assertExpr("(w + x + y + z)", binExpr("+", ident("w"), binExpr("+", ident("x", binExpr("+", ident("y"), ident("z")))))) +assertExpr("(w + x + y + z)", binExpr("+", binExpr("+", binExpr("+", ident("w"), ident("x")), ident("y")), ident("z"))); assertExpr("(x - y)", binExpr("-", ident("x"), ident("y"))); +assertExpr("(w - x - y - z)", binExpr("-", binExpr("-", binExpr("-", ident("w"), ident("x")), ident("y")), ident("z"))); assertExpr("(x * y)", binExpr("*", ident("x"), ident("y"))); assertExpr("(x / y)", binExpr("/", ident("x"), ident("y"))); assertExpr("(x % y)", binExpr("%", ident("x"), ident("y"))); @@ -275,7 +276,7 @@ assertExpr("(x ^= y)", aExpr("^=", ident("x"), ident("y"))); assertExpr("(x &= y)", aExpr("&=", ident("x"), ident("y"))); assertExpr("(x || y)", logExpr("||", ident("x"), ident("y"))); assertExpr("(x && y)", logExpr("&&", ident("x"), ident("y"))); -assertExpr("(w || x || y || z)", logExpr("||", ident("w"), logExpr("||", ident("x", logExpr("||", ident("y"), ident("z")))))) +assertExpr("(w || x || y || z)", logExpr("||", logExpr("||", logExpr("||", ident("w"), ident("x")), ident("y")), ident("z"))) assertExpr("(x ? y : z)", condExpr(ident("x"), ident("y"), ident("z"))); assertExpr("(x,y)", seqExpr([ident("x"),ident("y")])) assertExpr("(x,y,z)", seqExpr([ident("x"),ident("y"),ident("z")]))