1 | unit BFCodeTools;
|
---|
2 |
|
---|
3 | interface
|
---|
4 |
|
---|
5 | uses
|
---|
6 | Classes, SysUtils, StrUtils, Math;
|
---|
7 |
|
---|
8 | function GenerateNumber(Number: Integer): string;
|
---|
9 | function GenerateNumberSimple(Number: Integer): string;
|
---|
10 | function GenerateText(Text: string): string;
|
---|
11 | function FormatCode(Text: string): string;
|
---|
12 | function ShrinkCode(Text: string): string;
|
---|
13 |
|
---|
14 |
|
---|
15 | implementation
|
---|
16 |
|
---|
17 | function GenerateNumber(Number: Integer): string;
|
---|
18 | var
|
---|
19 | Base: Integer;
|
---|
20 | Multiplier: Integer;
|
---|
21 | Remainder: Integer;
|
---|
22 | begin
|
---|
23 | Result := '';
|
---|
24 | Base := Trunc(Sqrt(Number));
|
---|
25 | Remainder := Number;
|
---|
26 | if Base > 1 then begin
|
---|
27 | Multiplier := Trunc(Remainder / Base);
|
---|
28 | Result := Result + '>' + GenerateNumberSimple(Base) + '[<' +
|
---|
29 | GenerateNumberSimple(Multiplier) + '>-]<';
|
---|
30 | Remainder := Remainder - Base * Multiplier;
|
---|
31 | end;
|
---|
32 | Result := Result + GenerateNumberSimple(Remainder);
|
---|
33 | if Length(Result) > Number then
|
---|
34 | Result := GenerateNumberSimple(Number);
|
---|
35 | end;
|
---|
36 |
|
---|
37 | function GenerateNumberSimple(Number: Integer): string;
|
---|
38 | begin
|
---|
39 | Result := DupeString('+', Number);
|
---|
40 | end;
|
---|
41 |
|
---|
42 | function GenerateText(Text: string): string;
|
---|
43 | var
|
---|
44 | I: Integer;
|
---|
45 | MinValue: Integer;
|
---|
46 | begin
|
---|
47 | Result := '';
|
---|
48 |
|
---|
49 | if Length(Text) > 1 then begin
|
---|
50 | MinValue := MaxInt;
|
---|
51 | for I := 1 to Length(Text) do
|
---|
52 | MinValue := Min(MinValue, Ord(Text[I]));
|
---|
53 |
|
---|
54 | if MinValue > 1 then
|
---|
55 | Result := DupeString('>', Length(Text)) + GenerateNumber(MinValue) +
|
---|
56 | '[' + DupeString('<+', Length(Text)) + DupeString('>', Length(Text)) + '-]';
|
---|
57 | Result := Result + DupeString('<', Length(Text));
|
---|
58 | end;
|
---|
59 |
|
---|
60 | for I := 1 to Length(Text) do begin
|
---|
61 | Result := Result + GenerateNumberSimple(Ord(Text[I]) - MinValue) + '>';
|
---|
62 | end;
|
---|
63 | end;
|
---|
64 |
|
---|
65 | function FormatCode(Text: string): string;
|
---|
66 | var
|
---|
67 | IndentLevel: Integer;
|
---|
68 | I: Integer;
|
---|
69 | const
|
---|
70 | IndentText = ' ';
|
---|
71 | begin
|
---|
72 | Result := '';
|
---|
73 | IndentLevel := 0;
|
---|
74 | for I := 1 to Length(Text) do begin
|
---|
75 | if Text[I] = '[' then begin
|
---|
76 | Result := Result + LineEnding + DupeString(IndentText, IndentLevel) +
|
---|
77 | Text[I] + LineEnding ;
|
---|
78 | Inc(IndentLevel);
|
---|
79 | Result := Result + DupeString(IndentText, IndentLevel);
|
---|
80 | end
|
---|
81 | else if Text[I] = ']' then begin
|
---|
82 | Dec(IndentLevel);
|
---|
83 | Result := Result + LineEnding + DupeString(IndentText, IndentLevel) +
|
---|
84 | Text[I] + LineEnding + DupeString(IndentText, IndentLevel);
|
---|
85 | end
|
---|
86 | else if Text[I] > ' ' then
|
---|
87 | Result := Result + Text[I];
|
---|
88 | end;
|
---|
89 | end;
|
---|
90 |
|
---|
91 | // Remove all non-printable characters
|
---|
92 | function ShrinkCode(Text: string): string;
|
---|
93 | var
|
---|
94 | Pos: Integer;
|
---|
95 | I: Integer;
|
---|
96 | begin
|
---|
97 | Pos := 1;
|
---|
98 | for I := 1 to Length(Text) do begin
|
---|
99 | if Text[I] > ' ' then begin
|
---|
100 | Text[Pos] := Text[I];
|
---|
101 | Inc(Pos);
|
---|
102 | end;
|
---|
103 | end;
|
---|
104 | SetLength(Text, Pos - 1);
|
---|
105 | Result := Text;
|
---|
106 | end;
|
---|
107 |
|
---|
108 | end.
|
---|
109 |
|
---|