Ignore:
Timestamp:
Aug 5, 2010, 3:13:03 PM (14 years ago)
Author:
george
Message:

Enhanced C code generation.

File:
1 edited

Legend:

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

    r36 r42  
    77uses
    88  SysUtils, Variants, Classes, Graphics, Controls, Forms,
    9   Dialogs, StdCtrls, UPascalSource, UCodeProducer;
     9  Dialogs, StdCtrls, UPascalSource, UCodeProducer, StrUtils;
    1010
    1111type
     12
     13  { TCProducer }
     14
    1215  TCProducer = class(TCodeProducer)
     16  private
     17    function TranslateType(Name: string): string;
     18    function TranslateOperator(Name: string): string;
     19    procedure Emit(Text: string);
     20    procedure GenerateCommonBlock(CommonBlock: TCommonBlock;
     21      LabelPrefix: string);
     22    procedure GenerateProgram(ProgramBlock: TProgram);
     23    procedure GenerateBeginEnd(BeginEnd: TBeginEnd);
     24    procedure GenerateCommand(Command: TCommand);
     25    procedure GenerateWhileDo(WhileDo: TWhileDo);
     26    procedure GenerateIfThenElse(IfThenElse: TIfThenElse);
     27    procedure GenerateAssignment(Assignment: TAssignment);
     28    procedure GenerateMethodCall(MethodCall: TMethodCall);
     29    function GenerateExpression(Expression: TExpression): string;
     30  public
    1331    TextSource: TStringList;
     32    IndentationLength: Integer;
     33    Indetation: Integer;
    1434    procedure Produce; override;
    1535    constructor Create;
    1636    destructor Destroy; override;
    17   private
    18     procedure GenerateCommonBlock(CommonBlock: TCommonBlock;
    19       LabelPrefix: string);
    20     procedure GenerateProgram(ProgramBlock: TProgram);
    2137  end;
    2238
     
    2844begin
    2945  TextSource := TStringList.Create;
     46  IndentationLength := 2;
    3047end;
    3148
     
    3451  TextSource.Free;
    3552  inherited;
     53end;
     54
     55function TCProducer.TranslateType(Name: string): string;
     56begin
     57  if Name = 'Byte' then Result := 'uint8_t';
     58  if Name = 'Word' then Result := 'uint16_t';
     59  if Name = 'Void' then Result := 'void';
     60end;
     61
     62function TCProducer.TranslateOperator(Name: string): string;
     63begin
     64  if Name = '=' then Result := '=='
     65  else if Name = 'shl' then Result := '<<'
     66  else if Name = 'shr' then Result := '>>'
     67  else if Name = 'not' then Result := '!'
     68  else if Name = 'mod' then Result := '^'
     69  else if Name = ':=' then Result := '='
     70  else if Name = '@' then Result := '*'
     71  else if Name = 'and' then Result := '&'
     72  else if Name = 'or' then Result := '|'
     73  else if Name = 'xor' then Result := '^'
     74  else Result := Name;
     75end;
     76
     77procedure TCProducer.Emit(Text: string);
     78begin
     79  TextSource.Add(DupeString(' ', IndentationLength * Indetation) + Text);
    3680end;
    3781
     
    4791  I: Integer;
    4892begin
     93  Indetation := 0;;
    4994  with ProgramBlock do
    5095  for I := 0 to Modules.Count - 1 do
    5196    GenerateCommonBlock(TModule(Modules[I]), '');
     97end;
     98
     99procedure TCProducer.GenerateBeginEnd(BeginEnd: TBeginEnd);
     100var
     101  I: Integer;
     102begin
     103  Emit('{');
     104  Inc(Indetation);
     105
     106
     107  // Variables
     108  if BeginEnd.Parent is TCommonBlock then begin
     109    for I := 0 to BeginEnd.CommonBlock.Variables.Count - 1 do
     110    with TVariable(BeginEnd.CommonBlock.Variables[I]) do
     111      Emit(TranslateType(ValueType.Name) + ' ' + Name);
     112    Emit('');
     113  end;
     114
     115  // Commands
     116  for I := 0 to BeginEnd.Commands.Count - 1 do
     117    GenerateCommand(TCommand(BeginEnd.Commands[I]));
     118
     119  Dec(Indetation);
     120  Emit('}');
     121end;
     122
     123procedure TCProducer.GenerateCommand(Command: TCommand);
     124begin
     125  if Command is TBeginEnd then GenerateBeginEnd(TBeginEnd(Command))
     126  else if Command is TWhileDo then GenerateWhileDo(TWhileDo(Command))
     127  else if Command is TIfThenElse then GenerateIfThenElse(TIfThenElse(Command))
     128  else if Command is TAssignment then GenerateAssignment(TAssignment(Command))
     129  else if Command is TMethodCall then GenerateMethodCall(TMethodCall(Command));
     130end;
     131
     132procedure TCProducer.GenerateWhileDo(WhileDo: TWhileDo);
     133begin
     134  Emit('while (' + GenerateExpression(WhileDo.Condition) + ')');
     135  GenerateCommand(WhileDo.Command);
     136end;
     137
     138procedure TCProducer.GenerateIfThenElse(IfThenElse: TIfThenElse);
     139begin
     140  Emit('if(' + GenerateExpression(IfThenElse.Condition) + ')');
     141  GenerateCommand(IfThenElse.Command);
     142  Emit('else ');
     143  GenerateCommand(IfThenElse.ElseCommand);
     144end;
     145
     146procedure TCProducer.GenerateAssignment(Assignment: TAssignment);
     147begin
     148  Emit(Assignment.Target.Name + ' = ' + GenerateExpression(Assignment.Source) + ';');
     149end;
     150
     151procedure TCProducer.GenerateMethodCall(MethodCall: TMethodCall);
     152begin
     153  Emit(MethodCall.Method.Name + '();');
     154end;
     155
     156function TCProducer.GenerateExpression(Expression: TExpression): string;
     157begin
     158  case Expression.NodeType of
     159    ntConstant: Result := Expression.Value;
     160    ntVariable: Result := Expression.Variable.Name;
     161    ntFunction: Result := Expression.Method.Name;
     162    ntOperator: begin
     163      Result := GenerateExpression(TExpression(Expression.SubItems.First))
     164        + ' ' + TranslateOperator(Expression.OperatorName) + ' ' +
     165        GenerateExpression(TExpression(Expression.SubItems.Last));
     166    end;
     167    ntNone: ;
     168  end;
    52169end;
    53170
     
    58175begin
    59176  with CommonBlock do begin
    60     TextSource.Add('void ' + Name + '()');
    61     TextSource.Add('{');
    62 
    63     TextSource.Add('}');
     177    Emit('void ' + Name + '()');
     178    GenerateBeginEnd(Code);
    64179  end;
    65180end;
Note: See TracChangeset for help on using the changeset viewer.