Ignore:
Timestamp:
Nov 10, 2009, 7:14:14 AM (15 years ago)
Author:
george
Message:
  • Přidáno: Odsazování řádků při generování výstupu.
  • Přidáno: Definování a modelování definic datových typů.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Void/UOutputGenerator.pas

    r11 r13  
    66
    77uses
    8   Classes, SysUtils, UModel;
     8  Classes, SysUtils, UModel, StrUtils;
    99
    1010type
    11 
    12   { TOutputGenerator }
    13 
    1411  TOutputGenerator = class
    1512  private
     13    IndentCount: Integer;
    1614    Model: TModel;
     15    function Indent: string;
    1716  public
    1817    Output: TStringList;
     
    2221  end;
    2322
    24   { TPascalGenerator }
    25 
    2623  TPascalGenerator = class(TOutputGenerator)
    2724    procedure Generate(Model: TModel); override;
     
    2926  end;
    3027
    31   { TCGenerator }
    32 
    3328  TCGenerator = class(TOutputGenerator)
     29  private
     30    function ConvertType(Name: string): string;
     31  public
    3432    procedure Generate(Model: TModel); override;
    3533    procedure GenerateModule(Module: TModule);
     
    3937
    4038{ TOutputGenerator }
     39
     40function TOutputGenerator.Indent: string;
     41begin
     42  Result := DupeString('  ', IndentCount);
     43end;
    4144
    4245procedure TOutputGenerator.Generate(Model: TModel);
     
    7881    // var section
    7982    if Variables.Count > 0 then Output.Add('var');
     83    Inc(IndentCount);
    8084    for I := 0 to Variables.Count - 1 do
    8185      with TVariable(Variables[I]) do
    82         Output.Add('  ' + Name + ': ' + VarType + ';');
     86        Output.Add(Indent + Name + ': ' + VarType.Name + ';');
     87    Dec(IndentCount);
    8388
    8489    // Code block
    8590    Output.Add('begin');
     91    Inc(IndentCount);
    8692    for I := 0 to BeginEnd.Commands.Count - 1 do
    8793      with TCommand(BeginEnd.Commands[I]) do begin
    88         if Name = 'Assignment' then Output.Add(Parameters[0] + ' := ' + Parameters[1] + ';')
     94        if Name = 'Assignment' then Output.Add(Indent + Parameters[0] + ' := ' + Parameters[1] + ';')
    8995        else begin
    9096        Row := Name;
     
    95101          Row := Row + '(' + Copy(ParameterText, 1, Length(ParameterText) - 2) + ')';
    96102        end;
    97         Output.Add(Row + ';');
     103        Output.Add(Indent + Row + ';');
    98104        end;
    99105      end;
     106    Dec(IndentCount);
    100107    Output.Add('end.');
    101108  end;
     
    104111{ TCGenerator }
    105112
     113function TCGenerator.ConvertType(Name: string): string;
     114begin
     115  if Name = 'String' then Result := 'char*'
     116  else if Name = 'Byte' then Result := 'unsigned char'
     117  else if Name = 'ShortInt' then Result := 'signed char'
     118  else if Name = 'Word' then Result := 'unsigned int'
     119  else if Name = 'Integer' then Result := 'int'
     120  else if Name = 'Real' then Result := 'float'
     121  else if Name = 'Double' then Result := 'double'
     122  else if Name = 'Char' then Result := 'char'
     123  else Result := Name;
     124end;
     125
    106126procedure TCGenerator.Generate(Model: TModel);
    107127begin
    108128  inherited;
     129  IndentCount := 0;
    109130  GenerateModule(Model.Module);
    110131end;
     
    122143    Output.Add('int main()');
    123144    Output.Add('{');
     145    Inc(IndentCount);
    124146
    125147    // variable section
    126148    for I := 0 to Variables.Count - 1 do
    127149      with TVariable(Variables[I]) do
    128         Output.Add('  ' + VarType + ' ' + Name + ';');
     150        Output.Add(Indent + '  ' + ConvertType(VarType.Name) + ' ' + Name + ';');
     151    if Variables.Count > 0 then Output.Add('');
    129152
    130153    // Code block
    131154    for I := 0 to BeginEnd.Commands.Count - 1 do
    132155      with TCommand(BeginEnd.Commands[I]) do begin
    133         if Name = 'Assignment' then Output.Add(Parameters[0] + ' = ' + Parameters[1] + ';')
     156        if Name = 'Assignment' then Output.Add(Indent + Parameters[0] + ' = ' + Parameters[1] + ';')
    134157        else begin
    135158          if Name = 'WriteLn' then Row := 'printf'
     
    143166            Row := Row + Copy(ParameterText, 1, Length(ParameterText) - 2);
    144167          end;
    145           Output.Add(Row + ');');
     168          Output.Add(Indent + Row + ');');
    146169        end;
    147170      end;
     171    Dec(IndentCount);
    148172    Output.Add('}');
    149173  end;
Note: See TracChangeset for help on using the changeset viewer.