Ignore:
Timestamp:
Nov 9, 2009, 9:21:28 AM (15 years ago)
Author:
george
Message:
  • Upraveno: Oddělení modelu a generátoru kódu.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Void/UCompilator.pas

    r6 r7  
    66
    77uses
    8   Classes, SysUtils;
     8  Classes, SysUtils, UOutputGenerator, UModel;
    99
    1010type
    1111  TOnErrorEvent = procedure (Text: string; var Terminate: Boolean; Position: TPoint) of object;
    1212
    13   TVariable = class
    14     Name: string;
    15     VarType: string;
    16   end;
    17 
    18   TCommand = class
    19     Text: string;
    20   end;
    21 
    2213  TCompilator = class
    2314  private
    2415    FOnError: TOnErrorEvent;
    25     Variables: TList;
    26     Commands: TList;
    2716    ParsePosition: TPoint;
    2817    procedure DoError(Text: string);
    29     function FindVariableByName(AName: string): TVariable;
    3018  public
     19    Model: TModel;
    3120    SourceCode: TStringList;
    32     Output: TStringList;
     21    Generator: TOutputGenerator;
    3322    procedure Compile;
    34     procedure Clear;
    3523    procedure ProcessLine(Line: string);
    3624    constructor Create;
     
    5341end;
    5442
    55 function TCompilator.FindVariableByName(AName: string): TVariable;
    56 var
    57   I: Integer;
    58 begin
    59   I := 0;
    60   while (I < Variables.Count) and (TVariable(Variables[I]).Name <> AName) do
    61     Inc(I);
    62   if I < Variables.Count then Result := Variables[I] else Result := nil;
    63 end;
    64 
    6543procedure TCompilator.Compile;
    6644var
    6745  I: Integer;
    6846begin
    69   Clear;
     47  Model.Clear;
    7048
    7149  // Process source lines
     
    7553  end;
    7654
    77   // Prepare output
    78   Output.Clear;
    79   Output.Add('program Test;');
    80   Output.Add('{$APPTYPE CONSOLE}');
    81 
    82   // var section
    83   if Variables.Count > 0 then Output.Add('var');
    84   for I := 0 to Variables.Count - 1 do
    85     with TVariable(Variables[I]) do
    86       Output.Add('  ' + Name + ': ' + VarType + ';');
    87 
    88   // Code block
    89   Output.Add('begin');
    90   for I := 0 to Commands.Count - 1 do
    91     with TCommand(Commands[I]) do
    92       Output.Add(Text);
    93   Output.Add('end.');
    94 end;
    95 
    96 procedure TCompilator.Clear;
    97 var
    98   I: Integer;
    99 begin
    100   for I := 0 to Variables.Count - 1 do
    101     TVariable(Variables[I]).Destroy;
    102   Variables.Clear;
    103   for I := 0 to Commands.Count - 1 do
    104     TCommand(Commands[I]).Destroy;
    105   Commands.Clear;
    106   Output.Clear;
     55  Generator.Generate(Model);
    10756end;
    10857
     
    11362  VariableName: string;
    11463begin
     64  with Model do begin
    11565  Command := Parse(Line);
    11666  if Command = 'Write' then
     
    14191    Text := 'Exit;';
    14292  end else DoError('Unknown command ' + Command);
     93
     94  end;
    14395end;
    14496
     
    14698begin
    14799  SourceCode := TStringList.Create;
    148   Output := TStringList.Create;
    149   Variables := TList.Create;
    150   Commands := TList.Create;
     100  Model := TModel.Create;
    151101end;
    152102
     
    155105  I: Integer;
    156106begin
    157   Output.Destroy;
    158   for I := 0 to Variables.Count - 1 do
    159     TVariable(Variables[I]).Destroy;
    160   Variables.Destroy;
    161   for I := 0 to Commands.Count - 1 do
    162     TCommand(Commands[I]).Destroy;
    163   Commands.Destroy;
    164107  SourceCode.Destroy;
     108  Model.Destroy;
    165109  inherited Destroy;
    166110end;
Note: See TracChangeset for help on using the changeset viewer.