@@ -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