Merge branch 'master' of https://github.com/catseye/Befunge-93 into examples-fixes
Chris Pressey
3 years ago
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 | ================================================ | |
2 | 2 | |
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. | |
6 | 7 | |
7 | 8 | About Befunge-93 |
8 | 9 | ---------------- |
20 | 21 | * `README.markdown`: these post-modernist existential rants |
21 | 22 | * `LICENSE`: a bedtime story written to give your lawyer sweet dreams |
22 | 23 | * `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 | |
26 | 27 | * `eg/*`: Various and sundry contributed Befunge-93 programs |
27 | 28 | |
28 | 29 | For More Information |
29 | 30 | -------------------- |
30 | 31 | |
31 | See Cat's Eye Technologies' official [Befunge-93 Project Page][]. | |
32 | See the official entry for [Befunge-93][] at [Cat's Eye Technologies][]. | |
32 | 33 | |
33 | [Befunge-93 Project Page]: http://catseye.tc/projects/befunge93/ | |
34 | [Befunge-93]: http://catseye.tc/node/Befunge-93 | |
35 | [Cat's Eye Technologies]: http://catseye.tc/ | |
34 | 36 | |
35 | 37 | Happy Befunging! |
36 | 38 | 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 | 0 | /* ****************************************************************** |
1 | 1 | |
2 | 2 | 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. | |
6 | 6 | All rights reserved. |
7 | 7 | |
8 | 8 | Redistribution and use in source and binary forms, with or without |
38 | 38 | |
39 | 39 | Usage : |
40 | 40 | |
41 | bef [-d] [-o] [-q] [-i] [-=] [-l] [-t] | |
41 | bef [-d] [-o] [-u] [-q] [-i] [-=] [-l] [-t] | |
42 | 42 | [-r input-file] [-w output-file] |
43 | 43 | [-s stack-file] [-y delay] <befunge-source> |
44 | 44 | |
45 | 45 | -d: visual ANSI debugging display |
46 | 46 | -o: do not fix off-by-one error (for old sources) |
47 | -u: & on error or EOF is undefined, not -1 (backwards compat) | |
47 | 48 | -q: produce no output except Befunge program output ('quiet') |
48 | 49 | -i: ignore unsupported instructions |
49 | 50 | -=: use b97-ish = directives |
54 | 55 | -s: write contents of stack to log file |
55 | 56 | -y: specify debugging delay in milliseconds |
56 | 57 | |
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) | |
61 | 70 | |
62 | 71 | ****************************************************************** |
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) | |
63 | 81 | |
64 | 82 | v2.23: Aug 2012, Chris Pressey |
65 | 83 | delay given with -y now actually happens when compiled |
85 | 103 | |
86 | 104 | v2.20, Jul 2000, Chris Pressey |
87 | 105 | prettied up preprocessor directives a bit |
88 | added defines for Metroworks CodeWarrior | |
106 | added defines for Metrowerks CodeWarrior | |
89 | 107 | so that bef will build on MacOS |
90 | 108 | relicensed under BSD |
91 | 109 | |
122 | 140 | v2.00: Jun 1997, Chris Pressey |
123 | 141 | combines interpreter and debugger. |
124 | 142 | fixes ANSI error in debugger. |
143 | ||
125 | 144 | v1.02: Feb 1996, Chris Pressey |
126 | 145 | @ now pushes '@' onto the stack in stringmode instead of quitting. |
127 | 146 | |
159 | 178 | # include <console.h> |
160 | 179 | # define CONSOLE 1 |
161 | 180 | #endif /* __MWERKS__ */ |
181 | #ifdef _MSC_VER | |
182 | # define sleep(s) _sleep(1000 * s) | |
183 | #endif /* _MSC_VER */ | |
162 | 184 | |
163 | 185 | /********************************************************** #DEFINE'S */ |
164 | 186 | |
191 | 213 | int stringmode = 0; /* flag : are we in string mode? */ |
192 | 214 | int quiet = 0; /* flag : are we quiet? */ |
193 | 215 | int v10err_compat = 0; /* flag : emulate v1.0 off-by-one err? */ |
216 | int undef_input_int = 0; /* flag : undefined value on "&" err/EOF? */ | |
194 | 217 | int deldur = 25; /* debugging delay in milliseconds */ |
195 | 218 | int ignore_unsupported = 0; /* flag : ignore unsupported instructions? */ |
196 | 219 | int use_b97directives = 0; /* flag : use b97-esque directives? */ |
232 | 255 | { |
233 | 256 | if (argv[i][0] == '-') { |
234 | 257 | if (!strcmp(argv[i], "-o")) { v10err_compat = 1; } |
258 | else if (!strcmp(argv[i], "-u")) { undef_input_int = 1; } | |
235 | 259 | else if (!strcmp(argv[i], "-d")) { debug = 1; } |
236 | 260 | else if (!strcmp(argv[i], "-r")) { infile = 1; ia = i + 1; } |
237 | 261 | else if (!strcmp(argv[i], "-w")) { outfile = 1; oa = i + 1; } |
251 | 275 | } |
252 | 276 | if (!quiet) |
253 | 277 | { |
254 | printf ("Befunge-93 Interpreter/Debugger v2.23\n"); | |
278 | printf ("Befunge-93 Interpreter/Debugger v2.24\n"); | |
255 | 279 | } |
256 | 280 | |
257 | 281 | memset(pg, ' ', LINEWIDTH * PAGEHEIGHT); |
660 | 684 | case '&': /* Input Integer */ |
661 | 685 | { |
662 | 686 | signed long b; |
687 | if (!undef_input_int) { | |
688 | b = -1; | |
689 | } | |
663 | 690 | if (infile) |
664 | 691 | { |
665 | 692 | fscanf (fi, "%ld", &b); |
676 | 703 | int x, y; |
677 | 704 | long int p; |
678 | 705 | char t[172]; |
706 | if (!undef_input_int) { | |
707 | p = -1; | |
708 | } | |
679 | 709 | x = wherex(); |
680 | 710 | y = wherey(); |
681 | 711 | gettext(10, DEBUGROW, 80, DEBUGROW, t); |
904 | 934 | |
905 | 935 | void usage () |
906 | 936 | { |
907 | printf ("USAGE: bef [-d] [-o] [-q] [-i] [-=] [-l] [-t]\n"); | |
937 | printf ("USAGE: bef [-d] [-o] [-u] [-q] [-i] [-=] [-l] [-t]\n"); | |
908 | 938 | printf (" [-r input] [-w output] [-s stack] [-y delay] foo.bf\n"); |
909 | 939 | exit (1); |
910 | 940 | } |
0 | 0 | /******************************************************************** |
1 | 1 | |
2 | 2 | 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. | |
6 | 5 | All rights reserved. |
7 | 6 | |
8 | 7 | Redistribution and use in source and binary forms, with or without |
38 | 37 | |
39 | 38 | Usage : |
40 | 39 | |
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 | |
45 | 43 | -w : explicit width |
46 | 44 | -h : explicit height |
47 | 45 | |
48 | 46 | Known to Compile Under : |
49 | 47 | |
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 | ****************************************************************** | |
51 | 59 | |
52 | 60 | 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) | |
53 | 79 | |
54 | 80 | v0.94: Sep 2004, Chris Pressey |
55 | 81 | display correct version number |
56 | 82 | cleanup only, no functional changes |
57 | 83 | |
58 | 84 | v0.93, Jul 19 2000, Chris Pressey |
59 | added defines for Metroworks CodeWarrior | |
85 | added defines for Metrowerks CodeWarrior | |
60 | 86 | so that bef2c will build on MacOS |
61 | 87 | relicensed under BSD license |
62 | 88 | |
79 | 105 | int pageheight = 1; /* 25 */ |
80 | 106 | |
81 | 107 | #define cur pg[y * 80 + x] |
82 | #define shrink(s) s[strlen(s)-1]=0 | |
83 | 108 | |
84 | 109 | #define RIGHT "_%2.2d_%2.2d_R" |
85 | 110 | #define LEFT "_%2.2d_%2.2d_L" |
92 | 117 | #define x_left (x ? x-1 : linewidth-1) |
93 | 118 | |
94 | 119 | #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))) | |
96 | 121 | #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))) | |
98 | 123 | |
99 | 124 | #define ALL RIGHT ": " LEFT ": " UP ": " DOWN ":\n" |
100 | 125 | |
108 | 133 | char in[255]; |
109 | 134 | char pg[2000]; /* befunge 'page' of source */ |
110 | 135 | 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? */ | |
113 | 137 | |
114 | 138 | int labelrefs[8000]; /* postoptimization table */ |
115 | 139 | char s[255]; |
117 | 141 | |
118 | 142 | /********************************************************* PROTOTYPES */ |
119 | 143 | |
144 | void usage(char *); | |
120 | 145 | 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 | } | |
121 | 154 | |
122 | 155 | /******************************************************* MAIN PROGRAM */ |
123 | 156 | |
136 | 169 | |
137 | 170 | srand (time (0)); |
138 | 171 | |
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]); | |
146 | 175 | for (i = 1; i < argc; i++) |
147 | 176 | { |
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 | } | |
152 | 184 | } |
153 | 185 | if ((fi = fopen (argv[argc - 2], "r")) != NULL) /*** Input Phase */ |
154 | 186 | { |
155 | 187 | int x = 0, y = 0; |
156 | 188 | while (!feof (fi)) |
157 | 189 | { |
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 | { | |
164 | 194 | x = 0; |
165 | 195 | y++; |
166 | 196 | if (y >= 25) break; |
197 | else if (y > pageheight) pageheight=y; | |
167 | 198 | } else |
168 | 199 | { |
200 | cur = ch; | |
169 | 201 | x++; |
170 | 202 | if (x >= 80) |
171 | 203 | { |
172 | 204 | x = 0; |
173 | 205 | y++; |
174 | 206 | if (y >= 25) break; |
207 | else if (y > pageheight) pageheight=y; | |
208 | } else if (x > linewidth) | |
209 | { | |
210 | linewidth = x; | |
175 | 211 | } |
176 | 212 | } |
177 | 213 | } |
178 | fclose (fi); | |
179 | 214 | } else |
180 | 215 | { |
181 | 216 | 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++; | |
184 | 222 | |
185 | 223 | if (!(fo = fopen (argv[argc - 1], "w"))) /*** Output */ |
186 | 224 | { |
187 | 225 | 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); | |
191 | 230 | printf ("Compiling"); |
192 | 231 | |
193 | 232 | fprintf (fo, "/* %s converted to ANSI C from %s by bef2c */\n", |
208 | 247 | { |
209 | 248 | for(x = 0; x < linewidth; x++) |
210 | 249 | { |
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); | |
218 | 251 | } |
219 | 252 | } |
220 | 253 | |
270 | 303 | ECHO("a=pop();b=pop();push(b/a);"); |
271 | 304 | break; |
272 | 305 | case '%': |
273 | sprintf(t, "a=pop();b=pop();push(b%ca);", '%'); | |
306 | sprintf(t, "a=pop();b=pop();push(b%%a);"); | |
274 | 307 | fprintf(fo, RIGHT ": %s goto " RIGHT ";\n", x, y, t, x_right, y); |
275 | 308 | fprintf(fo, LEFT ": %s goto " LEFT ";\n", x, y, t, x_left, y); |
276 | 309 | fprintf(fo, UP ": %s goto " UP ";\n", x, y, t, x, y_up); |
283 | 316 | ECHO("a=pop();push(a);push(a);"); |
284 | 317 | break; |
285 | 318 | 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); | |
290 | 323 | break; |
291 | 324 | 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); | |
296 | 329 | break; |
297 | 330 | 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); | |
302 | 335 | break; |
303 | 336 | case '~': |
304 | 337 | fprintf(fo, RIGHT ": c=fgetc(stdin);push(c); goto " RIGHT ";\n", x, y, x_right, y); |
307 | 340 | fprintf(fo, DOWN ": c=fgetc(stdin);push(c); goto " DOWN ";\n", x, y, x, y_down); |
308 | 341 | break; |
309 | 342 | case '"': /* ha! */ |
310 | ECHO("puts(\"Error: compiled Befunge does not support stringmode\n\");"); | |
343 | ECHO("puts(\"Error: compiled Befunge does not support stringmode\\n\");"); | |
311 | 344 | break; |
312 | 345 | case '!': |
313 | 346 | ECHO("if(pop()) push(0); else push(1);"); |
320 | 353 | break; |
321 | 354 | case '?': |
322 | 355 | 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" | |
324 | 357 | " { case 0: goto " RIGHT ";\n" |
325 | 358 | " case 1: goto " LEFT ";\n" |
326 | 359 | " case 2: goto " UP ";\n" |
327 | 360 | " 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); | |
329 | 362 | break; |
330 | 363 | case '#': |
331 | 364 | fprintf(fo, RIGHT ": goto " RIGHT ";\n", x, y, x_2right, y); |
415 | 448 | } |
416 | 449 | } |
417 | 450 | } |
418 | fclose (fi); | |
419 | fclose (fo); | |
451 | if (fi) fclose (fi); | |
452 | if (fo) fclose (fo); | |
420 | 453 | if ((fi = fopen ("temp.c", "r")) != NULL) |
421 | 454 | { |
422 | 455 | if ((fo = fopen (argv[argc - 1], "w")) != NULL) |
0 | 0 | /******************************************************************** |
1 | 1 | |
2 | 2 | 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. | |
6 | 5 | All rights reserved. |
7 | 6 | |
8 | 7 | Redistribution and use in source and binary forms, with or without |
54 | 53 | although note that cells are stored as 2-byte words, |
55 | 54 | so too many repetitions may cause them to wrap back to 0. |
56 | 55 | |
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 | ||
57 | 70 | ****************************************************************** |
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 | |
58 | 80 | |
59 | 81 | v0.94: Sep 2004, Chris Pressey |
60 | 82 | cleanup only, no functional changes |
61 | 83 | |
62 | 84 | v0.93: Jul 2000, Chris Pressey |
63 | added defines for Metroworks CodeWarrior | |
85 | added defines for Metrowerks CodeWarrior | |
64 | 86 | so that befprof will build on MacOS |
65 | 87 | relicensed under BSD license |
66 | 88 | |
68 | 90 | original Befunge-93 Profiler 'befprof' distribution. |
69 | 91 | |
70 | 92 | ****************************************************************** */ |
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 | |
78 | 93 | |
79 | 94 | /********************************************************* #INCLUDE'S */ |
80 | 95 | |
164 | 179 | if ((pg == NULL) || (pgbuf == NULL)) |
165 | 180 | { |
166 | 181 | printf ("Error: can't allocate %d bytes of memory.\n", LINEWIDTH * PAGEHEIGHT); |
167 | exit(0); | |
182 | exit (1); | |
168 | 183 | } |
169 | 184 | memset(pg, ' ', LINEWIDTH * PAGEHEIGHT); |
170 | 185 | memset(pgbuf, ' ', LINEWIDTH * PAGEHEIGHT); |
175 | 190 | if (prof[i] == NULL) |
176 | 191 | { |
177 | 192 | printf ("Error: can't allocate %lu bytes of memory.\n", (long)(LINEWIDTH * PAGEHEIGHT * sizeof(long int))); |
178 | exit(0); | |
193 | exit (1); | |
179 | 194 | } |
180 | 195 | memset(prof[i], 0, LINEWIDTH * PAGEHEIGHT * sizeof(long int)); |
181 | 196 | } |
182 | 197 | |
183 | 198 | if (argc < 2) |
184 | 199 | { |
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); | |
187 | 202 | } |
188 | 203 | |
189 | 204 | strcpy(filename, argv[argc - 1]); |
210 | 225 | if (!strcmp(argv[i], "-w")) { strcpy(mapfilename, argv[i + 1]); } |
211 | 226 | } |
212 | 227 | |
213 | printf ("Befunge-93 Profiler v0.94\n"); | |
228 | printf ("Befunge-93 Profiler v1.0\n"); | |
214 | 229 | |
215 | 230 | if ((f = fopen (filename, "r")) != NULL) /*** Input Phase */ |
216 | 231 | { |
219 | 234 | while (!feof (f)) |
220 | 235 | { |
221 | 236 | curbuf = fgetc (f); |
237 | if (feof (f)) | |
238 | { | |
239 | curbuf = ' '; | |
240 | break; | |
241 | } | |
222 | 242 | if (curbuf == '\n') |
223 | 243 | { |
224 | 244 | curbuf = ' '; |
240 | 260 | } else |
241 | 261 | { |
242 | 262 | printf ("Error: couldn't open '%s' for input.\n", filename); |
243 | exit (0); | |
263 | exit (1); | |
244 | 264 | } |
245 | 265 | |
246 | 266 | if (!(fp = fopen (mapfilename, "w"))) |
247 | 267 | { |
248 | 268 | printf ("Error : couldn't open '%s' for output.\n", mapfilename); |
249 | exit (0); | |
269 | exit (1); | |
250 | 270 | } |
251 | 271 | |
252 | 272 | while (rep <= reps) |
265 | 285 | if (!(fi = fopen (argv[ia], "r"))) |
266 | 286 | { |
267 | 287 | printf ("Error : couldn't open '%s' for input.\n", argv[ia]); |
268 | exit (0); | |
288 | exit (1); | |
269 | 289 | } |
270 | 290 | } |
271 | 291 | |
556 | 576 | y += dy; |
557 | 577 | if (x < 0) |
558 | 578 | { |
559 | x = LINEWIDTH - 1; | |
579 | x += LINEWIDTH; | |
560 | 580 | } else |
561 | 581 | { |
562 | 582 | x = x % LINEWIDTH; |
563 | 583 | } |
564 | 584 | if (y < 0) |
565 | 585 | { |
566 | y = PAGEHEIGHT - 1; | |
586 | y += PAGEHEIGHT; | |
567 | 587 | } else |
568 | 588 | { |
569 | 589 | y = y % PAGEHEIGHT; |