@@ -62,7 +62,7 @@ public class PythonCompiler extends PythonParserBaseVisitor<Object> {
62
62
private PyClass curPyClass = null ;
63
63
private final List <CompilerException > compileErrors = new ArrayList <>();
64
64
private Label elifLabel ;
65
- private final Stack <Context > contextStack = new Stack <>();
65
+ final Stack <Context > contextStack = new Stack <>();
66
66
67
67
public final JvmWriter writer = new JvmWriter (this );
68
68
@@ -318,19 +318,41 @@ public Object visitCompound_stmt(PythonParser.Compound_stmtContext ctx) {
318
318
return visit (whileStmtContext );
319
319
}
320
320
321
+ PythonParser .For_stmtContext forStmtContext = ctx .for_stmt ();
322
+ if (forStmtContext != null ) {
323
+ return visit (forStmtContext );
324
+ }
325
+
321
326
throw new RuntimeException ("No supported matching compound_stmt found for:\n " + ctx .getText ());
322
327
}
323
328
329
+ @ Override
330
+ public Object visitFor_stmt (PythonParser .For_stmtContext ctx ) {
331
+ // PythonParser.Named_expressionContext namedExpressionContext = ctx.named_expression();
332
+ // if (namedExpressionContext != null) {
333
+ // Object visit = visit(namedExpressionContext);
334
+ // loadExpr(ctx, visit);
335
+ // } else {
336
+ // throw new RuntimeException("No supported matching named_expression found for:\n" + ctx.getText());
337
+ // }
338
+
339
+ throw new RuntimeException ("No supported matching for_stmt found for:\n " + ctx .getText ());
340
+ }
341
+
324
342
@ Override
325
343
public Object visitWhile_stmt (PythonParser .While_stmtContext ctx ) {
326
344
Label loopStart = new Label ();
327
345
Label loopEnd = new Label ();
346
+ Label elseBlock = null ;
347
+ if (ctx .else_block () != null ) {
348
+ elseBlock = new Label ();
349
+ }
328
350
329
351
// Loop start:
330
352
mv .visitLabel (loopStart );
331
353
332
354
// region Comparison(named_expression)
333
- pushContext (new WhileConditionContext (loopEnd ));
355
+ pushContext (new WhileConditionContext (loopEnd , elseBlock ));
334
356
PythonParser .Named_expressionContext namedExpressionContext = ctx .named_expression ();
335
357
if (namedExpressionContext != null ) {
336
358
Object visit = visit (namedExpressionContext );
@@ -343,18 +365,35 @@ public Object visitWhile_stmt(PythonParser.While_stmtContext ctx) {
343
365
// endregion Comparison
344
366
345
367
// region Loop(block)
368
+ Context loopContext = new WhileLoopContext (loopStart , loopEnd );
369
+ pushContext (loopContext );
346
370
PythonParser .BlockContext block = ctx .block ();
347
371
if (block != null ) {
348
372
visit (block );
349
373
}
350
374
if (context .needsPop ()) {
351
375
throw new RuntimeException ("Still values on stack for:\n " + ctx .getText ());
352
376
}
377
+ popContext ();
353
378
// endregion Loop
354
379
355
380
// Jump to loop start
356
381
mv .visitJumpInsn (GOTO , loopStart );
357
382
383
+ if (elseBlock != null ) {
384
+ mv .visitLabel (elseBlock );
385
+
386
+ // region Else(block)
387
+ PythonParser .BlockContext elseBlockContext = ctx .else_block ().block ();
388
+ if (elseBlockContext != null ) {
389
+ visit (elseBlockContext );
390
+ }
391
+ if (context .needsPop ()) {
392
+ throw new RuntimeException ("Still values on stack for:\n " + ctx .getText ());
393
+ }
394
+ // endregion Else
395
+ }
396
+
358
397
// Loop end:
359
398
mv .visitLabel (loopEnd );
360
399
@@ -980,6 +1019,21 @@ public Object visitSimple_stmt(PythonParser.Simple_stmtContext ctx) {
980
1019
writer .pop ();
981
1020
return visit ;
982
1021
}
1022
+
1023
+ TerminalNode aBreak = ctx .BREAK ();
1024
+ if (aBreak != null ) {
1025
+ Context context = writer .getLoopContext ();
1026
+ if (context instanceof WhileLoopContext whileConditionContext ) {
1027
+ mv .visitJumpInsn (GOTO , whileConditionContext .endLabel );
1028
+ return Unit .Instance ;
1029
+ // } else if (writer.getContext() instanceof ForConditionContext) {
1030
+ // ForConditionContext forConditionContext = (ForConditionContext) writer.getContext();
1031
+ // mv.visitJumpInsn(GOTO, forConditionContext.loopEnd);
1032
+ // return Unit.Instance;
1033
+ } else {
1034
+ throw new RuntimeException ("Not in a loop:\n " + ctx .getText ());
1035
+ }
1036
+ }
983
1037
throw new RuntimeException ("No supported matching simple_stmt found of type " + ctx .getClass ().getSimpleName () + " for:\n " + ctx .getText ());
984
1038
}
985
1039
0 commit comments