Changeset 19 for trunk/UBrainFuck.pas
- Timestamp:
- Feb 11, 2012, 10:18:19 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/UBrainFuck.pas
r18 r19 11 11 TBrainFuckInterpretter = class; 12 12 13 TCompilerTarget = (ctDelphi);14 TCompilerOptimization = (coNone, coNormal);15 16 { TBrainFuckCompiler }17 18 TBrainFuckCompiler = class19 private20 Indent: Integer;21 procedure AddLine(Text: string);22 public23 ProgramName: string;24 Source: string;25 Output: string;26 Target: TCompilerTarget;27 Optimization: TCompilerOptimization;28 procedure OptimizeSource;29 procedure Compile;30 end;31 13 32 14 TRunState = (rsStopped, rsPaused, rsRunning); … … 321 303 end; 322 304 323 { TBrainFuckCompiler }324 325 procedure TBrainFuckCompiler.AddLine(Text: string);326 begin327 Output := Output + DupeString(' ', Indent) + Text + LineEnding;328 end;329 330 procedure TBrainFuckCompiler.OptimizeSource;331 begin332 // Remove redundand code333 334 end;335 336 procedure TBrainFuckCompiler.Compile;337 var338 I: Integer;339 begin340 Indent := 0;341 Output := '';342 343 AddLine('program ' + ProgramName + ';');344 AddLine('');345 AddLine('{$APPTYPE CONSOLE}');346 AddLine('');347 AddLine('var');348 AddLine(' Memory: array[0..30000] of Char;');349 AddLine(' Position: Integer;');350 AddLine('begin');351 Inc(Indent);352 for I := 1 to Length(Source) do begin353 case Source[I] of354 '>': AddLine('Inc(Position);');355 '<': AddLine('Dec(Position);');356 '+': AddLine('Memory[Position] := Succ(Memory[Position]);');357 '-': AddLine('Memory[Position] := Pred(Memory[Position]);');358 '.': AddLine('Write(Memory[Position]);');359 ',': AddLine('Read(Memory[Position]);');360 '[': begin361 AddLine('while Memory[Position] <> #0 do begin');362 Inc(Indent);363 end;364 ']': begin365 Dec(Indent);366 AddLine('end;');367 end;368 end;369 end;370 Dec(Indent);371 AddLine('end.');372 end;373 374 305 end. 375 306
Note:
See TracChangeset
for help on using the changeset viewer.