- Timestamp:
- Oct 15, 2012, 12:00:21 PM (12 years ago)
- Location:
- branches/AS8051toC
- Files:
-
- 10 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/AS8051toC/AS8051toC.lpi
r22 r25 30 30 </local> 31 31 </RunParams> 32 <RequiredPackages Count=" 2">32 <RequiredPackages Count="3"> 33 33 <Item1> 34 <PackageName Value="SynEdit"/> 34 <PackageName Value="TemplateGenerics"/> 35 <DefaultFilename Value="Packages\TemplateGenerics\TemplateGenerics.lpk" Prefer="True"/> 35 36 </Item1> 36 37 <Item2> 38 <PackageName Value="SynEdit"/> 39 </Item2> 40 <Item3> 37 41 <PackageName Value="LCL"/> 38 </Item 2>42 </Item3> 39 43 </RequiredPackages> 40 <Units Count=" 4">44 <Units Count="6"> 41 45 <Unit0> 42 46 <Filename Value="AS8051toC.lpr"/> … … 62 66 <UnitName Value="UParser"/> 63 67 </Unit3> 68 <Unit4> 69 <Filename Value="USource.pas"/> 70 <IsPartOfProject Value="True"/> 71 <UnitName Value="USource"/> 72 </Unit4> 73 <Unit5> 74 <Filename Value="UCompiler.pas"/> 75 <IsPartOfProject Value="True"/> 76 <UnitName Value="UCompiler"/> 77 </Unit5> 64 78 </Units> 65 79 </ProjectOptions> … … 75 89 <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/> 76 90 </SearchPaths> 91 <Parsing> 92 <SyntaxOptions> 93 <SyntaxMode Value="Delphi"/> 94 </SyntaxOptions> 95 </Parsing> 77 96 <Linking> 78 97 <Options> -
branches/AS8051toC/AS8051toC.lpr
r22 r25 8 8 {$ENDIF}{$ENDIF} 9 9 Interfaces, // this includes the LCL widgetset 10 Forms, UMainForm, UFindFile, UParser 10 Forms, UMainForm, UFindFile, UParser, USource, TemplateGenerics, UCompiler 11 11 { you can add units after this }; 12 12 -
branches/AS8051toC/UMainForm.lfm
r22 r25 1 1 object FormMain: TFormMain 2 Left = 2 662 Left = 226 3 3 Height = 553 4 Top = 904 Top = 110 5 5 Width = 702 6 6 Caption = 'AS8051 to C converter' … … 10 10 OnDestroy = FormDestroy 11 11 OnShow = FormShow 12 LCLVersion = '1. 1'12 LCLVersion = '1.0.1.3' 13 13 WindowState = wsMaximized 14 14 object EditPath: TEdit … … 19 19 Anchors = [akTop, akLeft, akRight] 20 20 TabOrder = 0 21 Text = ' h:\Projekty\E341\Repository\trunk'21 Text = 'Test\Test.a51' 22 22 end 23 23 object ButtonConvert: TButton 24 Left = 724 Left = 8 25 25 Height = 25 26 Top = 3 426 Top = 32 27 27 Width = 75 28 28 Caption = 'Convert' … … 533 533 top = 168 534 534 end 535 object OpenDialog1: TOpenDialog 536 DefaultExt = '.a51;.asm' 537 left = 379 538 top = 168 539 end 535 540 end -
branches/AS8051toC/UMainForm.pas
r23 r25 17 17 ButtonConvert: TButton; 18 18 EditPath: TEdit; 19 OpenDialog1: TOpenDialog; 19 20 SynCppSyn1: TSynCppSyn; 20 21 SynMemo1: TSynMemo; … … 26 27 procedure FormShow(Sender: TObject); 27 28 private 28 Parser: TParser ;29 Parser: TParserASM51; 29 30 public 30 31 MainFile: TStringList; … … 32 33 Output: string; 33 34 Macros: TStringList; 35 function IsHexNumber(Text: string): Boolean; 36 function CheckInstructionParameter: Boolean; 34 37 procedure Emit(Text: string); 35 38 procedure Error(Text: string); … … 66 69 67 70 procedure TFormMain.ButtonConvertClick(Sender: TObject); 68 var 69 Path: string; 70 begin 71 Path := 'h:\Projekty\E341\Repository\trunk'; 72 MainFile.LoadFromFile(Path + DirectorySeparator + 'main.a51'); 71 begin 72 MainFile.LoadFromFile(EditPath.Text); 73 73 Parser.Source := LowerCase(MainFile.Text); 74 74 Parser.Position := 1; … … 76 76 Parse; 77 77 MainFileOut.Text := Output; 78 ForceDirectoriesUTF8( Path+ DirectorySeparator + 'C');79 MainFileOut.SaveToFile( Path+ DirectorySeparator + 'C' +78 ForceDirectoriesUTF8(ExtractFileDir(EditPath.Text) + DirectorySeparator + 'C'); 79 MainFileOut.SaveToFile(ExtractFileDir(EditPath.Text) + DirectorySeparator + 'C' + 80 80 DirectorySeparator + 'main.c'); 81 81 SynMemo1.Lines.Assign(MainFileOut); … … 86 86 Dir: string; 87 87 begin 88 if SelectDirectory('Výběr adresáře', EditPath.Text, Dir) then 89 EditPath.Text := Dir; 88 OpenDialog1.FileName := EditPath.Text; 89 if OpenDialog1.Execute then begin 90 EditPath.Text := OpenDialog1.FileName; 91 end; 90 92 end; 91 93 92 94 procedure TFormMain.FormCreate(Sender: TObject); 93 95 begin 94 Parser := TParser .Create;96 Parser := TParserASM51.Create; 95 97 Parser.OnError := Error; 96 98 MainFile := TStringList.Create; … … 111 113 ButtonConvertClick(Self); 112 114 //Close; 115 end; 116 117 function TFormMain.IsHexNumber(Text: string): Boolean; 118 begin 119 Result := (LowerCase(Copy(Text, Length(Text), 1)) = 'h') and 120 (Parser.IsHexNumber(Copy(Text, 1, Length(Text) - 1))); 121 end; 122 123 function TFormMain.CheckInstructionParameter: Boolean; 124 begin 125 Text := Parser.GetNext; 126 Result := False; 127 if Parser.IsNumber(Text) then Result := True 128 else if Text = '#' then begin 129 Result := True; 130 Parser.Expect('#'); 131 if IsHexNumber(Parser.GetNext) then Emit(Parser.ReadNext) 132 else if Parser.IsNumber(Parser.GetNext) then Emit(Parser.ReadNext) 133 else Error('Invalid parameter'); 134 end 135 else if Copy(Text, 1, 1) = '@' then begin 136 if IsHexNumber(Copy(Text, 2, Length(Text))) then Result := True; 137 Emit(Parser.ReadNext); 138 end; 113 139 end; 114 140 … … 176 202 end 177 203 else if ParseComment then 178 else if ParseMacro then179 204 else if ParseKeywords then 180 205 else if ParseInstruction then … … 200 225 end else Error('Unexpected token "' + Variable + '"'); 201 226 end 227 else if ParseMacro then 228 else if ParseMacroUsage then 202 229 else Error('Unexpected token "' + GetNext + '"'); 203 230 Emit(LineEnding); … … 208 235 var 209 236 MacroName: string; 210 begin 211 with Parser do begin 237 OldPos: Integer; 238 begin 239 with Parser do begin 240 OldPos := Position; 212 241 if GetNext = '%' then begin 213 242 Result := True; … … 269 298 Expect('('); 270 299 end; 271 end;// else Error('Unknown macro: ' + ReadNext); 300 end else begin 301 Position := OldPos; 302 Result := False; 303 end; 272 304 end else Result := False; 273 305 end; … … 299 331 Expect('('); 300 332 Emit('('); 301 if ParseMacroUsage then302 333 ParseDbExpression; 334 //else Emit(ReadNext); 303 335 while GetNext = ',' do begin 304 336 Expect(','); 305 337 Emit(','); 306 if ParseMacroUsage then 307 else Emit(ReadNext); 338 ParseDbExpression; 339 //if ParseMacroUsage then 340 //else if GetNext <> ')' then Emit(ReadNext); 308 341 end; 309 342 Emit(')'); … … 339 372 ReadNext; 340 373 end; 374 end else 375 if GetNext = 'using' then begin 376 Result := True; 377 Expect('using'); 378 Value := ReadNext; 379 Emit('// using reg bank ' + Value); 341 380 end else 342 381 if GetNext = 'cseg' then begin … … 411 450 Emit(';'); 412 451 end else 452 if GetNext = 'mov' then begin 453 Expect('mov'); 454 Result := True; 455 Emit('MOV('); 456 ParseDbExpression; 457 Expect(','); 458 Emit(','); 459 ParseDbExpression; 460 Emit(');'); 461 end else 413 462 if GetNext = 'reti' then begin 414 463 Result := True; … … 457 506 Emit('&& '); 458 507 end else 459 Error('Unknown operand "' + ReadNext + '"');508 Error('Unknown operand "' + ReadNext + '"'); 460 509 if ParseMacroUsage then 461 510 else if IsNumber(GetNext) then Emit(ReadNext) … … 482 531 end 483 532 else if GetNext = '''' then Emit('"' + ParseString + '"') 484 else if IsNumber(GetNext) then Emit(ReadNext) 485 else if IsIdentificator(GetNext) then Emit(ReadNext) 486 else Error('Unknown IF parameter "' + GetNext + '"'); 533 else if CheckInstructionParameter then 534 else if IsIdentificator(GetNext) then begin 535 Emit(ReadNext); 536 if ParseMacroUsage then ; 537 end else Error('Unknown IF parameter "' + GetNext + '"'); 487 538 488 539 if GetNext = '+' then begin … … 504 555 Expect('/'); 505 556 Emit('/'); 557 ParseDbExpression; 558 end else 559 if GetNext = 'shl' then begin 560 Expect('shl'); 561 Emit('<< '); 562 ParseDbExpression; 563 end else 564 if GetNext = 'shr' then begin 565 Expect('shr'); 566 Emit('>> '); 506 567 ParseDbExpression; 507 568 end else -
branches/AS8051toC/UParser.pas
r22 r25 17 17 TErrorEvent = procedure (Message: string) of object; 18 18 19 19 20 { TParser } 20 21 … … 23 24 FOnError: TErrorEvent; 24 25 public 26 procedure Error(Text: string); virtual; 27 function Expect(Text: string): Boolean; virtual; 28 function GetNext(ChangePos: Boolean = False): string; virtual; 29 function ReadNext: string; virtual; 30 property OnError: TErrorEvent read FOnError write FOnError; 31 end; 32 33 { TParserASM51 } 34 35 TParserASM51 = class(TParser) 36 public 25 37 Source: string; 26 38 Position: Integer; 27 procedure Error(Text: string); 28 function Expect(Text: string): Boolean; 29 function GetNext(ChangePos: Boolean = False): string; 30 function ReadNext: string; 39 function Expect(Text: string): Boolean; override; 40 function GetNext(ChangePos: Boolean = False): string; override; 41 function ReadNext: string; override; 31 42 function ReadChar: Char; 32 43 function NextChar: Char; … … 39 50 function IsKeyword(Text: string): Boolean; 40 51 function IsNumber(Text: string): Boolean; 52 function IsHexNumber(Text: string): Boolean; 41 53 function IsString(Text: string): Boolean; 42 54 function IsWhiteSpace(Character: char): boolean; 43 55 function Eof: Boolean; 44 property OnError: TErrorEvent read FOnError write FOnError; 56 end; 57 58 TParserMacro = class 59 45 60 end; 46 61 … … 48 63 implementation 49 64 50 { TParser }51 52 65 procedure TParser.Error(Text: string); 53 66 begin … … 56 69 57 70 function TParser.Expect(Text: string): Boolean; 71 begin 72 73 end; 74 75 function TParser.GetNext(ChangePos: Boolean): string; 76 begin 77 78 end; 79 80 function TParser.ReadNext: string; 81 begin 82 83 end; 84 85 { TParserASM51 } 86 87 function TParserASM51.Expect(Text: string): Boolean; 58 88 var 59 89 NextToken: string; … … 64 94 end; 65 95 66 function TParser .GetNext(ChangePos: Boolean = False): string;96 function TParserASM51.GetNext(ChangePos: Boolean = False): string; 67 97 var 68 98 OldPosition: Integer; … … 77 107 end; 78 108 end else 79 if Is Numeric(NextChar) then begin109 if IsHex(NextChar) then begin 80 110 Result := ReadChar; 81 while Is Numeric(NextChar) do begin111 while IsHex(NextChar) do begin 82 112 Result := Result + ReadChar; 83 113 end; 114 if NextChar = 'h' then Result := Result + ReadChar; 115 // else 84 116 end else Result := ReadChar; 85 117 if not ChangePos then Position := OldPosition; 86 118 end; 87 119 88 function TParser .ReadNext: string;120 function TParserASM51.ReadNext: string; 89 121 begin 90 122 Result := GetNext(True); 91 123 end; 92 124 93 function TParser .ReadChar: Char;125 function TParserASM51.ReadChar: Char; 94 126 begin 95 127 Result := NextChar; … … 97 129 end; 98 130 99 function TParser .NextChar: Char;131 function TParserASM51.NextChar: Char; 100 132 begin 101 133 Result := Source[Position]; 102 134 end; 103 135 104 function TParser .ReadEol: string;136 function TParserASM51.ReadEol: string; 105 137 begin 106 138 Result := ''; … … 112 144 end; 113 145 114 function TParser .IsAlphabetic(Character: char): boolean;146 function TParserASM51.IsAlphabetic(Character: char): boolean; 115 147 begin 116 148 Result := (Character in ['a'..'z']) or (Character in ['A'..'Z']); 117 149 end; 118 150 119 function TParser .IsAlphanumeric(Character: char): boolean;151 function TParserASM51.IsAlphanumeric(Character: char): boolean; 120 152 begin 121 153 Result := IsAlphabetic(Character) or IsNumeric(Character); 122 154 end; 123 155 124 function TParser .IsNumeric(Character: char): boolean;156 function TParserASM51.IsNumeric(Character: char): boolean; 125 157 begin 126 158 Result := Character in ['0'..'9']; 127 159 end; 128 160 129 function TParser.IsHex(Character: char): boolean; 130 begin 131 Result := IsNumeric(Character) or (Character in ['A'..'F']); 132 end; 133 134 function TParser.IsString(Text: string): Boolean; 161 function TParserASM51.IsHex(Character: char): boolean; 162 begin 163 Result := IsNumeric(Character) or (Character in ['A'..'F']) or 164 (Character in ['a'..'f']); 165 end; 166 167 function TParserASM51.IsString(Text: string): Boolean; 135 168 begin 136 169 raise Exception.Create('Not implemented'); 137 170 end; 138 171 139 function TParser .IsIdentificator(Text: string): boolean;172 function TParserASM51.IsIdentificator(Text: string): boolean; 140 173 var 141 174 I: integer; … … 154 187 end; 155 188 156 function TParser .IsWhiteSpace(Character: char): boolean;189 function TParserASM51.IsWhiteSpace(Character: char): boolean; 157 190 begin 158 191 Result := (Character <= ' '); 159 192 end; 160 193 161 function TParser .Eof: Boolean;162 begin 163 Result := Position > Length(Source);164 end; 165 166 function TParser .IsKeyword(Text: string): Boolean;194 function TParserASM51.Eof: Boolean; 195 begin 196 Result := (Position > Length(Source)) or (Trim(Copy(Source, Position, Length(Source))) = ''); 197 end; 198 199 function TParserASM51.IsKeyword(Text: string): Boolean; 167 200 var 168 201 I: integer; … … 174 207 end; 175 208 176 function TParser .IsNumber(Text: string): Boolean;209 function TParserASM51.IsNumber(Text: string): Boolean; 177 210 var 178 211 Num: Integer; … … 181 214 end; 182 215 216 function TParserASM51.IsHexNumber(Text: string): Boolean; 217 var 218 Num: Integer; 219 begin 220 Result := TryStrToInt('$' + Text, Num); 221 end; 222 183 223 184 224 end.
Note:
See TracChangeset
for help on using the changeset viewer.