Changeset 48


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

Added support of parameters for function call.

Location:
branches/DelphiToC
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • branches/DelphiToC/Analyze/UPascalParser.pas

    r47 r48  
    4949  TPascalParser = class(TBaseParser)
    5050    procedure ParseWhileDo(SourceCode: TWhileDo);
    51     function ParseExpression(SourceCode: TExpression): TExpression;
     51    procedure ParseExpression(SourceCode: TExpression);
    5252    procedure ParseUsedModuleList(SourceCode: TUsedModuleList);
    5353    procedure ParseModule(SourceCode: TModule);
     
    290290{ TExpression }
    291291
    292 function TPascalParser.ParseExpression(SourceCode: TExpression): TExpression;
     292procedure TPascalParser.ParseExpression(SourceCode: TExpression);
    293293var
    294294  Identifier: string;
     
    340340            end;
    341341          end else begin
    342             Method := CommonBlock.Methods.Search(Identifier);
     342            Method := CommonBlock.Functions.Search(Identifier);
    343343            if Assigned(Method) then begin
    344344              // Referenced method
     
    444444  LoopVariable: TVariable;
    445445  IdentName: string;
     446  FunctionName: string;
    446447begin
    447448  begin
     
    465466    if IsIdentificator(FNextToken) then begin
    466467      if Assigned(SourceCode.Variables.Search(FNextToken)) then begin
     468        // Variable assignment
    467469        Result := TAssignment.Create;
    468470        TAssignment(Result).CommonBlock := SourceCode;
     
    474476        ParseExpression(TAssignment(Result).Source);
    475477      end else
    476       if Assigned(SourceCode.Methods.Search(FNextToken)) then begin
     478      if Assigned(SourceCode.Functions.Search(FNextToken)) then begin
     479        // Function call
     480        FunctionName := ReadCode;
    477481        Result := TFunctionCall.Create;
    478482        TFunctionCall(Result).CommonBlock := SourceCode;
    479         TFunctionCall(Result).FunctionRef := SourceCode.Methods.Search(FNextToken);
    480         ReadCode;
    481   //      ParseMetVariable(TFunctionCall(Result).Target);
     483        TFunctionCall(Result).FunctionRef := SourceCode.Functions.Search(FunctionName);
     484        if FNextToken = '(' then begin
     485          Expect('(');
     486          with TFunctionCall(Result) do begin
     487            ParameterExpression.Add(TExpression.Create);
     488            TExpression(ParameterExpression.Last).CommonBlock := SourceCode;
     489            ParseExpression(TExpression(ParameterExpression.Last));
     490          end;
     491          Expect(')');
     492        end;
    482493      end else begin
    483494        Result := nil;
     
    561572        ParseTypeList(Types)
    562573      else if FNextToken = 'procedure' then
    563         ParseFunctionList(Methods)
     574        ParseFunctionList(Functions)
    564575      else if FNextToken = 'function' then
    565         ParseFunctionList(Methods)
     576        ParseFunctionList(Functions)
    566577      else begin
    567578        ParseBeginEnd(Code);
     
    583594    Expect('begin');
    584595    while (FNextToken <> 'end') and (FNextTokenType <> ttEndOfFile) do begin
    585       Commands.Add(nil);
    586596      NewCommand := ParseCommand(CommonBlock);
    587597      if Assigned(NewCommand) then Commands.Add(NewCommand);
  • branches/DelphiToC/DelphiToC.lpi

    r47 r48  
    4848        <TopLine Value="4"/>
    4949        <CursorPos X="1" Y="29"/>
    50         <UsageCount Value="107"/>
     50        <UsageCount Value="110"/>
    5151        <Loaded Value="True"/>
    5252      </Unit0>
     
    6262        <TopLine Value="56"/>
    6363        <CursorPos X="1" Y="79"/>
    64         <UsageCount Value="107"/>
     64        <UsageCount Value="110"/>
    6565        <Loaded Value="True"/>
    6666        <LoadedDesigner Value="True"/>
     
    7373        <TopLine Value="1"/>
    7474        <CursorPos X="1" Y="1"/>
    75         <UsageCount Value="107"/>
     75        <UsageCount Value="110"/>
    7676      </Unit2>
    7777      <Unit3>
     
    8181        <EditorIndex Value="5"/>
    8282        <WindowIndex Value="0"/>
    83         <TopLine Value="221"/>
    84         <CursorPos X="25" Y="240"/>
    85         <UsageCount Value="107"/>
     83        <TopLine Value="264"/>
     84        <CursorPos X="5" Y="277"/>
     85        <UsageCount Value="110"/>
    8686        <Loaded Value="True"/>
    8787      </Unit3>
     
    9292        <EditorIndex Value="13"/>
    9393        <WindowIndex Value="0"/>
    94         <TopLine Value="36"/>
    95         <CursorPos X="1" Y="41"/>
    96         <UsageCount Value="107"/>
     94        <TopLine Value="76"/>
     95        <CursorPos X="53" Y="96"/>
     96        <UsageCount Value="110"/>
    9797        <Loaded Value="True"/>
    9898      </Unit4>
     
    105105        <TopLine Value="112"/>
    106106        <CursorPos X="48" Y="128"/>
    107         <UsageCount Value="107"/>
     107        <UsageCount Value="110"/>
    108108        <Loaded Value="True"/>
    109109      </Unit5>
     
    116116        <TopLine Value="1"/>
    117117        <CursorPos X="15" Y="13"/>
    118         <UsageCount Value="107"/>
     118        <UsageCount Value="110"/>
    119119        <Loaded Value="True"/>
    120120      </Unit6>
     
    125125        <EditorIndex Value="10"/>
    126126        <WindowIndex Value="0"/>
    127         <TopLine Value="171"/>
    128         <CursorPos X="45" Y="175"/>
    129         <UsageCount Value="107"/>
     127        <TopLine Value="89"/>
     128        <CursorPos X="44" Y="96"/>
     129        <UsageCount Value="110"/>
    130130        <Loaded Value="True"/>
    131131      </Unit7>
     
    137137        <EditorIndex Value="0"/>
    138138        <WindowIndex Value="0"/>
    139         <TopLine Value="641"/>
    140         <CursorPos X="45" Y="653"/>
    141         <UsageCount Value="107"/>
     139        <TopLine Value="588"/>
     140        <CursorPos X="1" Y="596"/>
     141        <UsageCount Value="110"/>
    142142        <Loaded Value="True"/>
    143143      </Unit8>
     
    147147        <TopLine Value="1"/>
    148148        <CursorPos X="1" Y="1"/>
    149         <UsageCount Value="1"/>
     149        <UsageCount Value="0"/>
    150150        <DefaultSyntaxHighlighter Value="LFM"/>
    151151      </Unit9>
     
    157157        <TopLine Value="78"/>
    158158        <CursorPos X="14" Y="91"/>
    159         <UsageCount Value="54"/>
     159        <UsageCount Value="55"/>
    160160        <Loaded Value="True"/>
    161161      </Unit10>
     
    166166        <TopLine Value="1555"/>
    167167        <CursorPos X="1" Y="1"/>
    168         <UsageCount Value="5"/>
     168        <UsageCount Value="4"/>
    169169      </Unit11>
    170170      <Unit12>
     
    174174        <TopLine Value="61"/>
    175175        <CursorPos X="7" Y="68"/>
    176         <UsageCount Value="37"/>
     176        <UsageCount Value="38"/>
    177177        <Loaded Value="True"/>
    178178      </Unit12>
     
    183183        <TopLine Value="139"/>
    184184        <CursorPos X="16" Y="146"/>
    185         <UsageCount Value="37"/>
     185        <UsageCount Value="38"/>
    186186        <Loaded Value="True"/>
    187187      </Unit13>
     
    191191        <TopLine Value="934"/>
    192192        <CursorPos X="10" Y="947"/>
    193         <UsageCount Value="9"/>
     193        <UsageCount Value="8"/>
    194194      </Unit14>
    195195      <Unit15>
     
    198198        <TopLine Value="834"/>
    199199        <CursorPos X="11" Y="847"/>
    200         <UsageCount Value="5"/>
     200        <UsageCount Value="4"/>
    201201      </Unit15>
    202202      <Unit16>
     
    206206        <TopLine Value="153"/>
    207207        <CursorPos X="8" Y="166"/>
    208         <UsageCount Value="12"/>
     208        <UsageCount Value="13"/>
    209209        <Loaded Value="True"/>
    210210      </Unit16>
     
    215215        <EditorIndex Value="2"/>
    216216        <WindowIndex Value="0"/>
    217         <TopLine Value="178"/>
    218         <CursorPos X="67" Y="190"/>
    219         <UsageCount Value="69"/>
     217        <TopLine Value="247"/>
     218        <CursorPos X="56" Y="267"/>
     219        <UsageCount Value="72"/>
    220220        <Loaded Value="True"/>
    221221      </Unit17>
     
    226226        <TopLine Value="2656"/>
    227227        <CursorPos X="3" Y="2669"/>
    228         <UsageCount Value="5"/>
     228        <UsageCount Value="4"/>
    229229      </Unit18>
    230230      <Unit19>
     
    232232        <EditorIndex Value="11"/>
    233233        <WindowIndex Value="0"/>
    234         <TopLine Value="263"/>
    235         <CursorPos X="14" Y="276"/>
    236         <UsageCount Value="12"/>
     234        <TopLine Value="559"/>
     235        <CursorPos X="57" Y="571"/>
     236        <UsageCount Value="13"/>
    237237        <Loaded Value="True"/>
    238238      </Unit19>
     
    243243        <EditorIndex Value="8"/>
    244244        <WindowIndex Value="0"/>
    245         <TopLine Value="103"/>
    246         <CursorPos X="21" Y="116"/>
    247         <UsageCount Value="23"/>
     245        <TopLine Value="138"/>
     246        <CursorPos X="52" Y="151"/>
     247        <UsageCount Value="26"/>
    248248        <Loaded Value="True"/>
    249249      </Unit20>
    250250    </Units>
    251     <JumpHistory Count="29" HistoryIndex="28">
     251    <JumpHistory Count="30" HistoryIndex="29">
    252252      <Position1>
    253         <Filename Value="UPascalSource.pas"/>
    254         <Caret Line="441" Column="20" TopLine="429"/>
     253        <Filename Value="Produce\UProducerPascal.pas"/>
     254        <Caret Line="245" Column="17" TopLine="226"/>
    255255      </Position1>
    256256      <Position2>
    257         <Filename Value="UPascalSource.pas"/>
    258         <Caret Line="448" Column="49" TopLine="432"/>
     257        <Filename Value="Produce\UProducerPascal.pas"/>
     258        <Caret Line="152" Column="1" TopLine="143"/>
    259259      </Position2>
    260260      <Position3>
    261         <Filename Value="Analyze\UPascalParser.pas"/>
    262         <Caret Line="329" Column="1" TopLine="316"/>
     261        <Filename Value="Produce\UProducerPascal.pas"/>
     262        <Caret Line="59" Column="1" TopLine="46"/>
    263263      </Position3>
    264264      <Position4>
    265         <Filename Value="UPascalSource.pas"/>
    266         <Caret Line="435" Column="1" TopLine="430"/>
     265        <Filename Value="Produce\UProducerPascal.pas"/>
     266        <Caret Line="60" Column="1" TopLine="46"/>
    267267      </Position4>
    268268      <Position5>
    269         <Filename Value="UPascalSource.pas"/>
    270         <Caret Line="436" Column="1" TopLine="430"/>
     269        <Filename Value="Produce\UProducerPascal.pas"/>
     270        <Caret Line="61" Column="1" TopLine="46"/>
    271271      </Position5>
    272272      <Position6>
    273         <Filename Value="UPascalSource.pas"/>
    274         <Caret Line="437" Column="1" TopLine="430"/>
     273        <Filename Value="Produce\UProducerPascal.pas"/>
     274        <Caret Line="59" Column="1" TopLine="46"/>
    275275      </Position6>
    276276      <Position7>
    277         <Filename Value="UPascalSource.pas"/>
    278         <Caret Line="438" Column="1" TopLine="430"/>
     277        <Filename Value="Produce\UProducerPascal.pas"/>
     278        <Caret Line="62" Column="1" TopLine="46"/>
    279279      </Position7>
    280280      <Position8>
    281         <Filename Value="UPascalSource.pas"/>
    282         <Caret Line="440" Column="1" TopLine="430"/>
     281        <Filename Value="Produce\UProducerPascal.pas"/>
     282        <Caret Line="59" Column="1" TopLine="46"/>
    283283      </Position8>
    284284      <Position9>
    285         <Filename Value="UPascalSource.pas"/>
    286         <Caret Line="441" Column="1" TopLine="430"/>
     285        <Filename Value="Produce\UProducerPascal.pas"/>
     286        <Caret Line="62" Column="1" TopLine="46"/>
    287287      </Position9>
    288288      <Position10>
    289         <Filename Value="UPascalSource.pas"/>
    290         <Caret Line="442" Column="1" TopLine="430"/>
     289        <Filename Value="Produce\UProducerPascal.pas"/>
     290        <Caret Line="150" Column="1" TopLine="137"/>
    291291      </Position10>
    292292      <Position11>
    293         <Filename Value="UPascalSource.pas"/>
    294         <Caret Line="446" Column="1" TopLine="430"/>
     293        <Filename Value="Produce\UProducerPascal.pas"/>
     294        <Caret Line="152" Column="8" TopLine="134"/>
    295295      </Position11>
    296296      <Position12>
    297         <Filename Value="UPascalSource.pas"/>
    298         <Caret Line="447" Column="1" TopLine="430"/>
     297        <Filename Value="Produce\UProducerPascal.pas"/>
     298        <Caret Line="61" Column="18" TopLine="58"/>
    299299      </Position12>
    300300      <Position13>
    301         <Filename Value="UPascalSource.pas"/>
    302         <Caret Line="448" Column="1" TopLine="430"/>
     301        <Filename Value="Produce\UProducerPascal.pas"/>
     302        <Caret Line="34" Column="23" TopLine="21"/>
    303303      </Position13>
    304304      <Position14>
    305         <Filename Value="UPascalSource.pas"/>
    306         <Caret Line="452" Column="1" TopLine="431"/>
     305        <Filename Value="E:\Programy\Lazarus\fpc\2.4.0\source\rtl\objpas\classes\classesh.inc"/>
     306        <Caret Line="604" Column="27" TopLine="591"/>
    307307      </Position14>
    308308      <Position15>
    309         <Filename Value="UPascalSource.pas"/>
    310         <Caret Line="453" Column="1" TopLine="432"/>
     309        <Filename Value="Produce\UProducerPascal.pas"/>
     310        <Caret Line="152" Column="6" TopLine="135"/>
    311311      </Position15>
    312312      <Position16>
    313         <Filename Value="UPascalSource.pas"/>
    314         <Caret Line="455" Column="1" TopLine="434"/>
     313        <Filename Value="Produce\UProducerPascal.pas"/>
     314        <Caret Line="8" Column="49" TopLine="1"/>
    315315      </Position16>
    316316      <Position17>
    317         <Filename Value="Analyze\UPascalParser.pas"/>
    318         <Caret Line="329" Column="1" TopLine="316"/>
     317        <Filename Value="Produce\UProducerPascal.pas"/>
     318        <Caret Line="9" Column="51" TopLine="1"/>
    319319      </Position17>
    320320      <Position18>
    321         <Filename Value="UPascalSource.pas"/>
    322         <Caret Line="448" Column="1" TopLine="436"/>
     321        <Filename Value="Produce\UProducerPascal.pas"/>
     322        <Caret Line="74" Column="18" TopLine="61"/>
    323323      </Position18>
    324324      <Position19>
    325         <Filename Value="UPascalSource.pas"/>
    326         <Caret Line="544" Column="1" TopLine="531"/>
     325        <Filename Value="Produce\UProducerPascal.pas"/>
     326        <Caret Line="82" Column="11" TopLine="61"/>
    327327      </Position19>
    328328      <Position20>
    329         <Filename Value="UPascalSource.pas"/>
    330         <Caret Line="545" Column="1" TopLine="531"/>
     329        <Filename Value="Produce\UProducerPascal.pas"/>
     330        <Caret Line="124" Column="20" TopLine="105"/>
    331331      </Position20>
    332332      <Position21>
    333         <Filename Value="UPascalSource.pas"/>
    334         <Caret Line="546" Column="62" TopLine="532"/>
     333        <Filename Value="Produce\UProducerPascal.pas"/>
     334        <Caret Line="126" Column="1" TopLine="105"/>
    335335      </Position21>
    336336      <Position22>
    337337        <Filename Value="Produce\UProducerPascal.pas"/>
    338         <Caret Line="154" Column="51" TopLine="139"/>
     338        <Caret Line="138" Column="60" TopLine="125"/>
    339339      </Position22>
    340340      <Position23>
    341         <Filename Value="DelphiToC.lpr"/>
    342         <Caret Line="15" Column="42" TopLine="4"/>
     341        <Filename Value="Produce\UProducerPascal.pas"/>
     342        <Caret Line="152" Column="13" TopLine="139"/>
    343343      </Position23>
    344344      <Position24>
    345         <Filename Value="UPascalSource.pas"/>
    346         <Caret Line="241" Column="20" TopLine="221"/>
     345        <Filename Value="Produce\UProducerPascal.pas"/>
     346        <Caret Line="7" Column="62" TopLine="1"/>
    347347      </Position24>
    348348      <Position25>
    349         <Filename Value="Analyze\UPascalParser.pas"/>
    350         <Caret Line="565" Column="22" TopLine="552"/>
     349        <Filename Value="Produce\UProducerPascal.pas"/>
     350        <Caret Line="74" Column="18" TopLine="61"/>
    351351      </Position25>
    352352      <Position26>
    353         <Filename Value="Visual\USourceTree.pas"/>
    354         <Caret Line="194" Column="57" TopLine="186"/>
     353        <Filename Value="Produce\UProducerPascal.pas"/>
     354        <Caret Line="82" Column="11" TopLine="61"/>
    355355      </Position26>
    356356      <Position27>
    357357        <Filename Value="Produce\UProducerPascal.pas"/>
    358         <Caret Line="120" Column="1" TopLine="99"/>
     358        <Caret Line="124" Column="20" TopLine="111"/>
    359359      </Position27>
    360360      <Position28>
    361         <Filename Value="Analyze\UPascalParser.pas"/>
    362         <Caret Line="653" Column="28" TopLine="632"/>
     361        <Filename Value="Produce\UProducerPascal.pas"/>
     362        <Caret Line="138" Column="60" TopLine="125"/>
    363363      </Position28>
    364364      <Position29>
    365         <Filename Value="Analyze\UPascalParser.pas"/>
    366         <Caret Line="654" Column="40" TopLine="632"/>
     365        <Filename Value="Produce\UProducerPascal.pas"/>
     366        <Caret Line="152" Column="13" TopLine="139"/>
    367367      </Position29>
     368      <Position30>
     369        <Filename Value="Produce\UProducerPascal.pas"/>
     370        <Caret Line="171" Column="47" TopLine="154"/>
     371      </Position30>
    368372    </JumpHistory>
    369373  </ProjectOptions>
  • branches/DelphiToC/Example.pas

    r47 r48  
    1919  D: Byte;
    2020begin
    21   WriteLn;
     21  WriteLn(A);
    2222  begin
    2323    WriteLn;
  • branches/DelphiToC/Produce/UProducerC.pas

    r47 r48  
    2323      LabelPrefix: string);
    2424    procedure GenerateProgram(ProgramBlock: TProgram);
    25     procedure GenerateMethods(Methods: TFunctionList);
     25    procedure GenerateFunctions(Functions: TFunctionList);
    2626    procedure GenerateBeginEnd(BeginEnd: TBeginEnd);
    2727    procedure GenerateCommand(Command: TCommand);
     
    2929    procedure GenerateIfThenElse(IfThenElse: TIfThenElse);
    3030    procedure GenerateAssignment(Assignment: TAssignment);
    31     procedure GenerateMethodCall(MethodCall: TFunctionCall);
     31    procedure GenerateFunctionCall(FunctionCall: TFunctionCall);
    3232    function GenerateExpression(Expression: TExpression): string;
    3333  public
     
    5858function TCProducer.TranslateType(Name: string): string;
    5959begin
    60   if Name = 'Byte' then Result := 'uint8_t';
    61   if Name = 'Word' then Result := 'uint16_t';
    62   if Name = 'Void' then Result := 'void';
     60  if Name = 'Byte' then Result := 'uint8'
     61  else if Name = 'ShortInt' then Result := 'int8'
     62  else if Name = 'Word' then Result := 'int16'
     63  else if Name = 'SmallInt' then Result := 'int16'
     64  else if Name = 'Cardinal' then Result := 'uint32'
     65  else if Name = 'Integer' then Result := 'int32'
     66  else if Name = 'Void' then Result := 'void';
    6367end;
    6468
     
    115119end;
    116120
    117 procedure TCProducer.GenerateMethods(Methods: TFunctionList);
    118 var
    119   I: Integer;
    120 begin
    121   for I := 0 to Methods.Count - 1 do
    122   with TFunction(Methods[I]) do
     121procedure TCProducer.GenerateFunctions(Functions: TFunctionList);
     122var
     123  I: Integer;
     124  J: Integer;
     125  Line: string;
     126begin
     127  for I := 0 to Functions.Count - 1 do
     128  with TFunction(Functions[I]) do
    123129  begin
    124     Emit('void ' + Name + '()');
     130    if HaveResult then Line := TranslateType(ResultType.Name) + ' '
     131      else Line := 'void ';
     132    Line := Line + Name + '(';
     133    if Parameters.Count > 0 then
     134    for J := 0 to Parameters.Count - 1 do begin
     135      Line := Line + TranslateType(TParameter(Parameters[J]).ValueType.Name) +
     136        ' ' + TParameter(Parameters[J]).Name;
     137      if J < Parameters.Count - 1 then Line := Line + ', ';
     138    end;
     139    Line := Line + ')';
     140    Emit(Line);
    125141    GenerateBeginEnd(Code);
    126142    Emit('');
     
    158174  else if Command is TIfThenElse then GenerateIfThenElse(TIfThenElse(Command))
    159175  else if Command is TAssignment then GenerateAssignment(TAssignment(Command))
    160   else if Command is TFunctionCall then GenerateMethodCall(TFunctionCall(Command));
     176  else if Command is TFunctionCall then GenerateFunctionCall(TFunctionCall(Command));
    161177end;
    162178
     
    182198end;
    183199
    184 procedure TCProducer.GenerateMethodCall(MethodCall: TFunctionCall);
    185 begin
    186   Emit(MethodCall.FunctionRef.Name + '();');
     200procedure TCProducer.GenerateFunctionCall(FunctionCall: TFunctionCall);
     201var
     202  Line: string;
     203begin
     204  Line := FunctionCall.FunctionRef.Name + '(';
     205  Line := Line + ');';
     206  Emit(Line);
    187207end;
    188208
     
    208228begin
    209229  with CommonBlock do begin
    210     GenerateMethods(Methods);
     230    GenerateFunctions(Functions);
    211231    Emit('void ' + Name + '()');
    212232    GenerateBeginEnd(Code);
  • 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;
  • branches/DelphiToC/UPascalCompiler.pas

    r46 r48  
    8585          UsedType := nil;
    8686        end;
    87         with TFunction(Methods[Methods.Add(TFunction.Create)]) do begin
     87        with TFunction(Functions[Functions.Add(TFunction.Create)]) do begin
    8888          Name := 'Exit';
    8989          ResultType := TType(TModule(Modules[0]).Types[0]);
    9090        end;
    91         with TFunction(Methods[Methods.Add(TFunction.Create)]) do begin
     91        with TFunction(Functions[Functions.Add(TFunction.Create)]) do begin
    9292          Name := 'WriteLn';
    9393          ResultType := TType(TModule(Modules[0]).Types[0]);
     94          with TParameter(Parameters[Parameters.Add(TParameter.Create)]) do begin
     95            Name := 'Text';
     96            ValueType := TType(TModule(Modules[0]).Types[1]);
     97          end;
    9498        end;
    9599      end;
  • branches/DelphiToC/UPascalSource.pas

    r47 r48  
    5757  end;
    5858
     59  { TFunctionCall }
     60
    5961  TFunctionCall = class(TCommand)
    6062    FunctionRef: TFunction;
     63    ParameterExpression: TObjectList; // TObjectList<TExpression>
     64    constructor Create;
     65    destructor Destroy; override;
    6166  end;
    6267
     
    136141    Types: TTypeList;
    137142    Variables: TVariableList;
    138     Methods: TFunctionList;
     143    Functions: TFunctionList;
    139144    Code: TBeginEnd;
    140145    constructor Create; virtual;
     
    349354  Variables.Clear;
    350355  Constants.Clear;
    351   Methods.Clear;
     356  Functions.Clear;
    352357  Code.Clear;
    353358end;
     
    386391  Variables := TVariableList.Create;
    387392  Variables.Parent := Self;
    388   Methods := TFunctionList.Create;
    389   Methods.Parent := Self;
     393  Functions := TFunctionList.Create;
     394  Functions.Parent := Self;
    390395  Code := TBeginEnd.Create;
    391396  Code.Parent := Self;
     
    398403  Types.Destroy;
    399404  Variables.Destroy;
    400   Methods.Destroy;
     405  Functions.Destroy;
    401406  Code.Destroy;
    402407  inherited;
     
    416421  I := 0;
    417422  while (I < Count) and (LowerCase(TType(Items[I]).Name) <> LowerCase(Name)) do Inc(I);
    418   if I < Count then Result := TType(Items[I]) else begin
    419     if Assigned(Parent.Parent) then Result := Parent.Parent.Types.Search(Name)
    420       else begin
    421         Result := nil;
    422       end;
    423   end;
     423  if I < Count then Result := TType(Items[I]) else Result := nil;
     424
     425   if not Assigned(Result) and Assigned(Parent.Parent) then
     426     Result := Parent.Parent.Types.Search(Name);
    424427end;
    425428
     
    477480  while (I < Count) and (LowerCase(TFunction(Items[I]).Name) <> LowerCase(Name)) do Inc(I);
    478481  if I < Count then Result := TFunction(Items[I]) else begin
    479     if Assigned(Parent.Parent) then Result := Parent.Parent.Methods.Search(Name)
     482    if Assigned(Parent.Parent) then Result := Parent.Parent.Functions.Search(Name)
    480483      else begin
    481484        Result := nil;
     
    624627end;
    625628
     629{ TFunctionCall }
     630
     631constructor TFunctionCall.Create;
     632begin
     633  inherited;
     634  ParameterExpression := TObjectList.Create;
     635end;
     636
     637destructor TFunctionCall.Destroy;
     638begin
     639  ParameterExpression.Free;
     640  inherited Destroy;
     641end;
     642
    626643end.
    627644
  • branches/DelphiToC/Visual/USourceTree.pas

    r47 r48  
    191191      else NewNode := TreeView.Items.AddChild(Node, 'procedure ' + Name);
    192192    AddNodeParameterList(NewNode, Parameters);
    193     AddNodeMethodList(NewNode, Methods);
     193    AddNodeMethodList(NewNode, Functions);
    194194    AddNodeConstantList(NewNode, Constants);
    195195    AddNodeVariableList(NewNode, Variables);
     
    228228  NewNode := TreeView.Items.AddChild(Node, TypeName + ' ' + Module.Name);
    229229  AddNodeUses(NewNode, Module.UsedModules);
    230   AddNodeMethodList(NewNode, Module.Methods);
     230  AddNodeMethodList(NewNode, Module.Functions);
    231231  AddNodeConstantList(NewNode, Module.Constants);
    232232  AddNodeVariableList(NewNode, Module.Variables);
Note: See TracChangeset for help on using the changeset viewer.