Ignore:
Timestamp:
Mar 4, 2012, 5:26:20 PM (13 years ago)
Author:
chronos
Message:
  • Added: Compiled target code now can be browsed using Target project navigation.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Compiler/UCompiler.pas

    r47 r49  
    2121  private
    2222    FAnalyzer: TAnalyzer;
     23    FTarget: TTarget;
    2324    FOnErrorMessage: TErrorMessageEvent;
    2425    function GetSource(Name: string; var SourceCode: string): Boolean;
     26    function WriteTarget(Name: string; TargetCode: string): Boolean;
    2527    procedure ErrorMessage(Text: string; Position: TPoint; FileName: string);
    2628    procedure SetAnalyzer(const AValue: TAnalyzer);
    2729    procedure AnalyzeAll;
    2830    procedure ProduceAll;
     31    procedure SetTarget(AValue: TTarget);
    2932  public
    3033    AbstractCode: TProgram;
    3134    ErrorMessages: TListObject;
    3235    CompiledFolder: string;
    33     Target: TTarget;
     36
    3437    TargetFolder: string;
    3538    Targets: TListTarget;
     
    4346      write FOnErrorMessage;
    4447    property Analyzer: TAnalyzer read FAnalyzer write SetAnalyzer;
     48    property Target: TTarget read FTarget write SetTarget;
    4549  end;
    4650
     
    5357resourcestring
    5458  SNothingToAnalyze = 'Nothing to analyze';
     59  SRewritingExistedTarget = 'Reqriting existing target file %s';
    5560
    5661{ TCompiler }
     
    113118end;
    114119
     120function TCompiler.WriteTarget(Name: string; TargetCode: string): Boolean;
     121var
     122  I: Integer;
     123  F: TFileStream;
     124begin
     125  I := 0;
     126  while (I < TargetFiles.Count) and (ExtractFileNameOnly(TargetFiles[I]) <> Name) do Inc(I);
     127  if I >= TargetFiles.Count then begin
     128    try
     129      ForceDirectoriesUTF8(ExtractFileDir(Name));
     130      if FileExistsUTF8(Name) then
     131        F := TFileStream.Create(UTF8Decode(Name), fmOpenWrite)
     132        else F := TFileStream.Create(UTF8Decode(Name), fmCreate);
     133      F.Size := 0;
     134      if Length(TargetCode) > 0 then
     135        F.Write(TargetCode[1], Length(TargetCode));
     136      Result := True;
     137      TargetFiles.Add(Name);
     138    finally
     139      F.Free;
     140    end;
     141  end else begin
     142    Result := False;
     143    ErrorMessage(Format(SRewritingExistedTarget, [Name]), Point(0, 0), '');
     144  end;
     145end;
     146
    115147procedure TCompiler.ErrorMessage(Text: string; Position: TPoint; FileName: string);
    116148var
     
    160192      Target.Producer.Produce(TModule(Modules[I]));
    161193      Target.Producer.AssignToStringList(ProducedCode);
    162       TargetFileName := TargetFolder + DirectorySeparator +
    163         CompiledFolder + DirectorySeparator + Target.Name +
    164         DirectorySeparator + TModule(Modules[I]).TargetFile;
    165       ForceDirectoriesUTF8(ExtractFileDir(TargetFileName));
    166       ProducedCode.SaveToFile(TargetFileName);
     194      TargetFileName := TargetFolder + DirectorySeparator + TModule(Modules[I]).TargetFile;
     195      if Assigned(Target.Producer.OnWriteTarget) then
     196        Target.Producer.OnWriteTarget(TargetFileName, ProducedCode.Text);
    167197    end;
    168198  finally
     
    171201end;
    172202
     203procedure TCompiler.SetTarget(AValue: TTarget);
     204begin
     205  if FTarget = AValue then Exit;
     206  FTarget := AValue;
     207  FTarget.Producer.OnWriteTarget := WriteTarget;
     208end;
     209
    173210end.
Note: See TracChangeset for help on using the changeset viewer.