Ignore:
Timestamp:
Aug 9, 2010, 12:48:14 PM (14 years ago)
Author:
george
Message:

Support for function definition with result type.

File:
1 edited

Legend:

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

    r46 r47  
    77uses
    88  SysUtils, Variants, Classes, Graphics, Controls, Forms,
    9   Dialogs, StdCtrls, UPascalSource, UCodeProducer, StrUtils;
     9  Dialogs, UPascalSource, UCodeProducer, StrUtils;
    1010
    1111type
     
    2121      LabelPrefix: string);
    2222    procedure GenerateProgram(ProgramBlock: TProgram);
    23     procedure GenerateMethods(Methods: TFunctionList);
     23    procedure GenerateFunctions(Functions: TFunctionList);
    2424    procedure GenerateBeginEnd(BeginEnd: TBeginEnd);
    2525    procedure GenerateVariableList(Variables: TVariableList);
     
    2828    procedure GenerateIfThenElse(IfThenElse: TIfThenElse);
    2929    procedure GenerateAssignment(Assignment: TAssignment);
    30     procedure GenerateMethodCall(MethodCall: TMethodCall);
     30    procedure GenerateMethodCall(MethodCall: TFunctionCall);
    3131    function GenerateExpression(Expression: TExpression): string;
    3232  public
     
    9797end;
    9898
    99 procedure TProducerPascal.GenerateMethods(Methods: TFunctionList);
    100 var
    101   I: Integer;
    102 begin
    103   for I := 0 to Methods.Count - 1 do
    104   with TFunction(Methods[I]) do
     99procedure TProducerPascal.GenerateFunctions(Functions: TFunctionList);
     100var
     101  I: Integer;
     102  P: Integer;
     103  Line: string;
     104begin
     105  for I := 0 to Functions.Count - 1 do
     106  with TFunction(Functions[I]) do
    105107  begin
    106     Emit('procedure ' + Name + '');
     108    if HaveResult then
     109      Line := 'function ' + Name
     110      else Line := 'procedure ' + Name;
     111    if Parameters.Count > 0 then begin
     112      Line := Line + '(';
     113      for P := 0 to Parameters.Count - 1 do begin
     114        with TParameter(Parameters[P]) do
     115          Line := Line + Name + ': ' + ValueType.Name;
     116        if P < (Parameters.Count - 1) then Line := Line + '; ';
     117      end;
     118      Line := Line + ')';
     119    end;
     120    if HaveResult then Line := Line + ': ' + ResultType.Name;
     121    Emit(Line + ';');
    107122    GenerateBeginEnd(Code);
    108123    Emit('');
     
    121136
    122137  Dec(Indetation);
    123   Emit('end');
     138  Emit('end;');
    124139end;
    125140
     
    141156  else if Command is TIfThenElse then GenerateIfThenElse(TIfThenElse(Command))
    142157  else if Command is TAssignment then GenerateAssignment(TAssignment(Command))
    143   else if Command is TMethodCall then GenerateMethodCall(TMethodCall(Command));
     158  else if Command is TFunctionCall then GenerateMethodCall(TFunctionCall(Command));
    144159end;
    145160
     
    154169  Emit('if ' + GenerateExpression(IfThenElse.Condition) + ' then ');
    155170  GenerateCommand(IfThenElse.Command);
    156   Emit('else ');
    157   GenerateCommand(IfThenElse.ElseCommand);
     171  if Assigned(IfThenElse.ElseCommand) then begin
     172    Emit('else ');
     173    GenerateCommand(IfThenElse.ElseCommand);
     174  end;
    158175end;
    159176
    160177procedure TProducerPascal.GenerateAssignment(Assignment: TAssignment);
    161178begin
    162   Emit(Assignment.Target.Name + ' = ' + GenerateExpression(Assignment.Source) + ';');
    163 end;
    164 
    165 procedure TProducerPascal.GenerateMethodCall(MethodCall: TMethodCall);
    166 begin
    167   Emit(MethodCall.Method.Name + ';');
     179  Emit(Assignment.Target.Name + ' := ' + GenerateExpression(Assignment.Source) + ';');
     180end;
     181
     182procedure TProducerPascal.GenerateMethodCall(MethodCall: TFunctionCall);
     183begin
     184  Emit(MethodCall.FunctionRef.Name + ';');
    168185end;
    169186
     
    189206begin
    190207  with CommonBlock do begin
    191     GenerateMethods(Methods);
     208    GenerateFunctions(Methods);
    192209    Emit('procedure ' + Name + '');
    193210    GenerateBeginEnd(Code);
Note: See TracChangeset for help on using the changeset viewer.