-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbf.pas
262 lines (222 loc) · 5.23 KB
/
bf.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
{*
===================================================================================
Brainfuck v1.01 (build 172)
Pascal version developed by ajack (aka Adrian Chiang) on 30-Jul-2005.
Questions or comments, please e-mail me at: ajack2001my [at] yahoo.com
- Array is 30001 bytes in size.
- Size of array is 1 byte.
- Nested loops can be 65535 generations into the loop.
- Code size of a brainfuck program is 4MBsin size.
Source code tested successfully with:
Virtual Pascal v2.1 (build 279)
Free Pascal 2.0.0
Based on the brainfuck documentation found at:
http://cydathria.com
Brainfuck is a turing-complete programming language developed by
Urban Mueller:
http://wuarchive.wustl.edu/~umueller/ (DOWN)
Difference from standard brainfuck implementations:
- Added the '#' (debug) command. Will stop program and show contents
of a[0..9] of array. Must add command line parameter '-debug'
to work.
v1.01 - Change the BF_LoadProg from using ReadLn to BlockRead as the
previous version could not load programs that are longer than
255 characters on a single text line.
===================================================================================
LEGALESSE
This source code is public domain. The coder is not liable for anything
whatsoever. The only guarantee it has is that it will take up storage space in
your computer. Oh! It would be nice if you gave me credit if you use this source
code (in whole or in part).
===================================================================================
*}
PROGRAM Brainfuck;
USES
SysUtils, {/* Use this library for the FileExists command */}
CRT;
CONST
ASize = 30000; {* Brainfuck array size *}
LSize = 65535; {* Loop command '[', ']' nested loop depth *}
CSize = 1048576 * 4; {* Code size is 4MB *}
ProgVer = '1.01';
ProgBuild = '172';
VAR
Debug : Boolean;
A : Array [0..ASize] of Byte;
LP,
P : Word;
L : Array [0..LSize] of LongInt;
C : Array [0..CSize] of Char;
CEnd : LongInt;
PROCEDURE PushLoop (CP: LongInt);
BEGIN
L[LP] := CP;
Inc (LP);
END;
PROCEDURE PopLoop (VAR CP: LongInt);
BEGIN
Dec (LP);
CP := L[LP];
END;
PROCEDURE BF_Init;
VAR
I : LongInt;
BEGIN
FOR I := 0 TO ASize DO
A[I] := 0;
LP := 0;
END;
PROCEDURE BF_LoadProg;
CONST
BLen = 8192;
VAR
FN : String;
T : File;
AR,
I : LongInt;
B : Array [1..BLen] of Char;
BEGIN
CEnd := 0;
FN := UpperCase(ParamStr(2));
IF FN = '-DEBUG' THEN
Debug := True
ELSE
Debug := False;
FN := ParamStr(1);
IF NOT FileExists (FN) THEN
BEGIN
WriteLn ('Usage: BF <filename> [-debug]');
Halt;
END;
Assign (T, FN);
Reset (T, 1);
BlockRead (T, B, BLen, AR);
WHILE AR > 0 DO
BEGIN
FOR I := 1 TO AR DO
BEGIN
IF B[I] IN ['<', '>', '+', '-', '.', ',', '[', ']', '#'] THEN
BEGIN
C[CEnd] := B[I];
Inc (CEnd);
END;
END;
BlockRead (T, B, BLen, AR);
END;
Close (T);
WriteLn ('Program code size is ', CEnd, ' bytes.');
WriteLn;
END;
PROCEDURE BF_Runtime;
VAR
I,
Null,
CWend,
CNow : LongInt;
PROCEDURE _Print (B: Byte);
BEGIN
IF B = 10 THEN
WriteLn
ELSE
Write (Char(B));
END;
PROCEDURE _GetKey (VAR B: Byte);
BEGIN
B := Ord(ReadKey);
Write (Char(B));
END;
PROCEDURE _LoopStart;
VAR
Done : Boolean;
BEGIN
IF A[P] > 0 THEN
PushLoop (CNow)
ELSE
BEGIN
CWend := 0;
Done := False;
I := CNow + 1;
WHILE NOT Done DO
BEGIN
CASE C[I] OF
'[' : Inc (CWend);
']' : BEGIN
Dec (CWend);
IF CWend < 0 THEN
BEGIN
CNow := I;
Done := True;
END;
END;
END;
Inc (I);
END;
END;
END;
PROCEDURE _LoopEnd;
BEGIN
IF A[P] > 0 THEN
BEGIN
PopLoop (CNow);
PushLoop (CNow);
END
ELSE
PopLoop (Null);
END;
FUNCTION Filler (V, L: LongInt): String;
VAR
S : String;
BEGIN
Str (V, S);
WHILE Length (S) < L DO
S := '0' + S;
Filler := S;
END;
PROCEDURE _Debug;
VAR
I : Byte;
BEGIN
WriteLn ('P=', Filler(P, 5), ' IP=', Filler(CNow, 7));
WriteLn;
FOR I := 0 TO 4 DO
Write ('A[', I, ']=', Filler(A[I], 3), ' ');
WriteLn;
FOR I := 5 TO 9 DO
Write ('A[', I, ']=', Filler(A[I], 3), ' ');
WriteLn;
Halt;
END;
BEGIN
CNow := 0;
WHILE CNow <= CEnd DO
BEGIN
CASE C[CNow] OF
'>' : Inc (P);
'<' : Dec (P);
'+' : Inc (A[P]);
'-' : Dec (A[P]);
'.' : _Print (A[P]);
',' : _GetKey (A[P]);
'[' : _LoopStart;
']' : _LoopEnd;
'#' : IF Debug THEN _Debug;
END;
Inc (CNow);
END;
END;
PROCEDURE BF_DeInit;
BEGIN
END;
PROCEDURE BF_Hello;
BEGIN
WriteLn ('BF v', ProgVer, ' (Build ', ProgBuild, ') - Brainfuck interpreter. Created by Adrian Chiang.');
WriteLn ('(c) Copyright Renegade Demo Group, 2005-2007. All Rights Reserved.');
WriteLn;
END;
BEGIN
BF_Hello;
BF_Init;
BF_LoadProg;
BF_Runtime;
BF_DeInit;
END.