@@ -16,7 +16,6 @@ private struct Word {
1616 Node[] inlineNodes;
1717}
1818
19-
2019private struct Type {
2120 ulong size;
2221}
@@ -58,6 +57,7 @@ class BackendLinux86 : CompilerBackend {
5857 bool inScope;
5958 uint blockCounter;
6059 bool inWhile;
60+ uint currentLoop;
6161 Variable[] variables;
6262 Global[string ] globals;
6363 Array[] arrays;
@@ -340,6 +340,7 @@ class BackendLinux86 : CompilerBackend {
340340 override void CompileWhile (WhileNode node) {
341341 ++ blockCounter;
342342 uint blockNum = blockCounter;
343+ currentLoop = blockNum;
343344
344345 output ~= format(" jmp __while_%d_condition\n " , blockNum);
345346 output ~= format(" __while_%d:\n " , blockNum);
@@ -351,6 +352,8 @@ class BackendLinux86 : CompilerBackend {
351352 foreach (ref inode ; node.doWhile) {
352353 inWhile = true ;
353354 compiler.CompileNode(inode);
355+
356+ currentLoop = blockNum;
354357 }
355358
356359 // restore scope
@@ -359,15 +362,14 @@ class BackendLinux86 : CompilerBackend {
359362 }
360363 variables = oldVars;
361364
365+ inWhile = false ;
366+
362367 output ~= format(" __while_%d_condition:\n " , blockNum);
363368
364369 foreach (ref inode ; node.condition) {
365- inWhile = true ;
366370 compiler.CompileNode(inode);
367371 }
368372
369- inWhile = false ;
370-
371373 output ~= " sub r15, 8\n " ;
372374 output ~= " mov rax, [r15]\n " ;
373375 output ~= " cmp rax, 0\n " ;
@@ -553,4 +555,20 @@ class BackendLinux86 : CompilerBackend {
553555 NewConst(format(" %s.max" , node.name), node.values .maxElement());
554556 NewConst(format(" %s.sizeof" , node.name), types[node.name].size);
555557 }
558+
559+ override void CompileBreak (WordNode node) {
560+ if (! inWhile) {
561+ Error(node.error, " Not in while loop" );
562+ }
563+
564+ output ~= format(" jmp __while_%d_end\n " , currentLoop);
565+ }
566+
567+ override void CompileContinue (WordNode node) {
568+ if (! inWhile) {
569+ Error(node.error, " Not in while loop" );
570+ }
571+
572+ output ~= format(" jmp __while_%d_condition\n " , currentLoop);
573+ }
556574}
0 commit comments