Changeset 14
- Timestamp:
- Apr 9, 2009, 11:04:41 AM (16 years ago)
- Location:
- branches/DelphiToC
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DelphiToC/Example.pas
r12 r14 4 4 B: Byte; 5 5 begin 6 A := 'a' + (10 + 2 * 3 + 2) xor 10 / 2; 7 B := 20; 8 while A < 10 do A := A + 1; 9 6 A := 1; 10 7 end; -
branches/DelphiToC/UAssemblerSource.pas
r13 r14 25 25 26 26 TAssemblerProducer = class(TCodeProducer) 27 AssemblyCode: TList; // TList<TAssemblerLine>28 destructor Destroy; override;29 27 private 30 28 procedure AddInstruction(LabelName, Instruction, Operand1, … … 33 31 procedure GenerateExpression(Expression: TExpression; LabelPrefix: string); 34 32 procedure GenerateProgram(ProgramBlock: TProgram); 33 public 34 AssemblyCode: TList; // TList<TAssemblerLine> 35 procedure Produce; override; 36 constructor Create; 37 destructor Destroy; override; 35 38 end; 36 39 … … 95 98 end; 96 99 *) 100 101 constructor TAssemblerProducer.Create; 102 begin 103 AssemblyCode := TList.Create; 104 end; 97 105 98 106 destructor TAssemblerProducer.Destroy; … … 177 185 end; 178 186 179 180 187 procedure TAssemblerProducer.Produce; 188 begin 189 inherited; 190 GenerateProgram(ProgramCode); 191 end; 181 192 182 193 end. -
branches/DelphiToC/UCSource.pas
r12 r14 3 3 interface 4 4 5 uses 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, StdCtrls, UPascalSource; 8 9 type 10 TCSource = class 11 TextSource: TStringList; 12 PascalSource: TStringList; 13 procedure Generate; 14 constructor Create; 15 destructor Destroy; override; 16 end; 17 5 18 implementation 6 19 20 { TCSource } 21 22 constructor TCSource.Create; 23 begin 24 TextSource := TStringList.Create; 25 end; 26 27 destructor TCSource.Destroy; 28 begin 29 TextSource.Free; 30 inherited; 31 end; 32 33 procedure TCSource.Generate; 34 begin 35 36 end; 37 7 38 end. -
branches/DelphiToC/UCodeProducer.pas
r13 r14 3 3 interface 4 4 5 uses 6 UPascalSource; 7 5 8 type 6 9 TCodeProducer = class 10 ProgramCode: TProgram; 7 11 procedure Produce; virtual; abstract; 8 12 end; -
branches/DelphiToC/UMainForm.pas
r12 r14 5 5 uses 6 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, StdCtrls, UPascalSource ;7 Dialogs, StdCtrls, UPascalSource, UPascalCompiler, UAssemblerSource; 8 8 9 9 type … … 38 38 I: Integer; 39 39 begin 40 Compiler.Compile(Memo1.Lines); 40 MainForm.Memo3.Clear; 41 Compiler.SourceCode.Assign(Memo1.Lines); 42 Compiler.Compile; 41 43 Memo2.Clear; 42 for I := 0 to Compiler.AssemblyCode.Count - 1 do 43 Memo2.Lines.Add(TAssemblerLine(Compiler.AssemblyCode[I]).AsString); 44 with TAssemblerProducer(Compiler.Producer) do 45 for I := 0 to AssemblyCode.Count - 1 do 46 Memo2.Lines.Add(TAssemblerLine(AssemblyCode[I]).AsString); 44 47 end; 45 48 … … 66 69 67 70 procedure TMainForm.FormShow(Sender: TObject); 68 var69 I: Integer;70 71 begin 71 72 Memo1.Lines.LoadFromFile('Example.pas'); 72 MainForm.Memo3.Clear; 73 Compiler.Compile(Memo1.Lines); 74 Memo2.Clear; 75 for I := 0 to Compiler.AssemblyCode.Count - 1 do 76 Memo2.Lines.Add(TAssemblerLine(Compiler.AssemblyCode[I]).AsString); 73 Button1Click(Self); 77 74 end; 78 75 -
branches/DelphiToC/UPascalCompiler.pas
r13 r14 10 10 TCompiler = class 11 11 private 12 SourceCode: TStringList;13 CodePosition: Integer;14 12 ProgramCode: TProgram; 15 13 FOnErrorMessage: TOnErrorMessage; 16 14 procedure ErrorMessage(Text: string); 17 15 public 16 SourceCode: TStringList; 18 17 Parser: TPascalParser; 19 18 Producer: TCodeProducer; 20 19 constructor Create; 21 procedure Compile (SourceCode: TStrings);20 procedure Compile; 22 21 destructor Destroy; override; 23 22 property OnErrorMessage: TOnErrorMessage read FOnErrorMessage write FOnErrorMessage; … … 28 27 { TCompiler } 29 28 30 procedure TCompiler.Compile (SourceCode: TStrings);29 procedure TCompiler.Compile; 31 30 begin 32 Self.SourceCode.Assign(SourceCode); 33 CodePosition := 1; 31 Parser.CodePosition := 1; 34 32 Parser.ParseProgram(ProgramCode); 35 33 Producer.Produce; … … 39 37 begin 40 38 SourceCode := TStringList.Create; 39 ProgramCode := TProgram.Create; 41 40 Producer := TAssemblerProducer.Create; 42 ProgramCode := TProgram.Create; 41 Producer.ProgramCode := ProgramCode; 42 Parser := TPascalParser.Create; 43 Parser.SourceCode := SourceCode; 43 44 end; 44 45 45 46 destructor TCompiler.Destroy; 46 var47 I: Integer;48 47 begin 49 48 ProgramCode.Free; 50 49 SourceCode.Free; 50 Parser.Free; 51 Producer.Free; 51 52 end; 52 53 -
branches/DelphiToC/UPascalParser.pas
r13 r14 12 12 TPascalParser = class 13 13 private 14 CodePosition: Integer;15 14 ProgramCode: TProgram; 16 15 FOnErrorMessage: TOnErrorMessage; 17 16 procedure ErrorMessage(Text: string); 18 17 public 18 CodePosition: Integer; 19 19 SourceCode: TStringList; 20 20 function IsAlphanumeric(Character: Char): Boolean; … … 32 32 procedure ParseModuleUnit(Module: TModule); 33 33 procedure ParseModuleProgram(Module: TModule); 34 35 34 procedure ParseFunction(AFunction: TFunction); 36 35 procedure ParseVariableList(VariableList: TVariableList); … … 207 206 begin 208 207 with AProgram do begin 209 for I := 0 to Modules.Count - 1 do210 TModule(Modules[I]).Clear;211 Modules.Clear;212 with TModule(Modules[Modules.Add(TModule.Create)]) do begin213 Name := 'main';214 with TType(Types[Types.Add(TType.Create)]) do begin215 Name := 'byte';216 Size := 1;217 UsedType := nil;218 end;219 end;208 for I := 0 to Modules.Count - 1 do 209 TModule(Modules[I]).Clear; 210 Modules.Clear; 211 with TModule(Modules[Modules.Add(TModule.Create)]) do begin 212 Name := 'main'; 213 with TType(Types[Types.Add(TType.Create)]) do begin 214 Name := 'byte'; 215 Size := 1; 216 UsedType := nil; 217 end; 218 end; 220 219 ParseModule(TModule(Modules[0])); 221 220 end; … … 294 293 Constant: TConstant; 295 294 // Brackets: Integer; 296 Expressions: T List; // TList<TExpression>;295 Expressions: TExpressionList; 297 296 I: Integer; 298 297 II: Integer; 299 298 begin 300 Expressions := T List.Create;299 Expressions := TExpressionList.Create; 301 300 Expressions.Add(TExpression.Create); 302 301 with CommonBlock do begin … … 367 366 end else 368 367 begin 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 368 with TExpression(Expressions[Expressions.Count - 1]) do begin 369 SubItems[1] := TExpression.Create; 370 TExpression(SubItems[1]).NodeType := ntConstant; 371 372 if Identifier[1] = '''' then begin 373 SetLength(TExpression(SubItems[1]).Value, Length(Identifier)); 374 for I := 1 to Length(Identifier) do TExpression(SubItems[1]).Value[I - 1] := Byte(Identifier[I]); 375 end else begin 376 SetLength(TExpression(SubItems[1]).Value, 1); 377 TExpression(SubItems[1]).Value[0] := StrToInt(Identifier); 378 end; 379 end; 380 with TExpression(Expressions.Items[Expressions.Add(TExpression.Create)]) do begin 381 SubItems[0] := TExpression(Expressions[Expressions.Count - 2]).SubItems[1]; 382 end; 384 383 end; 385 384 end; … … 400 399 end; 401 400 Result := TExpression(Expressions[0]).SubItems[1]; 402 TExpression(Expressions[0]). Destroy;403 TExpression(Expressions[1]). Destroy;401 TExpression(Expressions[0]).SubItems[1] := nil; 402 TExpression(Expressions[1]).SubItems[0] := nil; 404 403 Expressions.Destroy; 405 404 end; … … 623 622 end; 624 623 625 626 627 628 629 630 631 624 end. -
branches/DelphiToC/UPascalSource.pas
r13 r14 24 24 TVariableList = class; 25 25 TFunctionList = class; 26 TExpressionList = class; 26 27 TExpression = class; 27 28 TOperationList = class; … … 56 57 Parent: TCommonBlock; 57 58 function Search(Name: string): TType; 59 destructor Destroy; override; 58 60 end; 59 61 … … 67 69 Parent: TCommonBlock; 68 70 function Search(Name: string): TConstant; 71 destructor Destroy; override; 69 72 end; 70 73 … … 78 81 Parent: TCommonBlock; 79 82 function Search(Name: string): TVariable; 83 destructor Destroy; override; 80 84 end; 81 85 … … 86 90 Value: TValue; 87 91 OperatorName: string; 88 SubItems: T List; // TList<TExpression>92 SubItems: TExpressionList; 89 93 Associated: Boolean; 90 94 constructor Create; 91 95 destructor Destroy; override; 92 96 end; 97 98 TExpressionList = class(TList) 99 destructor Destroy; override; 100 end; 101 93 102 94 103 TOperation = class … … 177 186 for I := 0 to Modules.Count - 1 do 178 187 TModule(Modules[I]).Free; 188 Modules.Free; 179 189 Device.Free; 180 190 end; … … 184 194 185 195 { TConstantList } 196 197 destructor TConstantList.Destroy; 198 var 199 I: Integer; 200 begin 201 for I := 0 to Count - 1 do 202 TConstant(Items[I]).Free; 203 inherited; 204 end; 186 205 187 206 function TConstantList.Search(Name: string): TConstant; … … 257 276 { TTypeList } 258 277 278 destructor TTypeList.Destroy; 279 var 280 I: Integer; 281 begin 282 for I := 0 to Count - 1 do 283 TType(Items[I]).Free; 284 inherited; 285 end; 286 259 287 function TTypeList.Search(Name: string): TType; 260 288 var … … 273 301 { TVariableList } 274 302 303 destructor TVariableList.Destroy; 304 var 305 I: Integer; 306 begin 307 for I := 0 to Count - 1 do 308 TVariable(Items[I]).Free; 309 inherited; 310 end; 311 275 312 function TVariableList.Search(Name: string): TVariable; 276 313 var … … 312 349 constructor TExpression.Create; 313 350 begin 314 SubItems := T List.Create;351 SubItems := TExpressionList.Create; 315 352 SubItems.Count := 2; 316 353 end; … … 318 355 destructor TExpression.Destroy; 319 356 begin 320 SubItems. Destroy;357 SubItems.Free; 321 358 inherited; 322 359 end; … … 341 378 end; 342 379 380 { TExpressionList } 381 382 destructor TExpressionList.Destroy; 383 var 384 I: Integer; 385 begin 386 for I := 0 to Count - 1 do 387 TExpression(Items[I]).Free; 388 inherited; 389 end; 390 343 391 end. 344 392
Note:
See TracChangeset
for help on using the changeset viewer.