Changeset 19 for trunk/UBrainFuck.pas


Ignore:
Timestamp:
Feb 11, 2012, 10:18:19 PM (12 years ago)
Author:
chronos
Message:
  • Modified: Compiler moved to separate unit file.
  • Added: Compiler to PHP.
  • Added: Loading list of available compilers and list them for selection in menu Program - Target.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/UBrainFuck.pas

    r18 r19  
    1111  TBrainFuckInterpretter = class;
    1212
    13   TCompilerTarget = (ctDelphi);
    14   TCompilerOptimization = (coNone, coNormal);
    15 
    16   { TBrainFuckCompiler }
    17 
    18   TBrainFuckCompiler = class
    19   private
    20     Indent: Integer;
    21     procedure AddLine(Text: string);
    22   public
    23     ProgramName: string;
    24     Source: string;
    25     Output: string;
    26     Target: TCompilerTarget;
    27     Optimization: TCompilerOptimization;
    28     procedure OptimizeSource;
    29     procedure Compile;
    30   end;
    3113
    3214  TRunState = (rsStopped, rsPaused, rsRunning);
     
    321303end;
    322304
    323 { TBrainFuckCompiler }
    324 
    325 procedure TBrainFuckCompiler.AddLine(Text: string);
    326 begin
    327   Output := Output + DupeString('  ', Indent) + Text + LineEnding;
    328 end;
    329 
    330 procedure TBrainFuckCompiler.OptimizeSource;
    331 begin
    332   // Remove redundand code
    333 
    334 end;
    335 
    336 procedure TBrainFuckCompiler.Compile;
    337 var
    338   I: Integer;
    339 begin
    340   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 begin
    353     case Source[I] of
    354       '>': 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       '[': begin
    361         AddLine('while Memory[Position] <> #0 do begin');
    362         Inc(Indent);
    363       end;
    364       ']': begin
    365         Dec(Indent);
    366         AddLine('end;');
    367       end;
    368     end;
    369   end;
    370   Dec(Indent);
    371   AddLine('end.');
    372 end;
    373 
    374305end.
    375306
Note: See TracChangeset for help on using the changeset viewer.