@@ -62,7 +62,7 @@ public class PythonCompiler extends PythonParserBaseVisitor<Object> {
6262 private PyClass curPyClass = null ;
6363 private final List <CompilerException > compileErrors = new ArrayList <>();
6464 private Label elifLabel ;
65- private final Stack <Context > contextStack = new Stack <>();
65+ final Stack <Context > contextStack = new Stack <>();
6666
6767 public final JvmWriter writer = new JvmWriter (this );
6868
@@ -318,19 +318,41 @@ public Object visitCompound_stmt(PythonParser.Compound_stmtContext ctx) {
318318 return visit (whileStmtContext );
319319 }
320320
321+ PythonParser .For_stmtContext forStmtContext = ctx .for_stmt ();
322+ if (forStmtContext != null ) {
323+ return visit (forStmtContext );
324+ }
325+
321326 throw new RuntimeException ("No supported matching compound_stmt found for:\n " + ctx .getText ());
322327 }
323328
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+
324342 @ Override
325343 public Object visitWhile_stmt (PythonParser .While_stmtContext ctx ) {
326344 Label loopStart = new Label ();
327345 Label loopEnd = new Label ();
346+ Label elseBlock = null ;
347+ if (ctx .else_block () != null ) {
348+ elseBlock = new Label ();
349+ }
328350
329351 // Loop start:
330352 mv .visitLabel (loopStart );
331353
332354 // region Comparison(named_expression)
333- pushContext (new WhileConditionContext (loopEnd ));
355+ pushContext (new WhileConditionContext (loopEnd , elseBlock ));
334356 PythonParser .Named_expressionContext namedExpressionContext = ctx .named_expression ();
335357 if (namedExpressionContext != null ) {
336358 Object visit = visit (namedExpressionContext );
@@ -343,18 +365,35 @@ public Object visitWhile_stmt(PythonParser.While_stmtContext ctx) {
343365 // endregion Comparison
344366
345367 // region Loop(block)
368+ Context loopContext = new WhileLoopContext (loopStart , loopEnd );
369+ pushContext (loopContext );
346370 PythonParser .BlockContext block = ctx .block ();
347371 if (block != null ) {
348372 visit (block );
349373 }
350374 if (context .needsPop ()) {
351375 throw new RuntimeException ("Still values on stack for:\n " + ctx .getText ());
352376 }
377+ popContext ();
353378 // endregion Loop
354379
355380 // Jump to loop start
356381 mv .visitJumpInsn (GOTO , loopStart );
357382
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+
358397 // Loop end:
359398 mv .visitLabel (loopEnd );
360399
@@ -980,6 +1019,21 @@ public Object visitSimple_stmt(PythonParser.Simple_stmtContext ctx) {
9801019 writer .pop ();
9811020 return visit ;
9821021 }
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+ }
9831037 throw new RuntimeException ("No supported matching simple_stmt found of type " + ctx .getClass ().getSimpleName () + " for:\n " + ctx .getText ());
9841038 }
9851039
0 commit comments