Changeset 42


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

Enhanced C code generation.

Location:
branches/DelphiToC
Files:
8 edited

Legend:

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

    r41 r42  
    104104resourcestring
    105105  SUnknownIdentifier = 'Unknown identificator "%s".';
     106  SIllegalExpression = 'Illegal expression "%s".';
    106107  SExpectedButFound = 'Expected "%s" but "%s" found.';
    107108  SRedefineIdentifier = 'Identificator "%s" redefinition.';
     
    430431    if NextCode = 'begin' then begin
    431432      Result := TBeginEnd.Create;
    432       Result.CommonBlock := SourceCode;
     433      TBeginEnd(Result).CommonBlock := SourceCode;
     434      //ShowMessage(IntToStr(Integer(SourceCode))
     435      // + ' ' + IntToStr(Integer(Result)));
    433436      TParserBeginEnd.Parse(Parser, TBeginEnd(Result));
    434437    end else
    435438    if NextCode = 'if' then begin
    436439      Result := TIfThenElse.Create;
    437       Result.CommonBlock := SourceCode;
     440      TIfThenElse(Result).CommonBlock := SourceCode;
    438441      TParserIfThenElse.Parse(Parser, TIfThenElse(Result));
    439442    end else
    440443    if NextCode = 'while' then begin
    441444      Result := TWhileDo.Create;
    442       Result.CommonBlock := SourceCode;
     445      TWhileDo(Result).CommonBlock := SourceCode;
    443446      TParserWhileDo.Parse(Parser, TWhileDo(Result));
    444447    end else
     
    446449      if Assigned(SourceCode.Variables.Search(NextCode)) then begin
    447450        Result := TAssignment.Create;
    448         Result.CommonBlock := SourceCode;
     451        TAssignment(Result).CommonBlock := SourceCode;
    449452        IdentName := ReadCode;
    450453        TAssignment(Result).Target := SourceCode.Variables.Search(IdentName);
     
    456459      if Assigned(SourceCode.Methods.Search(NextCode)) then begin
    457460        Result := TMethodCall.Create;
    458         Result.CommonBlock := SourceCode;
     461        TMethodCall(Result).CommonBlock := SourceCode;
     462        TMethodCall(Result).Method := SourceCode.Methods.Search(NextCode);
     463        ReadCode;
    459464  //      ParseMetVariable(TMethodCall(Result).Target);
    460465      end else begin
     
    462467        ErrorMessage(SUnknownIdentifier, [ReadCode]);
    463468      end;
     469    end else begin
     470      Result := nil;
     471      ErrorMessage(SIllegalExpression, [ReadCode]);
    464472    end;
    465473  end;
     
    650658    Modules.Clear;
    651659    with TModule(Modules[Modules.Add(TModule.Create)]) do begin
    652       Name := 'main';
     660      Name := 'Main';
    653661      with TType(Types[Types.Add(TType.Create)]) do begin
    654         Name := 'void';
     662        Name := 'Void';
    655663        Size := 0;
    656664        UsedType := nil;
    657665      end;
    658666      with TType(Types[Types.Add(TType.Create)]) do begin
    659         Name := 'byte';
     667        Name := 'Byte';
    660668        Size := 1;
    661669        UsedType := nil;
    662670      end;
    663671      with TFunction(Methods[Methods.Add(TFunction.Create)]) do begin
    664         Name := 'exit';
     672        Name := 'Exit';
     673        ResultType := TType(TModule(Modules[0]).Types[0]);
     674      end;
     675      with TFunction(Methods[Methods.Add(TFunction.Create)]) do begin
     676        Name := 'WriteLn';
    665677        ResultType := TType(TModule(Modules[0]).Types[0]);
    666678      end;
     
    699711  NewCommand: TCommand;
    700712begin
     713  //ShowMessage(IntToStr(Integer(SourceCode)) + ' ' + IntToStr(Integer(SourceCode.CommonBlock)));
    701714  with Parser, SourceCode do begin
    702715    Expect('begin');
     
    766779class procedure TParserIfThenElse.Parse(Parser: TPascalParser; SourceCode: TIfThenElse);
    767780begin
    768   with Parser do begin
     781  with Parser, Sourcecode do begin
    769782    Expect('if');
    770     Expect('than');
     783    Condition.CommonBlock := CommonBlock;
     784    TParserExpression.Parse(Parser, Condition);
     785    Expect('then');
     786    Command := TParserCommonBlock.ParseCommand(Parser, CommonBlock);
    771787    if NextCode = 'else' then begin
    772788      Expect('else');
     789      ElseCommand := TParserCommonBlock.ParseCommand(Parser, CommonBlock);
    773790    end;
    774791  end;
     
    790807    Expect('var');
    791808    while IsIdentificator(NextCode) do begin
     809      Identifiers.Clear;
    792810      VariableName := ReadCode;
    793811      Variable := Search(VariableName);
  • branches/DelphiToC/DelphiToC.lpi

    r41 r42  
    4040        <Filename Value="DelphiToC.lpr"/>
    4141        <IsPartOfProject Value="True"/>
    42         <EditorIndex Value="12"/>
     42        <EditorIndex Value="11"/>
    4343        <WindowIndex Value="0"/>
    4444        <TopLine Value="3"/>
    4545        <CursorPos X="39" Y="12"/>
    46         <UsageCount Value="60"/>
     46        <UsageCount Value="64"/>
    4747        <Loaded Value="True"/>
    4848      </Unit0>
     
    5656        <EditorIndex Value="7"/>
    5757        <WindowIndex Value="0"/>
    58         <TopLine Value="44"/>
    59         <CursorPos X="19" Y="53"/>
    60         <UsageCount Value="60"/>
     58        <TopLine Value="69"/>
     59        <CursorPos X="1" Y="91"/>
     60        <UsageCount Value="64"/>
    6161        <Loaded Value="True"/>
    6262        <LoadedDesigner Value="True"/>
     
    6969        <TopLine Value="1"/>
    7070        <CursorPos X="1" Y="1"/>
    71         <UsageCount Value="60"/>
     71        <UsageCount Value="64"/>
    7272      </Unit2>
    7373      <Unit3>
     
    7777        <EditorIndex Value="5"/>
    7878        <WindowIndex Value="0"/>
    79         <TopLine Value="113"/>
    80         <CursorPos X="27" Y="132"/>
    81         <UsageCount Value="60"/>
     79        <TopLine Value="264"/>
     80        <CursorPos X="59" Y="277"/>
     81        <UsageCount Value="64"/>
    8282        <Loaded Value="True"/>
    8383      </Unit3>
     
    8888        <EditorIndex Value="10"/>
    8989        <WindowIndex Value="0"/>
    90         <TopLine Value="22"/>
    91         <CursorPos X="1" Y="35"/>
    92         <UsageCount Value="60"/>
     90        <TopLine Value="38"/>
     91        <CursorPos X="38" Y="13"/>
     92        <UsageCount Value="64"/>
    9393        <Loaded Value="True"/>
    9494      </Unit4>
     
    101101        <TopLine Value="1"/>
    102102        <CursorPos X="9" Y="12"/>
    103         <UsageCount Value="60"/>
     103        <UsageCount Value="64"/>
    104104        <Loaded Value="True"/>
    105105      </Unit5>
     
    108108        <IsPartOfProject Value="True"/>
    109109        <UnitName Value="UCodeProducer"/>
     110        <EditorIndex Value="1"/>
    110111        <WindowIndex Value="0"/>
    111112        <TopLine Value="1"/>
    112113        <CursorPos X="1" Y="1"/>
    113         <UsageCount Value="60"/>
     114        <UsageCount Value="64"/>
     115        <Loaded Value="True"/>
    114116      </Unit6>
    115117      <Unit7>
     
    117119        <IsPartOfProject Value="True"/>
    118120        <UnitName Value="UCSource"/>
     121        <IsVisibleTab Value="True"/>
    119122        <EditorIndex Value="9"/>
    120123        <WindowIndex Value="0"/>
    121         <TopLine Value="3"/>
    122         <CursorPos X="3" Y="8"/>
    123         <UsageCount Value="60"/>
     124        <TopLine Value="156"/>
     125        <CursorPos X="67" Y="164"/>
     126        <UsageCount Value="64"/>
    124127        <Loaded Value="True"/>
    125128      </Unit7>
     
    128131        <IsPartOfProject Value="True"/>
    129132        <UnitName Value="UPascalParser"/>
    130         <IsVisibleTab Value="True"/>
    131133        <EditorIndex Value="0"/>
    132134        <WindowIndex Value="0"/>
    133         <TopLine Value="444"/>
    134         <CursorPos X="1" Y="461"/>
    135         <UsageCount Value="60"/>
     135        <TopLine Value="148"/>
     136        <CursorPos X="25" Y="153"/>
     137        <UsageCount Value="64"/>
    136138        <Loaded Value="True"/>
    137139      </Unit8>
     
    141143        <TopLine Value="1"/>
    142144        <CursorPos X="1" Y="1"/>
    143         <UsageCount Value="6"/>
     145        <UsageCount Value="5"/>
    144146        <DefaultSyntaxHighlighter Value="LFM"/>
    145147      </Unit9>
     
    151153        <TopLine Value="68"/>
    152154        <CursorPos X="14" Y="90"/>
    153         <UsageCount Value="30"/>
     155        <UsageCount Value="32"/>
    154156        <Loaded Value="True"/>
    155157      </Unit10>
     
    160162        <TopLine Value="1555"/>
    161163        <CursorPos X="1" Y="1"/>
    162         <UsageCount Value="10"/>
     164        <UsageCount Value="9"/>
    163165      </Unit11>
    164166      <Unit12>
    165167        <Filename Value="E:\Programy\Lazarus\fpc\2.4.0\source\rtl\objpas\sysutils\sysutilh.inc"/>
    166         <EditorIndex Value="2"/>
     168        <EditorIndex Value="3"/>
    167169        <WindowIndex Value="0"/>
    168170        <TopLine Value="61"/>
    169171        <CursorPos X="7" Y="68"/>
    170         <UsageCount Value="13"/>
     172        <UsageCount Value="15"/>
    171173        <Loaded Value="True"/>
    172174      </Unit12>
    173175      <Unit13>
    174176        <Filename Value="E:\Programy\Lazarus\fpc\2.4.0\source\rtl\objpas\sysutils\sysutils.inc"/>
    175         <EditorIndex Value="3"/>
     177        <EditorIndex Value="4"/>
    176178        <WindowIndex Value="0"/>
    177179        <TopLine Value="139"/>
    178180        <CursorPos X="16" Y="146"/>
    179         <UsageCount Value="13"/>
     181        <UsageCount Value="15"/>
    180182        <Loaded Value="True"/>
    181183      </Unit13>
    182184      <Unit14>
    183185        <Filename Value="E:\Programy\Lazarus\fpc\2.4.0\source\rtl\objpas\sysutils\sysstr.inc"/>
    184         <EditorIndex Value="4"/>
    185186        <WindowIndex Value="0"/>
    186187        <TopLine Value="934"/>
    187188        <CursorPos X="10" Y="947"/>
    188189        <UsageCount Value="13"/>
    189         <Loaded Value="True"/>
    190190      </Unit14>
    191191      <Unit15>
     
    198198      <Unit16>
    199199        <Filename Value="E:\Programy\Lazarus\fpc\2.4.0\source\rtl\inc\objpash.inc"/>
    200         <EditorIndex Value="11"/>
    201         <WindowIndex Value="0"/>
    202         <TopLine Value="171"/>
    203         <CursorPos X="21" Y="184"/>
     200        <WindowIndex Value="0"/>
     201        <TopLine Value="153"/>
     202        <CursorPos X="8" Y="166"/>
    204203        <UsageCount Value="12"/>
    205         <Loaded Value="True"/>
    206204      </Unit16>
    207205      <Unit17>
     
    209207        <IsPartOfProject Value="True"/>
    210208        <UnitName Value="USourceTree"/>
    211         <EditorIndex Value="1"/>
    212         <WindowIndex Value="0"/>
    213         <TopLine Value="52"/>
    214         <CursorPos X="39" Y="65"/>
    215         <UsageCount Value="22"/>
     209        <EditorIndex Value="2"/>
     210        <WindowIndex Value="0"/>
     211        <TopLine Value="112"/>
     212        <CursorPos X="1" Y="128"/>
     213        <UsageCount Value="26"/>
    216214        <Loaded Value="True"/>
    217215      </Unit17>
     
    222220        <TopLine Value="2656"/>
    223221        <CursorPos X="3" Y="2669"/>
    224         <UsageCount Value="10"/>
     222        <UsageCount Value="9"/>
    225223      </Unit18>
    226224    </Units>
    227225    <JumpHistory Count="30" HistoryIndex="29">
    228226      <Position1>
    229         <Filename Value="Analyze\UPascalParser.pas"/>
    230         <Caret Line="589" Column="1" TopLine="576"/>
     227        <Filename Value="Produce\UCSource.pas"/>
     228        <Caret Line="23" Column="48" TopLine="9"/>
    231229      </Position1>
    232230      <Position2>
    233         <Filename Value="Analyze\UPascalParser.pas"/>
    234         <Caret Line="705" Column="1" TopLine="692"/>
     231        <Filename Value="Produce\UCSource.pas"/>
     232        <Caret Line="26" Column="1" TopLine="10"/>
    235233      </Position2>
    236234      <Position3>
    237         <Filename Value="Analyze\UPascalParser.pas"/>
    238         <Caret Line="707" Column="1" TopLine="692"/>
     235        <Filename Value="Produce\UCSource.pas"/>
     236        <Caret Line="27" Column="59" TopLine="11"/>
    239237      </Position3>
    240238      <Position4>
    241         <Filename Value="Analyze\UPascalParser.pas"/>
    242         <Caret Line="703" Column="1" TopLine="692"/>
     239        <Filename Value="Produce\UCSource.pas"/>
     240        <Caret Line="28" Column="66" TopLine="11"/>
    243241      </Position4>
    244242      <Position5>
    245         <Filename Value="Analyze\UPascalParser.pas"/>
    246         <Caret Line="704" Column="46" TopLine="692"/>
     243        <Filename Value="Produce\UCSource.pas"/>
     244        <Caret Line="136" Column="3" TopLine="127"/>
    247245      </Position5>
    248246      <Position6>
    249         <Filename Value="Analyze\UPascalParser.pas"/>
    250         <Caret Line="462" Column="23" TopLine="469"/>
     247        <Filename Value="Produce\UCSource.pas"/>
     248        <Caret Line="85" Column="14" TopLine="76"/>
    251249      </Position6>
    252250      <Position7>
    253         <Filename Value="Analyze\UPascalParser.pas"/>
    254         <Caret Line="445" Column="1" TopLine="432"/>
     251        <Filename Value="Produce\UCSource.pas"/>
     252        <Caret Line="91" Column="38" TopLine="78"/>
    255253      </Position7>
    256254      <Position8>
    257         <Filename Value="Analyze\UPascalParser.pas"/>
    258         <Caret Line="446" Column="1" TopLine="432"/>
     255        <Filename Value="Produce\UCSource.pas"/>
     256        <Caret Line="92" Column="39" TopLine="79"/>
    259257      </Position8>
    260258      <Position9>
    261         <Filename Value="Analyze\UPascalParser.pas"/>
    262         <Caret Line="449" Column="1" TopLine="432"/>
     259        <Filename Value="Produce\UCSource.pas"/>
     260        <Caret Line="83" Column="53" TopLine="78"/>
    263261      </Position9>
    264262      <Position10>
    265         <Filename Value="Analyze\UPascalParser.pas"/>
    266         <Caret Line="448" Column="1" TopLine="432"/>
     263        <Filename Value="UPascalSource.pas"/>
     264        <Caret Line="61" Column="26" TopLine="48"/>
    267265      </Position10>
    268266      <Position11>
    269         <Filename Value="Analyze\UPascalParser.pas"/>
    270         <Caret Line="447" Column="1" TopLine="432"/>
     267        <Filename Value="UPascalSource.pas"/>
     268        <Caret Line="64" Column="28" TopLine="52"/>
    271269      </Position11>
    272270      <Position12>
    273         <Filename Value="Analyze\UPascalParser.pas"/>
    274         <Caret Line="450" Column="1" TopLine="432"/>
     271        <Filename Value="UPascalSource.pas"/>
     272        <Caret Line="23" Column="11" TopLine="10"/>
    275273      </Position12>
    276274      <Position13>
    277         <Filename Value="Analyze\UPascalParser.pas"/>
    278         <Caret Line="445" Column="1" TopLine="432"/>
     275        <Filename Value="UPascalSource.pas"/>
     276        <Caret Line="376" Column="17" TopLine="373"/>
    279277      </Position13>
    280278      <Position14>
    281         <Filename Value="Analyze\UPascalParser.pas"/>
    282         <Caret Line="446" Column="1" TopLine="432"/>
     279        <Filename Value="Produce\UCSource.pas"/>
     280        <Caret Line="83" Column="53" TopLine="78"/>
    283281      </Position14>
    284282      <Position15>
    285         <Filename Value="Analyze\UPascalParser.pas"/>
    286         <Caret Line="456" Column="1" TopLine="435"/>
     283        <Filename Value="Produce\UCSource.pas"/>
     284        <Caret Line="150" Column="1" TopLine="134"/>
    287285      </Position15>
    288286      <Position16>
    289         <Filename Value="Analyze\UPascalParser.pas"/>
    290         <Caret Line="461" Column="1" TopLine="448"/>
     287        <Filename Value="Produce\UCSource.pas"/>
     288        <Caret Line="147" Column="36" TopLine="134"/>
    291289      </Position16>
    292290      <Position17>
    293         <Filename Value="Analyze\UPascalParser.pas"/>
    294         <Caret Line="462" Column="1" TopLine="448"/>
     291        <Filename Value="Produce\UCSource.pas"/>
     292        <Caret Line="18" Column="38" TopLine="15"/>
    295293      </Position17>
    296294      <Position18>
    297295        <Filename Value="Analyze\UPascalParser.pas"/>
    298         <Caret Line="463" Column="1" TopLine="448"/>
     296        <Caret Line="7" Column="34" TopLine="1"/>
    299297      </Position18>
    300298      <Position19>
    301299        <Filename Value="Analyze\UPascalParser.pas"/>
    302         <Caret Line="429" Column="1" TopLine="416"/>
     300        <Caret Line="53" Column="21" TopLine="40"/>
    303301      </Position19>
    304302      <Position20>
    305303        <Filename Value="Analyze\UPascalParser.pas"/>
    306         <Caret Line="590" Column="1" TopLine="577"/>
     304        <Caret Line="54" Column="74" TopLine="40"/>
    307305      </Position20>
    308306      <Position21>
    309307        <Filename Value="Analyze\UPascalParser.pas"/>
    310         <Caret Line="429" Column="1" TopLine="416"/>
     308        <Caret Line="61" Column="21" TopLine="40"/>
    311309      </Position21>
    312310      <Position22>
    313311        <Filename Value="Analyze\UPascalParser.pas"/>
    314         <Caret Line="590" Column="1" TopLine="577"/>
     312        <Caret Line="62" Column="74" TopLine="40"/>
    315313      </Position22>
    316314      <Position23>
    317315        <Filename Value="Analyze\UPascalParser.pas"/>
    318         <Caret Line="706" Column="1" TopLine="693"/>
     316        <Caret Line="69" Column="21" TopLine="56"/>
    319317      </Position23>
    320318      <Position24>
    321319        <Filename Value="Analyze\UPascalParser.pas"/>
    322         <Caret Line="708" Column="1" TopLine="693"/>
     320        <Caret Line="70" Column="74" TopLine="56"/>
    323321      </Position24>
    324322      <Position25>
    325323        <Filename Value="Analyze\UPascalParser.pas"/>
    326         <Caret Line="704" Column="1" TopLine="693"/>
     324        <Caret Line="73" Column="17" TopLine="56"/>
    327325      </Position25>
    328326      <Position26>
    329327        <Filename Value="Analyze\UPascalParser.pas"/>
    330         <Caret Line="705" Column="1" TopLine="693"/>
     328        <Caret Line="74" Column="70" TopLine="56"/>
    331329      </Position26>
    332330      <Position27>
    333331        <Filename Value="Analyze\UPascalParser.pas"/>
    334         <Caret Line="706" Column="1" TopLine="693"/>
     332        <Caret Line="88" Column="31" TopLine="75"/>
    335333      </Position27>
    336334      <Position28>
    337335        <Filename Value="Analyze\UPascalParser.pas"/>
    338         <Caret Line="708" Column="1" TopLine="693"/>
     336        <Caret Line="89" Column="16" TopLine="75"/>
    339337      </Position28>
    340338      <Position29>
    341339        <Filename Value="Analyze\UPascalParser.pas"/>
    342         <Caret Line="704" Column="1" TopLine="693"/>
     340        <Caret Line="307" Column="22" TopLine="294"/>
    343341      </Position29>
    344342      <Position30>
    345343        <Filename Value="Analyze\UPascalParser.pas"/>
    346         <Caret Line="705" Column="1" TopLine="693"/>
     344        <Caret Line="153" Column="25" TopLine="148"/>
    347345      </Position30>
    348346    </JumpHistory>
     
    385383  </CompilerOptions>
    386384  <Debugging>
    387     <BreakPoints Count="1">
    388       <Item1>
    389         <Source Value="Analyze\UPascalParser.pas"/>
    390         <Line Value="445"/>
    391       </Item1>
    392     </BreakPoints>
    393385    <Exceptions Count="3">
    394386      <Item1>
  • branches/DelphiToC/Example.pas

    r41 r42  
    1111  B: Byte;
    1212  sS: Byte;
     13  D: Byte;
    1314begin
    1415  WriteLn;
    1516  begin
     17    WriteLn;
     18    Pokus;
     19    dsd;
    1620    begin
     21      WriteLn;
    1722    end;
    1823  end;
    1924  A := 1;
    20   While A < 1 do A := A + 1;
     25  if A = 2 then begin
     26  end;
     27  while A < 1 do A := A + 1;
    2128end.
  • 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;
  • branches/DelphiToC/UMainForm.lfm

    r37 r42  
    11object MainForm: TMainForm
    2   Left = 83
     2  Left = 150
    33  Height = 535
    4   Top = 68
     4  Top = 62
    55  Width = 883
    66  Caption = 'Pascal Compiler AVR'
  • branches/DelphiToC/UMainForm.pas

    r41 r42  
    2727    procedure ErrorMessage(Text: string);
    2828  private
    29     procedure FillTreeView;
    3029  public
    3130    Compiler: TCompiler;
     
    5251  SourceTree.Code := Compiler.ProgramCode;
    5352  SourceTree.Fill;
    54   //FillTreeView;
    5553  Memo2.Clear;
    5654  if Compiler.Producer is TAssemblerProducer then begin
     
    6866begin
    6967  MainForm.Memo3.Lines.Add(Text);
    70 end;
    71 
    72 procedure TMainForm.FillTreeView;
    73 var
    74   NewNode: TTreeNode;
    75   NewNode2: TTreeNode;
    76   NewNode3: TTreeNode;
    77   ModuleNode: TTreeNode;
    78   I: Integer;
    79   II: Integer;
    80   M: Integer;
    81 begin
    82   with TreeView1, Items, Compiler do begin
    83     BeginUpdate;
    84     Clear;
    85     AddChild(nil, 'Projekt');
    86     for M := 0 to ProgramCode.Modules.Count - 1 do
    87       with TModule(ProgramCode.Modules[M]) do begin
    88         ModuleNode := AddChild(TopItem, Name);
    89         NewNode := AddChild(ModuleNode, 'Typy');
    90         for I := 0 to Types.Count - 1 do
    91         with TType(Types[I]) do
    92           NewNode2 := AddChild(NewNode, Name);
    93         NewNode := AddChild(ModuleNode, 'Funkce');
    94         for I := 0 to Methods.Count - 1 do
    95         with TFunction(Methods[I]) do begin
    96           NewNode2 := AddChild(NewNode, Name);
    97           with NewNode2 do begin
    98             NewNode3 := AddChild(NewNode2, 'Parametery');
    99             for II := 0 to Parameters.Count - 1 do
    100               AddChild(NewNode3, TParameter(Parameters[II]).Name + ':' + TParameter(Parameters[II]).ValueType.Name);
    101           end;
    102         end;
    103         NewNode := AddChild(ModuleNode, 'Promìnné');
    104         for I := 0 to Variables.Count - 1 do
    105         with TVariable(Variables[I]) do
    106           NewNode2 := AddChild(NewNode, Name + ':' + ValueType.Name);
    107         NewNode := AddChild(ModuleNode, 'Konstanty');
    108         for I := 0 to Constants.Count - 1 do
    109         with TConstant(Constants[I]) do
    110           NewNode2 := AddChild(NewNode, Name + ':' + ValueType.Name + '=' + Value);
    111         NewNode := AddChild(ModuleNode, 'Program');
    112         for I := 0 to Code.Commands.Count - 1 do begin
    113           if TObject(Code.Commands[I]) is TBeginEnd then begin
    114             NewNode2 := AddChild(NewNode, 'Begin-End');
    115 
    116           end else
    117           if TObject(Code.Commands[I]) is TWhileDo then begin
    118             NewNode2 := AddChild(NewNode, 'While-Do');
    119             NewNode3 := AddChild(NewNode2, 'Podmínka');
    120 
    121             NewNode3 := AddChild(NewNode2, 'Povel');
    122           end else
    123           if TObject(Code.Commands[I]) is TAssignment then begin
    124             NewNode2 := AddChild(NewNode, TAssignment(Code.Commands[I]).Target.Name + ' := exp');
    125 
    126           end;
    127         end;
    128 
    129       end;
    130     TopItem.Expand(True);
    131     EndUpdate;
    132   end;
    13368end;
    13469
     
    15388procedure TMainForm.FormShow(Sender: TObject);
    15489begin
     90  WindowState := wsMaximized;
    15591  Memo1.Lines.LoadFromFile(ExampleFileName);
    15692  Button1Click(Self);
  • branches/DelphiToC/UPascalSource.pas

    r41 r42  
    4545
    4646  TCommand = class
     47    Parent: TObject;
    4748    CommonBlock: TCommonBlock;
    4849  end;
     
    5657
    5758  TMethodCall = class(TCommand)
    58     Method: TMethod;
     59    Method: TFunction;
    5960  end;
    6061
     
    9192  end;
    9293
     94  { TIfThenElse }
     95
    9396  TIfThenElse = class(TCommand)
    9497    Condition: TExpression;
    9598    Command: TCommand;
    9699    ElseCommand: TCommand;
     100    constructor Create;
     101    destructor Destroy; override;
    97102  end;
    98103
     
    322327begin
    323328  I := 0;
    324   while (I < Count) and (TConstant(Items[I]).Name <> Name) do Inc(I);
     329  while (I < Count) and (LowerCase(TConstant(Items[I]).Name) <> Name) do Inc(I);
    325330  if I < Count then Result := TConstant(Items[I]) else begin
    326331    if Assigned(Parent.Parent) then Result := Parent.Parent.Constants.Search(Name)
     
    377382  Methods.Parent := Self;
    378383  Code := TBeginEnd.Create;
     384  Code.Parent := Self;
    379385  Code.CommonBlock := Self;
    380386end;
     
    402408begin
    403409  I := 0;
    404   while (I < Count) and (TType(Items[I]).Name <> Name) do Inc(I);
     410  while (I < Count) and (LowerCase(TType(Items[I]).Name) <> Name) do Inc(I);
    405411  if I < Count then Result := TType(Items[I]) else begin
    406412    if Assigned(Parent.Parent) then Result := Parent.Parent.Types.Search(Name)
     
    423429begin
    424430  I := 0;
    425   while (I < Count) and (TVariable(Items[I]).Name <> Name) do Inc(I);
     431  while (I < Count) and (LowerCase(TVariable(Items[I]).Name) <> Name) do Inc(I);
    426432  if I < Count then Result := TVariable(Items[I]) else begin
    427433    if Assigned(Parent.Parent) then
     
    448454begin
    449455  I := 0;
    450   while (I < Count) and (TFunction(Items[I]).Name <> Name) do Inc(I);
     456  while (I < Count) and (LowerCase(TFunction(Items[I]).Name) <> Name) do Inc(I);
    451457  if I < Count then Result := TFunction(Items[I]) else begin
    452458    if Assigned(Parent.Parent) then Result := Parent.Parent.Methods.Search(Name)
     
    487493
    488494destructor TOperationList.Destroy;
    489 var
    490   I: Integer;
    491 begin
    492   for I := 0 to Count - 1 do
    493     TOperation(Items[I]).Free;
     495begin
    494496  inherited;
    495497end;
     
    536538constructor TBeginEnd.Create;
    537539begin
     540  inherited;
    538541  Commands := TCommandList.Create;
    539542end;
     
    587590end;
    588591
     592{ TIfThenElse }
     593
     594constructor TIfThenElse.Create;
     595begin
     596  Condition := TExpression.Create;
     597end;
     598
     599destructor TIfThenElse.Destroy;
     600begin
     601  Condition.Destroy;
     602  inherited Destroy;
     603end;
     604
    589605end.
    590606
  • branches/DelphiToC/Visual/USourceTree.pas

    r41 r42  
    1414  TSourceTree = class
    1515  private
     16    procedure AddNodeAssignment(Node: TTreeNode; Assignment: TAssignment);
    1617    procedure AddNodeBeginEnd(Node: TTreeNode; BeginEnd: TBeginEnd);
     18    procedure AddNodeIfThenElse(Node: TTreeNode; IfThenElse: TIfThenElse);
     19    procedure AddNodeMethodCall(Node: TTreeNode; Method: TMethodCall);
    1720    procedure AddNodeMethodList(Node: TTreeNode; Methods: TFunctionList);
    1821    procedure AddNodeTypeList(Node: TTreeNode; Types: TTypeList);
     
    6063end;
    6164
     65procedure TSourceTree.AddNodeIfThenElse(Node: TTreeNode; IfThenElse: TIfThenElse);
     66var
     67  NewNode: TTreeNode;
     68  NewNode2: TTreeNode;
     69  NewNode3: TTreeNode;
     70  I: Integer;
     71begin
     72  NewNode := TreeView.Items.AddChild(Node, 'if');
     73  NewNode2 := TreeView.Items.AddChild(NewNode, 'condition');
     74  AddNodeExpression(NewNode2, IfThenElse.Condition);
     75  NewNode2 := TreeView.Items.AddChild(NewNode, 'then');
     76  AddNodeCommand(NewNode2, IfThenElse.Command);
     77  if Assigned(IfThenElse.ElseCommand) then begin
     78    NewNode2 := TreeView.Items.AddChild(NewNode, 'else');
     79    AddNodeCommand(NewNode2, IfThenElse.ElseCommand);
     80  end;
     81end;
     82
     83procedure TSourceTree.AddNodeMethodCall(Node: TTreeNode; Method: TMethodCall);
     84var
     85  NewNode: TTreeNode;
     86begin
     87  NewNode := TreeView.Items.AddChild(Node, Method.Method.Name);
     88end;
     89
     90procedure TSourceTree.AddNodeAssignment(Node: TTreeNode; Assignment: TAssignment);
     91var
     92  NewNode: TTreeNode;
     93begin
     94  NewNode := TreeView.Items.AddChild(Node, Assignment.Target.Name + ' := ');
     95  AddNodeExpression(NewNode, Assignment.Source);
     96end;
     97
    6298procedure TSourceTree.AddNodeCommand(Node: TTreeNode; Command: TCommand);
    6399begin
     
    66102  if Command is TWhileDo then
    67103    AddNodeWhileDo(Node, TWhileDo(Command));
     104  if Command is TMethodCall then
     105    AddNodeMethodCall(Node, TMethodCall(Command));
     106  if Command is TIfThenElse then
     107    AddNodeIfThenElse(Node, TIfThenElse(Command));
     108  if Command is TAssignment then
     109    AddNodeAssignment(Node, TAssignment(Command));
    68110end;
    69111
    70112procedure TSourceTree.AddNodeExpression(Node: TTreeNode; Expression: TExpression
    71113  );
    72 begin
    73 
     114var
     115  NewNode: TTreeNode;
     116begin
     117  case Expression.NodeType of
     118    ntConstant: NewNode := TreeView.Items.AddChild(Node, Expression.Value);
     119    ntVariable: NewNode := TreeView.Items.AddChild(Node, Expression.Variable.Name);
     120    ntFunction: NewNode := TreeView.Items.AddChild(Node, Expression.Method.Name);
     121    ntOperator: begin
     122      NewNode := TreeView.Items.AddChild(Node, Expression.OperatorName);
     123      AddNodeExpression(NewNode, TExpression(Expression.SubItems.First));
     124      AddNodeExpression(NewNode, TExpression(Expression.SubItems.Last));
     125    end;
     126    ntNone: ;
     127  end;
    74128end;
    75129
Note: See TracChangeset for help on using the changeset viewer.