Ignore:
Timestamp:
Aug 9, 2010, 1:53:33 PM (14 years ago)
Author:
george
Message:

Added support of parameters for function call.

File:
1 edited

Legend:

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

    r47 r48  
    1515  TProducerPascal = class(TCodeProducer)
    1616  private
    17     procedure Emit(Text: string);
     17    procedure Emit(Text: string; NewLine: Boolean = True);
    1818    procedure GenerateUses(UsedModules: TUsedModuleList);
    1919    procedure GenerateModule(Module: TModule);
     
    2222    procedure GenerateProgram(ProgramBlock: TProgram);
    2323    procedure GenerateFunctions(Functions: TFunctionList);
     24    procedure GenerateConstants(Constants: TConstantList);
    2425    procedure GenerateBeginEnd(BeginEnd: TBeginEnd);
    2526    procedure GenerateVariableList(Variables: TVariableList);
     
    2829    procedure GenerateIfThenElse(IfThenElse: TIfThenElse);
    2930    procedure GenerateAssignment(Assignment: TAssignment);
    30     procedure GenerateMethodCall(MethodCall: TFunctionCall);
     31    procedure GenerateFunctionCall(ConstantCall: TFunctionCall);
    3132    function GenerateExpression(Expression: TExpression): string;
    3233  public
     
    5556end;
    5657
    57 procedure TProducerPascal.Emit(Text: string);
    58 begin
    59   TextSource.Add(DupeString(' ', IndentationLength * Indetation) + Text);
     58procedure TProducerPascal.Emit(Text: string; NewLine: Boolean = True);
     59begin
     60  if NewLine then TextSource.Add(DupeString(' ', IndentationLength * Indetation) + Text)
     61    else TextSource.Strings[TextSource.Count - 1] := TextSource.Strings[TextSource.Count - 1] + Text;
    6062end;
    6163
     
    6870  for I := 0 to UsedModules.Count - 1 do begin
    6971    Line := Line + TUsedModule(UsedModules[I]).Name;
    70     if I < UsedModules.Count then Line := Line + ', ';
    71   end;
    72   Emit(Line);
     72    if I < UsedModules.Count - 1 then Line := Line + ', ';
     73  end;
     74  Emit(Line + ';');
    7375  Emit('');
    7476end;
     
    7880  GenerateUses(Module.UsedModules);
    7981  GenerateCommonBlock(Module, '');
     82  Emit(';', False);
    8083end;
    8184
     
    125128end;
    126129
     130procedure TProducerPascal.GenerateConstants(Constants: TConstantList);
     131var
     132  I: Integer;
     133begin
     134  Emit('const');
     135  Inc(Indetation);
     136  for I := 0 to Constants.Count - 1 do
     137  with TConstant(Constants[I]) do
     138    Emit(Name + ': ' + ValueType.Name + ' = ' + Value + ';');
     139  Dec(Indetation);
     140  Emit('');
     141end;
     142
    127143procedure TProducerPascal.GenerateBeginEnd(BeginEnd: TBeginEnd);
    128144var
     
    132148  Inc(Indetation);
    133149  // Commands
    134   for I := 0 to BeginEnd.Commands.Count - 1 do
     150  for I := 0 to BeginEnd.Commands.Count - 1 do begin
    135151    GenerateCommand(TCommand(BeginEnd.Commands[I]));
     152    Emit(';', False);
     153  end;
    136154
    137155  Dec(Indetation);
    138   Emit('end;');
     156  Emit('end');
    139157end;
    140158
     
    144162begin
    145163  Emit('var');
     164  Inc(Indetation);
    146165  for I := 0 to Variables.Count - 1 do
    147166  with TVariable(Variables[I]) do
    148167    Emit(Name + ': ' + ValueType.Name + ';');
     168  Dec(Indetation);
    149169  Emit('');
    150170end;
     
    156176  else if Command is TIfThenElse then GenerateIfThenElse(TIfThenElse(Command))
    157177  else if Command is TAssignment then GenerateAssignment(TAssignment(Command))
    158   else if Command is TFunctionCall then GenerateMethodCall(TFunctionCall(Command));
     178  else if Command is TFunctionCall then GenerateFunctionCall(TFunctionCall(Command));
    159179end;
    160180
     
    177197procedure TProducerPascal.GenerateAssignment(Assignment: TAssignment);
    178198begin
    179   Emit(Assignment.Target.Name + ' := ' + GenerateExpression(Assignment.Source) + ';');
    180 end;
    181 
    182 procedure TProducerPascal.GenerateMethodCall(MethodCall: TFunctionCall);
    183 begin
    184   Emit(MethodCall.FunctionRef.Name + ';');
     199  Emit(Assignment.Target.Name + ' := ' + GenerateExpression(Assignment.Source));
     200end;
     201
     202procedure TProducerPascal.GenerateFunctionCall(ConstantCall: TFunctionCall);
     203var
     204  Line: string;
     205  I: Integer;
     206begin
     207  with ConstantCall do begin
     208    Line := FunctionRef.Name;
     209    if ParameterExpression.Count > 0 then begin
     210      Line := Line + '(';
     211      for I := 0 to ParameterExpression.Count - 1 do begin
     212        Line := Line + GenerateExpression(TExpression(ParameterExpression[I]));
     213        if I < ParameterExpression.Count - 1 then Line := Line + ', ';
     214      end;
     215      Line := Line + ')';
     216    end;
     217  end;
     218  Emit(Line);
    185219end;
    186220
     
    206240begin
    207241  with CommonBlock do begin
    208     GenerateFunctions(Methods);
    209     Emit('procedure ' + Name + '');
     242    GenerateFunctions(Functions);
     243    GenerateConstants(Constants);
     244    GenerateVariableList(Variables);
    210245    GenerateBeginEnd(Code);
    211246  end;
Note: See TracChangeset for help on using the changeset viewer.