Skip to content

Commit a6ef2b2

Browse files
committed
give ModuleRootNodes a return value so that embedder use cases work more naturally
1 parent 580f91d commit a6ef2b2

File tree

6 files changed

+52
-40
lines changed

6 files changed

+52
-40
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/debug/PythonDebugTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.junit.Before;
5555
import org.junit.Test;
5656

57+
import com.oracle.graal.python.test.PythonTests;
5758
import com.oracle.truffle.api.debug.Breakpoint;
5859
import com.oracle.truffle.api.debug.DebugStackFrame;
5960
import com.oracle.truffle.api.debug.DebugValue;
@@ -70,6 +71,7 @@ public class PythonDebugTest {
7071
public void before() {
7172
Builder newBuilder = Context.newBuilder();
7273
newBuilder.allowAllAccess(true);
74+
PythonTests.closeContext();
7375
tester = new DebuggerTester(newBuilder);
7476
}
7577

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/grammar/TestParserTranslator.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ <T> T getChild(Node result, int num, Class<? extends T> klass) {
137137
}
138138
if (n instanceof ExpressionNode.ExpressionStatementNode) {
139139
n = n.getChildren().iterator().next();
140+
} else if (n instanceof ExpressionNode.ExpressionWithSideEffects) {
141+
n = n.getChildren().iterator().next();
140142
}
141143
assertTrue("Expected an instance of " + klass + ", got " + n.getClass(), klass.isInstance(n));
142144
return klass.cast(n);

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@
2626
package com.oracle.graal.python.nodes;
2727

2828
import com.oracle.graal.python.PythonLanguage;
29-
import com.oracle.graal.python.builtins.objects.PNone;
30-
import com.oracle.graal.python.nodes.statement.StatementNode;
29+
import com.oracle.graal.python.nodes.expression.ExpressionNode;
3130
import com.oracle.truffle.api.CompilerAsserts;
3231
import com.oracle.truffle.api.frame.FrameDescriptor;
3332
import com.oracle.truffle.api.frame.FrameSlot;
@@ -38,22 +37,21 @@ public class ModuleRootNode extends PClosureRootNode {
3837

3938
private final String name;
4039

41-
@Child private StatementNode body;
40+
@Child private ExpressionNode body;
4241

43-
public ModuleRootNode(PythonLanguage language, String name, StatementNode body, FrameDescriptor descriptor, FrameSlot[] freeVarSlots) {
42+
public ModuleRootNode(PythonLanguage language, String name, ExpressionNode file, FrameDescriptor descriptor, FrameSlot[] freeVarSlots) {
4443
super(language, descriptor, freeVarSlots);
4544
this.name = "<module '" + name + "'>";
46-
this.body = body;
45+
this.body = file;
4746
}
4847

4948
@Override
5049
public Object execute(VirtualFrame frame) {
5150
addClosureCellsToLocals(frame);
52-
body.executeVoid(frame);
53-
return PNone.NONE;
51+
return body.execute(frame);
5452
}
5553

56-
public PNode getBody() {
54+
public ExpressionNode getBody() {
5755
return body;
5856
}
5957

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,9 @@ public <T> T duplicate(Node orig, Class<T> clazz) {
128128
return (T) NodeUtil.cloneNode(orig);
129129
}
130130

131-
public ModuleRootNode createModuleRoot(String name, StatementNode body, FrameDescriptor fd) {
132-
return createModuleRoot(name, body, fd, null);
133-
}
134-
135-
public ModuleRootNode createModuleRoot(String name, StatementNode body, FrameDescriptor fd, FrameSlot[] freeVarSlots) {
136-
body.markAsRoot();
137-
return new ModuleRootNode(language, name, body, fd, freeVarSlots);
131+
public ModuleRootNode createModuleRoot(String name, ExpressionNode file, FrameDescriptor fd) {
132+
file.markAsRoot();
133+
return new ModuleRootNode(language, name, file, fd, null);
138134
}
139135

140136
public FunctionRootNode createFunctionRoot(SourceSection sourceSection, String functionName, boolean isGenerator, FrameDescriptor frameDescriptor, ExpressionNode body,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/expression/ExpressionNode.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ public void executeVoid(VirtualFrame frame) {
120120
public NodeCost getCost() {
121121
return NodeCost.NONE;
122122
}
123+
124+
public ExpressionNode getExpression() {
125+
return node;
126+
}
123127
}
124128

125129
/**

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

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,7 @@ private SourceSection deriveSourceSection(RuleNode node) {
226226
@Override
227227
public Object visitFile_input(Python3Parser.File_inputContext ctx) {
228228
environment.enterScope(ctx.scope);
229-
StatementNode file = asBlock(super.visitFile_input(ctx));
230-
deriveSourceSection(ctx, file);
229+
ExpressionNode file = asExpression(super.visitFile_input(ctx));
231230
environment.leaveScope();
232231
return factory.createModuleRoot(name, file, ctx.scope.getFrameDescriptor());
233232
}
@@ -247,8 +246,7 @@ public Object visitEval_input(Python3Parser.Eval_inputContext ctx) {
247246
@Override
248247
public Object visitSingle_input(Python3Parser.Single_inputContext ctx) {
249248
environment.enterScope(ctx.scope);
250-
StatementNode body = asBlock(super.visitSingle_input(ctx));
251-
deriveSourceSection(ctx, body);
249+
ExpressionNode body = asExpression(super.visitSingle_input(ctx));
252250
environment.leaveScope();
253251
if (isInlineMode) {
254252
return body;
@@ -1306,7 +1304,7 @@ private StatementNode makeIfElse(Python3Parser.If_stmtContext ctx, int i) {
13061304
if (suiteCount <= i) {
13071305
return factory.createBlock();
13081306
}
1309-
CastToBooleanNode test = factory.toBooleanCastNode(asBlockOrPNode(ctx.test(i).accept(this)));
1307+
CastToBooleanNode test = factory.toBooleanCastNode((PNode) ctx.test(i).accept(this));
13101308
StatementNode ifBody = asBlock(ctx.suite(i).accept(this));
13111309
StatementNode elseBody;
13121310
int testCount = ctx.test().size();
@@ -1733,16 +1731,6 @@ private static <T> List<T> asList(Object accept) {
17331731
}
17341732
}
17351733

1736-
protected PNode asBlockOrPNode(Object accept) {
1737-
if (accept == null) {
1738-
return EmptyNode.create();
1739-
} else if (accept instanceof PNode) {
1740-
return (PNode) accept;
1741-
} else {
1742-
return asBlock(accept);
1743-
}
1744-
}
1745-
17461734
private StatementNode asBlock(Object accept) {
17471735
if (accept == null) {
17481736
return factory.createBlock();
@@ -1761,21 +1749,43 @@ private StatementNode asBlock(Object accept) {
17611749
list.add(asBlock(node));
17621750
}
17631751
StatementNode block = factory.createBlock(list);
1764-
SourceSection sourceSection = inputList.get(0).getSourceSection();
1765-
SourceSection sourceSection2 = inputList.get(inputList.size() - 1).getSourceSection();
1766-
if (sourceSection != null && sourceSection2 != null) {
1767-
block.assignSourceSection(createSourceSection(sourceSection.getCharIndex(), sourceSection2.getCharEndIndex() - sourceSection.getCharIndex()));
1768-
} else if (sourceSection != null) {
1769-
block.assignSourceSection(sourceSection);
1770-
} else {
1771-
block.assignSourceSection(sourceSection2);
1772-
}
17731752
return block;
17741753
} else {
17751754
throw new IllegalArgumentException();
17761755
}
17771756
}
17781757

1758+
private ExpressionNode asExpression(Object accept) {
1759+
StatementNode moduleBlock = null;
1760+
if (accept instanceof List) {
1761+
@SuppressWarnings("unchecked")
1762+
List<PNode> list = (List<PNode>) accept;
1763+
if (list.size() > 0) {
1764+
ExpressionNode asExpression = asExpression(list.remove(list.size() - 1));
1765+
StatementNode writeReturnValue = factory.createWriteLocal(asExpression, environment.getReturnSlot());
1766+
writeReturnValue.assignSourceSection(asExpression.getSourceSection());
1767+
list.add(writeReturnValue);
1768+
}
1769+
moduleBlock = asBlock(accept);
1770+
} else if (accept instanceof ExpressionNode.ExpressionStatementNode) {
1771+
moduleBlock = factory.createWriteLocal(((ExpressionNode.ExpressionStatementNode) accept).getExpression(), environment.getReturnSlot());
1772+
moduleBlock.assignSourceSection(((ExpressionNode.ExpressionStatementNode) accept).getSourceSection());
1773+
} else if (accept instanceof ExpressionNode) {
1774+
moduleBlock = factory.createWriteLocal((ExpressionNode) accept, environment.getReturnSlot());
1775+
moduleBlock.assignSourceSection(((ExpressionNode) accept).getSourceSection());
1776+
} else if (accept instanceof StatementNode) {
1777+
moduleBlock = factory.createWriteLocal(EmptyNode.create().withSideEffect((StatementNode) accept), environment.getReturnSlot());
1778+
} else if (accept == null) {
1779+
return EmptyNode.create();
1780+
}
1781+
ExpressionNode readReturn = factory.createReadLocal(environment.getReturnSlot());
1782+
if (moduleBlock != null) {
1783+
return readReturn.withSideEffect(moduleBlock);
1784+
} else {
1785+
return readReturn;
1786+
}
1787+
}
1788+
17791789
protected ExpressionNode asClassBody(Object accept, String qualName) {
17801790
List<PNode> body = asList(accept);
17811791
if (body.size() > 0 && body.get(0) instanceof StringLiteralNode) {
@@ -1824,7 +1834,7 @@ private StatementNode createGeneratorExpression(Python3Parser.Comp_forContext co
18241834
ExpressionNode condition = null;
18251835
Python3Parser.Comp_iterContext comp_iter = comp_for.comp_iter();
18261836
while (comp_iter != null && comp_iter.comp_if() != null) {
1827-
ExpressionNode nextIf = (ExpressionNode) asBlockOrPNode(comp_iter.comp_if().test_nocond().accept(this));
1837+
ExpressionNode nextIf = (ExpressionNode) comp_iter.comp_if().test_nocond().accept(this);
18281838
if (condition == null) {
18291839
condition = nextIf;
18301840
} else {

0 commit comments

Comments
 (0)