1 | unit UBrainfuckCode;
|
---|
2 |
|
---|
3 | {$mode delphi}{$H+}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | Classes, SysUtils, USource, SpecializedList;
|
---|
9 |
|
---|
10 | type
|
---|
11 | TBrainFuckCommand = (cmNoOperation, cmInc, cmDec, cmPointerInc, cmPointerDec,
|
---|
12 | cmOutput, cmInput, cmLoopStart, cmLoopEnd, cmDebug);
|
---|
13 |
|
---|
14 | { TSourceBrainFuck }
|
---|
15 |
|
---|
16 | TSourceBrainFuck = class(TSource)
|
---|
17 | Code: TListByte;
|
---|
18 | constructor Create;
|
---|
19 | destructor Destroy; override;
|
---|
20 | end;
|
---|
21 |
|
---|
22 | { TConvertorTextToBF }
|
---|
23 |
|
---|
24 | TConvertorTextToBF = class(TConvertor)
|
---|
25 | constructor Create; override;
|
---|
26 | procedure Convert(Input, Output: TSourceList); override;
|
---|
27 | end;
|
---|
28 |
|
---|
29 |
|
---|
30 | implementation
|
---|
31 |
|
---|
32 | uses
|
---|
33 | ULDModuleBasic;
|
---|
34 |
|
---|
35 | { TSourceBrainFuck }
|
---|
36 |
|
---|
37 | constructor TSourceBrainFuck.Create;
|
---|
38 | begin
|
---|
39 | Code := TListByte.Create;
|
---|
40 | end;
|
---|
41 |
|
---|
42 | destructor TSourceBrainFuck.Destroy;
|
---|
43 | begin
|
---|
44 | Code.Free;
|
---|
45 | inherited Destroy;
|
---|
46 | end;
|
---|
47 |
|
---|
48 | { TConvertorTextToBF }
|
---|
49 |
|
---|
50 | constructor TConvertorTextToBF.Create;
|
---|
51 | begin
|
---|
52 | inherited Create;
|
---|
53 | Name := 'TextToBF';
|
---|
54 | InputType := TSourceText;
|
---|
55 | OutputType := TSourceBrainFuck;
|
---|
56 | end;
|
---|
57 |
|
---|
58 | procedure TConvertorTextToBF.Convert(Input, Output: TSourceList);
|
---|
59 | var
|
---|
60 | I: Integer;
|
---|
61 | Code: string;
|
---|
62 | Index: Integer;
|
---|
63 | begin
|
---|
64 | inherited;
|
---|
65 | //DebugSteps.Clear;
|
---|
66 | TSourceBrainFuck(Output).Code.Clear;
|
---|
67 | Index := 0;
|
---|
68 | Code := TSourceText(Input).Content.Text;
|
---|
69 | for I := 1 to Length(Code) do begin
|
---|
70 | case Code[I] of
|
---|
71 | '+': begin
|
---|
72 | TSourceBrainFuck(Output).Code.Add(Byte(cmInc));
|
---|
73 | end;
|
---|
74 | '-': begin
|
---|
75 | TSourceBrainFuck(Output).Code.Add(Byte(cmDec));
|
---|
76 | end;
|
---|
77 | '>': begin
|
---|
78 | TSourceBrainFuck(Output).Code.Add(Byte(cmPointerInc));
|
---|
79 | end;
|
---|
80 | '<': begin
|
---|
81 | TSourceBrainFuck(Output).Code.Add(Byte(cmPointerDec));
|
---|
82 | end;
|
---|
83 | ',': begin
|
---|
84 | TSourceBrainFuck(Output).Code.Add(Byte(cmInput));
|
---|
85 | end;
|
---|
86 | '.': begin
|
---|
87 | TSourceBrainFuck(Output).Code.Add(Byte(cmOutput));
|
---|
88 | end;
|
---|
89 | '[': begin
|
---|
90 | TSourceBrainFuck(Output).Code.Add(Byte(cmLoopStart));
|
---|
91 | end;
|
---|
92 | ']': begin
|
---|
93 | TSourceBrainFuck(Output).Code.Add(Byte(cmLoopEnd));
|
---|
94 | end
|
---|
95 | end;
|
---|
96 | end;
|
---|
97 | end;
|
---|
98 |
|
---|
99 | end.
|
---|
100 |
|
---|