Press enter to see results or esc to cancel.

IF Statement

This procedure processes If statement. Both then and else statements are processed recursively. If condition expression is constant at compile time only then or else statement is generated.

Procedure TStatement.Process_IF_Statement;
Var THEN_Statement, ELSE_Statement: TStatement;
    JumpToSkip_ELSE_Statement: Word;
    Condition: TExpression;
begin
  GetNextToken;
  ELSE_Statement.StatementCode := 0;
  JumpToSkip_ELSE_Statement := 0;
  Condition.ExpectBooleanExpression;
  ExpectTokenAndGetNext (Token_THEN);
  THEN_Statement.ProcessStatement;
  If CheckAndGetNextToken (Token_ELSE) then ELSE_Statement.ProcessStatement;
  If Condition.Location = elConstant then
    begin
      Case Condition.Value.Byte of
        0: StatementCode := ELSE_Statement.StatementCode;
        else StatementCode := THEN_Statement.StatementCode;
      end;
      Exit;
    end;
  StoreCode_icGoSub (Condition.IntermediateCodeOffset);
  StoreCode_icGoSub (THEN_Statement.StatementCode);
  If ELSE_Statement.StatementCode <> 0 then GenerateCodeForNearJump (JumpToSkip_ELSE_Statement, JMP_ShortDirect);
  GenerateLabelAndSetJumpsToIt (Condition.Value.LastJumpToFalse);
  StoreCode_icGoSub (ELSE_Statement.StatementCode);
  GenerateLabelAndSetJumpsToIt (JumpToSkip_ELSE_Statement);
  StatementCode := EndSubroutine;
end;