@@ -226,8 +226,7 @@ private SourceSection deriveSourceSection(RuleNode node) {
226
226
@ Override
227
227
public Object visitFile_input (Python3Parser .File_inputContext ctx ) {
228
228
environment .enterScope (ctx .scope );
229
- StatementNode file = asBlock (super .visitFile_input (ctx ));
230
- deriveSourceSection (ctx , file );
229
+ ExpressionNode file = asExpression (super .visitFile_input (ctx ));
231
230
environment .leaveScope ();
232
231
return factory .createModuleRoot (name , file , ctx .scope .getFrameDescriptor ());
233
232
}
@@ -247,8 +246,7 @@ public Object visitEval_input(Python3Parser.Eval_inputContext ctx) {
247
246
@ Override
248
247
public Object visitSingle_input (Python3Parser .Single_inputContext ctx ) {
249
248
environment .enterScope (ctx .scope );
250
- StatementNode body = asBlock (super .visitSingle_input (ctx ));
251
- deriveSourceSection (ctx , body );
249
+ ExpressionNode body = asExpression (super .visitSingle_input (ctx ));
252
250
environment .leaveScope ();
253
251
if (isInlineMode ) {
254
252
return body ;
@@ -1306,7 +1304,7 @@ private StatementNode makeIfElse(Python3Parser.If_stmtContext ctx, int i) {
1306
1304
if (suiteCount <= i ) {
1307
1305
return factory .createBlock ();
1308
1306
}
1309
- CastToBooleanNode test = factory .toBooleanCastNode (asBlockOrPNode ( ctx .test (i ).accept (this ) ));
1307
+ CastToBooleanNode test = factory .toBooleanCastNode (( PNode ) ctx .test (i ).accept (this ));
1310
1308
StatementNode ifBody = asBlock (ctx .suite (i ).accept (this ));
1311
1309
StatementNode elseBody ;
1312
1310
int testCount = ctx .test ().size ();
@@ -1733,16 +1731,6 @@ private static <T> List<T> asList(Object accept) {
1733
1731
}
1734
1732
}
1735
1733
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
-
1746
1734
private StatementNode asBlock (Object accept ) {
1747
1735
if (accept == null ) {
1748
1736
return factory .createBlock ();
@@ -1761,21 +1749,43 @@ private StatementNode asBlock(Object accept) {
1761
1749
list .add (asBlock (node ));
1762
1750
}
1763
1751
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
- }
1773
1752
return block ;
1774
1753
} else {
1775
1754
throw new IllegalArgumentException ();
1776
1755
}
1777
1756
}
1778
1757
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
+
1779
1789
protected ExpressionNode asClassBody (Object accept , String qualName ) {
1780
1790
List <PNode > body = asList (accept );
1781
1791
if (body .size () > 0 && body .get (0 ) instanceof StringLiteralNode ) {
@@ -1824,7 +1834,7 @@ private StatementNode createGeneratorExpression(Python3Parser.Comp_forContext co
1824
1834
ExpressionNode condition = null ;
1825
1835
Python3Parser .Comp_iterContext comp_iter = comp_for .comp_iter ();
1826
1836
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 );
1828
1838
if (condition == null ) {
1829
1839
condition = nextIf ;
1830
1840
} else {
0 commit comments