Changeset 22
- Timestamp:
- Jun 26, 2012, 10:42:14 AM (12 years ago)
- Location:
- branches/AS8051toC
- Files:
-
- 1 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/AS8051toC/AS8051toC.lpi
r21 r22 38 38 </Item2> 39 39 </RequiredPackages> 40 <Units Count=" 3">40 <Units Count="4"> 41 41 <Unit0> 42 42 <Filename Value="AS8051toC.lpr"/> … … 57 57 <UnitName Value="UFindFile"/> 58 58 </Unit2> 59 <Unit3> 60 <Filename Value="UParser.pas"/> 61 <IsPartOfProject Value="True"/> 62 <UnitName Value="UParser"/> 63 </Unit3> 59 64 </Units> 60 65 </ProjectOptions> -
branches/AS8051toC/AS8051toC.lpr
r21 r22 8 8 {$ENDIF}{$ENDIF} 9 9 Interfaces, // this includes the LCL widgetset 10 Forms, UMainForm, UFindFile 10 Forms, UMainForm, UFindFile, UParser 11 11 { you can add units after this }; 12 12 -
branches/AS8051toC/UMainForm.lfm
r21 r22 11 11 OnShow = FormShow 12 12 LCLVersion = '1.1' 13 WindowState = wsMaximized 13 14 object EditPath: TEdit 14 15 Left = 8 -
branches/AS8051toC/UMainForm.pas
r21 r22 8 8 Classes, SysUtils, FileUtil, SynHighlighterCpp, SynEdit, SynMemo, 9 9 SynHighlighterAny, SynHighlighterPas, Forms, Controls, Graphics, Dialogs, 10 StdCtrls; 11 12 const 13 Instructions: array[0..40] of string = ('JB', 'JNB', 'NOP', 'LCALL', 'SJMP', 14 'PUSH', 'POP', 'MOV', 'ADD', 'SUB', 'MUL', 'DIV', 'RET', 'RETI', 'MOVC', 15 'CLR', 'SETB', 'CALL', 'INC', 'DEC', 'ORL', 'ANL', 'SUBB', 'XCH', 'CPL', 16 'JC', 'JNC', 'JZ', 'JNZ', 'DJNZ', 'CJNE', 'DB', 'DBIT', 'DS', 'PUBLIC', 17 'EXTRN', 'CSEG', 'RSEG', 'JMP', 'DW', 'SWAP'); 10 StdCtrls, UParser; 11 18 12 type 19 20 { TParser }21 22 TParser = class23 Source: string;24 Position: Integer;25 function Expect(Text: string): Boolean;26 function Next: string;27 function ReadNext: string;28 private29 function IsAlphabetic(Character: char): boolean;30 function IsAlphanumeric(Character: char): boolean;31 function IsHex(Character: char): boolean;32 function IsIdentificator(Text: string): boolean;33 function IsKeyword(Text: string): Boolean;34 function IsNumeric(Character: char): boolean;35 function IsString(Text: string): Boolean;36 function IsWhiteSpace(Character: char): boolean;37 end;38 39 13 { TFormMain } 40 14 … … 52 26 procedure FormShow(Sender: TObject); 53 27 private 54 { private declarations }28 Parser: TParser; 55 29 public 56 30 MainFile: TStringList; 57 31 MainFileOut: TStringList; 58 procedure Parse(List: TStringList); 59 function ParseLine(Line: string): string; 32 Output: string; 33 Macros: TStringList; 34 procedure Emit(Text: string); 35 procedure Error(Text: string); 36 procedure Parse; 37 procedure ParseLine; 38 function ParseMacro: Boolean; 39 function ParseComment: Boolean; 40 function ParseMacroUsage: Boolean; 41 function ParseKeywords: Boolean; 42 function ParseInstruction: Boolean; 43 procedure ParseIfExpression; 44 procedure ParseDbExpression; 45 function ParseString: string; 60 46 end; 61 47 … … 65 51 implementation 66 52 67 { TParser } 68 69 function TParser.Expect(Text: string): Boolean; 70 begin 71 Result := Next = Text; 72 end; 73 74 function TParser.Next: string; 75 begin 76 Result := ''; 77 // Source[Position] 78 end; 79 80 function TParser.ReadNext: string; 81 begin 82 Result := Next; 83 Inc(Position, Length(Result)); 84 end; 85 86 function TParser.IsAlphabetic(Character: char): boolean; 87 begin 88 Result := (Character in ['a'..'z']) or (Character in ['A'..'Z']); 89 end; 90 91 function TParser.IsAlphanumeric(Character: char): boolean; 92 begin 93 Result := IsAlphabetic(Character) or IsNumeric(Character); 94 end; 95 96 function TParser.IsNumeric(Character: char): boolean; 97 begin 98 Result := Character in ['0'..'9']; 99 end; 100 101 function TParser.IsHex(Character: char): boolean; 102 begin 103 Result := IsNumeric(Character) or (Character in ['A'..'F']); 104 end; 105 106 function TParser.IsString(Text: string): Boolean; 107 begin 108 raise Exception.Create('Not implemented'); 109 end; 110 111 function TParser.IsIdentificator(Text: string): boolean; 112 var 113 I: integer; 114 begin 115 Result := True; 116 if Length(Text) = 0 then 117 Result := False; 118 //if IsKeyWord(Text) then 119 // Result := False; 120 if Length(Text) > 0 then 121 if not (Text[1] in ['a'..'z', 'A'..'Z', '%', '_']) then 122 Result := False; 123 for I := 2 to Length(Text) do 124 if not (Text[i] in ['a'..'z', 'A'..'Z', '0'..'9', '_']) then 125 Result := False; 126 end; 127 128 function TParser.IsWhiteSpace(Character: char): boolean; 129 begin 130 Result := (Character = ' ') or (Character = #8); 131 end; 132 133 function TParser.IsKeyword(Text: string): Boolean; 53 {$R *.lfm} 54 55 { TFormMain } 56 57 function TFormMain.IsKeyword(Text: string): Boolean; 134 58 var 135 59 I: integer; … … 141 65 end; 142 66 143 {$R *.lfm}144 145 { TFormMain }146 147 function TFormMain.IsKeyword(Text: string): Boolean;148 var149 I: integer;150 begin151 Result := False;152 for I := 0 to High(Instructions) do153 if Instructions[I] = Text then154 Result := True;155 end;156 157 67 procedure TFormMain.ButtonConvertClick(Sender: TObject); 158 68 var … … 161 71 Path := 'h:\Projekty\E341\Repository\trunk'; 162 72 MainFile.LoadFromFile(Path + DirectorySeparator + 'main.a51'); 163 Parse(MainFile); 73 Parser.Source := LowerCase(MainFile.Text); 74 Parser.Position := 1; 75 Output := ''; 76 Parse; 77 MainFileOut.Text := Output; 164 78 ForceDirectoriesUTF8(Path + DirectorySeparator + 'C'); 165 79 MainFileOut.SaveToFile(Path + DirectorySeparator + 'C' + … … 178 92 procedure TFormMain.FormCreate(Sender: TObject); 179 93 begin 94 Parser := TParser.Create; 95 Parser.OnError := Error; 180 96 MainFile := TStringList.Create; 181 97 MainFileOut := TStringList.Create; 98 Macros := TStringList.Create; 182 99 end; 183 100 184 101 procedure TFormMain.FormDestroy(Sender: TObject); 185 102 begin 103 Macros.Free; 186 104 MainFileOut.Free; 187 105 MainFile.Free; 106 Parser.Free; 188 107 end; 189 108 … … 194 113 end; 195 114 196 procedure TFormMain.Parse(List: TStringList); 115 procedure TFormMain.Emit(Text: string); 116 begin 117 Output := Output + Text; 118 end; 119 120 procedure TFormMain.Error(Text: string); 121 begin 122 Emit('Error: ' + Text + LineEnding); 123 Parser.Position := Length(Parser.Source) + 1; 124 end; 125 126 procedure TFormMain.Parse; 197 127 var 198 128 I: Integer; … … 203 133 begin 204 134 with MainFileOut do begin 205 Add('#include "opcodes.h"'); 206 Add(''); 207 Add('void main()'); 208 Add('{'); 209 Add('}'); 210 for I := 0 to List.Count - 1 do begin 211 Line := Trim(List[I]); 212 while Pos(' ', Line) > 0 do 213 Line := StringReplace(Line, ' ', ' ', [rfReplaceAll]); 214 if Pos(';', Line) > 0 then begin 215 Comment := Trim(Copy(Line, Pos(';', Line) + 1, Length(Line))); 216 Line := Trim(Copy(Line, 1, Pos(';', Line) - 1)); 217 end else Comment := ''; 218 if Length(Line) > 0 then Line := ParseLine(Line) + ' '; 219 if Comment <> '' then Add(Line + '// ' + Comment) 220 else Add(Line); 135 while not Parser.Eof do begin 136 ParseLine; 137 if Parser.GetNext = 'end' then Break; 221 138 end; 222 139 end; 223 140 end; 224 141 225 function TFormMain.ParseLine(Line: string): string;142 procedure TFormMain.ParseLine; 226 143 var 227 144 Part: string; … … 229 146 Instruction: string; 230 147 Macro: string; 231 begin 232 if Line[1] = '$' then begin 233 Part := Trim(Copy(Line, 2, Pos('(', Line) - 2)); 234 if LowerCase(Part) = 'include' then begin 235 Line := Copy(Line, Pos('(', Line) + 1, Length(Line)); 236 Line := LowerCase(Copy(Line, 1, Pos(')', Line) - 1)); 237 Line := StringReplace(Line, '.i51', '.h', [rfReplaceAll]); 238 //Result := '#include "' + Line + '"'; 148 Variable: string; 149 begin 150 with Parser do begin 151 if GetNext = '$' then begin 152 Expect('$'); 153 if GetNext = 'include' then begin 154 Expect('include'); 155 Expect('('); 156 Emit('#include '''); 157 Emit(ReadNext); 158 Expect('.'); 159 Emit('.'); 160 Emit(ReadNext); 161 Emit(''''); 162 Expect(')'); 163 end else Error('Unknown token "' + GetNext + '"'); 164 end 165 else if GetNext = ')' then begin 166 Expect(')'); 167 if GetNext = 'else' then begin 168 Expect('else'); 169 Emit('#else '); 170 Expect('('); 171 end else 172 if GetNext = 'fi' then begin 173 Expect('fi'); 174 Emit('#endif '); 175 end else Error('Unknown token "' + GetNext + '"'); 176 end 177 else if ParseComment then 178 else if ParseMacro then 179 else if ParseKeywords then 180 else if ParseInstruction then 181 else if IsIdentificator(GetNext) then begin 182 Variable := ReadNext; 183 if GetNext = ':' then begin 184 Expect(':'); 185 Emit(Variable + ':'); 186 end else 187 if GetNext = 'equ' then begin 188 Expect('equ'); 189 Emit('#define ' + Variable + ' '); 190 ParseDbExpression; 191 Emit(';'); 192 end else 193 if GetNext = 'segment' then begin 194 Expect('segment'); 195 ReadNext; 196 end else Error('Unexpected token "' + Variable + '"'); 197 end 198 else Error('Unexpected token "' + GetNext + '"'); 199 Emit(LineEnding); 200 end; 201 end; 202 203 function TFormMain.ParseMacro: Boolean; 204 var 205 MacroName: string; 206 begin 207 with Parser do begin 208 if GetNext = '%' then begin 209 Result := True; 210 Expect('%'); 211 if GetNext = 'define' then begin 212 Expect('define'); 213 Emit('#define '); 214 Expect('('); 215 MacroName := ReadNext; 216 Emit(MacroName); 217 Expect(')'); 218 Emit(' '); 219 Expect('('); 220 while GetNext <> ')' do begin 221 Emit(ReadNext); 222 end; 223 Expect(')'); 224 Emit(';'); 225 end else 226 if GetNext = 'if' then begin 227 Expect('if'); 228 Emit('#if '); 229 Expect('('); 230 ParseIfExpression; 231 Expect(')'); 232 Expect('then'); 233 Expect('('); 234 end else Error('Unknown macro: ' + ReadNext); 235 end else Result := False; 236 end; 237 end; 238 239 function TFormMain.ParseComment: Boolean; 240 begin 241 with Parser do begin 242 if GetNext = ';' then begin 243 Result := True; 244 Expect(';'); 245 Emit('//'); 246 Emit(ReadEol); 247 end else Result := False; 248 end; 249 end; 250 251 function TFormMain.ParseMacroUsage: Boolean; 252 var 253 MacroName: string; 254 begin 255 with Parser do begin 256 if GetNext = '%' then begin 257 Result := True; 258 Expect('%'); 259 MacroName := ReadNext; 260 Emit(MacroName); 261 end else Result := False; 262 end; 263 end; 264 265 function TFormMain.ParseKeywords: Boolean; 266 var 267 Value: string; 268 begin 269 with Parser do begin 270 if GetNext = 'extrn' then begin 271 Expect('extrn'); 272 Result := True; 273 ReadNext; 274 Expect('('); 275 ReadNext; 276 while GetNext = ',' do begin 277 Expect(','); 278 ReadNext; 279 end; 280 Expect(')'); 281 end else 282 if GetNext = 'public' then begin 283 Result := True; 284 Expect('public'); 285 ReadNext; 286 while GetNext = ',' do begin 287 Expect(','); 288 ReadNext; 289 end; 290 end else 291 if GetNext = 'cseg' then begin 292 Result := True; 293 Expect('cseg'); 294 Expect('at'); 295 Value := ReadNext; 296 if GetNext = 'h' then begin 297 Expect('h'); 298 //Emit('0x' + Value); 299 end; // else Emit(Value); 300 end else 301 if GetNext = 'rseg' then begin 302 Result := True; 303 Expect('rseg'); 304 Value := ReadNext; 305 if GetNext = 'h' then begin 306 Expect('h'); 307 //Emit('0x' + Value); 308 end; // else Emit(Value); 309 end else 310 if GetNext = 'dbit' then begin 311 Result := True; 312 Expect('dbit'); 313 Emit('char '); 314 Emit(ReadNext); 315 end else 316 if GetNext = 'db' then begin 317 Result := True; 318 Expect('db'); 319 Emit('char '); 320 ParseDbExpression; 321 while GetNext = ',' do begin 322 Expect(','); 323 Emit(','); 324 ParseDbExpression; 325 end; 326 end else 327 if GetNext = 'ds' then begin 328 Result := True; 329 Expect('ds'); 330 Emit('char* '); 331 ParseDbExpression; 332 while GetNext = ',' do begin 333 Expect(','); 334 Emit(','); 335 ParseDbExpression; 336 end; 337 end else Result := False; 338 end; 339 end; 340 341 function TFormMain.ParseInstruction: Boolean; 342 begin 343 with Parser do begin 344 if GetNext = 'jmp' then begin 345 Expect('jmp'); 346 Result := True; 347 Emit('goto '); 348 Emit(ReadNext); 349 Emit(';'); 350 end else 351 if GetNext = 'reti' then begin 352 Result := True; 353 Expect('reti'); 354 end else Result := False; 355 end; 356 end; 357 358 procedure TFormMain.ParseIfExpression; 359 begin 360 with Parser do begin 361 if ParseMacroUsage then 362 else if IsNumber(GetNext) then Emit(ReadNext) 363 else Error('Unknown IF parameter "' + GetNext + '"'); 364 if GetNext <> ')' then begin 365 if GetNext = 'eq' then begin 366 Expect('eq'); 367 Emit('= '); 368 end else 369 if GetNext = 'ne' then begin 370 Expect('ne'); 371 Emit('!= '); 372 end else 373 if GetNext = 'gt' then begin 374 Expect('gt'); 375 Emit('> '); 376 end else 377 if GetNext = 'ge' then begin 378 Expect('ge'); 379 Emit('>= '); 380 end else 381 if GetNext = 'lt' then begin 382 Expect('lt'); 383 Emit('< '); 384 end else 385 if GetNext = 'le' then begin 386 Expect('le'); 387 Emit('<= '); 388 end else 389 Error('Unknown operand "' + ReadNext + '"'); 390 if ParseMacroUsage then 391 else if IsNumber(GetNext) then Emit(ReadNext) 392 else Error('Unknown IF parameter "' + GetNext + '"'); 239 393 end; 240 end else if Line[1] = '%' then begin 241 Macro := Copy(Line, 2, Pos('(', Line) - 2); 242 Line := Trim(Copy(Line, Pos('(', Line) + 1, Length(Line))); 243 if Macro = 'IF' then OutLine := '#IF ' + Line 244 else if Macro = 'FI' then OutLine := '#ENDIF ' + Line 245 else if Macro = 'SET' then OutLine := '#DEFINE ' + Line 246 else if Macro = 'DEFINE' then OutLine := '#DEFINE ' + Line 247 else if Macro = '*DEFINE' then OutLine := '#DEFINE ' + Line 248 else OutLine := '// !%' + Macro + ' ' + Line; 249 Result := OutLine; 250 end else begin 251 if Pos(':', Line) > 0 then begin 252 OutLine := Copy(Line, 1, Pos(':', Line) - 1) + ': '; 253 Line := Trim(Copy(Line, Pos(':', Line) + 1, Length(Line))); 254 end else OutLine := ''; 255 256 if Pos(' ', Line) > 0 then begin 257 Instruction := Copy(Line, 1, Pos(' ', Line) - 1); 258 Line := Trim(Copy(Line, Pos(' ', Line) + 1, Length(Line))); 259 end else begin 260 Instruction := Line; 261 Line := ''; 394 end; 395 end; 396 397 procedure TFormMain.ParseDbExpression; 398 begin 399 with Parser do begin 400 401 if ParseMacroUsage then 402 else if GetNext = '$' then begin 403 Expect('$'); 404 Emit('$'); 405 end 406 else if GetNext = '(' then begin 407 Expect('('); 408 Emit('('); 409 ParseDbExpression; 410 Expect(')'); 411 Emit(')'); 412 end 413 else if GetNext = '''' then Emit('"' + ParseString + '"') 414 else if IsNumber(GetNext) then Emit(ReadNext) 415 else if IsIdentificator(GetNext) then Emit(ReadNext) 416 else Error('Unknown IF parameter "' + GetNext + '"'); 417 418 if GetNext = '+' then begin 419 Expect('+'); 420 Emit('+'); 421 ParseDbExpression; 422 end else 423 if GetNext = '-' then begin 424 Expect('-'); 425 Emit('-'); 426 ParseDbExpression; 427 end else 428 if GetNext = '*' then begin 429 Expect('*'); 430 Emit('*'); 431 ParseDbExpression; 432 end else 433 if GetNext = '/' then begin 434 Expect('/'); 435 Emit('/'); 436 ParseDbExpression; 262 437 end; 263 if IsKeyWord(Instruction) then OutLine := OutLine + Instruction + '(' + Line + ');' 264 else if Trim(Instruction) = '' then 265 else OutLine := OutLine + '// !' + Instruction + ' ' + Line; 266 Result := OutLine; 438 end; 439 end; 440 441 function TFormMain.ParseString: string; 442 begin 443 with Parser do begin 444 Result := ''; 445 Expect(''''); 446 while GetNext <> '''' do begin 447 Result := Result + ReadNext; 448 end; 449 Expect(''''); 267 450 end; 268 451 end;
Note:
See TracChangeset
for help on using the changeset viewer.