Merge pull request #186 from Aralox/Issue-185-chained-assignment

Support chained assignment statements, e.g. `a = b = c`
This commit is contained in:
Michael Hansen
2020-10-26 08:40:50 -07:00
committed by GitHub
7 changed files with 200 additions and 14 deletions

View File

@@ -16,7 +16,7 @@ public:
NODE_CONVERT, NODE_KEYWORD, NODE_RAISE, NODE_EXEC, NODE_BLOCK,
NODE_COMPREHENSION, NODE_LOADBUILDCLASS, NODE_AWAITABLE,
NODE_FORMATTEDVALUE, NODE_JOINEDSTR, NODE_CONST_MAP,
NODE_ANNOTATED_VAR,
NODE_ANNOTATED_VAR, NODE_CHAINSTORE,
// Empty node types
NODE_LOCALS,
@@ -71,11 +71,27 @@ public:
void removeLast();
void append(PycRef<ASTNode> node) { m_nodes.emplace_back(std::move(node)); }
protected:
ASTNodeList(list_t nodes, ASTNode::Type type)
: ASTNode(type), m_nodes(std::move(nodes)) { }
private:
list_t m_nodes;
};
class ASTChainStore : public ASTNodeList {
public:
ASTChainStore(list_t nodes, PycRef<ASTNode> src)
: ASTNodeList(nodes, NODE_CHAINSTORE), m_src(std::move(src)) { }
PycRef<ASTNode> src() const { return m_src; }
private:
PycRef<ASTNode> m_src;
};
class ASTObject : public ASTNode {
public:
ASTObject(PycRef<PycObject> obj)

View File

@@ -12,6 +12,9 @@
// NOTE: Nested f-strings not supported.
#define F_STRING_QUOTE "'''"
static void append_to_chain_store(PycRef<ASTNode> chainStore, PycRef<ASTNode> item,
FastStack& stack, PycRef<ASTBlock> curblock);
/* Use this to determine if an error occurred (and therefore, if we should
* avoid cleaning the output tree) */
static bool cleanBuild;
@@ -288,6 +291,9 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}
stack.push(map);
} else {
if (stack.top().type() == ASTNode::NODE_CHAINSTORE) {
stack.pop();
}
stack.push(new ASTMap());
}
break;
@@ -679,7 +685,20 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
}
break;
case Pyc::DUP_TOP:
stack.push(stack.top());
{
if (stack.top().type() == PycObject::TYPE_NULL) {
stack.push(stack.top());
} else if (stack.top().type() == ASTNode::NODE_CHAINSTORE) {
auto chainstore = stack.top();
stack.pop();
stack.push(stack.top());
stack.push(chainstore);
} else {
stack.push(stack.top());
ASTNodeList::list_t targets;
stack.push(new ASTChainStore(targets, stack.top()));
}
}
break;
case Pyc::DUP_TOP_TWO:
{
@@ -791,6 +810,9 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
break;
case Pyc::EXEC_STMT:
{
if (stack.top().type() == ASTNode::NODE_CHAINSTORE) {
stack.pop();
}
PycRef<ASTNode> loc = stack.top();
stack.pop();
PycRef<ASTNode> glob = stack.top();
@@ -1725,6 +1747,9 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
{
PycRef<ASTNode> one = stack.top();
stack.pop();
if (stack.top().type() == ASTNode::NODE_CHAINSTORE) {
stack.pop();
}
PycRef<ASTNode> two = stack.top();
stack.pop();
@@ -1738,6 +1763,9 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.pop();
PycRef<ASTNode> two = stack.top();
stack.pop();
if (stack.top().type() == ASTNode::NODE_CHAINSTORE) {
stack.pop();
}
PycRef<ASTNode> three = stack.top();
stack.pop();
stack.push(one);
@@ -1753,6 +1781,9 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.pop();
PycRef<ASTNode> three = stack.top();
stack.pop();
if (stack.top().type() == ASTNode::NODE_CHAINSTORE) {
stack.pop();
}
PycRef<ASTNode> four = stack.top();
stack.pop();
stack.push(one);
@@ -1889,8 +1920,11 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.pop();
PycRef<ASTNode> seq = stack.top();
stack.pop();
curblock->append(new ASTStore(seq, tup));
if (seq.type() == ASTNode::NODE_CHAINSTORE) {
append_to_chain_store(seq, tup, stack, curblock);
} else {
curblock->append(new ASTStore(seq, tup));
}
}
} else {
PycRef<ASTNode> name = stack.top();
@@ -1898,8 +1932,11 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
PycRef<ASTNode> value = stack.top();
stack.pop();
PycRef<ASTNode> attr = new ASTBinary(name, new ASTName(code->getName(operand)), ASTBinary::BIN_ATTR);
curblock->append(new ASTStore(value, attr));
if (value.type() == ASTNode::NODE_CHAINSTORE) {
append_to_chain_store(value, attr, stack, curblock);
} else {
curblock->append(new ASTStore(value, attr));
}
}
}
break;
@@ -1919,13 +1956,22 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
PycRef<ASTNode> seq = stack.top();
stack.pop();
curblock->append(new ASTStore(seq, tup));
if (seq.type() == ASTNode::NODE_CHAINSTORE) {
append_to_chain_store(seq, tup, stack, curblock);
} else {
curblock->append(new ASTStore(seq, tup));
}
}
} else {
PycRef<ASTNode> value = stack.top();
stack.pop();
PycRef<ASTNode> name = new ASTName(code->getCellVar(operand));
curblock->append(new ASTStore(value, name));
if (value.type() == ASTNode::NODE_CHAINSTORE) {
append_to_chain_store(value, name, stack, curblock);
} else {
curblock->append(new ASTStore(value, name));
}
}
}
break;
@@ -1956,6 +2002,8 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
if (tuple != NULL)
tuple->setRequireParens(false);
curblock.cast<ASTIterBlock>()->setIndex(tup);
} else if (seq.type() == ASTNode::NODE_CHAINSTORE) {
append_to_chain_store(seq, tup, stack, curblock);
} else {
curblock->append(new ASTStore(seq, tup));
}
@@ -1983,6 +2031,8 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
&& !curblock->inited()) {
curblock.cast<ASTWithBlock>()->setExpr(value);
curblock.cast<ASTWithBlock>()->setVar(name);
} else if (value.type() == ASTNode::NODE_CHAINSTORE) {
append_to_chain_store(value, name, stack, curblock);
} else {
curblock->append(new ASTStore(value, name));
}
@@ -2011,6 +2061,8 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
if (tuple != NULL)
tuple->setRequireParens(false);
curblock.cast<ASTIterBlock>()->setIndex(tup);
} else if (seq.type() == ASTNode::NODE_CHAINSTORE) {
append_to_chain_store(seq, tup, stack, curblock);
} else {
curblock->append(new ASTStore(seq, tup));
}
@@ -2018,7 +2070,11 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
} else {
PycRef<ASTNode> value = stack.top();
stack.pop();
curblock->append(new ASTStore(value, name));
if (value.type() == ASTNode::NODE_CHAINSTORE) {
append_to_chain_store(value, name, stack, curblock);
} else {
curblock->append(new ASTStore(value, name));
}
}
/* Mark the global as used */
@@ -2047,6 +2103,8 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
if (tuple != NULL)
tuple->setRequireParens(false);
curblock.cast<ASTIterBlock>()->setIndex(tup);
} else if (seq.type() == ASTNode::NODE_CHAINSTORE) {
append_to_chain_store(seq, tup, stack, curblock);
} else {
curblock->append(new ASTStore(seq, tup));
}
@@ -2080,6 +2138,8 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
&& !curblock->inited()) {
curblock.cast<ASTWithBlock>()->setExpr(value);
curblock.cast<ASTWithBlock>()->setVar(name);
} else if (value.type() == ASTNode::NODE_CHAINSTORE) {
append_to_chain_store(value, name, stack, curblock);
} else {
curblock->append(new ASTStore(value, name));
@@ -2157,8 +2217,11 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
stack.pop();
PycRef<ASTNode> seq = stack.top();
stack.pop();
curblock->append(new ASTStore(seq, tup));
if (seq.type() == ASTNode::NODE_CHAINSTORE) {
append_to_chain_store(seq, tup, stack, curblock);
} else {
curblock->append(new ASTStore(seq, tup));
}
}
} else {
PycRef<ASTNode> subscr = stack.top();
@@ -2189,6 +2252,8 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
} else {
if (dest.type() == ASTNode::NODE_MAP) {
dest.cast<ASTMap>()->add(subscr, src);
} else if (src.type() == ASTNode::NODE_CHAINSTORE) {
append_to_chain_store(src, new ASTSubscr(dest, subscr), stack, curblock);
} else {
curblock->append(new ASTStore(src, new ASTSubscr(dest, subscr)));
}
@@ -2255,6 +2320,10 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
&& !curblock->inited()) {
tup->setRequireParens(true);
curblock.cast<ASTIterBlock>()->setIndex(tup);
} else if (stack.top().type() == ASTNode::NODE_CHAINSTORE) {
auto chainStore = stack.top();
stack.pop();
append_to_chain_store(chainStore, tup, stack, curblock);
} else {
curblock->append(new ASTStore(stack.top(), tup));
stack.pop();
@@ -2319,6 +2388,18 @@ PycRef<ASTNode> BuildFromCode(PycRef<PycCode> code, PycModule* mod)
return new ASTNodeList(defblock->nodes());
}
static void append_to_chain_store(PycRef<ASTNode> chainStore, PycRef<ASTNode> item,
FastStack& stack, PycRef<ASTBlock> curblock)
{
stack.pop(); // ignore identical source object.
chainStore.cast<ASTChainStore>()->append(item);
if (stack.top().type() == PycObject::TYPE_NULL) {
curblock->append(chainStore);
} else {
stack.push(chainStore);
}
}
static int cmp_prec(PycRef<ASTNode> parent, PycRef<ASTNode> child)
{
/* Determine whether the parent has higher precedence than therefore
@@ -3028,6 +3109,15 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
}
}
break;
case ASTNode::NODE_CHAINSTORE:
{
for (auto& dest : node.cast<ASTChainStore>()->nodes()) {
print_src(dest, mod);
fputs(" = ", pyc_output);
}
print_src(node.cast<ASTChainStore>()->src(), mod);
}
break;
case ASTNode::NODE_SUBSCR:
{
print_src(node.cast<ASTSubscr>()->name(), mod);

Binary file not shown.

Binary file not shown.

View File

@@ -0,0 +1,39 @@
a = [y, z] = x = (k1, k2, k3) = [] = c = myfunc(x) + 3
x = y = g = {keyA: X}
global store_global
Gx = Gy = Gz = Gq1
Gx = [Gy, Gz] = Gq2
a = b = store_global = c
def func_with_global():
global Gx, Gy, Gz, Gq
Gx = Gy = Gz = Gq
y = store_subscr[0] = x
a[0] = b[x] = c[3] = D[4]
a[0] = (b[x], c[3]) = D[4]
a[0] = Q = [b[x], c[3]] = F = D[4]
q = v = arr[a:b:c] = x
class store_attr1:
def __init__(self, a,b,c):
self.a = self.b = self.c = x
self.d = y
class store_attr2:
def __init__(self, a,b,c): self.a = (self.b, self.c) = x
a.b = c.d = e.f + g.h
def store_deref():
a = I
a = b = c = R1
a = (b, c) = R2
def store_fast():
x = a
y = b
z = c
p = q = r = s
p = [q, r] = s

View File

@@ -0,0 +1,43 @@
a = ( y , z ) = x = ( k1 , k2 , k3 ) = ( ) = c = myfunc ( x ) + 3 <EOL>
x = y = g = { keyA : X } <EOL>
Gx = Gy = Gz = Gq1 <EOL>
Gx = ( Gy , Gz ) = Gq2 <EOL>
a = b = store_global = c <EOL>
def func_with_global ( ) : <EOL>
<INDENT>
global Gx , Gy , Gz <EOL>
Gx = Gy = Gz = Gq <EOL>
<OUTDENT>
y = store_subscr [ 0 ] = x <EOL>
a [ 0 ] = b [ x ] = c [ 3 ] = D [ 4 ] <EOL>
a [ 0 ] = ( b [ x ] , c [ 3 ] ) = D [ 4 ] <EOL>
a [ 0 ] = Q = ( b [ x ] , c [ 3 ] ) = F = D [ 4 ] <EOL>
q = v = arr [ a : b : c ] = x <EOL>
class store_attr1 : <EOL>
<INDENT>
def __init__ ( self , a , b , c ) : <EOL>
<INDENT>
self . a = self . b = self . c = x <EOL>
self . d = y <EOL>
<OUTDENT>
<OUTDENT>
class store_attr2 : <EOL>
<INDENT>
def __init__ ( self , a , b , c ) : <EOL>
<INDENT>
self . a = ( self . b , self . c ) = x <EOL>
<OUTDENT>
<OUTDENT>
a . b = c . d = e . f + g . h <EOL>
def store_deref ( ) : <EOL>
<INDENT>
a = I <EOL>
a = b = c = R1 <EOL>
a = ( b , c ) = R2 <EOL>
def store_fast ( ) : <EOL>
<INDENT>
x = a <EOL>
y = b <EOL>
z = c <EOL>
p = q = r = s <EOL>
p = ( q , r ) = s <EOL>

View File

@@ -1,8 +1,6 @@
var1 = 'x' <EOL>
var2 = 'y' <EOL>
x = 1.23456 <EOL>
s1 = 1.23456 <EOL>
var3 = 1.23456 <EOL>
x = s1 = var3 = 1.23456 <EOL>
a = 15 <EOL>
some_dict = { } <EOL>
some_dict [ 2 ] = 3 <EOL>