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/UProducerC.pas

    r52 r53  
    1717    function TranslateType(Name: string): string;
    1818    function TranslateOperator(Name: string): string;
    19     procedure Emit(Text: string);
     19    procedure Emit(AText: string; NewLine: Boolean = True);
    2020    procedure GenerateUses(UsedModules: TUsedModuleList);
    2121    procedure GenerateModule(Module: TModule);
    2222    procedure GenerateCommonBlock(CommonBlock: TCommonBlock;
    2323      LabelPrefix: string);
     24    procedure GenerateType(AType: TType);
     25    procedure GenerateTypes(Types: TTypeList);
    2426    procedure GenerateProgram(ProgramBlock: TProgram);
    2527    procedure GenerateFunctions(Functions: TFunctionList);
     
    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);
     
    6467  else if Name = 'Cardinal' then Result := 'uint32'
    6568  else if Name = 'Integer' then Result := 'int32'
    66   else if Name = 'Void' then Result := 'void';
     69  else if Name = 'Void' then Result := 'void'
     70  else Result := Name;
    6771end;
    6872
     
    8286end;
    8387
    84 procedure TProducerC.Emit(Text: string);
    85 begin
    86   TextSource.Add(DupeString(' ', IndentationLength * Indetation) + Text);
     88procedure TProducerC.Emit(AText: string; NewLine: Boolean = True);
     89begin
     90  with TextSource do begin
     91    if Count = 0 then Add('');
     92    if Strings[Count - 1] = '' then
     93      Strings[Count - 1] := Strings[Count - 1] + DupeString(' ', IndentationLength * Indetation);
     94    Strings[Count - 1] := Strings[Count - 1] + AText;
     95    if NewLine then Add('');
     96  end;
    8797end;
    8898
     
    180190  if Command is TBeginEnd then GenerateBeginEnd(TBeginEnd(Command))
    181191  else if Command is TWhileDo then GenerateWhileDo(TWhileDo(Command))
     192  else if Command is TForToDo then GenerateForToDo(TForToDo(Command))
    182193  else if Command is TIfThenElse then GenerateIfThenElse(TIfThenElse(Command))
    183194  else if Command is TAssignment then GenerateAssignment(TAssignment(Command))
     
    189200  Emit('while (' + GenerateExpression(WhileDo.Condition) + ')');
    190201  GenerateCommand(WhileDo.Command);
     202end;
     203
     204procedure TProducerC.GenerateForToDo(ForToDo: TForToDo);
     205begin
     206  with ForToDo do begin
     207    Emit('for(' + ControlVariable.Name + ' = ' +
     208      GenerateExpression(Start) + '; ' + ControlVariable.Name + ' < ' +
     209      GenerateExpression(Stop) + '; ' + ControlVariable.Name + '++)');
     210    GenerateCommand(Command);
     211  end;
    191212end;
    192213
     
    246267begin
    247268  with CommonBlock do begin
     269    GenerateTypes(Types);
    248270    GenerateFunctions(Functions);
    249271    Emit('void ' + Name + '()');
     
    252274end;
    253275
     276procedure TProducerC.GenerateType(AType: TType);
     277var
     278  I: Integer;
     279begin
     280  if AType is TTypeRecord then begin
     281    Emit('typedef struct');
     282    Emit('{');
     283    Inc(Indetation);
     284    for I := 0 to TTypeRecord(AType).Items.Count - 1 do begin
     285      GenerateType(TType(TTypeRecord(AType).Items[I]));
     286      Emit(';');
     287    end;
     288    Dec(Indetation);
     289    Emit('} ' + TranslateType(AType.Name), False);
     290  end else
     291  if AType is TTypeArray then begin
     292    GenerateType(TTypeArray(AType).ItemType);
     293    Emit('* ', False);
     294
     295(*    if Assigned(TTypeArray(AType).IndexType) then begin
     296      Emit(AType.Name + '[', False);
     297      Emit('[', False);
     298      GenerateType(TTypeArray(AType).IndexType);
     299      Emit(']', False);
     300    end;
     301    Emit(' of ', False);
     302    if Assigned(TTypeArray(AType).ItemType) then*)
     303    Emit(TranslateType(AType.Name), False);
     304  end else begin
     305    if Assigned(AType.UsedType) then begin
     306      GenerateType(AType.UsedType);
     307      Emit(' ', False);
     308    end;
     309    Emit(TranslateType(AType.Name), False);
     310  end;
     311end;
     312
     313procedure TProducerC.GenerateTypes(Types: TTypeList);
     314var
     315  I: Integer;
     316begin
     317  if Types.Count > 0 then begin
     318    Inc(Indetation);
     319    for I := 0 to Types.Count - 1 do
     320    with TType(Types[I]) do
     321    if (not System) then begin
     322      GenerateType(TType(Types[I]));
     323      Emit(';');
     324    end;
     325    Dec(Indetation);
     326    Emit('');
     327  end;
     328end;
     329
    254330
    255331
Note: See TracChangeset for help on using the changeset viewer.