Menu

Nesting.pas

Developers
a2z
2004-03-02
2004-03-03
  • a2z

    a2z - 2004-03-02

    I think Nesting.pas should be something like below.

    It really is an overkill to make it full class with an objectlist an all. This is pure and simple. All users of this code manipulate the 'Levels' array directly.

    Interface

    Type
      TNestingLevelType = (
        nlBlock, // generic code indent
        nlCaseSelector,
        nlRecordType,
        nlRecordVariantSection,
        nlProcedure,
        nlRoundBracket, nlSquareBracket,
        nlStatementLabel
        );

      TNestingLevelList = Class(TObject) { a list of the items above }
      Private
      Public
        Levels: Array[Low(TNestingLevelType)..High(TNestingLevelType)] Of Integer;
        Constructor Create;
        Destructor Destroy; Override;
        Procedure Clear;
        Function FinalTest: String;
        Function Total: Integer;
      End;

    Implementation

    Uses
      SysUtils;

    Constructor TNestingLevelList.Create;
    Begin
      Inherited;
      Clear;
    End;

    Destructor TNestingLevelList.Destroy;
    Begin
      Inherited;
    End;

    Procedure TNestingLevelList.Clear;
    Var
      Index1: TNestingLevelType;
    Begin
      For Index1 := Low(TNestingLevelType) To High(TNestingLevelType) Do Levels[Index1] := 0;
    End;

    { at the end of it all, all should be back to zero }

    Function TNestingLevelList.FinalTest: String;
    Var
      Index1: TNestingLevelType;
    Begin
      Result := '';
      For Index1 := Low(TNestingLevelType) To High(TNestingLevelType) Do Begin
        If Levels[Index1] > 0 Then Begin
          Result := 'Final nesting level = ' + IntToStr(Levels[Index1]);
          Break;
        End;
      End;
    End;

    Function TNestingLevelList.Total: Integer;
    Var
      Index1: TNestingLevelType;
    Begin
      Result := 0;
      For Index1 := Low(TNestingLevelType) To High(TNestingLevelType) Do Result := Result + Levels[Index1];
    End;

    End.

     
    • Anthony Steele

      Anthony Steele - 2004-03-02

      That is simpler and just as readable. Is it significantly faster?

       
      • a2z

        a2z - 2004-03-02

        Didn't test for speed. It definitely is not slower.

        BTW, why dont you scrap that whole class and integrate it into TSourceToken where it really belongs.
        You dont need another object to simply encapsulate an array.

         
        • Anthony Steele

          Anthony Steele - 2004-03-02

          I've used the "fiValues: array[TNestingLevelType] of integer;" idea

          It is a few seconds faster, and several lines shorter.

           
    • Anthony Steele

      Anthony Steele - 2004-03-02

      > why dont you scrap that whole class and integrate it into TSourceToken

      I'd prefer to keep in in an object. There is some consistency checking with the data.

       
      • a2z

        a2z - 2004-03-03

        > I'd prefer to keep in in an object. There is
        > some consistency checking with the data.

        You could always do that (sanity check) in the TSourceToken too. After all it is one TNestingLevels per TSourceToken. Just an idea. It is of course upto you.

         

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.