Ignore:
Timestamp:
Jun 26, 2023, 6:08:23 PM (17 months ago)
Author:
chronos
Message:
  • Added: Support for procedures.
  • Added: Project pascal file can be opened from main menu. Last file name is remembered.
  • Modified: Improved XML output of source structure.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/xpascal/Source.pas

    r232 r233  
    148148  end;
    149149
     150  { TProcedure }
     151
     152  TProcedure = class(TSourceNode)
     153  protected
     154    function GetFieldsCount: Integer; override;
     155  public
     156    Name: string;
     157    InternalName: string;
     158    Params: TFunctionParameters;
     159    Block: TBlock;
     160    ParentType: TType;
     161    procedure GetValue(Index: Integer; out Value); override;
     162    function GetField(Index: Integer): TField; override;
     163    procedure SetValue(Index: Integer; var Value); override;
     164    constructor Create;
     165    destructor Destroy; override;
     166  end;
     167
     168  { TProcedures }
     169
     170  TProcedures = class(TSourceNodeList<TProcedure>)
     171    ParentType: TType;
     172    function SearchByName(Name: string): TProcedure;
     173    function AddNew(Name: string): TProcedure;
     174  end;
     175
    150176  TCommand = class(TSourceNode)
    151177  end;
     
    164190  public
    165191    FunctionDef: TFunction;
     192    Params: TExpressions;
     193    procedure GetValue(Index: Integer; out Value); override;
     194    function GetField(Index: Integer): TField; override;
     195    procedure SetValue(Index: Integer; var Value); override;
     196    constructor Create;
     197    destructor Destroy; override;
     198  end;
     199
     200  { TProcedureCall }
     201
     202  TProcedureCall = class(TCommand)
     203  protected
     204    function GetFieldsCount: Integer; override;
     205  public
     206    ProcedureDef: TProcedure;
    166207    Params: TExpressions;
    167208    procedure GetValue(Index: Integer; out Value); override;
     
    365406    Constants: TConstants;
    366407    Functions: TFunctions;
     408    Procedures: TProcedures;
    367409    Types: TTypes;
    368410    BeginEnd: TBeginEnd;
     
    375417    function GetVariable(Name: string): TVariable;
    376418    function GetFunction(Name: string): TFunction;
     419    function GetProcedure(Name: string): TProcedure;
    377420    constructor Create;
    378421    destructor Destroy; override;
     
    426469end;
    427470
     471{ TProcedureCall }
     472
     473function TProcedureCall.GetFieldsCount: Integer;
     474begin
     475  Result := 2;
     476end;
     477
     478procedure TProcedureCall.GetValue(Index: Integer; out Value);
     479begin
     480  if Index = 0 then TProcedure(Value) := ProcedureDef
     481  else if Index = 1 then TExpressions(Value) := Params
     482  else inherited;
     483end;
     484
     485function TProcedureCall.GetField(Index: Integer): TField;
     486begin
     487  if Index = 0 then Result := TField.Create(dtObject, 'Procedure')
     488  else if Index = 1 then Result := TField.Create(dtObject, 'Parameters')
     489  else inherited;
     490end;
     491
     492procedure TProcedureCall.SetValue(Index: Integer; var Value);
     493begin
     494  if Index = 0 then ProcedureDef := TProcedure(Value)
     495  else if Index = 1 then Params := TExpressions(Value)
     496  else inherited;
     497end;
     498
     499constructor TProcedureCall.Create;
     500begin
     501  Params := TExpressions.Create;
     502end;
     503
     504destructor TProcedureCall.Destroy;
     505begin
     506  FreeAndNil(Params);
     507  inherited;
     508end;
     509
     510{ TProcedure }
     511
     512function TProcedures.SearchByName(Name: string): TProcedure;
     513var
     514  I: Integer;
     515begin
     516  I := 0;
     517  while (I < Count) and (TProcedure(Items[I]).Name <> Name) do Inc(I);
     518  if I < Count then Result := TProcedure(Items[I])
     519    else Result := nil;
     520end;
     521
     522function TProcedures.AddNew(Name: string): TProcedure;
     523begin
     524  Result := TProcedure.Create;
     525  Result.Name := Name;
     526  Result.ParentType := ParentType;
     527  Add(Result);
     528end;
     529
     530function TProcedure.GetFieldsCount: Integer;
     531begin
     532  Result := 3;
     533end;
     534
     535procedure TProcedure.GetValue(Index: Integer; out Value);
     536begin
     537  if Index = 0 then TBlock(Value) := Block
     538  else if Index = 1 then TFunctionParameters(Value) := Params
     539  else if Index = 2 then string(Value) := Name
     540  else inherited;
     541end;
     542
     543function TProcedure.GetField(Index: Integer): TField;
     544begin
     545  if Index = 0 then Result := TField.Create(dtObject, 'Block')
     546  else if Index = 1 then Result := TField.Create(dtList, 'Parameters')
     547  else if Index = 2 then Result := TField.Create(dtString, 'Name')
     548  else inherited;
     549end;
     550
     551procedure TProcedure.SetValue(Index: Integer; var Value);
     552begin
     553  if Index = 0 then Block := TBlock(Value)
     554  else if Index = 1 then Params := TFunctionParameters(Value)
     555  else if Index = 2 then Name := string(Value)
     556  else inherited;
     557end;
     558
     559constructor TProcedure.Create;
     560begin
     561  Params := TFunctionParameters.Create;
     562  Block := TBlock.Create;
     563end;
     564
     565destructor TProcedure.Destroy;
     566begin
     567  FreeAndNil(Block);
     568  FreeAndNil(Params);
     569  inherited;
     570end;
     571
    428572{ TExpressionBrackets }
    429573
     
    771915  if Index = 0 then TBlock(Value) := Block
    772916  else if Index = 1 then TFunctionParameters(Value) := Params
    773   else if Index = 2 then TType(Value) := ResultType
    774   else if Index = 3 then string(Value) := Name
     917  else if Index = 2 then string(Value) := Name
     918  else if Index = 3 then TType(Value) := ResultType
    775919  else inherited;
    776920end;
     
    780924  if Index = 0 then Result := TField.Create(dtObject, 'Block')
    781925  else if Index = 1 then Result := TField.Create(dtList, 'Parameters')
    782   else if Index = 2 then Result := TField.Create(dtObject, 'ResultType')
    783   else if Index = 3 then Result := TField.Create(dtString, 'Name')
     926  else if Index = 2 then Result := TField.Create(dtString, 'Name')
     927  else if Index = 3 then Result := TField.Create(dtObject, 'ResultType')
    784928  else inherited;
    785929end;
     
    794938  if Index = 0 then Block := TBlock(Value)
    795939  else if Index = 1 then Params := TFunctionParameters(Value)
    796   else if Index = 2 then ResultType := TType(Value)
    797   else if Index = 3 then Name := string(Value)
     940  else if Index = 2 then Name := string(Value)
     941  else if Index = 3 then ResultType := TType(Value)
    798942  else inherited;
    799943end;
     
    10521196begin
    10531197  if Index = 0 then Result := TField.Create(dtObject, 'Function')
    1054   else if Index = 1 then Result := TField.Create(dtObject, 'Parameters')
     1198  else if Index = 1 then Result := TField.Create(dtList, 'Parameters')
    10551199  else inherited;
    10561200end;
     
    11391283  else if Index = 3 then TConstants(Value) := Constants
    11401284  else if Index = 4 then TFunctions(Value) := Functions
     1285  else if Index = 5 then TProcedures(Value) := Procedures
    11411286  else inherited;
    11421287end;
     
    11441289function TBlock.GetField(Index: Integer): TField;
    11451290begin
    1146   if Index = 0 then Result := TField.Create(dtObject, 'Block')
     1291  if Index = 0 then Result := TField.Create(dtObject, 'BeginEnd')
    11471292  else if Index = 1 then Result := TField.Create(dtList, 'Types')
    11481293  else if Index = 2 then Result := TField.Create(dtList, 'Variables')
    11491294  else if Index = 3 then Result := TField.Create(dtList, 'Constants')
    11501295  else if Index = 4 then Result := TField.Create(dtList, 'Functions')
     1296  else if Index = 5 then Result := TField.Create(dtList, 'Procedures')
    11511297  else inherited;
    11521298end;
     
    11541300function TBlock.GetFieldsCount: Integer;
    11551301begin
    1156   Result := 5;
     1302  Result := 6;
    11571303end;
    11581304
     
    11641310  else if Index = 3 then Constants := TConstants(Value)
    11651311  else if Index = 4 then Functions := TFunctions(Value)
     1312  else if Index = 5 then Procedures := TProcedures(Value)
    11661313  else inherited;
    11671314end;
     
    11701317begin
    11711318  Functions.Clear;
     1319  Procedures.Clear;
    11721320  Constants.Clear;
    11731321  Variables.Clear;
     
    12011349  if not Assigned(Result) and Assigned(ParentBlock) then
    12021350    Result := ParentBlock.GetFunction(Name);
     1351end;
     1352
     1353function TBlock.GetProcedure(Name: string): TProcedure;
     1354begin
     1355  Result := Procedures.SearchByName(Name);
     1356  if not Assigned(Result) and Assigned(ParentBlock) then
     1357    Result := ParentBlock.GetProcedure(Name);
    12031358end;
    12041359
     
    12111366  Functions := TFunctions.Create;
    12121367  Functions.Parent := Self;
     1368  Procedures := TProcedures.Create;
     1369  Procedures.Parent := Self;
    12131370  Types := TTypes.Create;
    12141371  Types.Parent := Self;
     
    12241381  FreeAndNil(Constants);
    12251382  FreeAndNil(Functions);
     1383  FreeAndNil(Procedures);
    12261384  inherited;
    12271385end;
     
    12371395function TBeginEnd.GetField(Index: Integer): TField;
    12381396begin
    1239   if Index = 0 then Result := TField.Create(dtList, 'Command')
     1397  if Index = 0 then Result := TField.Create(dtList, 'Commands')
    12401398  else inherited;
    12411399end;
Note: See TracChangeset for help on using the changeset viewer.