Ignore:
Timestamp:
Aug 10, 2010, 10:14:57 AM (14 years ago)
Author:
george
Message:

Project renamed to Transpascal.
Added support for generatin cycle For-to-do.
Added support for parsing and generating structured types as record and array.
Emit procedure in TProducerPascal and TProducerC reworked.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DelphiToC/Produce/UProducerPascal.pas

    r52 r53  
    1515  TProducerPascal = class(TProducer)
    1616  private
    17     procedure Emit(Text: string; NewLine: Boolean = True);
     17    procedure Emit(AText: string; NewLine: Boolean = True);
    1818    procedure GenerateUses(UsedModules: TUsedModuleList);
    1919    procedure GenerateModule(Module: TModule);
     20    procedure GenerateType(AType: TType; AssignSymbol: Char = ':');
     21    procedure GenerateTypes(Types: TTypeList);
    2022    procedure GenerateCommonBlock(CommonBlock: TCommonBlock;
    2123      LabelPrefix: string);
     
    2729    procedure GenerateCommand(Command: TCommand);
    2830    procedure GenerateWhileDo(WhileDo: TWhileDo);
     31    procedure GenerateForToDo(ForToDo: TForToDo);
    2932    procedure GenerateIfThenElse(IfThenElse: TIfThenElse);
    3033    procedure GenerateAssignment(Assignment: TAssignment);
     
    5659end;
    5760
    58 procedure TProducerPascal.Emit(Text: string; NewLine: Boolean = True);
    59 begin
    60   if NewLine then TextSource.Add(DupeString(' ', IndentationLength * Indetation) + Text)
    61     else TextSource.Strings[TextSource.Count - 1] := TextSource.Strings[TextSource.Count - 1] + Text;
     61procedure TProducerPascal.Emit(AText: string; NewLine: Boolean = True);
     62begin
     63  with TextSource do begin
     64    if Count = 0 then Add('');
     65    if Strings[Count - 1] = '' then
     66      Strings[Count - 1] := Strings[Count - 1] + DupeString(' ', IndentationLength * Indetation);
     67    Strings[Count - 1] := Strings[Count - 1] + AText;
     68    if NewLine then Add('');
     69  end;
    6270end;
    6371
     
    8391  GenerateCommonBlock(Module, '');
    8492  Emit('.', False);
     93end;
     94
     95procedure TProducerPascal.GenerateType(AType: TType; AssignSymbol: Char = ':');
     96var
     97  I: Integer;
     98begin
     99  if AType is TTypeRecord then begin
     100    Emit(AType.Name + ' ' + AssignSymbol + ' record');
     101    Inc(Indetation);
     102    for I := 0 to TTypeRecord(AType).Items.Count - 1 do begin
     103      GenerateType(TType(TTypeRecord(AType).Items[I]));
     104      Emit(';');
     105    end;
     106    Dec(Indetation);
     107    Emit('end', False);
     108  end else
     109  if AType is TTypeArray then begin
     110    Emit(AType.Name + ' ' + AssignSymbol + ' array ', False);
     111    if Assigned(TTypeArray(AType).IndexType) then begin
     112      Emit('[', False);
     113      GenerateType(TTypeArray(AType).IndexType);
     114      Emit(']', False);
     115    end;
     116    Emit(' of ', False);
     117    if Assigned(TTypeArray(AType).ItemType) then
     118      GenerateType(TTypeArray(AType).ItemType);
     119  end else begin
     120    Emit(AType.Name, False);
     121    if Assigned(AType.UsedType) then begin
     122      Emit(' ' + AssignSymbol + ' ', False);
     123      GenerateType(AType.UsedType);
     124    end;
     125  end;
     126end;
     127
     128procedure TProducerPascal.GenerateTypes(Types: TTypeList);
     129var
     130  I: Integer;
     131begin
     132  if Types.Count > 0 then begin
     133    Emit('type');
     134    Inc(Indetation);
     135    for I := 0 to Types.Count - 1 do
     136    with TType(Types[I]) do
     137    if (not System) then begin
     138      GenerateType(TType(Types[I]), '=');
     139      Emit(';');
     140    end;
     141    Dec(Indetation);
     142    Emit('');
     143  end;
    85144end;
    86145
     
    127186    Emit(Line + ';');
    128187    GenerateBeginEnd(Code);
    129     Emit(';', False);
     188    Emit(';');
    130189    Emit('');
    131190  end;
     
    136195  I: Integer;
    137196begin
    138   Emit('const');
    139   Inc(Indetation);
    140   for I := 0 to Constants.Count - 1 do
    141   with TConstant(Constants[I]) do
    142     Emit(Name + ': ' + ValueType.Name + ' = ' + Value + ';');
    143   Dec(Indetation);
    144   Emit('');
     197  if Constants.Count > 0 then begin
     198    Emit('const');
     199    Inc(Indetation);
     200    for I := 0 to Constants.Count - 1 do
     201    with TConstant(Constants[I]) do
     202    if not System then begin
     203      Emit(Name + ': ' + ValueType.Name + ' = ' + Value + ';');
     204    end;
     205    Dec(Indetation);
     206    Emit('');
     207  end;
    145208end;
    146209
     
    154217  for I := 0 to BeginEnd.Commands.Count - 1 do begin
    155218    GenerateCommand(TCommand(BeginEnd.Commands[I]));
    156     Emit(';', False);
     219    Emit(';');
    157220  end;
    158221
    159222  Dec(Indetation);
    160   Emit('end');
     223  Emit('end', False);
    161224end;
    162225
     
    178241  if Command is TBeginEnd then GenerateBeginEnd(TBeginEnd(Command))
    179242  else if Command is TWhileDo then GenerateWhileDo(TWhileDo(Command))
     243  else if Command is TForToDo then GenerateForToDo(TForToDo(Command))
    180244  else if Command is TIfThenElse then GenerateIfThenElse(TIfThenElse(Command))
    181245  else if Command is TAssignment then GenerateAssignment(TAssignment(Command))
     
    189253end;
    190254
     255procedure TProducerPascal.GenerateForToDo(ForToDo: TForToDo);
     256begin
     257  with ForToDo do begin
     258    Emit('for ' + ControlVariable.Name + ' := ' +
     259      GenerateExpression(Start) + ' to ' + GenerateExpression(Stop) + ' do ');
     260    GenerateCommand(Command);
     261  end;
     262end;
     263
    191264procedure TProducerPascal.GenerateIfThenElse(IfThenElse: TIfThenElse);
    192265begin
     
    194267  GenerateCommand(IfThenElse.Command);
    195268  if Assigned(IfThenElse.ElseCommand) then begin
    196     Emit('else ');
     269    Emit(' else ');
    197270    GenerateCommand(IfThenElse.ElseCommand);
    198271  end;
     
    201274procedure TProducerPascal.GenerateAssignment(Assignment: TAssignment);
    202275begin
    203   Emit(Assignment.Target.Name + ' := ' + GenerateExpression(Assignment.Source));
     276  Emit(Assignment.Target.Name + ' := ' + GenerateExpression(Assignment.Source), False);
    204277end;
    205278
     
    220293    end;
    221294  end;
    222   Emit(Line);
     295  Emit(Line, False);
    223296end;
    224297
     
    244317begin
    245318  with CommonBlock do begin
     319    GenerateTypes(Types);
    246320    GenerateFunctions(Functions);
    247321    GenerateConstants(Constants);
Note: See TracChangeset for help on using the changeset viewer.