source: tags/1.3.0/BFCodeTools.pas

Last change on this file was 151, checked in by chronos, 5 months ago
  • Added: Execute compiled program with targets without executor inside console window to see its output.
File size: 2.6 KB
Line 
1unit BFCodeTools;
2
3interface
4
5uses
6 Classes, SysUtils, StrUtils, Math;
7
8function GenerateNumber(Number: Integer): string;
9function GenerateNumberSimple(Number: Integer): string;
10function GenerateText(Text: string): string;
11function FormatCode(Text: string): string;
12function ShrinkCode(Text: string): string;
13
14
15implementation
16
17function GenerateNumber(Number: Integer): string;
18var
19 Base: Integer;
20 Multiplier: Integer;
21 Remainder: Integer;
22begin
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);
35end;
36
37function GenerateNumberSimple(Number: Integer): string;
38begin
39 Result := DupeString('+', Number);
40end;
41
42function GenerateText(Text: string): string;
43var
44 I: Integer;
45 MinValue: Integer;
46begin
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;
63end;
64
65function FormatCode(Text: string): string;
66var
67 IndentLevel: Integer;
68 I: Integer;
69const
70 IndentText = ' ';
71begin
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;
89end;
90
91// Remove all non-printable characters
92function ShrinkCode(Text: string): string;
93var
94 Pos: Integer;
95 I: Integer;
96begin
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;
106end;
107
108end.
109
Note: See TracBrowser for help on using the repository browser.