From a504452b7b33025aef01da5a8766b4459ba6318e Mon Sep 17 00:00:00 2001 From: John Richards Date: Thu, 7 Oct 2021 01:51:53 -0400 Subject: [PATCH 1/2] Adds support for LIST_EXTEND opcode Addresses #199 --- ASTree.cpp | 27 +++++++++++++++++++++++++++ tests/compiled/list_extend.3.9.pyc | Bin 0 -> 273 bytes tests/input/list_extend.py | 8 ++++++++ tests/tokenized/list_extend.txt | 8 ++++++++ 4 files changed, 43 insertions(+) create mode 100644 tests/compiled/list_extend.3.9.pyc create mode 100644 tests/input/list_extend.py create mode 100644 tests/tokenized/list_extend.txt diff --git a/ASTree.cpp b/ASTree.cpp index ba31382..c92480e 100644 --- a/ASTree.cpp +++ b/ASTree.cpp @@ -1455,6 +1455,33 @@ PycRef BuildFromCode(PycRef code, PycModule* mod) } } break; + case Pyc::LIST_EXTEND_A: + { + PycRef rhs = stack.top(); + stack.pop(); + PycRef lhs = stack.top().cast(); + stack.pop(); + + if (rhs.type() != ASTNode::NODE_OBJECT) { + fprintf(stderr, "Unsupported argument found for LIST_EXTEND\n"); + break; + } + + // I've only ever seen this be a SMALL_TUPLE, but let's be careful... + PycRef obj = rhs.cast()->object(); + if (obj->type() != PycObject::TYPE_TUPLE && obj->type() != PycObject::TYPE_SMALL_TUPLE) { + fprintf(stderr, "Unsupported argument type found for LIST_EXTEND\n"); + break; + } + + ASTList::value_t result = lhs->values(); + for (const auto& it : obj.cast()->values()) { + result.push_back(new ASTObject(it)); + } + + stack.push(new ASTList(result)); + } + break; case Pyc::LOAD_ATTR_A: { PycRef name = stack.top(); diff --git a/tests/compiled/list_extend.3.9.pyc b/tests/compiled/list_extend.3.9.pyc new file mode 100644 index 0000000000000000000000000000000000000000..19fcbe6f902b6a42ce3b0186aced1dd8a75777c8 GIT binary patch literal 273 zcmYk0I}XAy42I*TFCJy+EwXfh1*t-upbJ|EP{AQWDi5_q0*Mo_asxK5l$9x0V8Uj? zR=z*}^~-XVBz-~kxG$?4>6?fD@@SaTYjc7TD4tNV0x6(}(s-qZzJXF=K=ifHKwe-( z8B#9fJo*cvA|)Nr62~i@Bq(WN;sPE+M)dPs%>N#PPb)Z8Sy)@xC|MMRikboW&5)50 zET9E#VQay>M3b`5_@wo&?N<8I=>ultY?_^}HqQ4)JlA^0r?ewN4BR1g3_g4TGvhT0 literal 0 HcmV?d00001 diff --git a/tests/input/list_extend.py b/tests/input/list_extend.py new file mode 100644 index 0000000..416a3c7 --- /dev/null +++ b/tests/input/list_extend.py @@ -0,0 +1,8 @@ +a = [1, 2, 3] +b = ['4', 5, 6] +c = [7, (8, 9, 10)] +d = [None] +e = [('a', 'b', 'c')] +f = [a, b] +g = [] +a = [None, None, None] diff --git a/tests/tokenized/list_extend.txt b/tests/tokenized/list_extend.txt new file mode 100644 index 0000000..9bb99d4 --- /dev/null +++ b/tests/tokenized/list_extend.txt @@ -0,0 +1,8 @@ +a = [ 1 , 2 , 3 ] +b = [ '4' , 5 , 6 ] +c = [ 7 , ( 8 , 9 , 10 ) ] +d = [ None ] +e = [ ( 'a' , 'b' , 'c' ) ] +f = [ a , b ] +g = [ ] +a = [ None , None , None ] From 15f2a4013e8d7ec953f219c7cedfb1b0223a83c8 Mon Sep 17 00:00:00 2001 From: John Richards Date: Thu, 7 Oct 2021 02:02:00 -0400 Subject: [PATCH 2/2] Adds an extra test case --- tests/compiled/list_extend.3.9.pyc | Bin 273 -> 312 bytes tests/input/list_extend.py | 1 + tests/tokenized/list_extend.txt | 1 + 3 files changed, 2 insertions(+) diff --git a/tests/compiled/list_extend.3.9.pyc b/tests/compiled/list_extend.3.9.pyc index 19fcbe6f902b6a42ce3b0186aced1dd8a75777c8..87e8208ee8721de5f15dc941473b013846f362a7 100644 GIT binary patch delta 174 zcmbQpw1Y`Ik(ZZ?0SM;z#3l9v>Bk@rGGGC69DumkYofNXObS~HdkRMiXF5|la};|z zLkia-#wdmqmS6@=?uj{0PDPABrA162f*DA>1nJgf_Ve@8iSmF5ULbLcG36Fx o>Mh2!Ta4+q7&BHf6p4VOL?+%;6A=KiI2b{Y4G1|HSs3{k0VIGOhyVZp delta 135 zcmdnNG?7U=k(ZZ?0SG2{#U<7N>Bk@rGGGF79DumkW}>z+Uphky+aks&h7^`y22J*f zRZiNP%zl1;n(Rf~Ks7}?Ac7Z2++s|*#h7}FG3^#(`bvf(A&|Jx#D8i+d_WclBM7nq JAqOK1BLEv@7S{j( diff --git a/tests/input/list_extend.py b/tests/input/list_extend.py index 416a3c7..3d3ebcc 100644 --- a/tests/input/list_extend.py +++ b/tests/input/list_extend.py @@ -5,4 +5,5 @@ d = [None] e = [('a', 'b', 'c')] f = [a, b] g = [] +h = [1, 2, [3, 4]] a = [None, None, None] diff --git a/tests/tokenized/list_extend.txt b/tests/tokenized/list_extend.txt index 9bb99d4..8d626ae 100644 --- a/tests/tokenized/list_extend.txt +++ b/tests/tokenized/list_extend.txt @@ -5,4 +5,5 @@ d = [ None ] e = [ ( 'a' , 'b' , 'c' ) ] f = [ a , b ] g = [ ] +h = [ 1 , 2 , [ 3 , 4 ] ] a = [ None , None , None ]