Ignore:
Timestamp:
Nov 5, 2010, 1:50:02 PM (14 years ago)
Author:
george
Message:
  • Added: Support for unit initialization and finalization sections.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Compiler/Analyze/UPascalParser.pas

    r6 r7  
    3333    function ParseUnitInterface(SourceCode: TModuleUnit): Boolean;
    3434    function ParseUnitImplementation(SourceCode: TModuleUnit): Boolean;
    35     procedure ParseProgram(SourceCode: TModuleProgram);
     35    function ParseProgram(var SourceCode: TModuleProgram; ProgramCode: TProgram): Boolean;
    3636    procedure ParseCommonBlock(SourceCode: TCommonBlock; EndSymbol: char = ';';
    3737      WithBody: Boolean = True);
     
    396396begin
    397397  Self.ProgramCode := ProgramCode;
    398   if not ParseUnit(TModuleUnit(Result), ProgramCode) then begin
    399     Result := TModuleProgram.Create;
    400     Result.ParentProgram := ProgramCode;
    401     ParseProgram(TModuleProgram(Result));
    402   end else
     398  if not ParseUnit(TModuleUnit(Result), ProgramCode) then
     399  if not ParseProgram(TModuleProgram(Result), ProgramCode) then
    403400    ErrorMessage(SUnknownModuleType, [NextToken]);
    404401end;
    405402
    406 procedure TPascalParser.ParseProgram(SourceCode: TModuleProgram);
     403function TPascalParser.ParseProgram(var SourceCode: TModuleProgram; ProgramCode: TProgram): Boolean;
    407404var
    408405  Identifier: string;
    409406begin
    410407  with SourceCode do begin
     408    SourceCode := TModuleProgram.Create;
     409    SourceCode.ParentProgram := ProgramCode;
    411410    if NextToken = 'program' then begin
    412411      Expect('program');
     
    421420    ParseCommonBlock(Body, '.');
    422421    SourceCode.ParentProgram.Modules.Add(SourceCode);
     422    Result := True;
    423423  end;
    424424end;
     
    427427var
    428428  NewModule: TModule;
     429  NewCommand: TCommand;
    429430begin
    430431  if NextToken = 'unit' then begin
     
    447448    if NextToken = 'initialization' then begin
    448449      Expect('initialization');
     450      while (NextToken <> 'end') and (NextToken <> 'finalization')
     451      and (NextTokenType <> ttEndOfFile) do
     452      begin
     453        NewCommand := ParseCommand(SourceCode.InititializeSection);
     454        if Assigned(NewCommand) then
     455          SourceCode.InititializeSection.Code.Commands.Add(NewCommand);
     456       //ShowMessage(NextCode);
     457        if NextToken = ';' then
     458          ReadToken;
     459      end;
    449460    end;
    450461    if NextToken = 'finalization' then begin
    451462      Expect('finalization');
    452     end;
     463      while (NextToken <> 'end') and (NextTokenType <> ttEndOfFile) do
     464      begin
     465        NewCommand := ParseCommand(SourceCode.FinalalizeSection);
     466        if Assigned(NewCommand) then
     467          SourceCode.FinalalizeSection.Code.Commands.Add(NewCommand);
     468       //ShowMessage(NextCode);
     469        if NextToken = ';' then
     470          ReadToken;
     471      end;
     472    end;
     473    Expect('end');
     474    Expect('.');
    453475    Result := True;
    454476  end else Result := False;
     
    487509begin
    488510  with SourceCode do begin
    489     while (NextToken <> EndSymbol) do begin
     511    while (NextToken <> EndSymbol) and (NextTokenType <> ttEndOfFile) do begin
    490512      if not ParseVariableList(Variables) then
    491513      if not ParseConstantList(Constants) then
     
    12701292end;
    12711293
    1272 
    12731294end.
    12741295
Note: See TracChangeset for help on using the changeset viewer.