git @ Cat's Eye Technologies Befunge-93 / rel_2_24
Merge pull request #12 from catseye/develop-2018-1 Release version 2.24. Chris Pressey authored 3 years ago GitHub committed 3 years ago
9 changed file(s) with 248 addition(s) and 106 deletion(s). Raw diff Collapse all Expand all
0 _UAEFSDB.___
01 bin/bef
12 bin/befprof
23 bin/bef2c
00 syntax: glob
11
2 _UAEFSDB.___
23 bin/bef
34 bin/befprof
45 bin/bef2c
0 # NMAKE Makefile for bef.
1
2 PROGS=bin\bef.exe bin\befprof.exe bin\bef2c.exe
3 CC=cl
4 O=.obj
5 RM_F=del
6
7 WARNS= -Wall -wd4820 -wd4131 -wd4459 -wd4242 -wd4244 -wd4996 \
8 -wd4668 -wd4255 -wd4710 -wd4706 -wd4702 -wd4711 -wd4456 -wd5045
9
10 CFLAGS=$(CFLAGS) -Ox -nologo
11 CFLAGS=$(CFLAGS) $(WARNS) $(EXTRA_CFLAGS)
12
13 !ifdef DEBUG
14 CFLAGS=$(CFLAGS) -Zi
15 !endif
16
17 all: $(PROGS)
18
19 bin\bef.exe: src\bef.c
20 $(CC) $(CFLAGS) src\bef.c -Fe:bin\bef
21
22 bin\befprof.exe: src\befprof.c
23 $(CC) $(CFLAGS) src\befprof.c -Fe:bin\befprof
24
25 bin\bef2c.exe: src\bef2c.c
26 $(CC) $(CFLAGS) src\bef2c.c -Fe:bin\bef2c
27
28 clean:
29 $(RM_F) *$(O) src\*$(O)
30
31 distclean:
32 $(RM_F) $(PROGS)
0 Welcome to the NEW Befunge-93 Reference Distribution
1 ====================================================
0 Welcome to the Befunge-93 Reference Distribution
1 ================================================
22
3 Since `bef` is a reference implementation (so far as it goes) of Befunge-93,
4 we saw fit to once again combine the specification and implementation into a
5 single distribution.
3 This is the reference distribution for the Befunge-93 programming language.
4 It contains the specification and reference implementation (`bef`), which
5 are direct descendants of the originals, along with a few related tools and
6 many example programs.
67
78 About Befunge-93
89 ----------------
2021 * `README.markdown`: these post-modernist existential rants
2122 * `LICENSE`: a bedtime story written to give your lawyer sweet dreams
2223 * `doc/Befunge-93.markdown`: the specification for Befunge-93, such as it is
23 * `src/bef.c`: Befunge-93 reference interpreter/debugger v2.23 source code
24 * `src/bef2c.c`: Befunge-93 to ANSI C compiler v0.94 source code
25 * `src/befprof.c`: Befunge-93 profiler v0.94 source code
24 * `src/bef.c`: Befunge-93 reference interpreter/debugger v2.24 source code
25 * `src/bef2c.c`: Befunge-93 to ANSI C compiler v1.0 source code
26 * `src/befprof.c`: Befunge-93 profiler v1.0 source code
2627 * `eg/*`: Various and sundry contributed Befunge-93 programs
2728
2829 For More Information
2930 --------------------
3031
31 See Cat's Eye Technologies' official [Befunge-93 Project Page][].
32 See the official entry for [Befunge-93][] at [Cat's Eye Technologies][].
3233
33 [Befunge-93 Project Page]: http://catseye.tc/projects/befunge93/
34 [Befunge-93 Project Page]: http://catseye.tc/node/Befunge-93
35 [Cat's Eye Technologies]: http://catseye.tc/
3436
3537 Happy Befunging!
3638 Chris Pressey
37 Winnipeg, Manitoba
38 August 25, 2012
39 London, England
40 September 13, 2018
0 @echo off
1 rem --------------------------------------------------------------
2 rem Batchfile that compiles the bef sources under Borland C++ 3.1.
3 rem Uses "large" memory model for befprof b/c it uses >64K data.
4 rem --------------------------------------------------------------
5
6 @echo on
7 bcc -ebin\bef.exe src\bef.c
8 bcc -ml -ebin\befprof.exe src\befprof.c
9 bcc -ebin\bef2c.exe src\bef2c.c
0 ;
1 ; To build the Befunge-93 reference executables using DICE C
2 ; under AmigaDOS 1.3, you can either run
3 ;
4 ; protect build.seq srwd
5 ; build.seq
6 ;
7 ; or you can simply type the following commands into the shell:
8 ;
9 dcc src/bef.c -o bin/bef
10 dcc src/befprof.c -o bin/befprof
11 ;dcc src/bef2c.c -o bin/bef2c ; DICE C can't handle this one.
00 /* ******************************************************************
11
22 bef.c - The Original Befunge-93 Interpreter/Debugger in ANSI C
3 v2.23-and-then-some-TODO-describe-the-changes-then-bump-this
4
5 Copyright (c)1993-2015, Chris Pressey, Cat's Eye Technologies.
3 v2.24
4
5 Copyright (c)1993-2018, Chris Pressey, Cat's Eye Technologies.
66 All rights reserved.
77
88 Redistribution and use in source and binary forms, with or without
3838
3939 Usage :
4040
41 bef [-d] [-o] [-q] [-i] [-=] [-l] [-t]
41 bef [-d] [-o] [-u] [-q] [-i] [-=] [-l] [-t]
4242 [-r input-file] [-w output-file]
4343 [-s stack-file] [-y delay] <befunge-source>
4444
4545 -d: visual ANSI debugging display
4646 -o: do not fix off-by-one error (for old sources)
47 -u: & on error or EOF is undefined, not -1 (backwards compat)
4748 -q: produce no output except Befunge program output ('quiet')
4849 -i: ignore unsupported instructions
4950 -=: use b97-ish = directives
5455 -s: write contents of stack to log file
5556 -y: specify debugging delay in milliseconds
5657
57 Compiles Under:
58
59 Borland C++ v3.1 (16-bit MS-DOS)
60 DJGPP v2.952 (32-bit Protected-Mode MS-DOS)
58 Known to Compile Under :
59
60 gcc 5.4.0 (Ubuntu 16.04)
61 gcc 4.5.3 (NetBSD 6.1.5)
62 DICE C 3.15 (AmigaDOS 1.3)
63 DJGPP 2.05 gcc 8.1.0 (32-bit Protected-Mode, FreeDOS 1.1)
64 Borland C++ v3.1 (16-bit, FreeDOS 1.1)
65 Microsoft Visual C++ 14.15 (see NMakefile)
66
67 Has, in the Past, been Known to Compile Under:
68
69 Metrowerks CodeWarrior (MacOS)
6170
6271 ******************************************************************
72
73 History:
74
75 v2.24: Sep 2018, Chris Pressey
76 when & encounters an error or EOF condition it pushes
77 -1 onto the stack instead of an undefined value;
78 added -u option to retain old behaviour in this case
79 (thanks to James Holderness for noticing and suggesting)
80 support for compiling with MSVC (also by James Holderness)
6381
6482 v2.23: Aug 2012, Chris Pressey
6583 delay given with -y now actually happens when compiled
85103
86104 v2.20, Jul 2000, Chris Pressey
87105 prettied up preprocessor directives a bit
88 added defines for Metroworks CodeWarrior
106 added defines for Metrowerks CodeWarrior
89107 so that bef will build on MacOS
90108 relicensed under BSD
91109
122140 v2.00: Jun 1997, Chris Pressey
123141 combines interpreter and debugger.
124142 fixes ANSI error in debugger.
143
125144 v1.02: Feb 1996, Chris Pressey
126145 @ now pushes '@' onto the stack in stringmode instead of quitting.
127146
159178 # include <console.h>
160179 # define CONSOLE 1
161180 #endif /* __MWERKS__ */
181 #ifdef _MSC_VER
182 # define sleep(s) _sleep(1000 * s)
183 #endif /* _MSC_VER */
162184
163185 /********************************************************** #DEFINE'S */
164186
191213 int stringmode = 0; /* flag : are we in string mode? */
192214 int quiet = 0; /* flag : are we quiet? */
193215 int v10err_compat = 0; /* flag : emulate v1.0 off-by-one err? */
216 int undef_input_int = 0; /* flag : undefined value on "&" err/EOF? */
194217 int deldur = 25; /* debugging delay in milliseconds */
195218 int ignore_unsupported = 0; /* flag : ignore unsupported instructions? */
196219 int use_b97directives = 0; /* flag : use b97-esque directives? */
232255 {
233256 if (argv[i][0] == '-') {
234257 if (!strcmp(argv[i], "-o")) { v10err_compat = 1; }
258 else if (!strcmp(argv[i], "-u")) { undef_input_int = 1; }
235259 else if (!strcmp(argv[i], "-d")) { debug = 1; }
236260 else if (!strcmp(argv[i], "-r")) { infile = 1; ia = i + 1; }
237261 else if (!strcmp(argv[i], "-w")) { outfile = 1; oa = i + 1; }
251275 }
252276 if (!quiet)
253277 {
254 printf ("Befunge-93 Interpreter/Debugger v2.23\n");
278 printf ("Befunge-93 Interpreter/Debugger v2.24\n");
255279 }
256280
257281 memset(pg, ' ', LINEWIDTH * PAGEHEIGHT);
660684 case '&': /* Input Integer */
661685 {
662686 signed long b;
687 if (!undef_input_int) {
688 b = -1;
689 }
663690 if (infile)
664691 {
665692 fscanf (fi, "%ld", &b);
676703 int x, y;
677704 long int p;
678705 char t[172];
706 if (!undef_input_int) {
707 p = -1;
708 }
679709 x = wherex();
680710 y = wherey();
681711 gettext(10, DEBUGROW, 80, DEBUGROW, t);
904934
905935 void usage ()
906936 {
907 printf ("USAGE: bef [-d] [-o] [-q] [-i] [-=] [-l] [-t]\n");
937 printf ("USAGE: bef [-d] [-o] [-u] [-q] [-i] [-=] [-l] [-t]\n");
908938 printf (" [-r input] [-w output] [-s stack] [-y delay] foo.bf\n");
909939 exit (1);
910940 }
00 /********************************************************************
11
22 bef2c.c - Befunge-93 to ANSI C Compiler in ANSI C
3 v0.94 Sep 20 2004 Chris Pressey, Cat's-Eye Technologies
4
5 Copyright (c)1997-2012, Chris Pressey, Cat's Eye Technologies.
3
4 Copyright (c)1997-2018, Chris Pressey, Cat's Eye Technologies.
65 All rights reserved.
76
87 Redistribution and use in source and binary forms, with or without
3837
3938 Usage :
4039
41 bef2c [-p] [-o] [-w width] [-h height] <befunge-source> <c-destination>
42
43 -p : suppress pre-optimization
44 -o : suppress post-optimization
40 bef2c [-O] [-w width] [-h height] <befunge-source> <c-destination>
41
42 -O : enable [buggy] post-optimization phase
4543 -w : explicit width
4644 -h : explicit height
4745
4846 Known to Compile Under :
4947
50 Borland C++ v3.1 (DOS)
48 gcc 5.4.0 (Ubuntu 16.04)
49 gcc 4.5.3 (NetBSD 6.1.5)
50 DJGPP 2.05 gcc 8.1.0 (32-bit Protected-Mode, FreeDOS 1.1)
51 Borland C++ v3.1 (16-bit, FreeDOS 1.1)
52 Microsoft Visual C++ 14.15 (see NMakefile)
53
54 Has, in the Past, been Known to Compile Under:
55
56 Metrowerks CodeWarrior (MacOS)
57
58 ******************************************************************
5159
5260 History:
61
62 v1.0: Sep 2018, Chris Pressey
63 fixes submitted by https://github.com/serprex (thanks!):
64 - avoid double free due to double fclose
65 - handle trampoline at leftmost/topmost edges
66 - don't load NULs into playfield
67 - use %% instead of %c with '%' being passed in
68 - avoid freeing fo/fi on failure to open
69 removed -p flag as it is equivalent to
70 `-w 80 -h 25`, just use that instead
71 replaced -o (disable post-optimization) flag
72 with -O (enable post-optimization) because buggy
73 show usage and exit if unrecognized command-line
74 options are given
75 exit with a non-zero exit code if an error occurs
76 when detecing pageheight, add one on the assumption
77 that the source does not end with a newline
78 (this lets some existing example programs compile)
5379
5480 v0.94: Sep 2004, Chris Pressey
5581 display correct version number
5682 cleanup only, no functional changes
5783
5884 v0.93, Jul 19 2000, Chris Pressey
59 added defines for Metroworks CodeWarrior
85 added defines for Metrowerks CodeWarrior
6086 so that bef2c will build on MacOS
6187 relicensed under BSD license
6288
79105 int pageheight = 1; /* 25 */
80106
81107 #define cur pg[y * 80 + x]
82 #define shrink(s) s[strlen(s)-1]=0
83108
84109 #define RIGHT "_%2.2d_%2.2d_R"
85110 #define LEFT "_%2.2d_%2.2d_L"
92117 #define x_left (x ? x-1 : linewidth-1)
93118
94119 #define y_2down (y+2)%pageheight
95 #define y_2up ((y>1) ? y-2 : ((pageheight-2)<0 ? 0 : (pageheight-2)))
120 #define y_2up ((y>1) ? y-2 : ((pageheight-2)<0 ? 0 : (y-2+pageheight)))
96121 #define x_2right (x+2)%linewidth
97 #define x_2left ((x>1) ? x-2 : ((linewidth-2)<0 ? 0 : (linewidth-2)))
122 #define x_2left ((x>1) ? x-2 : ((linewidth-2)<0 ? 0 : (x-2+linewidth)))
98123
99124 #define ALL RIGHT ": " LEFT ": " UP ": " DOWN ":\n"
100125
108133 char in[255];
109134 char pg[2000]; /* befunge 'page' of source */
110135 int x = 0, y = 0, d = 0; /* loopers */
111 int pre_optimize = 1; /* flag: optimize before compile? */
112 int post_optimize = 1; /* flag: optimize after compile? */
136 int post_optimize = 0; /* flag: optimize after compile? */
113137
114138 int labelrefs[8000]; /* postoptimization table */
115139 char s[255];
117141
118142 /********************************************************* PROTOTYPES */
119143
144 void usage(char *);
120145 int main (int, char **);
146
147 /********************************************************** FUNCTIONS */
148
149 void usage(char *e)
150 {
151 printf ("USAGE : %s [-O] [-w width] [-h height] <befunge-source> <c-destination>\n", e);
152 exit (1);
153 }
121154
122155 /******************************************************* MAIN PROGRAM */
123156
136169
137170 srand (time (0));
138171
139 printf ("Befunge-93 to ANSI C Compiler v0.94\n");
140
141 if (argc < 3)
142 {
143 printf ("USAGE : %s [-p] [-o] [-w width] [-h height] <befunge-source> <c-destination>\n", argv[0]);
144 exit (0);
145 }
172 printf ("Befunge-93 to ANSI C Compiler v1.0\n");
173
174 if (argc < 3) usage(argv[0]);
146175 for (i = 1; i < argc; i++)
147176 {
148 if (!strcmp(argv[i], "-p")) { pre_optimize = 0; linewidth=80; pageheight=25; }
149 if (!strcmp(argv[i], "-o")) { post_optimize = 0; }
150 if (!strcmp(argv[i], "-w")) { linewidth = atoi(argv[i+1]); }
151 if (!strcmp(argv[i], "-h")) { pageheight = atoi(argv[i+1]); }
177 if (argv[i][0] == '-')
178 {
179 if (!strcmp(argv[i], "-O")) { post_optimize = 1; }
180 else if (!strcmp(argv[i], "-w")) { linewidth = atoi(argv[i+1]); }
181 else if (!strcmp(argv[i], "-h")) { pageheight = atoi(argv[i+1]); }
182 else usage(argv[0]);
183 }
152184 }
153185 if ((fi = fopen (argv[argc - 2], "r")) != NULL) /*** Input Phase */
154186 {
155187 int x = 0, y = 0;
156188 while (!feof (fi))
157189 {
158 cur = fgetc (fi);
159 if ((x+1)>linewidth) linewidth=x+1;
160 if ((y+1)>pageheight) pageheight=y+1;
161 if (cur == '\n')
162 {
163 cur = ' ';
190 int ch = fgetc (fi);
191 if (ch == -1) break;
192 if (ch == '\n')
193 {
164194 x = 0;
165195 y++;
166196 if (y >= 25) break;
197 else if (y > pageheight) pageheight=y;
167198 } else
168199 {
200 cur = ch;
169201 x++;
170202 if (x >= 80)
171203 {
172204 x = 0;
173205 y++;
174206 if (y >= 25) break;
207 else if (y > pageheight) pageheight=y;
208 } else if (x > linewidth)
209 {
210 linewidth = x;
175211 }
176212 }
177213 }
178 fclose (fi);
179214 } else
180215 {
181216 printf ("Error : couldn't open '%s' for input.\n", argv[argc - 1]);
182 exit (0);
183 }
217 exit (1);
218 }
219
220 /* Most Befunge-93 sources do not end with a newline. Therefore: */
221 if (pageheight < 25) pageheight++;
184222
185223 if (!(fo = fopen (argv[argc - 1], "w"))) /*** Output */
186224 {
187225 printf ("Error : couldn't open '%s' for output.\n", argv[argc - 1]);
188 exit (0);
189 }
190
226 exit (1);
227 }
228
229 printf ("Loaded %d columns by %d rows.\n", linewidth, pageheight);
191230 printf ("Compiling");
192231
193232 fprintf (fo, "/* %s converted to ANSI C from %s by bef2c */\n",
208247 {
209248 for(x = 0; x < linewidth; x++)
210249 {
211 if (cur!='\\')
212 {
213 fprintf (fo, " pg[%d]='%c';\n", y * 80 + x, cur);
214 } else
215 {
216 fprintf (fo, " pg[%d]='%c%c';\n", y * 80 + x, cur, cur);
217 }
250 if (cur) fprintf (fo, " pg[%d]=%d;\n", y * 80 + x, cur);
218251 }
219252 }
220253
270303 ECHO("a=pop();b=pop();push(b/a);");
271304 break;
272305 case '%':
273 sprintf(t, "a=pop();b=pop();push(b%ca);", '%');
306 sprintf(t, "a=pop();b=pop();push(b%%a);");
274307 fprintf(fo, RIGHT ": %s goto " RIGHT ";\n", x, y, t, x_right, y);
275308 fprintf(fo, LEFT ": %s goto " LEFT ";\n", x, y, t, x_left, y);
276309 fprintf(fo, UP ": %s goto " UP ";\n", x, y, t, x, y_up);
283316 ECHO("a=pop();push(a);push(a);");
284317 break;
285318 case '.':
286 fprintf(fo, RIGHT ": fprintf(stdout,\"%cld \",pop());fflush(stdout); goto " RIGHT ";\n", x, y, '%', x_right, y);
287 fprintf(fo, LEFT ": fprintf(stdout,\"%cld \",pop());fflush(stdout); goto " LEFT ";\n", x, y, '%', x_left, y);
288 fprintf(fo, UP ": fprintf(stdout,\"%cld \",pop());fflush(stdout); goto " UP ";\n", x, y, '%', x, y_up);
289 fprintf(fo, DOWN ": fprintf(stdout,\"%cld \",pop());fflush(stdout); goto " DOWN ";\n", x, y, '%', x, y_down);
319 fprintf(fo, RIGHT ": fprintf(stdout,\"%%ld \",pop());fflush(stdout); goto " RIGHT ";\n", x, y, x_right, y);
320 fprintf(fo, LEFT ": fprintf(stdout,\"%%ld \",pop());fflush(stdout); goto " LEFT ";\n", x, y, x_left, y);
321 fprintf(fo, UP ": fprintf(stdout,\"%%ld \",pop());fflush(stdout); goto " UP ";\n", x, y, x, y_up);
322 fprintf(fo, DOWN ": fprintf(stdout,\"%%ld \",pop());fflush(stdout); goto " DOWN ";\n", x, y, x, y_down);
290323 break;
291324 case ',':
292 fprintf(fo, RIGHT ": fprintf(stdout,\"%cc\",pop());fflush(stdout); goto " RIGHT ";\n", x, y, '%', x_right, y);
293 fprintf(fo, LEFT ": fprintf(stdout,\"%cc\",pop());fflush(stdout); goto " LEFT ";\n", x, y, '%', x_left, y);
294 fprintf(fo, UP ": fprintf(stdout,\"%cc\",pop());fflush(stdout); goto " UP ";\n", x, y, '%', x, y_up);
295 fprintf(fo, DOWN ": fprintf(stdout,\"%cc\",pop());fflush(stdout); goto " DOWN ";\n", x, y, '%', x, y_down);
325 fprintf(fo, RIGHT ": fprintf(stdout,\"%%c\",pop());fflush(stdout); goto " RIGHT ";\n", x, y, x_right, y);
326 fprintf(fo, LEFT ": fprintf(stdout,\"%%c\",pop());fflush(stdout); goto " LEFT ";\n", x, y, x_left, y);
327 fprintf(fo, UP ": fprintf(stdout,\"%%c\",pop());fflush(stdout); goto " UP ";\n", x, y, x, y_up);
328 fprintf(fo, DOWN ": fprintf(stdout,\"%%c\",pop());fflush(stdout); goto " DOWN ";\n", x, y, x, y_down);
296329 break;
297330 case '&':
298 fprintf(fo, RIGHT ": fscanf(stdin,\"%cld\",&b);push(b); goto " RIGHT ";\n", x, y, '%', x_right, y);
299 fprintf(fo, LEFT ": fscanf(stdin,\"%cld\",&b);push(b); goto " LEFT ";\n", x, y, '%', x_left, y);
300 fprintf(fo, UP ": fscanf(stdin,\"%cld\",&b);push(b); goto " UP ";\n", x, y, '%', x, y_up);
301 fprintf(fo, DOWN ": fscanf(stdin,\"%cld\",&b);push(b); goto " DOWN ";\n", x, y, '%', x, y_down);
331 fprintf(fo, RIGHT ": fscanf(stdin,\"%%ld\",&b);push(b); goto " RIGHT ";\n", x, y, x_right, y);
332 fprintf(fo, LEFT ": fscanf(stdin,\"%%ld\",&b);push(b); goto " LEFT ";\n", x, y, x_left, y);
333 fprintf(fo, UP ": fscanf(stdin,\"%%ld\",&b);push(b); goto " UP ";\n", x, y, x, y_up);
334 fprintf(fo, DOWN ": fscanf(stdin,\"%%ld\",&b);push(b); goto " DOWN ";\n", x, y, x, y_down);
302335 break;
303336 case '~':
304337 fprintf(fo, RIGHT ": c=fgetc(stdin);push(c); goto " RIGHT ";\n", x, y, x_right, y);
307340 fprintf(fo, DOWN ": c=fgetc(stdin);push(c); goto " DOWN ";\n", x, y, x, y_down);
308341 break;
309342 case '"': /* ha! */
310 ECHO("puts(\"Error: compiled Befunge does not support stringmode\n\");");
343 ECHO("puts(\"Error: compiled Befunge does not support stringmode\\n\");");
311344 break;
312345 case '!':
313346 ECHO("if(pop()) push(0); else push(1);");
320353 break;
321354 case '?':
322355 fprintf(fo, ALL, x, y, x, y, x, y, x, y);
323 fprintf(fo, " switch ((rand () / 32) %c 4) \n"
356 fprintf(fo, " switch ((rand () / 32) %% 4) \n"
324357 " { case 0: goto " RIGHT ";\n"
325358 " case 1: goto " LEFT ";\n"
326359 " case 2: goto " UP ";\n"
327360 " case 3: goto " DOWN "; }\n",
328 '%', x_right, y, x_left, y, x, y_up, x, y_down);
361 x_right, y, x_left, y, x, y_up, x, y_down);
329362 break;
330363 case '#':
331364 fprintf(fo, RIGHT ": goto " RIGHT ";\n", x, y, x_2right, y);
415448 }
416449 }
417450 }
418 fclose (fi);
419 fclose (fo);
451 if (fi) fclose (fi);
452 if (fo) fclose (fo);
420453 if ((fi = fopen ("temp.c", "r")) != NULL)
421454 {
422455 if ((fo = fopen (argv[argc - 1], "w")) != NULL)
00 /********************************************************************
11
22 befprof.c - Befunge-93 Profiler
3 v0.94 Sep 20 2004 Chris Pressey, Cat's-Eye Technologies
4
5 Copyright (c)1998-2012, Chris Pressey, Cat's Eye Technologies.
3
4 Copyright (c)1998-2018, Chris Pressey, Cat's Eye Technologies.
65 All rights reserved.
76
87 Redistribution and use in source and binary forms, with or without
5453 although note that cells are stored as 2-byte words,
5554 so too many repetitions may cause them to wrap back to 0.
5655
56 Known to Compile Under :
57
58 gcc 5.4.0 (Ubuntu 16.04)
59 gcc 4.5.3 (NetBSD 6.1.5)
60 DICE C 3.15 (AmigaDOS 1.3)
61 DJGPP 2.05 gcc 8.1.0 (32-bit Protected-Mode, FreeDOS 1.1)
62 Borland C++ v3.1 (16-bit, FreeDOS 1.1)
63 (NOTE: use "compact" or "large" memory model!)
64 Microsoft Visual C++ 14.15 (see NMakefile)
65
66 Has, in the Past, been Known to Compile Under:
67
68 Metrowerks CodeWarrior (MacOS)
69
5770 ******************************************************************
71
72 History:
73
74 v1.0: Sep 2018, Chris Pressey
75 handle trampoline at leftmost/topmost edges
76 (thanks to https://github.com/serprex for this fix!)
77 exit with non-zero error code on error
78 remove unimplemented -q option from usage help text
79 don't load invalid (past-EOF) bytes into playfield
5880
5981 v0.94: Sep 2004, Chris Pressey
6082 cleanup only, no functional changes
6183
6284 v0.93: Jul 2000, Chris Pressey
63 added defines for Metroworks CodeWarrior
85 added defines for Metrowerks CodeWarrior
6486 so that befprof will build on MacOS
6587 relicensed under BSD license
6688
6890 original Befunge-93 Profiler 'befprof' distribution.
6991
7092 ****************************************************************** */
71
72 /********************************************************* #PRAGMA'S */
73
74 /* This switches Borland C++ v3.1 to compact memory model, which doesn't crash */
75 #ifdef __BORLANDC__
76 #pragma option -mc
77 #endif
7893
7994 /********************************************************* #INCLUDE'S */
8095
164179 if ((pg == NULL) || (pgbuf == NULL))
165180 {
166181 printf ("Error: can't allocate %d bytes of memory.\n", LINEWIDTH * PAGEHEIGHT);
167 exit(0);
182 exit (1);
168183 }
169184 memset(pg, ' ', LINEWIDTH * PAGEHEIGHT);
170185 memset(pgbuf, ' ', LINEWIDTH * PAGEHEIGHT);
175190 if (prof[i] == NULL)
176191 {
177192 printf ("Error: can't allocate %lu bytes of memory.\n", (long)(LINEWIDTH * PAGEHEIGHT * sizeof(long int)));
178 exit(0);
193 exit (1);
179194 }
180195 memset(prof[i], 0, LINEWIDTH * PAGEHEIGHT * sizeof(long int));
181196 }
182197
183198 if (argc < 2)
184199 {
185 printf ("USAGE: befprof [-l] [-q] [-i] [-n count] [-r input] [-w foo.map] foo.bf\n");
186 exit (0);
200 printf ("USAGE: befprof [-l] [-i] [-n count] [-r input] [-w foo.map] foo.bf\n");
201 exit (1);
187202 }
188203
189204 strcpy(filename, argv[argc - 1]);
210225 if (!strcmp(argv[i], "-w")) { strcpy(mapfilename, argv[i + 1]); }
211226 }
212227
213 printf ("Befunge-93 Profiler v0.94\n");
228 printf ("Befunge-93 Profiler v1.0\n");
214229
215230 if ((f = fopen (filename, "r")) != NULL) /*** Input Phase */
216231 {
219234 while (!feof (f))
220235 {
221236 curbuf = fgetc (f);
237 if (feof (f))
238 {
239 curbuf = ' ';
240 break;
241 }
222242 if (curbuf == '\n')
223243 {
224244 curbuf = ' ';
240260 } else
241261 {
242262 printf ("Error: couldn't open '%s' for input.\n", filename);
243 exit (0);
263 exit (1);
244264 }
245265
246266 if (!(fp = fopen (mapfilename, "w")))
247267 {
248268 printf ("Error : couldn't open '%s' for output.\n", mapfilename);
249 exit (0);
269 exit (1);
250270 }
251271
252272 while (rep <= reps)
265285 if (!(fi = fopen (argv[ia], "r")))
266286 {
267287 printf ("Error : couldn't open '%s' for input.\n", argv[ia]);
268 exit (0);
288 exit (1);
269289 }
270290 }
271291
556576 y += dy;
557577 if (x < 0)
558578 {
559 x = LINEWIDTH - 1;
579 x += LINEWIDTH;
560580 } else
561581 {
562582 x = x % LINEWIDTH;
563583 }
564584 if (y < 0)
565585 {
566 y = PAGEHEIGHT - 1;
586 y += PAGEHEIGHT;
567587 } else
568588 {
569589 y = y % PAGEHEIGHT;