Press enter to see results or esc to cancel.

WHILE Statement

This procedure processes While statement. If condition expression is constant at compile time and evaluates to False, no code is generated. All jumps out of the loop (Break, Continue) are also taken into account.

Procedure TStatement.Process_WHILE_Statement;
Var WhileCondition: TExpression;
    Statement: TStatement;
    SavedLastJumpOutOfBlock, SavedLastJumpToNextBlockIteration: Word;
begin
  SavedLastJumpOutOfBlock := LastJumpOutOfBlock;
  SavedLastJumpToNextBlockIteration := LastJumpToNextBlockIteration;
  LastJumpOutOfBlock := 0;
  LastJumpToNextBlockIteration := 0;
  GetNextToken;
  WhileCondition.ExpectBooleanExpression;
  ExpectTokenAndGetNext (Token_DO);
  Statement.ProcessStatement;
  If WhileCondition.Location = elConstant then
    begin
      If WhileCondition.Value.Byte = 0 then
        begin
          StatementCode := 0;
          LastJumpOutOfBlock := SavedLastJumpOutOfBlock;
          LastJumpToNextBlockIteration := SavedLastJumpToNextBlockIteration;
          Exit;
        end;
      WhileCondition.Value.LastJumpToFalse := 0;
    end;
  StoreCode_icGoSub (Statement.StatementCode);
  GenerateCodeForNearJump (LastJumpToNextBlockIteration, JMP_ShortDirect);
  StatementCode := EndSubroutine;
  GenerateLabelAndSetJumpsToIt (LastJumpToNextBlockIteration);
  StoreCode_icGoSub (WhileCondition.IntermediateCodeOffset);
  StoreCode_icGoSub (StatementCode);
  JoinJumpsOfBothExpressions (LastJumpOutOfBlock, WhileCondition.Value.LastJumpToFalse);
  GenerateLabelAndSetJumpsToIt (LastJumpOutOfBlock);
  StatementCode := EndSubroutine;
  LastJumpOutOfBlock := SavedLastJumpOutOfBlock;
  LastJumpToNextBlockIteration := SavedLastJumpToNextBlockIteration;
end;