- Timestamp:
- Oct 15, 2012, 2:10:48 PM (12 years ago)
- Location:
- branches/AS8051toC
- Files:
-
- 42 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/AS8051toC/Test/C/main.c
r25 r26 1 // aktivuj wdg 2 MOV(wdtrst,01eh); 3 MOV(wdtrst,0e1h); 4 MOV(clkreg,01); 5 MOV(ie,000h); 6 // vymazu ram 7 MOV(r0,0ffh); 8 sss: 9 l(cl) 10 Error: Unexpected token ":" 11 1 //AKTIVUJWDGMOVWDTRST,#01EhMOVWDTRST,#0E1hMOVCLKREG,#01MOVIE,#000h;VYMAZURAMMOVR0,#0FFhsss: -
branches/AS8051toC/UCompiler.pas
r25 r26 6 6 7 7 uses 8 Classes, SysUtils ;8 Classes, SysUtils, UParser, SpecializedList; 9 9 10 10 type 11 12 { TCompilerMacro } 13 14 TCompilerMacro = class 15 private 16 Output: string; 17 function ParseString: string; 18 public 19 Macros: TStringList; 20 Parser: TParserASM51; 21 procedure ParseLine; 22 function ParseMacro: Boolean; 23 function ParseMacroUsage: Boolean; 24 procedure ParseExpression; 25 procedure ParseIfExpression; 26 procedure Emit(Text: string); 27 function ParseComment: Boolean; 28 constructor Create; 29 destructor Destroy; override; 30 function Compile(Source: string): string; 31 end; 32 33 { TCompiler } 34 11 35 TCompiler = class 12 36 private 37 Output: string; 38 public 39 Parser: TParserASM51; 40 Symbols: TListObject; 41 procedure Emit(Text: string); 42 function IsHexNumber(Text: string): Boolean; 43 function CheckIdentificator: Boolean; 44 function CheckInstructionParameter: Boolean; 45 function ParseComment: Boolean; 46 procedure ParseDbExpression; 47 function ParseInstruction: Boolean; 48 function ParseKeywords: Boolean; 49 procedure ParseLine; 50 function ParseString: string; 51 function Compile(Source: string): string; 52 constructor Create; 53 destructor Destroy; override; 13 54 end; 14 55 15 56 implementation 16 57 58 { TCompilerMacro } 59 60 procedure TCompilerMacro.ParseLine; 61 begin 62 with Parser do begin 63 if GetNext = '%' then begin 64 if ParseComment then 65 if ParseMacroUsage then Emit(ReadNext) 66 else if ParseMacro then Emit(ReadNext) 67 else ; 68 end else begin 69 Output := Output + ReadNext; 70 end; 71 end; 72 end; 73 74 procedure TCompilerMacro.ParseExpression; 75 begin 76 with Parser do begin 77 78 if ParseMacroUsage then 79 else if GetNext = '$' then begin 80 Expect('$'); 81 Emit('$'); 82 end 83 else if GetNext = '(' then begin 84 Expect('('); 85 Emit('('); 86 ParseExpression; 87 Expect(')'); 88 Emit(')'); 89 end 90 else if GetNext = '''' then Emit('"' + ParseString + '"') 91 else Error('Unknown IF parameter "' + GetNext + '"'); 92 93 if GetNext = '+' then begin 94 Expect('+'); 95 Emit('+'); 96 ParseExpression; 97 end else 98 if GetNext = '-' then begin 99 Expect('-'); 100 Emit('-'); 101 ParseExpression; 102 end else 103 if GetNext = '*' then begin 104 Expect('*'); 105 Emit('*'); 106 ParseExpression; 107 end else 108 if GetNext = '/' then begin 109 Expect('/'); 110 Emit('/'); 111 ParseExpression; 112 end else 113 if GetNext = 'shl' then begin 114 Expect('shl'); 115 Emit('<< '); 116 ParseExpression; 117 end else 118 if GetNext = 'shr' then begin 119 Expect('shr'); 120 Emit('>> '); 121 ParseExpression; 122 end else 123 if GetNext = 'or' then begin 124 Expect('or'); 125 Emit('|| '); 126 ParseExpression; 127 end else 128 if GetNext = 'and' then begin 129 Expect('and'); 130 Emit('&& '); 131 ParseExpression; 132 end; 133 end; 134 end; 135 136 procedure TCompilerMacro.Emit(Text: string); 137 begin 138 Output := Output + Text; 139 end; 140 141 function TCompilerMacro.ParseComment: Boolean; 142 begin 143 with Parser do begin 144 if GetNext = ';' then begin 145 Result := True; 146 Expect(';'); 147 Emit('//'); 148 Emit(ReadEol); 149 end else Result := False; 150 end; 151 end; 152 153 constructor TCompilerMacro.Create; 154 begin 155 Macros := TStringList.Create; 156 Parser := TParserASM51.Create; 157 end; 158 159 destructor TCompilerMacro.Destroy; 160 begin 161 FreeAndNil(Parser); 162 FreeAndNil(Macros); 163 inherited Destroy; 164 end; 165 166 function TCompilerMacro.ParseString: string; 167 begin 168 with Parser do begin 169 Result := ''; 170 Expect(''''); 171 while GetNext <> '''' do begin 172 Result := Result + ReadNext; 173 end; 174 Expect(''''); 175 end; 176 end; 177 178 function TCompilerMacro.Compile(Source: string): string; 179 var 180 OldPos: Integer; 181 begin 182 Output := ''; 183 Parser.Position := 1; 184 Parser.Source := Source; 185 while not Parser.Eof do begin 186 OldPos := Parser.Position; 187 ParseLine; 188 if Parser.GetNext = 'end' then Break; 189 if OldPos = Parser.Position then Break; 190 end; 191 Result := Output; 192 end; 193 194 { TCompiler } 195 196 function TCompiler.Compile(Source: string): string; 197 begin 198 Output := ''; 199 Parser.Position := 1; 200 Parser.Source := Source; 201 while not Parser.Eof do begin 202 ParseLine; 203 if Parser.GetNext = 'end' then Break; 204 end; 205 Result := Output; 206 end; 207 208 constructor TCompiler.Create; 209 begin 210 Parser := TParserASM51.Create; 211 Symbols := TListObject.Create; 212 end; 213 214 destructor TCompiler.Destroy; 215 begin 216 FreeAndNil(Symbols); 217 FreeAndNil(Parser); 218 inherited; 219 end; 220 221 procedure TCompiler.ParseLine; 222 var 223 Part: string; 224 OutLine: string; 225 Instruction: string; 226 Macro: string; 227 Variable: string; 228 begin 229 with Parser do begin 230 if GetNext = '$' then begin 231 Expect('$'); 232 if GetNext = 'include' then begin 233 Expect('include'); 234 Expect('('); 235 Emit('#include '''); 236 Emit(ReadNext); 237 Expect('.'); 238 Emit('.'); 239 Emit(ReadNext); 240 Emit(''''); 241 Expect(')'); 242 end else Error('Unknown token "' + GetNext + '"'); 243 end 244 else if GetNext = ')' then begin 245 Expect(')'); 246 if GetNext = 'else' then begin 247 Expect('else'); 248 Emit('#else '); 249 Expect('('); 250 end else 251 if GetNext = 'fi' then begin 252 Expect('fi'); 253 Emit('#endif '); 254 end; // else Error('Unknown token "' + GetNext + '"'); 255 end 256 else if ParseComment then 257 else if ParseKeywords then 258 else if ParseInstruction then 259 else if IsIdentificator(GetNext) then CheckIdentificator 260 else Error('Unexpected token "' + GetNext + '"'); 261 Emit(LineEnding); 262 end; 263 end; 264 265 function TCompilerMacro.ParseMacro: Boolean; 266 var 267 MacroName: string; 268 OldPos: Integer; 269 begin 270 with Parser do begin 271 OldPos := Position; 272 if GetNext = '%' then begin 273 Result := True; 274 Expect('%'); 275 if GetNext = 'define' then begin 276 Expect('define'); 277 Emit('#define '); 278 Expect('('); 279 MacroName := ReadNext; 280 Emit(MacroName); 281 Expect(')'); 282 Emit(' '); 283 Expect('('); 284 while GetNext <> ')' do begin 285 Emit(ReadNext); 286 end; 287 Expect(')'); 288 Emit(';'); 289 end else 290 if GetNext = 'if' then begin 291 Expect('if'); 292 Emit('#if '); 293 Expect('('); 294 ParseIfExpression; 295 Expect(')'); 296 Expect('then'); 297 Expect('('); 298 end else 299 if GetNext = 'set' then begin 300 Expect('set'); 301 Emit('#define '); 302 Expect('('); 303 Emit(ReadNext + ' '); 304 Expect(','); 305 ParseExpression; 306 Expect(')'); 307 Emit(';'); 308 end else 309 if GetNext = '*' then begin 310 Expect('*'); 311 if GetNext = 'define' then begin 312 Expect('define'); 313 Emit('#define '); 314 Expect('('); 315 Emit(ReadNext); 316 Expect('('); 317 Emit('(' + ReadNext); 318 while GetNext = ',' do begin 319 Expect(','); 320 Emit(', ' + ReadNext); 321 end; 322 Expect(')'); 323 Emit(')'); 324 Expect(')'); 325 if GetNext = 'local' then begin 326 Expect('local'); 327 ReadNext; 328 end; 329 Expect('('); 330 end; 331 end else begin 332 Position := OldPos; 333 Result := False; 334 end; 335 end else Result := False; 336 end; 337 end; 338 339 procedure TCompiler.Emit(Text: string); 340 begin 341 Output := Output + Text; 342 end; 343 344 function TCompiler.ParseComment: Boolean; 345 begin 346 with Parser do begin 347 if GetNext = ';' then begin 348 Result := True; 349 Expect(';'); 350 Emit('//'); 351 Emit(ReadEol); 352 end else Result := False; 353 end; 354 end; 355 356 function TCompilerMacro.ParseMacroUsage: Boolean; 357 var 358 MacroName: string; 359 begin 360 with Parser do begin 361 if GetNext = '%' then begin 362 Result := True; 363 Expect('%'); 364 MacroName := ReadNext; 365 Emit(MacroName); 366 if GetNext = '(' then begin 367 Expect('('); 368 Emit('('); 369 ParseExpression; 370 //else Emit(ReadNext); 371 while GetNext = ',' do begin 372 Expect(','); 373 Emit(','); 374 ParseExpression; 375 //if ParseMacroUsage then 376 //else if GetNext <> ')' then Emit(ReadNext); 377 end; 378 Emit(')'); 379 Expect(')'); 380 end; 381 end else Result := False; 382 end; 383 end; 384 385 function TCompiler.ParseKeywords: Boolean; 386 var 387 Value: string; 388 begin 389 with Parser do begin 390 if GetNext = 'extrn' then begin 391 Expect('extrn'); 392 Result := True; 393 ReadNext; 394 Expect('('); 395 ReadNext; 396 while GetNext = ',' do begin 397 Expect(','); 398 ReadNext; 399 end; 400 Expect(')'); 401 end else 402 if GetNext = 'public' then begin 403 Result := True; 404 Expect('public'); 405 ReadNext; 406 while GetNext = ',' do begin 407 Expect(','); 408 ReadNext; 409 end; 410 end else 411 if GetNext = 'using' then begin 412 Result := True; 413 Expect('using'); 414 Value := ReadNext; 415 Emit('// using reg bank ' + Value); 416 end else 417 if GetNext = 'cseg' then begin 418 Result := True; 419 Expect('cseg'); 420 Expect('at'); 421 Value := ReadNext; 422 if GetNext = 'h' then begin 423 Expect('h'); 424 //Emit('0x' + Value); 425 end; // else Emit(Value); 426 end else 427 if GetNext = 'rseg' then begin 428 Result := True; 429 Expect('rseg'); 430 Value := ReadNext; 431 if GetNext = 'h' then begin 432 Expect('h'); 433 //Emit('0x' + Value); 434 end; // else Emit(Value); 435 end else 436 if GetNext = 'dbit' then begin 437 Result := True; 438 Expect('dbit'); 439 Emit('char '); 440 Emit(ReadNext); 441 end else 442 if GetNext = 'db' then begin 443 Result := True; 444 Expect('db'); 445 Emit('char '); 446 ParseDbExpression; 447 while GetNext = ',' do begin 448 Expect(','); 449 Emit(','); 450 ParseDbExpression; 451 end; 452 end else 453 if GetNext = 'dw' then begin 454 Result := True; 455 Expect('dw'); 456 Emit('char '); 457 ParseDbExpression; 458 while GetNext = ',' do begin 459 Expect(','); 460 Emit(','); 461 ParseDbExpression; 462 end; 463 end else 464 if GetNext = 'ds' then begin 465 Result := True; 466 Expect('ds'); 467 Emit('char* '); 468 ParseDbExpression; 469 while GetNext = ',' do begin 470 Expect(','); 471 Emit(','); 472 ParseDbExpression; 473 end; 474 end else Result := False; 475 end; 476 end; 477 478 function TCompiler.ParseInstruction: Boolean; 479 begin 480 with Parser do begin 481 if GetNext = 'jmp' then begin 482 Expect('jmp'); 483 Result := True; 484 Emit('goto '); 485 Emit(ReadNext); 486 Emit(';'); 487 end else 488 if GetNext = 'mov' then begin 489 Expect('mov'); 490 Result := True; 491 Emit('MOV('); 492 ParseDbExpression; 493 Expect(','); 494 Emit(','); 495 ParseDbExpression; 496 Emit(');'); 497 end else 498 if GetNext = 'reti' then begin 499 Result := True; 500 Expect('reti'); 501 end else Result := False; 502 end; 503 end; 504 505 procedure TCompilerMAcro.ParseIfExpression; 506 begin 507 with Parser do begin 508 if ParseMacroUsage then 509 else if IsNumber(GetNext) then Emit(ReadNext) 510 else Error('Unknown IF parameter "' + GetNext + '"'); 511 if GetNext <> ')' then begin 512 if GetNext = 'eq' then begin 513 Expect('eq'); 514 Emit('= '); 515 end else 516 if GetNext = 'ne' then begin 517 Expect('ne'); 518 Emit('!= '); 519 end else 520 if GetNext = 'gt' then begin 521 Expect('gt'); 522 Emit('> '); 523 end else 524 if GetNext = 'ge' then begin 525 Expect('ge'); 526 Emit('>= '); 527 end else 528 if GetNext = 'lt' then begin 529 Expect('lt'); 530 Emit('< '); 531 end else 532 if GetNext = 'le' then begin 533 Expect('le'); 534 Emit('<= '); 535 end else 536 if GetNext = 'or' then begin 537 Expect('or'); 538 Emit('|| '); 539 end else 540 if GetNext = 'and' then begin 541 Expect('and'); 542 Emit('&& '); 543 end else 544 Error('Unknown operand "' + ReadNext + '"'); 545 if ParseMacroUsage then 546 else if IsNumber(GetNext) then Emit(ReadNext) 547 else Error('Unknown IF parameter "' + GetNext + '"'); 548 end; 549 end; 550 end; 551 552 procedure TCompiler.ParseDbExpression; 553 begin 554 with Parser do begin 555 556 if GetNext = '$' then begin 557 Expect('$'); 558 Emit('$'); 559 end 560 else if GetNext = '(' then begin 561 Expect('('); 562 Emit('('); 563 ParseDbExpression; 564 Expect(')'); 565 Emit(')'); 566 end 567 else if GetNext = '''' then Emit('"' + ParseString + '"') 568 else if CheckInstructionParameter then 569 else if IsIdentificator(GetNext) then begin 570 Emit(ReadNext); 571 end else Error('Unknown IF parameter "' + GetNext + '"'); 572 573 if GetNext = '+' then begin 574 Expect('+'); 575 Emit('+'); 576 ParseDbExpression; 577 end else 578 if GetNext = '-' then begin 579 Expect('-'); 580 Emit('-'); 581 ParseDbExpression; 582 end else 583 if GetNext = '*' then begin 584 Expect('*'); 585 Emit('*'); 586 ParseDbExpression; 587 end else 588 if GetNext = '/' then begin 589 Expect('/'); 590 Emit('/'); 591 ParseDbExpression; 592 end else 593 if GetNext = 'shl' then begin 594 Expect('shl'); 595 Emit('<< '); 596 ParseDbExpression; 597 end else 598 if GetNext = 'shr' then begin 599 Expect('shr'); 600 Emit('>> '); 601 ParseDbExpression; 602 end else 603 if GetNext = 'or' then begin 604 Expect('or'); 605 Emit('|| '); 606 ParseDbExpression; 607 end else 608 if GetNext = 'and' then begin 609 Expect('and'); 610 Emit('&& '); 611 ParseDbExpression; 612 end; 613 end; 614 end; 615 616 function TCompiler.ParseString: string; 617 begin 618 with Parser do begin 619 Result := ''; 620 Expect(''''); 621 while GetNext <> '''' do begin 622 Result := Result + ReadNext; 623 end; 624 Expect(''''); 625 end; 626 end; 627 628 function TCompiler.IsHexNumber(Text: string): Boolean; 629 begin 630 Result := (LowerCase(Copy(Text, Length(Text), 1)) = 'h') and 631 (Parser.IsHexNumber(Copy(Text, 1, Length(Text) - 1))); 632 end; 633 634 function TCompiler.CheckInstructionParameter: Boolean; 635 var 636 Text: string; 637 begin 638 Text := Parser.GetNext; 639 Result := False; 640 if Parser.IsNumber(Text) then Result := True 641 else if Text = '#' then begin 642 Result := True; 643 Parser.Expect('#'); 644 if IsHexNumber(Parser.GetNext) then Emit(Parser.ReadNext) 645 else if Parser.IsNumber(Parser.GetNext) then Emit(Parser.ReadNext) 646 else Parser.Error('Invalid parameter'); 647 end 648 else if Copy(Text, 1, 1) = '@' then begin 649 if IsHexNumber(Copy(Text, 2, Length(Text))) then Result := True; 650 Emit(Parser.ReadNext); 651 end; 652 end; 653 654 function TCompiler.CheckIdentificator: Boolean; 655 var 656 Variable: string; 657 begin 658 with Parser do begin 659 Variable := ReadNext; 660 if GetNext = '%' then begin 661 Expect('%'); 662 Variable := Variable + '##' + ReadNext; 663 end; 664 if GetNext = ':' then begin 665 Expect(':'); 666 Emit(Variable + ':'); 667 end else 668 if GetNext = 'equ' then begin 669 Expect('equ'); 670 Emit('#define ' + Variable + ' '); 671 ParseDbExpression; 672 Emit(';'); 673 end else 674 if GetNext = 'segment' then begin 675 Expect('segment'); 676 ReadNext; 677 end else Error('Unexpected token "' + Variable + '"'); 678 end; 679 end; 680 681 17 682 end. 18 683 -
branches/AS8051toC/UMainForm.lfm
r25 r26 1 1 object FormMain: TFormMain 2 Left = 2 262 Left = 215 3 3 Height = 553 4 Top = 1104 Top = 99 5 5 Width = 702 6 6 Caption = 'AS8051 to C converter' -
branches/AS8051toC/UMainForm.pas
r25 r26 8 8 Classes, SysUtils, FileUtil, SynHighlighterCpp, SynEdit, SynMemo, 9 9 SynHighlighterAny, SynHighlighterPas, Forms, Controls, Graphics, Dialogs, 10 StdCtrls, UParser ;10 StdCtrls, UParser, UCompiler; 11 11 12 12 type … … 27 27 procedure FormShow(Sender: TObject); 28 28 private 29 Parser: TParserASM51;30 29 public 31 30 MainFile: TStringList; 32 31 MainFileOut: TStringList; 33 Output: string;34 32 Macros: TStringList; 35 function IsHexNumber(Text: string): Boolean; 36 function CheckInstructionParameter: Boolean; 37 procedure Emit(Text: string); 33 Compiler: TCompiler; 34 CompilerMacro: TCompilerMacro; 38 35 procedure Error(Text: string); 39 procedure Parse;40 procedure ParseLine;41 function ParseMacro: Boolean;42 function ParseComment: Boolean;43 function ParseMacroUsage: Boolean;44 function ParseKeywords: Boolean;45 function ParseInstruction: Boolean;46 procedure ParseIfExpression;47 procedure ParseDbExpression;48 function ParseString: string;49 36 end; 50 37 … … 71 58 begin 72 59 MainFile.LoadFromFile(EditPath.Text); 73 Parser.Source := LowerCase(MainFile.Text); 74 Parser.Position := 1; 75 Output := ''; 76 Parse; 77 MainFileOut.Text := Output; 60 MainFileOut.Text := CompilerMacro.Compile(MainFile.Text); 61 MainFileOut.Text := Compiler.Compile(MainFileOut.Text); 78 62 ForceDirectoriesUTF8(ExtractFileDir(EditPath.Text) + DirectorySeparator + 'C'); 79 63 MainFileOut.SaveToFile(ExtractFileDir(EditPath.Text) + DirectorySeparator + 'C' + … … 94 78 procedure TFormMain.FormCreate(Sender: TObject); 95 79 begin 96 Parser := TParserASM51.Create;97 Parser.OnError := Error;98 80 MainFile := TStringList.Create; 99 81 MainFileOut := TStringList.Create; 100 Macros := TStringList.Create; 82 CompilerMacro := TCompilerMacro.Create; 83 CompilerMacro.Parser.OnError := Error; 84 Compiler := TCompiler.Create; 85 Compiler.Parser.OnError := Error; 101 86 end; 102 87 103 88 procedure TFormMain.FormDestroy(Sender: TObject); 104 89 begin 105 Macros.Free;106 MainFileOut.Free;107 MainFile.Free;108 Parser.Free;90 FreeAndNil(MainFile); 91 FreeAndNil(MainFileOut); 92 FreeAndNil(CompilerMacro); 93 FreeAndNil(Compiler); 109 94 end; 110 95 … … 115 100 end; 116 101 117 function TFormMain.IsHexNumber(Text: string): Boolean;102 procedure TFormMain.Error(Text: string); 118 103 begin 119 Result := (LowerCase(Copy(Text, Length(Text), 1)) = 'h') and120 (Parser.IsHexNumber(Copy(Text, 1, Length(Text) - 1)));104 Compiler.Emit('Error: ' + Text + LineEnding); 105 Compiler.Parser.Position := Length(Compiler.Parser.Source) + 1; 121 106 end; 122 107 123 function TFormMain.CheckInstructionParameter: Boolean;124 begin125 Text := Parser.GetNext;126 Result := False;127 if Parser.IsNumber(Text) then Result := True128 else if Text = '#' then begin129 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 end135 else if Copy(Text, 1, 1) = '@' then begin136 if IsHexNumber(Copy(Text, 2, Length(Text))) then Result := True;137 Emit(Parser.ReadNext);138 end;139 end;140 108 141 procedure TFormMain.Emit(Text: string);142 begin143 Output := Output + Text;144 end;145 146 procedure TFormMain.Error(Text: string);147 begin148 Emit('Error: ' + Text + LineEnding);149 Parser.Position := Length(Parser.Source) + 1;150 end;151 152 procedure TFormMain.Parse;153 var154 I: Integer;155 Line: string;156 Part: string;157 Comment: string;158 OutLine: string;159 begin160 with MainFileOut do begin161 while not Parser.Eof do begin162 ParseLine;163 if Parser.GetNext = 'end' then Break;164 end;165 end;166 end;167 168 procedure TFormMain.ParseLine;169 var170 Part: string;171 OutLine: string;172 Instruction: string;173 Macro: string;174 Variable: string;175 begin176 with Parser do begin177 if GetNext = '$' then begin178 Expect('$');179 if GetNext = 'include' then begin180 Expect('include');181 Expect('(');182 Emit('#include ''');183 Emit(ReadNext);184 Expect('.');185 Emit('.');186 Emit(ReadNext);187 Emit('''');188 Expect(')');189 end else Error('Unknown token "' + GetNext + '"');190 end191 else if GetNext = ')' then begin192 Expect(')');193 if GetNext = 'else' then begin194 Expect('else');195 Emit('#else ');196 Expect('(');197 end else198 if GetNext = 'fi' then begin199 Expect('fi');200 Emit('#endif ');201 end; // else Error('Unknown token "' + GetNext + '"');202 end203 else if ParseComment then204 else if ParseKeywords then205 else if ParseInstruction then206 else if IsIdentificator(GetNext) then begin207 Variable := ReadNext;208 if GetNext = '%' then begin209 Expect('%');210 Variable := Variable + '##' + ReadNext;211 end;212 if GetNext = ':' then begin213 Expect(':');214 Emit(Variable + ':');215 end else216 if GetNext = 'equ' then begin217 Expect('equ');218 Emit('#define ' + Variable + ' ');219 ParseDbExpression;220 Emit(';');221 end else222 if GetNext = 'segment' then begin223 Expect('segment');224 ReadNext;225 end else Error('Unexpected token "' + Variable + '"');226 end227 else if ParseMacro then228 else if ParseMacroUsage then229 else Error('Unexpected token "' + GetNext + '"');230 Emit(LineEnding);231 end;232 end;233 234 function TFormMain.ParseMacro: Boolean;235 var236 MacroName: string;237 OldPos: Integer;238 begin239 with Parser do begin240 OldPos := Position;241 if GetNext = '%' then begin242 Result := True;243 Expect('%');244 if GetNext = 'define' then begin245 Expect('define');246 Emit('#define ');247 Expect('(');248 MacroName := ReadNext;249 Emit(MacroName);250 Expect(')');251 Emit(' ');252 Expect('(');253 while GetNext <> ')' do begin254 Emit(ReadNext);255 end;256 Expect(')');257 Emit(';');258 end else259 if GetNext = 'if' then begin260 Expect('if');261 Emit('#if ');262 Expect('(');263 ParseIfExpression;264 Expect(')');265 Expect('then');266 Expect('(');267 end else268 if GetNext = 'set' then begin269 Expect('set');270 Emit('#define ');271 Expect('(');272 Emit(ReadNext + ' ');273 Expect(',');274 ParseDbExpression;275 Expect(')');276 Emit(';');277 end else278 if GetNext = '*' then begin279 Expect('*');280 if GetNext = 'define' then begin281 Expect('define');282 Emit('#define ');283 Expect('(');284 Emit(ReadNext);285 Expect('(');286 Emit('(' + ReadNext);287 while GetNext = ',' do begin288 Expect(',');289 Emit(', ' + ReadNext);290 end;291 Expect(')');292 Emit(')');293 Expect(')');294 if GetNext = 'local' then begin295 Expect('local');296 ReadNext;297 end;298 Expect('(');299 end;300 end else begin301 Position := OldPos;302 Result := False;303 end;304 end else Result := False;305 end;306 end;307 308 function TFormMain.ParseComment: Boolean;309 begin310 with Parser do begin311 if GetNext = ';' then begin312 Result := True;313 Expect(';');314 Emit('//');315 Emit(ReadEol);316 end else Result := False;317 end;318 end;319 320 function TFormMain.ParseMacroUsage: Boolean;321 var322 MacroName: string;323 begin324 with Parser do begin325 if GetNext = '%' then begin326 Result := True;327 Expect('%');328 MacroName := ReadNext;329 Emit(MacroName);330 if GetNext = '(' then begin331 Expect('(');332 Emit('(');333 ParseDbExpression;334 //else Emit(ReadNext);335 while GetNext = ',' do begin336 Expect(',');337 Emit(',');338 ParseDbExpression;339 //if ParseMacroUsage then340 //else if GetNext <> ')' then Emit(ReadNext);341 end;342 Emit(')');343 Expect(')');344 end;345 end else Result := False;346 end;347 end;348 349 function TFormMain.ParseKeywords: Boolean;350 var351 Value: string;352 begin353 with Parser do begin354 if GetNext = 'extrn' then begin355 Expect('extrn');356 Result := True;357 ReadNext;358 Expect('(');359 ReadNext;360 while GetNext = ',' do begin361 Expect(',');362 ReadNext;363 end;364 Expect(')');365 end else366 if GetNext = 'public' then begin367 Result := True;368 Expect('public');369 ReadNext;370 while GetNext = ',' do begin371 Expect(',');372 ReadNext;373 end;374 end else375 if GetNext = 'using' then begin376 Result := True;377 Expect('using');378 Value := ReadNext;379 Emit('// using reg bank ' + Value);380 end else381 if GetNext = 'cseg' then begin382 Result := True;383 Expect('cseg');384 Expect('at');385 Value := ReadNext;386 if GetNext = 'h' then begin387 Expect('h');388 //Emit('0x' + Value);389 end; // else Emit(Value);390 end else391 if GetNext = 'rseg' then begin392 Result := True;393 Expect('rseg');394 Value := ReadNext;395 if GetNext = 'h' then begin396 Expect('h');397 //Emit('0x' + Value);398 end; // else Emit(Value);399 end else400 if GetNext = 'dbit' then begin401 Result := True;402 Expect('dbit');403 Emit('char ');404 Emit(ReadNext);405 end else406 if GetNext = 'db' then begin407 Result := True;408 Expect('db');409 Emit('char ');410 ParseDbExpression;411 while GetNext = ',' do begin412 Expect(',');413 Emit(',');414 ParseDbExpression;415 end;416 end else417 if GetNext = 'dw' then begin418 Result := True;419 Expect('dw');420 Emit('char ');421 ParseDbExpression;422 while GetNext = ',' do begin423 Expect(',');424 Emit(',');425 ParseDbExpression;426 end;427 end else428 if GetNext = 'ds' then begin429 Result := True;430 Expect('ds');431 Emit('char* ');432 ParseDbExpression;433 while GetNext = ',' do begin434 Expect(',');435 Emit(',');436 ParseDbExpression;437 end;438 end else Result := False;439 end;440 end;441 442 function TFormMain.ParseInstruction: Boolean;443 begin444 with Parser do begin445 if GetNext = 'jmp' then begin446 Expect('jmp');447 Result := True;448 Emit('goto ');449 Emit(ReadNext);450 Emit(';');451 end else452 if GetNext = 'mov' then begin453 Expect('mov');454 Result := True;455 Emit('MOV(');456 ParseDbExpression;457 Expect(',');458 Emit(',');459 ParseDbExpression;460 Emit(');');461 end else462 if GetNext = 'reti' then begin463 Result := True;464 Expect('reti');465 end else Result := False;466 end;467 end;468 469 procedure TFormMain.ParseIfExpression;470 begin471 with Parser do begin472 if ParseMacroUsage then473 else if IsNumber(GetNext) then Emit(ReadNext)474 else Error('Unknown IF parameter "' + GetNext + '"');475 if GetNext <> ')' then begin476 if GetNext = 'eq' then begin477 Expect('eq');478 Emit('= ');479 end else480 if GetNext = 'ne' then begin481 Expect('ne');482 Emit('!= ');483 end else484 if GetNext = 'gt' then begin485 Expect('gt');486 Emit('> ');487 end else488 if GetNext = 'ge' then begin489 Expect('ge');490 Emit('>= ');491 end else492 if GetNext = 'lt' then begin493 Expect('lt');494 Emit('< ');495 end else496 if GetNext = 'le' then begin497 Expect('le');498 Emit('<= ');499 end else500 if GetNext = 'or' then begin501 Expect('or');502 Emit('|| ');503 end else504 if GetNext = 'and' then begin505 Expect('and');506 Emit('&& ');507 end else508 Error('Unknown operand "' + ReadNext + '"');509 if ParseMacroUsage then510 else if IsNumber(GetNext) then Emit(ReadNext)511 else Error('Unknown IF parameter "' + GetNext + '"');512 end;513 end;514 end;515 516 procedure TFormMain.ParseDbExpression;517 begin518 with Parser do begin519 520 if ParseMacroUsage then521 else if GetNext = '$' then begin522 Expect('$');523 Emit('$');524 end525 else if GetNext = '(' then begin526 Expect('(');527 Emit('(');528 ParseDbExpression;529 Expect(')');530 Emit(')');531 end532 else if GetNext = '''' then Emit('"' + ParseString + '"')533 else if CheckInstructionParameter then534 else if IsIdentificator(GetNext) then begin535 Emit(ReadNext);536 if ParseMacroUsage then ;537 end else Error('Unknown IF parameter "' + GetNext + '"');538 539 if GetNext = '+' then begin540 Expect('+');541 Emit('+');542 ParseDbExpression;543 end else544 if GetNext = '-' then begin545 Expect('-');546 Emit('-');547 ParseDbExpression;548 end else549 if GetNext = '*' then begin550 Expect('*');551 Emit('*');552 ParseDbExpression;553 end else554 if GetNext = '/' then begin555 Expect('/');556 Emit('/');557 ParseDbExpression;558 end else559 if GetNext = 'shl' then begin560 Expect('shl');561 Emit('<< ');562 ParseDbExpression;563 end else564 if GetNext = 'shr' then begin565 Expect('shr');566 Emit('>> ');567 ParseDbExpression;568 end else569 if GetNext = 'or' then begin570 Expect('or');571 Emit('|| ');572 ParseDbExpression;573 end else574 if GetNext = 'and' then begin575 Expect('and');576 Emit('&& ');577 ParseDbExpression;578 end;579 end;580 end;581 582 function TFormMain.ParseString: string;583 begin584 with Parser do begin585 Result := '';586 Expect('''');587 while GetNext <> '''' do begin588 Result := Result + ReadNext;589 end;590 Expect('''');591 end;592 end;593 109 594 110 end. -
branches/AS8051toC/USource.pas
r25 r26 30 30 end; 31 31 32 TAsmSource = class 33 Labels: TListObject; 34 // Items: 35 end; 36 32 37 33 38 implementation
Note:
See TracChangeset
for help on using the changeset viewer.