Skip to content

Commit a786437

Browse files
committed
[GR-9160] Change scope parsing to single-pass-and-fixup
PullRequest: graalpython/151
2 parents 41887ed + 890585e commit a786437

File tree

18 files changed

+2151
-2248
lines changed

18 files changed

+2151
-2248
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
2+
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3+
#
4+
# The Universal Permissive License (UPL), Version 1.0
5+
#
6+
# Subject to the condition set forth below, permission is hereby granted to any
7+
# person obtaining a copy of this software, associated documentation and/or
8+
# data (collectively the "Software"), free of charge and under any and all
9+
# copyright rights in the Software, and any and all patent rights owned or
10+
# freely licensable by each licensor hereunder covering either (i) the
11+
# unmodified Software as contributed to or provided by such licensor, or (ii)
12+
# the Larger Works (as defined below), to deal in both
13+
#
14+
# (a) the Software, and
15+
#
16+
# (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
17+
# one is included with the Software each a "Larger Work" to which the Software
18+
# is contributed by such licensors),
19+
#
20+
# without restriction, including without limitation the rights to copy, create
21+
# derivative works of, display, perform, and distribute the Software and make,
22+
# use, sell, offer for sale, import, export, have made, and have sold the
23+
# Software and the Larger Work(s), and to sublicense the foregoing rights on
24+
# either these or other terms.
25+
#
26+
# This license is subject to the following condition:
27+
#
28+
# The above copyright notice and either this complete permission notice or at a
29+
# minimum a reference to the UPL must be included in all copies or substantial
30+
# portions of the Software.
31+
#
32+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
33+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
34+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
35+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
36+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
37+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38+
# SOFTWARE.
39+
40+
def test_lambda_simple():
41+
l = lambda x : x * 2
42+
v = l(5)
43+
assert v == 10
44+
45+
def test_lambda_condition_special():
46+
v = [a for a in (1,2,3) if lambda x : x*2]
47+
assert v == [1,2,3]
48+

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/NodeFactory.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
import java.math.BigInteger;
2929
import java.util.List;
30-
import java.util.Set;
3130

3231
import com.oracle.graal.python.PythonLanguage;
3332
import com.oracle.graal.python.builtins.objects.complex.PComplex;
@@ -274,7 +273,7 @@ public PNode createListLiteral(PNode[] values) {
274273
return ListLiteralNode.create(values);
275274
}
276275

277-
public PNode createSetLiteral(Set<PNode> values) {
276+
public PNode createSetLiteral(List<PNode> values) {
278277
PNode[] convertedValues = values.toArray(new PNode[values.size()]);
279278
return new SetLiteralNode(convertedValues);
280279
}
@@ -524,7 +523,7 @@ public PNode createSetAttribute(PNode object, String key, PNode rhs) {
524523
return SetAttributeNode.create(key, object, rhs);
525524
}
526525

527-
public PNode createDestructuringAssignment(PNode rhs, List<ReadNode> slots, int starredIndex, PNode[] assignments) {
526+
public PNode createDestructuringAssignment(PNode rhs, ReadNode[] slots, int starredIndex, PNode[] assignments) {
528527
return DestructuringAssignmentNode.create(rhs, slots, starredIndex, assignments);
529528
}
530529

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/PNode.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
@ImportStatic({PGuards.class, PythonOptions.class, SpecialMethodNames.class, SpecialAttributeNames.class, BuiltinNames.class})
4646
@GenerateWrapper
4747
public abstract class PNode extends PBaseNode implements InstrumentableNode {
48+
49+
public static final PNode[] EMPTY_ARRAY = new PNode[0];
50+
4851
@CompilationFinal private SourceSection sourceSection;
4952
@CompilationFinal private boolean isStmt = false;
5053
@CompilationFinal private boolean isRoot = false;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/DestructuringAssignmentNode.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SyntaxError;
3333

3434
import java.util.Arrays;
35-
import java.util.List;
3635

3736
import com.oracle.graal.python.builtins.modules.BuiltinFunctions;
3837
import com.oracle.graal.python.builtins.modules.BuiltinFunctionsFactory;
@@ -68,18 +67,18 @@ public final class DestructuringAssignmentNode extends PNode implements WriteNod
6867
private final ConditionProfile errorProfile2 = ConditionProfile.createBinaryProfile();
6968
private final int starredIndex;
7069

71-
public DestructuringAssignmentNode(PNode rhs, List<ReadNode> slots, int starredIndex, PNode[] assignments) {
70+
public DestructuringAssignmentNode(PNode rhs, ReadNode[] slots, int starredIndex, PNode[] assignments) {
7271
this.rhs = rhs;
7372
this.starredIndex = starredIndex;
7473
this.assignments = assignments;
75-
this.slots = new WriteNode[slots.size()];
76-
for (int i = 0; i < slots.size(); i++) {
77-
this.slots[i] = (WriteNode) slots.get(i).makeWriteNode(null);
74+
this.slots = new WriteNode[slots.length];
75+
for (int i = 0; i < slots.length; i++) {
76+
this.slots[i] = (WriteNode) slots[i].makeWriteNode(null);
7877
}
7978
this.lenNode = starredIndex == -1 ? null : BuiltinFunctionsFactory.LenNodeFactory.create();
8079
}
8180

82-
public static PNode create(PNode rhs, List<ReadNode> slots, int starredIndex, PNode[] assignments) {
81+
public static PNode create(PNode rhs, ReadNode[] slots, int starredIndex, PNode[] assignments) {
8382
return new DestructuringAssignmentNode(rhs, slots, starredIndex, assignments);
8483
}
8584

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/frame/FrameSlotIDs.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,4 @@
2727

2828
public abstract class FrameSlotIDs {
2929
public static final String RETURN_SLOT_ID = "<return_val>";
30-
public static final String LIST_COMPREHENSION_SLOT_ID = "<list_comp_val>";
3130
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/BuiltinFunctionRootNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public Object execute(VirtualFrame frame) {
145145
}
146146

147147
public BuiltinFunctionRootNode(PythonLanguage language, Builtin builtin, NodeFactory<? extends PythonBuiltinBaseNode> factory, boolean declaresExplicitSelf) {
148-
super(language, null);
148+
super(language);
149149
this.builtin = builtin;
150150
this.factory = factory;
151151
this.declaresExplicitSelf = declaresExplicitSelf;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/AssignmentTranslator.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,10 @@ public class AssignmentTranslator extends Python3BaseVisitor<PNode> {
5959

6060
private final NodeFactory factory;
6161
private final TranslationEnvironment environment;
62-
private final PythonBaseTreeTranslator<?> translator;
62+
private final PythonTreeTranslator translator;
6363
private final PythonCore core;
6464

65-
public AssignmentTranslator(PythonCore core, TranslationEnvironment environment, PythonBaseTreeTranslator<?> translator) {
65+
public AssignmentTranslator(PythonCore core, TranslationEnvironment environment, PythonTreeTranslator translator) {
6666
this.core = core;
6767
this.factory = core.getLanguage().getNodeFactory();
6868
this.environment = environment;
@@ -103,37 +103,37 @@ private PNode createAssignment(PNode lhs, PNode rhs) {
103103
}
104104

105105
private PNode createDestructuringAssignment(PNode[] leftHandSides, PNode rhs) {
106-
List<PNode> statements = new ArrayList<>();
107-
List<ReadNode> temps = new ArrayList<>();
106+
PNode[] statements = new PNode[leftHandSides.length];
107+
ReadNode[] temps = new ReadNode[leftHandSides.length];
108108
int starredIndex = -1;
109109
for (int i = 0; i < leftHandSides.length; i++) {
110110
ReadNode tempRead = environment.makeTempLocalVariable();
111-
temps.add(tempRead);
111+
temps[i] = tempRead;
112112
if (leftHandSides[i] instanceof StarredExpressionNode) {
113113
if (starredIndex != -1) {
114114
throw core.raise(SyntaxError, "two starred expressions in assignment");
115115
}
116116
starredIndex = i;
117-
statements.add(createAssignment(((StarredExpressionNode) leftHandSides[i]).getValue(), (PNode) tempRead));
117+
statements[i] = createAssignment(((StarredExpressionNode) leftHandSides[i]).getValue(), (PNode) tempRead);
118118
} else {
119-
statements.add(createAssignment(leftHandSides[i], (PNode) tempRead));
119+
statements[i] = createAssignment(leftHandSides[i], (PNode) tempRead);
120120
}
121121
}
122-
return factory.createDestructuringAssignment(rhs, temps, starredIndex, statements.toArray(new PNode[0]));
122+
return factory.createDestructuringAssignment(rhs, temps, starredIndex, statements);
123123
}
124124

125125
private PNode createMultiAssignment(List<NormassignContext> normassign, PNode mostRhs, PNode mostLhs) {
126126
ReadNode tmp = environment.makeTempLocalVariable();
127127
PNode tmpWrite = tmp.makeWriteNode(mostRhs);
128-
List<PNode> assignments = new ArrayList<>();
129-
assignments.add(tmpWrite);
130-
assignments.add(createAssignment(mostLhs, (PNode) tmp));
128+
PNode[] assignments = new PNode[normassign.size() + 1];
129+
assignments[0] = tmpWrite;
130+
assignments[1] = createAssignment(mostLhs, (PNode) tmp);
131131
for (int i = 0; i < normassign.size() - 1; i++) {
132132
NormassignContext normassignContext = normassign.get(i);
133133
if (normassignContext.yield_expr() != null) {
134134
throw core.raise(SyntaxError, "assignment to yield expression not possible");
135135
}
136-
assignments.add(createAssignment(normassignContext.accept(this), (PNode) tmp));
136+
assignments[i + 2] = createAssignment(normassignContext.accept(this), (PNode) tmp);
137137
}
138138
return factory.createBlock(assignments);
139139
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/InlineScopeTranslator.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

0 commit comments

Comments
 (0)