Mercurial > repo
comparison interps/2l/2lc.c @ 996:859f9b4339e6
<Gregor> tar xf egobot.tar.xz
author | HackBot |
---|---|
date | Sun, 09 Dec 2012 19:30:08 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
995:6883f5911eb7 | 996:859f9b4339e6 |
---|---|
1 /* | |
2 * Copyright (c) 2004, 2005 Gregor Richards | |
3 * | |
4 * Permission is hereby granted, free of charge, to any person obtaining a copy | |
5 * of this software and associated documentation files (the "Software"), to deal | |
6 * in the Software without restriction, including without limitation the rights | |
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
8 * copies of the Software, and to permit persons to whom the Software is | |
9 * furnished to do so, subject to the following conditions: | |
10 * | |
11 * The above copyright notice and this permission notice shall be included in | |
12 * all copies or substantial portions of the Software. | |
13 * | |
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
20 * SOFTWARE. | |
21 */ | |
22 | |
23 #include <stdio.h> | |
24 #include <string.h> | |
25 | |
26 #define XY(x,y) (((y)*1024)+(x)) | |
27 #define XYD(x,y,d) (((y)*1024*4)+((x)*4)+((d)-1)) | |
28 | |
29 char pbuf[1024*1024]; | |
30 char implbuf[1024*1024*4]; | |
31 | |
32 void impl(int px, int py, int dir) { | |
33 dir = dir % 4; | |
34 if (dir == 0) dir = 4; | |
35 | |
36 while (1) { | |
37 /* first, move */ | |
38 switch (dir) { | |
39 case 1: /* up */ | |
40 py--; | |
41 break; | |
42 case 2: /* right */ | |
43 px++; | |
44 break; | |
45 case 3: /* down */ | |
46 py++; | |
47 break; | |
48 case 4: /* left */ | |
49 px--; | |
50 break; | |
51 } | |
52 | |
53 /* quit if out of bounds */ | |
54 if (px < 0 || py < 0 || px >= 1024 || py >= 1024) { | |
55 printf("return 0;\n"); | |
56 return; | |
57 } | |
58 | |
59 if (implbuf[XYD(px, py, dir)]) { | |
60 printf("goto x%dy%dd%d;\n", px, py, dir); | |
61 return; | |
62 } else { | |
63 printf("x%dy%dd%d:\n", px, py, dir); | |
64 /* mark as implemented */ | |
65 implbuf[XYD(px, py, dir)] = 1; | |
66 } | |
67 | |
68 switch(pbuf[XY(px, py)]) { | |
69 case '+': /* branch */ | |
70 /* go back */ | |
71 switch (dir) { | |
72 case 1: /* up */ | |
73 py++; | |
74 break; | |
75 case 2: /* right */ | |
76 px--; | |
77 break; | |
78 case 3: /* down */ | |
79 py--; | |
80 break; | |
81 case 4: /* left */ | |
82 px++; | |
83 break; | |
84 } | |
85 | |
86 printf("if (*mptr) {\n"); | |
87 impl(px, py, dir+1); | |
88 printf("} else {\n"); | |
89 impl(px, py, dir-1); | |
90 printf("}\n"); | |
91 return; | |
92 | |
93 break; | |
94 | |
95 case '*': /* memory operator */ | |
96 if (dir == 2 || dir == 4) { | |
97 printf("if (mptr == mbuf + 1) {\n"); | |
98 printf("if (mbuf[0] == 0) { mbuf[0] = getchar(); } else { putchar(mbuf[0]); fflush(stdout); mbuf[0] = 0; }\n"); | |
99 printf("} else {\n"); | |
100 } | |
101 | |
102 switch (dir) { | |
103 case 1: /* up */ | |
104 printf("mptr--;\n"); | |
105 break; | |
106 case 2: /* right */ | |
107 printf("(*mptr)++;\n"); | |
108 break; | |
109 case 3: /* down */ | |
110 printf("mptr++;\n"); | |
111 break; | |
112 case 4: /* left */ | |
113 printf("(*mptr)--;\n"); | |
114 break; | |
115 } | |
116 | |
117 if (dir == 2 || dir == 4) { | |
118 printf("}\n"); | |
119 } | |
120 break; | |
121 } | |
122 } | |
123 } | |
124 | |
125 int main(int argc, char **argv) | |
126 { | |
127 FILE *pinp; | |
128 char pline[1024]; | |
129 int py; | |
130 | |
131 if (argc < 2) { | |
132 printf("Use: %s <program>\n", argv[0]); | |
133 return 1; | |
134 } | |
135 | |
136 memset(pbuf, 0, 1024*1024); | |
137 memset(implbuf, 0, 1024*1024*4); | |
138 py = 0; | |
139 | |
140 /* read in the file */ | |
141 pinp = fopen(argv[1], "r"); | |
142 if (pinp == NULL) { | |
143 perror("fopen"); | |
144 return 1; | |
145 } | |
146 | |
147 while (!feof(pinp)) { | |
148 fgets(pline, 1024, pinp); | |
149 strcpy(pbuf + XY(0, py), pline); | |
150 py++; | |
151 } | |
152 | |
153 fclose(pinp); | |
154 | |
155 printf("#include <stdio.h>\nint main()\n{\n"); | |
156 printf("int mbuf[32256];\n"); | |
157 printf("int *mptr = mbuf + 2;\n"); | |
158 printf("memset(mbuf, 0, 32256);\n"); | |
159 | |
160 impl(0, -1, 3); | |
161 | |
162 printf("return 0;\n}\n"); | |
163 return 0; | |
164 } |