Changeset 19
- Timestamp:
- Apr 9, 2009, 2:08:56 PM (16 years ago)
- Location:
- branches/DelphiToC
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DelphiToC/Example.pas
r14 r19 1 1 program Test; 2 3 procedure Pokus; 4 begin 5 end; 6 7 const 8 Verze: Byte = 10; 2 9 var 3 a: B yte;10 a: Bpyte; 4 11 B: Byte; 12 sS: Byte; 5 13 begin 6 14 A := 1; 7 end ;15 end. -
branches/DelphiToC/UCSource.pas
r14 r19 5 5 uses 6 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, StdCtrls, UPascalSource ;7 Dialogs, StdCtrls, UPascalSource, UCodeProducer; 8 8 9 9 type 10 TC Source = class10 TCProducer = class(TCodeProducer) 11 11 TextSource: TStringList; 12 PascalSource: TStringList; 13 procedure Generate; 12 procedure Produce; override; 14 13 constructor Create; 15 14 destructor Destroy; override; 15 private 16 procedure GenerateCommonBlock(CommonBlock: TCommonBlock; 17 LabelPrefix: string); 18 procedure GenerateProgram(ProgramBlock: TProgram); 16 19 end; 17 20 18 21 implementation 19 22 20 { TC Source}23 { TCProducer } 21 24 22 constructor TC Source.Create;25 constructor TCProducer.Create; 23 26 begin 24 27 TextSource := TStringList.Create; 25 28 end; 26 29 27 destructor TC Source.Destroy;30 destructor TCProducer.Destroy; 28 31 begin 29 32 TextSource.Free; … … 31 34 end; 32 35 33 procedure TC Source.Generate;36 procedure TCProducer.Produce; 34 37 begin 35 38 inherited; 39 GenerateProgram(ProgramCode); 36 40 end; 37 41 42 procedure TCProducer.GenerateProgram(ProgramBlock: TProgram); 43 var 44 I: Integer; 45 begin 46 with ProgramBlock do 47 for I := 0 to Modules.Count - 1 do 48 GenerateCommonBlock(TModule(Modules[I]), ''); 49 end; 50 51 procedure TCProducer.GenerateCommonBlock(CommonBlock: TCommonBlock; LabelPrefix: string); 52 var 53 I: Integer; 54 LabelName: string; 55 begin 56 with CommonBlock do begin 57 TextSource.Add('void ' + Name + '()'); 58 TextSource.Add('{'); 59 60 TextSource.Add('}'); 61 end; 62 end; 63 64 65 38 66 end. -
branches/DelphiToC/UMainForm.dfm
r12 r19 3 3 Top = 0 4 4 Caption = 'Pascal Compiler AVR' 5 ClientHeight = 6456 ClientWidth = 7505 ClientHeight = 535 6 ClientWidth = 883 7 7 Color = clBtnFace 8 8 Font.Charset = DEFAULT_CHARSET … … 12 12 Font.Style = [] 13 13 OldCreateOrder = False 14 Position = poDesktopCenter 14 15 OnClose = FormClose 15 16 OnCreate = FormCreate 16 17 OnDestroy = FormDestroy 17 18 OnShow = FormShow 19 DesignSize = ( 20 883 21 535) 18 22 PixelsPerInch = 96 19 23 TextHeight = 13 … … 21 25 Left = 8 22 26 Top = 8 23 Width = 401 24 Height = 529 27 Width = 281 28 Height = 424 29 Anchors = [akLeft, akTop, akBottom] 25 30 Font.Charset = DEFAULT_CHARSET 26 31 Font.Color = clWindowText … … 31 36 ScrollBars = ssBoth 32 37 TabOrder = 0 38 ExplicitHeight = 435 33 39 end 34 40 object Button1: TButton 35 41 Left = 8 36 Top = 61542 Top = 510 37 43 Width = 75 38 44 Height = 22 45 Anchors = [akLeft, akBottom] 39 46 Caption = 'Kompilovat' 40 47 TabOrder = 1 41 48 OnClick = Button1Click 49 ExplicitTop = 615 42 50 end 43 51 object Memo2: TMemo 44 Left = 41552 Left = 600 45 53 Top = 8 46 Width = 321 47 Height = 529 54 Width = 277 55 Height = 424 56 Anchors = [akLeft, akTop, akRight, akBottom] 48 57 Font.Charset = DEFAULT_CHARSET 49 58 Font.Color = clWindowText … … 54 63 ScrollBars = ssBoth 55 64 TabOrder = 2 65 ExplicitWidth = 281 66 ExplicitHeight = 435 56 67 end 57 68 object Memo3: TMemo 58 69 Left = 8 59 Top = 54360 Width = 72870 Top = 438 71 Width = 869 61 72 Height = 66 73 Anchors = [akLeft, akRight, akBottom] 62 74 ScrollBars = ssBoth 63 75 TabOrder = 3 76 ExplicitTop = 543 77 ExplicitWidth = 728 78 end 79 object TreeView1: TTreeView 80 Left = 296 81 Top = 8 82 Width = 298 83 Height = 424 84 Anchors = [akLeft, akTop, akBottom] 85 Indent = 19 86 TabOrder = 4 64 87 end 65 88 end -
branches/DelphiToC/UMainForm.pas
r14 r19 5 5 uses 6 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, StdCtrls, UPascalSource, UPascalCompiler, UAssemblerSource; 7 Dialogs, StdCtrls, UPascalSource, UPascalCompiler, UAssemblerSource, 8 UCSource, ComCtrls; 8 9 9 10 type … … 13 14 Memo2: TMemo; 14 15 Memo3: TMemo; 16 TreeView1: TTreeView; 15 17 procedure FormShow(Sender: TObject); 16 18 procedure FormClose(Sender: TObject; var Action: TCloseAction); … … 20 22 procedure ErrorMessage(Text: string); 21 23 private 22 { Private declarations }24 procedure FillTreeView; 23 25 public 24 26 Compiler: TCompiler; … … 41 43 Compiler.SourceCode.Assign(Memo1.Lines); 42 44 Compiler.Compile; 45 FillTreeView; 43 46 Memo2.Clear; 44 with TAssemblerProducer(Compiler.Producer) do 45 for I := 0 to AssemblyCode.Count - 1 do 46 Memo2.Lines.Add(TAssemblerLine(AssemblyCode[I]).AsString); 47 if Compiler.Producer is TAssemblerProducer then begin 48 with TAssemblerProducer(Compiler.Producer) do 49 for I := 0 to AssemblyCode.Count - 1 do 50 Memo2.Lines.Add(TAssemblerLine(AssemblyCode[I]).AsString); 51 end else 52 if Compiler.Producer is TCProducer then begin 53 with TCProducer(Compiler.Producer) do 54 Memo2.Lines.Assign(TextSource); 55 end; 47 56 end; 48 57 … … 50 59 begin 51 60 MainForm.Memo3.Lines.Add(Text); 61 end; 62 63 procedure TMainForm.FillTreeView; 64 var 65 NewNode: TTreeNode; 66 NewNode2: TTreeNode; 67 ModuleNode: TTreeNode; 68 I: Integer; 69 M: Integer; 70 begin 71 with TreeView1, Items, Compiler do begin 72 BeginUpdate; 73 Clear; 74 AddChild(nil, 'Projekt'); 75 for M := 0 to ProgramCode.Modules.Count - 1 do 76 with TModule(ProgramCode.Modules[M]) do begin 77 ModuleNode := AddChild(TopItem, Name); 78 NewNode := AddChild(ModuleNode, 'Typy'); 79 for I := 0 to Types.Count - 1 do 80 with TType(Types[I]) do 81 NewNode2 := AddChild(NewNode, Name); 82 NewNode := AddChild(ModuleNode, 'Funkce'); 83 for I := 0 to Methods.Count - 1 do 84 with TFunction(Methods[I]) do 85 NewNode2 := AddChild(NewNode, Name); 86 NewNode := AddChild(ModuleNode, 'Promìnné'); 87 for I := 0 to Variables.Count - 1 do 88 with TVariable(Variables[I]) do 89 NewNode2 := AddChild(NewNode, Name); 90 NewNode := AddChild(ModuleNode, 'Konstanty'); 91 for I := 0 to Constants.Count - 1 do 92 with TConstant(Constants[I]) do 93 NewNode2 := AddChild(NewNode, Name); 94 NewNode := AddChild(ModuleNode, 'Program'); 95 end; 96 TopItem.Expand(True); 97 EndUpdate; 98 end; 52 99 end; 53 100 -
branches/DelphiToC/UPascalCompiler.pas
r14 r19 5 5 uses 6 6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 7 Dialogs, StdCtrls, UPascalSource, UCodeProducer, UPascalParser, UAssemblerSource; 7 Dialogs, StdCtrls, UPascalSource, UCodeProducer, UPascalParser, UAssemblerSource, 8 UCSource; 8 9 9 10 type 10 11 TCompiler = class 11 12 private 12 ProgramCode: TProgram;13 13 FOnErrorMessage: TOnErrorMessage; 14 14 procedure ErrorMessage(Text: string); 15 15 public 16 ProgramCode: TProgram; 16 17 SourceCode: TStringList; 17 18 Parser: TPascalParser; … … 38 39 SourceCode := TStringList.Create; 39 40 ProgramCode := TProgram.Create; 40 Producer := T AssemblerProducer.Create;41 Producer := TCProducer.Create; 41 42 Producer.ProgramCode := ProgramCode; 42 43 Parser := TPascalParser.Create; 43 44 Parser.SourceCode := SourceCode; 45 Parser.OnErrorMessage := ErrorMessage; 44 46 end; 45 47 -
branches/DelphiToC/UPascalParser.pas
r14 r19 28 28 function IsOperator(Text: string): Boolean; 29 29 procedure ParseProgram(AProgram: TProgram); 30 procedure ParseFunctionList(FunctionList: TFunctionList);31 30 procedure ParseModule(Module: TModule); 32 31 procedure ParseModuleUnit(Module: TModule); 33 32 procedure ParseModuleProgram(Module: TModule); 34 procedure ParseFunction( AFunction: TFunction);33 procedure ParseFunction(FunctionList: TFunctionList); 35 34 procedure ParseVariableList(VariableList: TVariableList); 36 35 procedure ParseVariable(Variable: TVariable); … … 39 38 procedure ParseTypeList(TypeList: TTypeList); 40 39 procedure ParseType(AType: TType); 41 procedure ParseCommonBlockDefinitions(CommonBlock: TCommonBlock );40 procedure ParseCommonBlockDefinitions(CommonBlock: TCommonBlock; EndSymbol: string = ';'); 42 41 function ParseCommonBlockExpression(CommonBlock: TCommonBlock): TExpression; 43 42 procedure ParseCommonBlockProgramCode(CommonBlock: TCommonBlock); 44 43 procedure ParseCommonBlockOperation(CommonBlock: TCommonBlock); 45 procedure Parse;44 procedure Log(Text: string); 46 45 property OnErrorMessage: TOnErrorMessage read FOnErrorMessage write FOnErrorMessage; 47 46 end; … … 58 57 procedure TPascalParser.Expect(Code: string); 59 58 begin 59 Log('Expected: ' + Code + ' Readed: ' + NextCode); 60 60 if NextCode <> Code then begin 61 61 ErrorMessage('Expected ' + Code + ' but ' + NextCode + ' found.'); … … 112 112 begin 113 113 Result := (Character = ' ') or (Character = #13) or (Character = #10); 114 end; 115 116 procedure TPascalParser.Log(Text: string); 117 const 118 LogFileName = 'ParseLog.txt'; 119 var 120 LogFile: TextFile; 121 begin 122 AssignFile(LogFile, LogFileName); 123 if FileExists(LogFileName) then Append(LogFile) 124 else Rewrite(LogFile); 125 WriteLn(LogFile, Text); 126 CloseFile(LogFile); 114 127 end; 115 128 … … 189 202 begin 190 203 Result := NextCode(True); 191 end; 192 193 procedure TPascalParser.ParseFunction(AFunction: TFunction); 194 begin 195 with AFunction do begin 196 if NextCode = 'var' then ParseVariableList(TVariableList(Variables)) 197 else if NextCode = 'const' then ParseConstantList(TConstantList(Constants)) 198 else if NextCode = 'type' then ParseTypeList(TTypeList(Types)) 199 else ParseProgram(ProgramCode); 204 Log('Read: ' + Result); 205 end; 206 207 procedure TPascalParser.ParseFunction(FunctionList: TFunctionList); 208 begin 209 with FunctionList do begin 210 with TFunction(Items[Add(TFunction.Create)]) do begin 211 Expect('procedure'); 212 Name := ReadCode; 213 Expect(';'); 214 ParseCommonBlockDefinitions(Items[Count - 1]); 215 end; 200 216 end; 201 217 end; … … 205 221 I: Integer; 206 222 begin 223 Log('==== Parse start ===='); 207 224 with AProgram do begin 208 225 for I := 0 to Modules.Count - 1 do … … 227 244 228 245 procedure TPascalParser.ParseConstantList(ConstantList: TConstantList); 229 begin 230 // Compiler.Expect('const'); 231 // while Compiler.IsIdentificator(Compiler.NextCode) do 232 // TConstant(Items[Add(TConstant.Create)]).Parse(Compiler); 246 var 247 Identifiers: TStringList; 248 NewValueType: TType; 249 TypeName: string; 250 ConstantName: string; 251 Constant: TConstant; 252 I: Integer; 253 ConstantValue: string; 254 begin 255 Identifiers := TStringList.Create; 256 with ConstantList do begin 257 Expect('const'); 258 while IsIdentificator(NextCode) do begin 259 ConstantName := ReadCode; 260 Constant := Search(ConstantName); 261 if not Assigned(Constant) then begin 262 Identifiers.Add(ConstantName); 263 while NextCode = ',' do begin 264 Expect(','); 265 Identifiers.Add(ReadCode); 266 end; 267 end else ErrorMessage('Pøedefinování existující konstanty.'); 268 Expect(':'); 269 TypeName := ReadCode; 270 NewValueType := Parent.Types.Search(TypeName); 271 Expect('='); 272 ConstantValue := ReadCode; 273 Expect(';'); 274 275 if NewValueType = nil then ErrorMessage('Typ ' + TypeName + ' nebyl definován.') 276 else for I := 0 to Identifiers.Count - 1 do 277 with TConstant(Items[Add(TConstant.Create)]) do begin 278 Name := Identifiers[I]; 279 ValueType := NewValueType; 280 Value := ConstantValue; 281 end; 282 end; 283 end; 284 Identifiers.Destroy; 233 285 end; 234 286 … … 253 305 end; 254 306 end; 255 ParseCommonBlockDefinitions(Module );307 ParseCommonBlockDefinitions(Module, '.'); 256 308 end; 257 309 end; … … 266 318 end; 267 319 268 procedure TPascalParser.Parse; 269 begin 270 271 end; 272 273 procedure TPascalParser.ParseCommonBlockDefinitions(CommonBlock: TCommonBlock); 320 procedure TPascalParser.ParseCommonBlockDefinitions(CommonBlock: TCommonBlock; EndSymbol: string = ';'); 274 321 begin 275 322 with CommonBlock do begin 276 while NextCode <> '.'do begin323 while NextCode <> EndSymbol do begin 277 324 if NextCode = 'var' then ParseVariableList(TVariableList(Variables)) 278 325 else if NextCode = 'const' then ParseConstantList(TConstantList(Constants)) 279 326 else if NextCode = 'type' then ParseTypeList(TTypeList(Types)) 327 else if NextCode = 'procedure' then ParseFunction(Methods) 280 328 else begin 281 329 ParseCommonBlockProgramCode(CommonBlock); … … 283 331 end; 284 332 end; 333 Expect(EndSymbol); 285 334 end; 286 335 end; … … 371 420 372 421 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]); 422 TExpression(SubItems[1]).Value := Identifier; 423 //SetLength(TExpression(SubItems[1]).Value, Length(Identifier)); 424 //for I := 1 to Length(Identifier) do 425 // TExpression(SubItems[1]).Value[I - 1] := Byte(Identifier[I]); 375 426 end else begin 376 SetLength(TExpression(SubItems[1]).Value, 1);377 TExpression(SubItems[1]).Value[0] := StrToInt(Identifier);427 //SetLength(TExpression(SubItems[1]).Value, 1); 428 //TExpression(SubItems[1]).Value[0] := StrToInt(Identifier); 378 429 end; 379 430 end; … … 518 569 with TExpression(SubItems[1]) do begin 519 570 NodeType := ntConstant; 520 SetLength(Value, 1); 521 Value[0] := 1; 571 //SetLength(Value, 1); 572 //Value[0] := 1; 573 Value := 1; 522 574 end; 523 575 end; … … 617 669 end; 618 670 619 procedure TPascalParser.ParseFunctionList(FunctionList: TFunctionList);620 begin621 622 end;623 624 671 end. -
branches/DelphiToC/UPascalSource.pas
r14 r19 17 17 TNodeType = (ntNone, ntVariable, ntFunction, ntConstant, ntOperator); 18 18 19 TValue = array of Byte;19 TValue = Variant; //array of Byte; 20 20 21 21 TCommonBlock = class; … … 28 28 TOperationList = class; 29 29 TFunction = class; 30 TVariable = class; 31 TConstant = class; 30 32 31 33 TDevice = class 32 34 Family: string; 33 35 Memory: array[TMemoryType] of Integer; 36 end; 37 38 TContext = class 39 40 end; 41 42 TCommandList = class; 43 44 TCommand = class 45 46 end; 47 48 TBeginEnd = class(TCommand) 49 Commands: TCommandList; 50 end; 51 52 TWhileDo = class(TCommand) 53 Condition: TExpression; 54 Command: TCommand; 55 end; 56 57 WithDo = class(TCommand) 58 Context: TContext; 59 Command: TCommand; 60 end; 61 62 RepeatUntil = class(TCommand) 63 Block: TCommandList; 64 Condition: TExpression; 65 end; 66 67 ForToDo = class(TCommand) 68 ControlVariable: TVariable; 69 Start: TExpression; 70 Stop: TExpression; 71 Command: TCommand; 72 end; 73 74 IfThenElse = class(TCommand) 75 Condition: TExpression; 76 Command: TCommand; 77 ElseCommand: TCommand; 78 end; 79 80 TCaseOfEndBranche = class 81 Constant: TConstant; 82 Command: TCommand; 83 end; 84 85 CaseOfEnd = class(TCommand) 86 Expression: TExpression; 87 Branches: TList; // TList<TCaseOfEndBranche> 88 ElseCommand: TCommand; 89 end; 90 91 TryFinally = class(TCommand) 92 Block: TCommandList; 93 FinallyBlock: TCommandList; 94 end; 95 96 TryExcept = class(TCommand) 97 Block: TCommandList; 98 ExceptBlock: TCommandList; 99 end; 100 101 102 103 TCommandList = class(TList) 104 34 105 end; 35 106 … … 54 125 end; 55 126 127 TTypeRecordItem = class 128 Name: string; 129 DataType: TType; 130 end; 131 132 TTypeRecord = class 133 Items: TList; // TList<TTypeRecordItem> 134 end; 135 136 TTypeArray = class 137 //Range: TTypeRange; 138 ItemType: TType; 139 end; 140 56 141 TTypeList = class(TList) 57 142 Parent: TCommonBlock; … … 100 185 end; 101 186 102 103 187 TOperation = class 104 188 Instruction: TInstruction; … … 125 209 Parent: TCommonBlock; 126 210 function Search(Name: string): TFunction; 211 destructor Destroy; override; 127 212 end; 128 213 … … 331 416 { TFunctionList } 332 417 418 destructor TFunctionList.Destroy; 419 var 420 I: Integer; 421 begin 422 for I := 0 to Count - 1 do 423 TFunction(Items[I]).Free; 424 inherited; 425 end; 426 333 427 function TFunctionList.Search(Name: string): TFunction; 334 428 var
Note:
See TracChangeset
for help on using the changeset viewer.