Changeset 42
- Timestamp:
- Aug 5, 2010, 3:13:03 PM (14 years ago)
- Location:
- branches/DelphiToC
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DelphiToC/Analyze/UPascalParser.pas
r41 r42 104 104 resourcestring 105 105 SUnknownIdentifier = 'Unknown identificator "%s".'; 106 SIllegalExpression = 'Illegal expression "%s".'; 106 107 SExpectedButFound = 'Expected "%s" but "%s" found.'; 107 108 SRedefineIdentifier = 'Identificator "%s" redefinition.'; … … 430 431 if NextCode = 'begin' then begin 431 432 Result := TBeginEnd.Create; 432 Result.CommonBlock := SourceCode; 433 TBeginEnd(Result).CommonBlock := SourceCode; 434 //ShowMessage(IntToStr(Integer(SourceCode)) 435 // + ' ' + IntToStr(Integer(Result))); 433 436 TParserBeginEnd.Parse(Parser, TBeginEnd(Result)); 434 437 end else 435 438 if NextCode = 'if' then begin 436 439 Result := TIfThenElse.Create; 437 Result.CommonBlock := SourceCode;440 TIfThenElse(Result).CommonBlock := SourceCode; 438 441 TParserIfThenElse.Parse(Parser, TIfThenElse(Result)); 439 442 end else 440 443 if NextCode = 'while' then begin 441 444 Result := TWhileDo.Create; 442 Result.CommonBlock := SourceCode;445 TWhileDo(Result).CommonBlock := SourceCode; 443 446 TParserWhileDo.Parse(Parser, TWhileDo(Result)); 444 447 end else … … 446 449 if Assigned(SourceCode.Variables.Search(NextCode)) then begin 447 450 Result := TAssignment.Create; 448 Result.CommonBlock := SourceCode;451 TAssignment(Result).CommonBlock := SourceCode; 449 452 IdentName := ReadCode; 450 453 TAssignment(Result).Target := SourceCode.Variables.Search(IdentName); … … 456 459 if Assigned(SourceCode.Methods.Search(NextCode)) then begin 457 460 Result := TMethodCall.Create; 458 Result.CommonBlock := SourceCode; 461 TMethodCall(Result).CommonBlock := SourceCode; 462 TMethodCall(Result).Method := SourceCode.Methods.Search(NextCode); 463 ReadCode; 459 464 // ParseMetVariable(TMethodCall(Result).Target); 460 465 end else begin … … 462 467 ErrorMessage(SUnknownIdentifier, [ReadCode]); 463 468 end; 469 end else begin 470 Result := nil; 471 ErrorMessage(SIllegalExpression, [ReadCode]); 464 472 end; 465 473 end; … … 650 658 Modules.Clear; 651 659 with TModule(Modules[Modules.Add(TModule.Create)]) do begin 652 Name := ' main';660 Name := 'Main'; 653 661 with TType(Types[Types.Add(TType.Create)]) do begin 654 Name := ' void';662 Name := 'Void'; 655 663 Size := 0; 656 664 UsedType := nil; 657 665 end; 658 666 with TType(Types[Types.Add(TType.Create)]) do begin 659 Name := ' byte';667 Name := 'Byte'; 660 668 Size := 1; 661 669 UsedType := nil; 662 670 end; 663 671 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'; 665 677 ResultType := TType(TModule(Modules[0]).Types[0]); 666 678 end; … … 699 711 NewCommand: TCommand; 700 712 begin 713 //ShowMessage(IntToStr(Integer(SourceCode)) + ' ' + IntToStr(Integer(SourceCode.CommonBlock))); 701 714 with Parser, SourceCode do begin 702 715 Expect('begin'); … … 766 779 class procedure TParserIfThenElse.Parse(Parser: TPascalParser; SourceCode: TIfThenElse); 767 780 begin 768 with Parser do begin781 with Parser, Sourcecode do begin 769 782 Expect('if'); 770 Expect('than'); 783 Condition.CommonBlock := CommonBlock; 784 TParserExpression.Parse(Parser, Condition); 785 Expect('then'); 786 Command := TParserCommonBlock.ParseCommand(Parser, CommonBlock); 771 787 if NextCode = 'else' then begin 772 788 Expect('else'); 789 ElseCommand := TParserCommonBlock.ParseCommand(Parser, CommonBlock); 773 790 end; 774 791 end; … … 790 807 Expect('var'); 791 808 while IsIdentificator(NextCode) do begin 809 Identifiers.Clear; 792 810 VariableName := ReadCode; 793 811 Variable := Search(VariableName); -
branches/DelphiToC/DelphiToC.lpi
r41 r42 40 40 <Filename Value="DelphiToC.lpr"/> 41 41 <IsPartOfProject Value="True"/> 42 <EditorIndex Value="1 2"/>42 <EditorIndex Value="11"/> 43 43 <WindowIndex Value="0"/> 44 44 <TopLine Value="3"/> 45 45 <CursorPos X="39" Y="12"/> 46 <UsageCount Value="6 0"/>46 <UsageCount Value="64"/> 47 47 <Loaded Value="True"/> 48 48 </Unit0> … … 56 56 <EditorIndex Value="7"/> 57 57 <WindowIndex Value="0"/> 58 <TopLine Value=" 44"/>59 <CursorPos X="1 9" Y="53"/>60 <UsageCount Value="6 0"/>58 <TopLine Value="69"/> 59 <CursorPos X="1" Y="91"/> 60 <UsageCount Value="64"/> 61 61 <Loaded Value="True"/> 62 62 <LoadedDesigner Value="True"/> … … 69 69 <TopLine Value="1"/> 70 70 <CursorPos X="1" Y="1"/> 71 <UsageCount Value="6 0"/>71 <UsageCount Value="64"/> 72 72 </Unit2> 73 73 <Unit3> … … 77 77 <EditorIndex Value="5"/> 78 78 <WindowIndex Value="0"/> 79 <TopLine Value=" 113"/>80 <CursorPos X=" 27" Y="132"/>81 <UsageCount Value="6 0"/>79 <TopLine Value="264"/> 80 <CursorPos X="59" Y="277"/> 81 <UsageCount Value="64"/> 82 82 <Loaded Value="True"/> 83 83 </Unit3> … … 88 88 <EditorIndex Value="10"/> 89 89 <WindowIndex Value="0"/> 90 <TopLine Value=" 22"/>91 <CursorPos X=" 1" Y="35"/>92 <UsageCount Value="6 0"/>90 <TopLine Value="38"/> 91 <CursorPos X="38" Y="13"/> 92 <UsageCount Value="64"/> 93 93 <Loaded Value="True"/> 94 94 </Unit4> … … 101 101 <TopLine Value="1"/> 102 102 <CursorPos X="9" Y="12"/> 103 <UsageCount Value="6 0"/>103 <UsageCount Value="64"/> 104 104 <Loaded Value="True"/> 105 105 </Unit5> … … 108 108 <IsPartOfProject Value="True"/> 109 109 <UnitName Value="UCodeProducer"/> 110 <EditorIndex Value="1"/> 110 111 <WindowIndex Value="0"/> 111 112 <TopLine Value="1"/> 112 113 <CursorPos X="1" Y="1"/> 113 <UsageCount Value="60"/> 114 <UsageCount Value="64"/> 115 <Loaded Value="True"/> 114 116 </Unit6> 115 117 <Unit7> … … 117 119 <IsPartOfProject Value="True"/> 118 120 <UnitName Value="UCSource"/> 121 <IsVisibleTab Value="True"/> 119 122 <EditorIndex Value="9"/> 120 123 <WindowIndex Value="0"/> 121 <TopLine Value=" 3"/>122 <CursorPos X=" 3" Y="8"/>123 <UsageCount Value="6 0"/>124 <TopLine Value="156"/> 125 <CursorPos X="67" Y="164"/> 126 <UsageCount Value="64"/> 124 127 <Loaded Value="True"/> 125 128 </Unit7> … … 128 131 <IsPartOfProject Value="True"/> 129 132 <UnitName Value="UPascalParser"/> 130 <IsVisibleTab Value="True"/>131 133 <EditorIndex Value="0"/> 132 134 <WindowIndex Value="0"/> 133 <TopLine Value=" 444"/>134 <CursorPos X=" 1" Y="461"/>135 <UsageCount Value="6 0"/>135 <TopLine Value="148"/> 136 <CursorPos X="25" Y="153"/> 137 <UsageCount Value="64"/> 136 138 <Loaded Value="True"/> 137 139 </Unit8> … … 141 143 <TopLine Value="1"/> 142 144 <CursorPos X="1" Y="1"/> 143 <UsageCount Value=" 6"/>145 <UsageCount Value="5"/> 144 146 <DefaultSyntaxHighlighter Value="LFM"/> 145 147 </Unit9> … … 151 153 <TopLine Value="68"/> 152 154 <CursorPos X="14" Y="90"/> 153 <UsageCount Value="3 0"/>155 <UsageCount Value="32"/> 154 156 <Loaded Value="True"/> 155 157 </Unit10> … … 160 162 <TopLine Value="1555"/> 161 163 <CursorPos X="1" Y="1"/> 162 <UsageCount Value=" 10"/>164 <UsageCount Value="9"/> 163 165 </Unit11> 164 166 <Unit12> 165 167 <Filename Value="E:\Programy\Lazarus\fpc\2.4.0\source\rtl\objpas\sysutils\sysutilh.inc"/> 166 <EditorIndex Value=" 2"/>168 <EditorIndex Value="3"/> 167 169 <WindowIndex Value="0"/> 168 170 <TopLine Value="61"/> 169 171 <CursorPos X="7" Y="68"/> 170 <UsageCount Value="1 3"/>172 <UsageCount Value="15"/> 171 173 <Loaded Value="True"/> 172 174 </Unit12> 173 175 <Unit13> 174 176 <Filename Value="E:\Programy\Lazarus\fpc\2.4.0\source\rtl\objpas\sysutils\sysutils.inc"/> 175 <EditorIndex Value=" 3"/>177 <EditorIndex Value="4"/> 176 178 <WindowIndex Value="0"/> 177 179 <TopLine Value="139"/> 178 180 <CursorPos X="16" Y="146"/> 179 <UsageCount Value="1 3"/>181 <UsageCount Value="15"/> 180 182 <Loaded Value="True"/> 181 183 </Unit13> 182 184 <Unit14> 183 185 <Filename Value="E:\Programy\Lazarus\fpc\2.4.0\source\rtl\objpas\sysutils\sysstr.inc"/> 184 <EditorIndex Value="4"/>185 186 <WindowIndex Value="0"/> 186 187 <TopLine Value="934"/> 187 188 <CursorPos X="10" Y="947"/> 188 189 <UsageCount Value="13"/> 189 <Loaded Value="True"/>190 190 </Unit14> 191 191 <Unit15> … … 198 198 <Unit16> 199 199 <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"/> 204 203 <UsageCount Value="12"/> 205 <Loaded Value="True"/>206 204 </Unit16> 207 205 <Unit17> … … 209 207 <IsPartOfProject Value="True"/> 210 208 <UnitName Value="USourceTree"/> 211 <EditorIndex Value=" 1"/>212 <WindowIndex Value="0"/> 213 <TopLine Value=" 52"/>214 <CursorPos X=" 39" Y="65"/>215 <UsageCount Value="2 2"/>209 <EditorIndex Value="2"/> 210 <WindowIndex Value="0"/> 211 <TopLine Value="112"/> 212 <CursorPos X="1" Y="128"/> 213 <UsageCount Value="26"/> 216 214 <Loaded Value="True"/> 217 215 </Unit17> … … 222 220 <TopLine Value="2656"/> 223 221 <CursorPos X="3" Y="2669"/> 224 <UsageCount Value=" 10"/>222 <UsageCount Value="9"/> 225 223 </Unit18> 226 224 </Units> 227 225 <JumpHistory Count="30" HistoryIndex="29"> 228 226 <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"/> 231 229 </Position1> 232 230 <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"/> 235 233 </Position2> 236 234 <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"/> 239 237 </Position3> 240 238 <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"/> 243 241 </Position4> 244 242 <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"/> 247 245 </Position5> 248 246 <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"/> 251 249 </Position6> 252 250 <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"/> 255 253 </Position7> 256 254 <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"/> 259 257 </Position8> 260 258 <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"/> 263 261 </Position9> 264 262 <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"/> 267 265 </Position10> 268 266 <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"/> 271 269 </Position11> 272 270 <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"/> 275 273 </Position12> 276 274 <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"/> 279 277 </Position13> 280 278 <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"/> 283 281 </Position14> 284 282 <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"/> 287 285 </Position15> 288 286 <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"/> 291 289 </Position16> 292 290 <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"/> 295 293 </Position17> 296 294 <Position18> 297 295 <Filename Value="Analyze\UPascalParser.pas"/> 298 <Caret Line=" 463" Column="1" TopLine="448"/>296 <Caret Line="7" Column="34" TopLine="1"/> 299 297 </Position18> 300 298 <Position19> 301 299 <Filename Value="Analyze\UPascalParser.pas"/> 302 <Caret Line=" 429" Column="1" TopLine="416"/>300 <Caret Line="53" Column="21" TopLine="40"/> 303 301 </Position19> 304 302 <Position20> 305 303 <Filename Value="Analyze\UPascalParser.pas"/> 306 <Caret Line="5 90" Column="1" TopLine="577"/>304 <Caret Line="54" Column="74" TopLine="40"/> 307 305 </Position20> 308 306 <Position21> 309 307 <Filename Value="Analyze\UPascalParser.pas"/> 310 <Caret Line=" 429" Column="1" TopLine="416"/>308 <Caret Line="61" Column="21" TopLine="40"/> 311 309 </Position21> 312 310 <Position22> 313 311 <Filename Value="Analyze\UPascalParser.pas"/> 314 <Caret Line=" 590" Column="1" TopLine="577"/>312 <Caret Line="62" Column="74" TopLine="40"/> 315 313 </Position22> 316 314 <Position23> 317 315 <Filename Value="Analyze\UPascalParser.pas"/> 318 <Caret Line=" 706" Column="1" TopLine="693"/>316 <Caret Line="69" Column="21" TopLine="56"/> 319 317 </Position23> 320 318 <Position24> 321 319 <Filename Value="Analyze\UPascalParser.pas"/> 322 <Caret Line="70 8" Column="1" TopLine="693"/>320 <Caret Line="70" Column="74" TopLine="56"/> 323 321 </Position24> 324 322 <Position25> 325 323 <Filename Value="Analyze\UPascalParser.pas"/> 326 <Caret Line="7 04" Column="1" TopLine="693"/>324 <Caret Line="73" Column="17" TopLine="56"/> 327 325 </Position25> 328 326 <Position26> 329 327 <Filename Value="Analyze\UPascalParser.pas"/> 330 <Caret Line="7 05" Column="1" TopLine="693"/>328 <Caret Line="74" Column="70" TopLine="56"/> 331 329 </Position26> 332 330 <Position27> 333 331 <Filename Value="Analyze\UPascalParser.pas"/> 334 <Caret Line=" 706" Column="1" TopLine="693"/>332 <Caret Line="88" Column="31" TopLine="75"/> 335 333 </Position27> 336 334 <Position28> 337 335 <Filename Value="Analyze\UPascalParser.pas"/> 338 <Caret Line=" 708" Column="1" TopLine="693"/>336 <Caret Line="89" Column="16" TopLine="75"/> 339 337 </Position28> 340 338 <Position29> 341 339 <Filename Value="Analyze\UPascalParser.pas"/> 342 <Caret Line=" 704" Column="1" TopLine="693"/>340 <Caret Line="307" Column="22" TopLine="294"/> 343 341 </Position29> 344 342 <Position30> 345 343 <Filename Value="Analyze\UPascalParser.pas"/> 346 <Caret Line=" 705" Column="1" TopLine="693"/>344 <Caret Line="153" Column="25" TopLine="148"/> 347 345 </Position30> 348 346 </JumpHistory> … … 385 383 </CompilerOptions> 386 384 <Debugging> 387 <BreakPoints Count="1">388 <Item1>389 <Source Value="Analyze\UPascalParser.pas"/>390 <Line Value="445"/>391 </Item1>392 </BreakPoints>393 385 <Exceptions Count="3"> 394 386 <Item1> -
branches/DelphiToC/Example.pas
r41 r42 11 11 B: Byte; 12 12 sS: Byte; 13 D: Byte; 13 14 begin 14 15 WriteLn; 15 16 begin 17 WriteLn; 18 Pokus; 19 dsd; 16 20 begin 21 WriteLn; 17 22 end; 18 23 end; 19 24 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; 21 28 end. -
branches/DelphiToC/Produce/UCSource.pas
r36 r42 7 7 uses 8 8 SysUtils, Variants, Classes, Graphics, Controls, Forms, 9 Dialogs, StdCtrls, UPascalSource, UCodeProducer ;9 Dialogs, StdCtrls, UPascalSource, UCodeProducer, StrUtils; 10 10 11 11 type 12 13 { TCProducer } 14 12 15 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 13 31 TextSource: TStringList; 32 IndentationLength: Integer; 33 Indetation: Integer; 14 34 procedure Produce; override; 15 35 constructor Create; 16 36 destructor Destroy; override; 17 private18 procedure GenerateCommonBlock(CommonBlock: TCommonBlock;19 LabelPrefix: string);20 procedure GenerateProgram(ProgramBlock: TProgram);21 37 end; 22 38 … … 28 44 begin 29 45 TextSource := TStringList.Create; 46 IndentationLength := 2; 30 47 end; 31 48 … … 34 51 TextSource.Free; 35 52 inherited; 53 end; 54 55 function TCProducer.TranslateType(Name: string): string; 56 begin 57 if Name = 'Byte' then Result := 'uint8_t'; 58 if Name = 'Word' then Result := 'uint16_t'; 59 if Name = 'Void' then Result := 'void'; 60 end; 61 62 function TCProducer.TranslateOperator(Name: string): string; 63 begin 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; 75 end; 76 77 procedure TCProducer.Emit(Text: string); 78 begin 79 TextSource.Add(DupeString(' ', IndentationLength * Indetation) + Text); 36 80 end; 37 81 … … 47 91 I: Integer; 48 92 begin 93 Indetation := 0;; 49 94 with ProgramBlock do 50 95 for I := 0 to Modules.Count - 1 do 51 96 GenerateCommonBlock(TModule(Modules[I]), ''); 97 end; 98 99 procedure TCProducer.GenerateBeginEnd(BeginEnd: TBeginEnd); 100 var 101 I: Integer; 102 begin 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('}'); 121 end; 122 123 procedure TCProducer.GenerateCommand(Command: TCommand); 124 begin 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)); 130 end; 131 132 procedure TCProducer.GenerateWhileDo(WhileDo: TWhileDo); 133 begin 134 Emit('while (' + GenerateExpression(WhileDo.Condition) + ')'); 135 GenerateCommand(WhileDo.Command); 136 end; 137 138 procedure TCProducer.GenerateIfThenElse(IfThenElse: TIfThenElse); 139 begin 140 Emit('if(' + GenerateExpression(IfThenElse.Condition) + ')'); 141 GenerateCommand(IfThenElse.Command); 142 Emit('else '); 143 GenerateCommand(IfThenElse.ElseCommand); 144 end; 145 146 procedure TCProducer.GenerateAssignment(Assignment: TAssignment); 147 begin 148 Emit(Assignment.Target.Name + ' = ' + GenerateExpression(Assignment.Source) + ';'); 149 end; 150 151 procedure TCProducer.GenerateMethodCall(MethodCall: TMethodCall); 152 begin 153 Emit(MethodCall.Method.Name + '();'); 154 end; 155 156 function TCProducer.GenerateExpression(Expression: TExpression): string; 157 begin 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; 52 169 end; 53 170 … … 58 175 begin 59 176 with CommonBlock do begin 60 TextSource.Add('void ' + Name + '()'); 61 TextSource.Add('{'); 62 63 TextSource.Add('}'); 177 Emit('void ' + Name + '()'); 178 GenerateBeginEnd(Code); 64 179 end; 65 180 end; -
branches/DelphiToC/UMainForm.lfm
r37 r42 1 1 object MainForm: TMainForm 2 Left = 832 Left = 150 3 3 Height = 535 4 Top = 6 84 Top = 62 5 5 Width = 883 6 6 Caption = 'Pascal Compiler AVR' -
branches/DelphiToC/UMainForm.pas
r41 r42 27 27 procedure ErrorMessage(Text: string); 28 28 private 29 procedure FillTreeView;30 29 public 31 30 Compiler: TCompiler; … … 52 51 SourceTree.Code := Compiler.ProgramCode; 53 52 SourceTree.Fill; 54 //FillTreeView;55 53 Memo2.Clear; 56 54 if Compiler.Producer is TAssemblerProducer then begin … … 68 66 begin 69 67 MainForm.Memo3.Lines.Add(Text); 70 end;71 72 procedure TMainForm.FillTreeView;73 var74 NewNode: TTreeNode;75 NewNode2: TTreeNode;76 NewNode3: TTreeNode;77 ModuleNode: TTreeNode;78 I: Integer;79 II: Integer;80 M: Integer;81 begin82 with TreeView1, Items, Compiler do begin83 BeginUpdate;84 Clear;85 AddChild(nil, 'Projekt');86 for M := 0 to ProgramCode.Modules.Count - 1 do87 with TModule(ProgramCode.Modules[M]) do begin88 ModuleNode := AddChild(TopItem, Name);89 NewNode := AddChild(ModuleNode, 'Typy');90 for I := 0 to Types.Count - 1 do91 with TType(Types[I]) do92 NewNode2 := AddChild(NewNode, Name);93 NewNode := AddChild(ModuleNode, 'Funkce');94 for I := 0 to Methods.Count - 1 do95 with TFunction(Methods[I]) do begin96 NewNode2 := AddChild(NewNode, Name);97 with NewNode2 do begin98 NewNode3 := AddChild(NewNode2, 'Parametery');99 for II := 0 to Parameters.Count - 1 do100 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 do105 with TVariable(Variables[I]) do106 NewNode2 := AddChild(NewNode, Name + ':' + ValueType.Name);107 NewNode := AddChild(ModuleNode, 'Konstanty');108 for I := 0 to Constants.Count - 1 do109 with TConstant(Constants[I]) do110 NewNode2 := AddChild(NewNode, Name + ':' + ValueType.Name + '=' + Value);111 NewNode := AddChild(ModuleNode, 'Program');112 for I := 0 to Code.Commands.Count - 1 do begin113 if TObject(Code.Commands[I]) is TBeginEnd then begin114 NewNode2 := AddChild(NewNode, 'Begin-End');115 116 end else117 if TObject(Code.Commands[I]) is TWhileDo then begin118 NewNode2 := AddChild(NewNode, 'While-Do');119 NewNode3 := AddChild(NewNode2, 'Podmínka');120 121 NewNode3 := AddChild(NewNode2, 'Povel');122 end else123 if TObject(Code.Commands[I]) is TAssignment then begin124 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;133 68 end; 134 69 … … 153 88 procedure TMainForm.FormShow(Sender: TObject); 154 89 begin 90 WindowState := wsMaximized; 155 91 Memo1.Lines.LoadFromFile(ExampleFileName); 156 92 Button1Click(Self); -
branches/DelphiToC/UPascalSource.pas
r41 r42 45 45 46 46 TCommand = class 47 Parent: TObject; 47 48 CommonBlock: TCommonBlock; 48 49 end; … … 56 57 57 58 TMethodCall = class(TCommand) 58 Method: T Method;59 Method: TFunction; 59 60 end; 60 61 … … 91 92 end; 92 93 94 { TIfThenElse } 95 93 96 TIfThenElse = class(TCommand) 94 97 Condition: TExpression; 95 98 Command: TCommand; 96 99 ElseCommand: TCommand; 100 constructor Create; 101 destructor Destroy; override; 97 102 end; 98 103 … … 322 327 begin 323 328 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); 325 330 if I < Count then Result := TConstant(Items[I]) else begin 326 331 if Assigned(Parent.Parent) then Result := Parent.Parent.Constants.Search(Name) … … 377 382 Methods.Parent := Self; 378 383 Code := TBeginEnd.Create; 384 Code.Parent := Self; 379 385 Code.CommonBlock := Self; 380 386 end; … … 402 408 begin 403 409 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); 405 411 if I < Count then Result := TType(Items[I]) else begin 406 412 if Assigned(Parent.Parent) then Result := Parent.Parent.Types.Search(Name) … … 423 429 begin 424 430 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); 426 432 if I < Count then Result := TVariable(Items[I]) else begin 427 433 if Assigned(Parent.Parent) then … … 448 454 begin 449 455 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); 451 457 if I < Count then Result := TFunction(Items[I]) else begin 452 458 if Assigned(Parent.Parent) then Result := Parent.Parent.Methods.Search(Name) … … 487 493 488 494 destructor TOperationList.Destroy; 489 var 490 I: Integer; 491 begin 492 for I := 0 to Count - 1 do 493 TOperation(Items[I]).Free; 495 begin 494 496 inherited; 495 497 end; … … 536 538 constructor TBeginEnd.Create; 537 539 begin 540 inherited; 538 541 Commands := TCommandList.Create; 539 542 end; … … 587 590 end; 588 591 592 { TIfThenElse } 593 594 constructor TIfThenElse.Create; 595 begin 596 Condition := TExpression.Create; 597 end; 598 599 destructor TIfThenElse.Destroy; 600 begin 601 Condition.Destroy; 602 inherited Destroy; 603 end; 604 589 605 end. 590 606 -
branches/DelphiToC/Visual/USourceTree.pas
r41 r42 14 14 TSourceTree = class 15 15 private 16 procedure AddNodeAssignment(Node: TTreeNode; Assignment: TAssignment); 16 17 procedure AddNodeBeginEnd(Node: TTreeNode; BeginEnd: TBeginEnd); 18 procedure AddNodeIfThenElse(Node: TTreeNode; IfThenElse: TIfThenElse); 19 procedure AddNodeMethodCall(Node: TTreeNode; Method: TMethodCall); 17 20 procedure AddNodeMethodList(Node: TTreeNode; Methods: TFunctionList); 18 21 procedure AddNodeTypeList(Node: TTreeNode; Types: TTypeList); … … 60 63 end; 61 64 65 procedure TSourceTree.AddNodeIfThenElse(Node: TTreeNode; IfThenElse: TIfThenElse); 66 var 67 NewNode: TTreeNode; 68 NewNode2: TTreeNode; 69 NewNode3: TTreeNode; 70 I: Integer; 71 begin 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; 81 end; 82 83 procedure TSourceTree.AddNodeMethodCall(Node: TTreeNode; Method: TMethodCall); 84 var 85 NewNode: TTreeNode; 86 begin 87 NewNode := TreeView.Items.AddChild(Node, Method.Method.Name); 88 end; 89 90 procedure TSourceTree.AddNodeAssignment(Node: TTreeNode; Assignment: TAssignment); 91 var 92 NewNode: TTreeNode; 93 begin 94 NewNode := TreeView.Items.AddChild(Node, Assignment.Target.Name + ' := '); 95 AddNodeExpression(NewNode, Assignment.Source); 96 end; 97 62 98 procedure TSourceTree.AddNodeCommand(Node: TTreeNode; Command: TCommand); 63 99 begin … … 66 102 if Command is TWhileDo then 67 103 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)); 68 110 end; 69 111 70 112 procedure TSourceTree.AddNodeExpression(Node: TTreeNode; Expression: TExpression 71 113 ); 72 begin 73 114 var 115 NewNode: TTreeNode; 116 begin 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; 74 128 end; 75 129
Note:
See TracChangeset
for help on using the changeset viewer.