Changeset 52 for trunk/Target/UTarget.pas
- Timestamp:
- Jul 26, 2012, 3:11:08 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Target/UTarget.pas
r50 r52 66 66 end; 67 67 68 TBrainFuckCommand = (cmNoOperation, cmInc, cmDec, cmPointerInc, cmPointerDec, 69 cmOutput, cmInput, cmLoopStart, cmLoopEnd, cmDebug); 70 68 71 { TTarget } 69 72 70 73 TTarget = class 71 pr ivate74 protected 72 75 FCompiled: Boolean; 76 function SourceReadNext: Char; 77 function IsOpcode(Opcode: Char): Boolean; 78 function CheckClear: Boolean; 79 function CheckOccurence(C: TBrainFuckCommand): Integer; 73 80 protected 74 81 FSourceCode: string; 82 FProgram: array of TBrainFuckCommand; 83 FProgramIndex: Integer; 75 84 FTargetCode: string; 85 FTargetIndex: Integer; 76 86 Indent: Integer; 77 87 FState: TRunState; 78 88 FOnChangeState: TNotifyEvent; 89 procedure LoadProgram; 79 90 procedure SetSourceCode(AValue: string); virtual; 80 91 function GetTargetCode: string; virtual; … … 119 130 property Compiled: Boolean read FCompiled write FCompiled; 120 131 property ExecutionPosition: Integer read GetExecutionPosition; 132 property ProgramIndex: Integer read FProgramIndex; 121 133 end; 122 134 … … 377 389 procedure TTarget.Compile; 378 390 begin 391 LoadProgram; 379 392 Compiled := True; 380 393 end; … … 489 502 end; 490 503 504 function TTarget.CheckOccurence(C: TBrainFuckCommand): Integer; 505 begin 506 Result := 1; 507 if Optimization = coNormal then 508 while ((FProgramIndex + 1) <= Length(FProgram)) and (FProgram[FProgramIndex + 1] = C) do begin 509 Inc(Result); 510 Inc(FProgramIndex); 511 end; 512 end; 513 514 procedure TTarget.LoadProgram; 515 var 516 I: Integer; 517 begin 518 inherited; 519 DebugSteps.Clear; 520 SetLength(FProgram, Length(FSourceCode)); 521 FProgramIndex := 0; 522 for I := 1 to Length(FSourceCode) do begin 523 case FSourceCode[I] of 524 '+': begin 525 FProgram[FProgramIndex] := cmInc; 526 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 527 end; 528 '-': begin 529 FProgram[FProgramIndex] := cmDec; 530 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 531 end; 532 '>': begin 533 FProgram[FProgramIndex] := cmPointerInc; 534 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 535 end; 536 '<': begin 537 FProgram[FProgramIndex] := cmPointerDec; 538 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 539 end; 540 ',': begin 541 FProgram[FProgramIndex] := cmInput; 542 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 543 end; 544 '.': begin 545 FProgram[FProgramIndex] := cmOutput; 546 DebugSteps.AddStep(I - 1, FProgramIndex, soNormal); 547 end; 548 '[': begin 549 FProgram[FProgramIndex] := cmLoopStart; 550 DebugSteps.AddStep(I - 1, FProgramIndex, soStepIn); 551 end; 552 ']': begin 553 FProgram[FProgramIndex] := cmLoopEnd; 554 DebugSteps.AddStep(I - 1, FProgramIndex, soStepOut); 555 end 556 else Dec(FProgramIndex); 557 end; 558 Inc(FProgramIndex); 559 end; 560 SetLength(FProgram, FProgramIndex); 561 end; 562 563 function TTarget.SourceReadNext: Char; 564 begin 565 // while FProgramIndex; 566 end; 567 568 function TTarget.IsOpcode(Opcode: Char): Boolean; 569 begin 570 Result := (Opcode = '+') or (Opcode = '-') or (Opcode = '<') or (Opcode = '>') or 571 (Opcode = '[') or (Opcode = ']') or (Opcode = ',') or (Opcode = '.'); 572 end; 573 574 function TTarget.CheckClear: Boolean; 575 begin 576 Result := (FProgram[FProgramIndex] = cmLoopStart) and (Length(FProgram) >= FProgramIndex + 2) and 577 (FProgram[FProgramIndex + 1] = cmDec) and (FProgram[FProgramIndex + 2] = cmLoopEnd); 578 end; 579 491 580 end. 492 581
Note:
See TracChangeset
for help on using the changeset viewer.