Ignore:
Timestamp:
Oct 21, 2010, 1:20:57 PM (14 years ago)
Author:
george
Message:
  • Enhanced: Tokenizerm, parsing of record type, generation C code for record type.
  • Added: Logging of debug information.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Transpascal/Compiler/Produce/UProducerC.pas

    r75 r76  
    1919    function TranslateType(Name: string): string;
    2020    function TranslateOperator(Name: string): string;
    21     procedure Emit(AText: string; NewLine: Boolean = True);
     21    procedure Emit(AText: string);
     22    procedure EmitLn(AText: string = '');
    2223    procedure GenerateUses(UsedModules: TUsedModuleList);
    2324    procedure GenerateModule(Module: TModule);
     
    2728    procedure GenerateTypes(Types: TTypeList);
    2829    procedure GenerateProgram(ProgramBlock: TProgram);
    29     procedure GenerateFunctions(Functions: TFunctionList);
     30    procedure GenerateFunctions(Functions: TFunctionList;
     31      Prefix: string = '');
    3032    procedure GenerateBeginEnd(BeginEnd: TBeginEnd);
    3133    procedure GenerateVariableList(VariableList: TVariableList);
     
    9496end;
    9597
    96 procedure TProducerC.Emit(AText: string; NewLine: Boolean = True);
     98procedure TProducerC.EmitLn(AText: string = '');
     99begin
     100  Emit(AText);
     101  TextSource.Add('');
     102end;
     103
     104procedure TProducerC.Emit(AText: string);
    97105begin
    98106  with TextSource do begin
     
    101109      Strings[Count - 1] := Strings[Count - 1] + DupeString(' ', IndentationLength * Indetation);
    102110    Strings[Count - 1] := Strings[Count - 1] + AText;
    103     if NewLine then Add('');
    104111  end;
    105112end;
     
    111118  for I := 0 to UsedModules.Count - 1 do
    112119    if Dialect = pdDynamicC then
    113       Emit('#use "' + TUsedModule(UsedModules[I]).Name + '.lib"')
    114       else Emit('#include "' + TUsedModule(UsedModules[I]).Name + '.h"');
    115   Emit('');
     120      EmitLn('#use "' + TUsedModule(UsedModules[I]).Name + '.lib"')
     121      else EmitLn('#include "' + TUsedModule(UsedModules[I]).Name + '.h"');
     122  EmitLn;
    116123end;
    117124
    118125procedure TProducerC.GenerateModule(Module: TModule);
    119126begin
    120   if Dialect = pdDynamicC then Emit('#use "platform.lib"')
    121     else Emit('#include "platform.h"');
    122   Emit('');
     127  if Dialect = pdDynamicC then EmitLn('#use "platform.lib"')
     128    else EmitLn('#include "platform.h"');
     129  EmitLn;
    123130  if Module is TModuleProgram then begin
    124131    TModuleProgram(Module).Body.Name := 'main';
     
    149156end;
    150157
    151 procedure TProducerC.GenerateFunctions(Functions: TFunctionList);
     158procedure TProducerC.GenerateFunctions(Functions: TFunctionList;
     159  Prefix: string = '');
    152160var
    153161  I: Integer;
     
    157165  for I := 0 to Functions.Count - 1 do
    158166  with TFunction(Functions[I]) do
    159   if not System then
    160   begin
    161     if HaveResult then Line := TranslateType(ResultType.Name) + ' '
     167  if not System then begin
     168    if HaveResult and Assigned(ResultType) then Line := TranslateType(ResultType.Name) + ' '
    162169      else Line := 'void ';
    163     Line := Line + Name + '(';
     170    Line := Line + Prefix + Name + '(';
    164171    if Parameters.Count > 0 then
    165172    for J := 0 to Parameters.Count - 1 do begin
     
    169176    end;
    170177    Line := Line + ')';
    171     Emit(Line);
     178    EmitLn(Line);
    172179    GenerateBeginEnd(Code);
    173     Emit('');
     180    EmitLn;
    174181  end;
    175182end;
     
    179186  I: Integer;
    180187begin
    181   Emit('{');
     188  EmitLn('{');
    182189  Inc(Indetation);
    183190
     
    192199
    193200  Dec(Indetation);
    194   Emit('}');
     201  EmitLn('}');
    195202end;
    196203
     
    201208  for I := 0 to VariableList.Count - 1 do
    202209    GenerateVariable(TVariable(VariableList[I]));
    203 //  Emit('');
     210//  EmitLn;
    204211end;
    205212
     
    207214begin
    208215  with Variable do
    209     Emit(TranslateType(ValueType.Name) + ' ' + Name + ';');
     216    EmitLn(TranslateType(ValueType.Name) + ' ' + Name + ';');
    210217end;
    211218
     
    222229procedure TProducerC.GenerateWhileDo(WhileDo: TWhileDo);
    223230begin
    224   Emit('while (' + GenerateExpression(WhileDo.Condition) + ')');
    225   GenerateCommand(WhileDo.Command);
     231  EmitLn('while (' + GenerateExpression(WhileDo.Condition) + ')');
     232  if Assigned(WhileDo.Command) then GenerateCommand(WhileDo.Command);
    226233end;
    227234
     
    229236begin
    230237  with ForToDo do begin
    231     Emit('for(' + ControlVariable.Name + ' = ' +
     238    if Assigned(ControlVariable) then
     239    EmitLn('for(' + ControlVariable.Name + ' = ' +
    232240      GenerateExpression(Start) + '; ' + ControlVariable.Name + ' < ' +
    233241      GenerateExpression(Stop) + '; ' + ControlVariable.Name + '++)');
     
    238246procedure TProducerC.GenerateIfThenElse(IfThenElse: TIfThenElse);
    239247begin
    240   Emit('if(' + GenerateExpression(IfThenElse.Condition) + ')');
     248  EmitLn('if(' + GenerateExpression(IfThenElse.Condition) + ')');
    241249  GenerateCommand(IfThenElse.Command);
    242250  if Assigned(IfThenElse.ElseCommand) then begin
    243     Emit('else ');
     251    EmitLn('else ');
    244252    GenerateCommand(IfThenElse.ElseCommand);
    245253  end;
     
    248256procedure TProducerC.GenerateAssignment(Assignment: TAssignment);
    249257begin
    250   if Assignment.Target.Name = 'Result' then Emit('return(' + GenerateExpression(Assignment.Source) + ');')
    251     else Emit(Assignment.Target.Name + ' = ' + GenerateExpression(Assignment.Source) + ';');
     258  if Assignment.Target.Name = 'Result' then EmitLn('return(' + GenerateExpression(Assignment.Source) + ');')
     259    else EmitLn(Assignment.Target.Name + ' = ' + GenerateExpression(Assignment.Source) + ';');
    252260end;
    253261
     
    266274    end;
    267275    Line := Line + ');';
    268     Emit(Line);
     276    EmitLn(Line);
    269277  end;
    270278end;
     
    302310    GenerateTypes(Types);
    303311    GenerateFunctions(Functions);
    304     Emit('void ' + Name + '()');
     312    EmitLn('void ' + Name + '()');
    305313    GenerateBeginEnd(Code);
    306314  end;
     
    313321  if Assigned(AType) then begin
    314322  if AType is TTypeRecord then begin
    315     Emit('struct');
    316     Emit('{');
     323    EmitLn('struct');
     324    EmitLn('{');
    317325    Inc(Indetation);
    318326    GenerateVariableList(TTypeRecord(AType).CommonBlock.Variables);
    319327    Dec(Indetation);
    320     Emit('} ' + TranslateType(AType.Name), False);
     328    EmitLn('} ' + TranslateType(AType.Name) + ';');
     329    EmitLn;
     330    GenerateFunctions(TTypeRecord(AType).CommonBlock.Functions, AType.Name + '_');
    321331  end else
    322332  if AType is TTypeArray then begin
    323333    GenerateType(TTypeArray(AType).ItemType);
    324     Emit('* ', False);
     334    EmitLn('* ');
    325335
    326336(*    if Assigned(TTypeArray(AType).IndexType) then begin
    327       Emit(AType.Name + '[', False);
    328       Emit('[', False);
     337      Emit(AType.Name + '[');
     338      Emit('[');
    329339      GenerateType(TTypeArray(AType).IndexType);
    330       Emit(']', False);
    331     end;
    332     Emit(' of ', False);
     340      Emit(']');
     341    end;
     342    Emit(' of ');
    333343    if Assigned(TTypeArray(AType).ItemType) then*)
    334     Emit(TranslateType(AType.Name), False);
     344    Emit(TranslateType(AType.Name));
     345  end else
     346  if AType is TTypePointer then begin
     347    if Assigned(AType.UsedType) then begin
     348      Emit(AType.UsedType.Name);
     349      Emit(' *');
     350    end;
     351    Emit(TranslateType(AType.Name));
    335352  end else begin
    336353    if Assigned(AType.UsedType) then begin
    337354      //GenerateType(AType.UsedType);
    338       Emit(AType.UsedType.Name, False);
    339       Emit(' ', False);
    340     end;
    341     Emit(TranslateType(AType.Name), False);
     355      Emit(AType.UsedType.Name);
     356      Emit(' ');
     357    end;
     358    Emit(TranslateType(AType.Name));
    342359  end;
    343360  end;
     
    353370    with TType(Types[I]) do
    354371    if (not System) then begin
    355       Emit('typedef ', False);
     372      Emit('typedef ');
    356373      GenerateType(TType(Types[I]));
    357       Emit(';');
     374      EmitLn(';');
    358375    end;
    359376    Dec(Indetation);
    360     Emit('');
    361   end;
    362 end;
    363 
    364 
     377    EmitLn('');
     378  end;
     379end;
    365380
    366381end.
Note: See TracChangeset for help on using the changeset viewer.