Ignore:
Timestamp:
Feb 25, 2012, 6:04:21 PM (13 years ago)
Author:
chronos
Message:
  • Modified: Compiler now require source file list which should be composed of project files and project dependency packages files.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Compiler/UCompiler.pas

    r20 r38  
    66
    77uses
    8   SysUtils, Variants, Classes, Contnrs, UAnalyzers, UProducers,
    9   Dialogs, USourceCode, UProducer, UAnalyzer;
     8  SysUtils, Variants, Classes, Contnrs, UAnalyzers, UProducers, FileUtil,
     9  Dialogs, USourceCode, UProducer, UAnalyzer, SpecializedList;
    1010
    1111type
     
    3636    FOnErrorMessage: TErrorMessageEvent;
    3737    FProducerType: TProducerType;
     38    FProducer: TProducer;
     39    function GetSource(Name: string; var SourceCode: string): Boolean;
    3840    procedure ErrorMessage(Text: string; Position: TPoint; FileName: string);
    3941    procedure SetAnalyzer(const AValue: TAnalyzer);
     42    procedure SetProducer(AValue: TProducer);
     43    procedure AnalyzeAll;
     44    procedure ProduceAll;
    4045  public
    41     ProgramCode: TProgram;
    42     Producer: TProducer;
    43     ErrorMessages: TObjectList;
     46    AbstractCode: TProgram;
     47    ErrorMessages: TListObject;
    4448    CompiledFolder: string;
    4549    SupportedTargets: TCompilerTargetList;
     
    4852    Producers: TListProducer;
    4953    Analyzers: TListAnalyzer;
     54    SourceFiles: TListString;
    5055    constructor Create;
    5156    destructor Destroy; override;
    5257    procedure Init;
    53     procedure Compile(ModuleName: string; Source: TStringList);
     58    procedure Compile;
    5459    property OnErrorMessage: TErrorMessageEvent read FOnErrorMessage
    5560      write FOnErrorMessage;
    5661    property Analyzer: TAnalyzer read FAnalyzer write SetAnalyzer;
     62    property Producer: TProducer read FProducer write SetProducer;
    5763  end;
    5864
    5965implementation
    6066
     67resourcestring
     68  SNothingToAnalyze = 'Nothing to analyze';
     69
    6170{ TCompiler }
    6271
    63 procedure TCompiler.Compile(ModuleName: string; Source: TStringList);
    64 var
    65   NewModule: TModule;
    66   ProducedCode: TStringList;
    67   I: Integer;
    68   TargetFileName: string;
    69 begin
    70   try
    71     ProducedCode := TStringList.Create;
    72     Analyzer.FileName := ModuleName;
    73     Analyzer.SourceCode := Source.Text;
    74     Analyzer.Process;
    75     //ShowMessage(IntToHex(Integer(Addr(Analyzer.OnGetSource)), 8));
    76     Analyzer.ParseModule(ProgramCode);
    77     with ProgramCode do
    78     for I := 0 to Modules.Count - 1 do begin
    79       Producer.Produce(TModule(Modules[I]));
    80       Producer.AssignToStringList(ProducedCode);
    81       TargetFileName := TargetFolder + DirectorySeparator +
    82         CompiledFolder + DirectorySeparator + Producer.Name +
    83         DirectorySeparator + TModule(Modules[I]).TargetFile;
    84       ForceDirectories(ExtractFileDir(TargetFileName));
    85       ProducedCode.SaveToFile(TargetFileName);
    86     end;
    87   finally
    88     ProducedCode.Free;
    89   end;
     72procedure TCompiler.Compile;
     73begin
     74  AnalyzeAll;
     75  ProduceAll;
    9076end;
    9177
     
    9480  Analyzers := TListAnalyzer.Create;
    9581  Producers := TListProducer.Create;
     82  SourceFiles := TListString.Create;
    9683
    9784  RegisterAnalyzers(Self);
     
    10693  SupportedTargets.Add('Dallas', 'DS89C450', '8052');
    10794
    108   ProgramCode := TProgram.Create;
     95  AbstractCode := TProgram.Create;
    10996  Producer := nil;
    11097  Analyzer := nil;
    111   ErrorMessages := TObjectList.Create;
     98  ErrorMessages := TListObject.Create;
    11299  CompiledFolder := 'Compiled';
    113100end;
     
    116103begin
    117104  ErrorMessages.Clear;
    118   ProgramCode.Modules.Clear;
     105  AbstractCode.Modules.Clear;
    119106end;
    120107
    121108destructor TCompiler.Destroy;
    122109begin
    123   ProgramCode.Free;
     110  AbstractCode.Free;
    124111  Analyzers.Free;
    125112  Producers.Free;
    126113  ErrorMessages.Free;
    127114  SupportedTargets.Free;
     115  SourceFiles.Free;
     116end;
     117
     118function TCompiler.GetSource(Name: string; var SourceCode: string): Boolean;
     119var
     120  I: Integer;
     121  F: TFileStream;
     122begin
     123  I := 0;
     124  while (I < SourceFiles.Count) and (ExtractFileNameOnly(SourceFiles[I]) <> Name) do Inc(I);
     125  if I < SourceFiles.Count then begin
     126    if FileExistsUTF8(SourceFiles[I]) then
     127      try
     128        F := TFileStream.Create(UTF8Decode(SourceFiles[I]), fmOpenRead);
     129        SetLength(SourceCode, F.Size);
     130        if F.Size > 0 then
     131          F.Read(SourceCode[1], F.Size);
     132        Result := True;
     133      finally
     134        F.Free;
     135      end else Result := False;
     136  end else Result := False;
    128137end;
    129138
     
    144153  if FAnalyzer = AValue then exit;
    145154  FAnalyzer := AValue;
    146   if Assigned(Analyzer) then
     155  if Assigned(Analyzer) then begin
    147156    Analyzer.OnErrorMessage := ErrorMessage;
     157    Analyzer.OnGetSource := GetSource;
     158  end;
     159end;
     160
     161procedure TCompiler.SetProducer(AValue: TProducer);
     162begin
     163  if FProducer = AValue then Exit;
     164  FProducer := AValue;
     165end;
     166
     167procedure TCompiler.AnalyzeAll;
     168begin
     169  if SourceFiles.Count > 0 then begin
     170    Analyzer.FileName := SourceFiles[0];
     171    Analyzer.OnGetSource(ExtractFileNameOnly(Analyzer.FileName), Analyzer.SourceCode);
     172    Analyzer.Process;
     173    //ShowMessage(IntToHex(Integer(Addr(Analyzer.OnGetSource)), 8));
     174    Analyzer.ParseModule(AbstractCode);
     175  end else raise Exception.Create(SNothingToAnalyze);
     176end;
     177
     178procedure TCompiler.ProduceAll;
     179var
     180  NewModule: TModule;
     181  ProducedCode: TStringList;
     182  I: Integer;
     183  TargetFileName: string;
     184begin
     185  try
     186    ProducedCode := TStringList.Create;
     187    with AbstractCode do
     188    for I := 0 to Modules.Count - 1 do begin
     189      Producer.Produce(TModule(Modules[I]));
     190      Producer.AssignToStringList(ProducedCode);
     191      TargetFileName := TargetFolder + DirectorySeparator +
     192        CompiledFolder + DirectorySeparator + Producer.Name +
     193        DirectorySeparator + TModule(Modules[I]).TargetFile;
     194      ForceDirectoriesUTF8(ExtractFileDir(TargetFileName));
     195      ProducedCode.SaveToFile(TargetFileName);
     196    end;
     197  finally
     198    ProducedCode.Free;
     199  end;
    148200end;
    149201
Note: See TracChangeset for help on using the changeset viewer.