/
2l_graue.c
254 lines (225 loc) · 4.82 KB
/
2l_graue.c
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
/*
Interpreter for Gregor Richards's 2L language.
Define POINTER_REVERSE for the incorrect up/down swap in Gregor's interpreter.
Written in 2005 by Scott Feeney aka Graue
http://esolangs.org/wiki/2L
http://esolangs.org/wiki/User:Graue
To the extent possible under law, the author(s) have dedicated all copyright
and related and neighboring rights to this software to the public domain
worldwide. This software is distributed without any warranty.
You should have received a copy of the CC0 Public Domain Dedication along
with this software. If not, see:
http://creativecommons.org/publicdomain/zero/1.0/
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
static /*@null@*/ char *read_arbitrary_length_string(FILE *fp);
static void run(char **code, int lines, int maxline);
int main(int argc, char *argv[])
{
char **code;
FILE *src;
int codespace, codelines = 0, maxline = 0;
if (argc != 2)
{
fprintf(stderr, "Incorrect invocation\n");
return 1;
}
src = fopen(argv[1], "r");
if (src == NULL)
{
fprintf(stderr, "Unreadable file\n");
return 2;
}
codespace = 10;
code = malloc(codespace * sizeof(char*));
if (code == NULL)
return 3;
for (;;)
{
int len;
if (codelines == codespace)
{
codespace *= 2;
code = realloc(code, codespace * sizeof(char*));
if (!code)
return 4;
}
code[codelines] = read_arbitrary_length_string(src);
if (code[codelines] == NULL)
break;
len = (int) strlen(code[codelines]);
if (len > maxline)
maxline = len;
codelines++;
}
(void) fclose(src);
run(code, codelines, maxline);
free(code);
return 0;
}
static char *addmem(/*@null@*/ char *mem, int cursize, int newsize)
{
mem = realloc(mem, (size_t) newsize);
if (!mem)
{
fprintf(stderr, "Out of memory\n");
exit(EXIT_FAILURE);
}
memset(&mem[cursize], 0, (size_t) (newsize - cursize));
return mem;
}
#define UP 31337
#define DOWN 666
#define LEFT 420
#define RIGHT 69
static void run(char **code, int lines, int maxline)
{
char *mem = NULL;
int memsize = 10, cell, dir = DOWN, codex, codey;
int i, *lengths;
mem = addmem(mem, 0, memsize);
cell = 2; /* TL0 and TL1 are special */
codex = 0;
codey = 0;
lengths = malloc(sizeof(int) * lines);
if (lengths == NULL)
exit(EXIT_FAILURE);
for (i = 0; i < lines; i++)
lengths[i] = (int) strlen(code[i]);
for (;;)
{
char c;
if (codex >= lengths[codey])
c = ' ';
else
c = code[codey][codex];
if (c == '*') /* all IO and tape operations */
{
#ifdef POINTER_REVERSE
if (dir == DOWN)
#else
if (dir == UP)
#endif
{
/* move the pointer to the right */
cell++;
if (cell == memsize)
{
mem = addmem(mem, memsize, memsize*2);
memsize *= 2;
}
}
#ifdef POINTER_REVERSE
else if (dir == UP)
#else
else if (dir == DOWN)
#endif
{
/* move the pointer to the left */
if (cell == 0)
{
fprintf(stderr, "Underflow error\n");
exit(EXIT_FAILURE);
}
cell--;
}
else if (cell == 1)
{
/* trying to change TL1 */
assert(dir == LEFT || dir == RIGHT);
if ((int) mem[0] != 0) /* output a character */
(void) putchar(mem[0]);
else /* input a character */
{
int d;
d = getchar();
if (d == EOF)
d = 0;
mem[0] = (char) d;
}
}
else if (dir == LEFT)
{
/* decrement the current cell */
((int) mem[cell])--;
}
else
{
/* increment the current cell */
assert(dir == RIGHT);
((int) mem[cell])++;
}
}
else if (c == '+') /* conditional turn */
{
/* first back up one */
if (dir == UP) codey++;
else if (dir == DOWN) codey--;
else if (dir == LEFT) codex++;
else /* RIGHT */ codex--;
if ((int) mem[cell] != 0) /* turn right */
{
if (dir == UP) dir = RIGHT;
else if (dir == RIGHT) dir = DOWN;
else if (dir == DOWN) dir = LEFT;
else /* LEFT */ dir = UP;
}
else /* turn left */
{
if (dir == DOWN) dir = RIGHT;
else if (dir == RIGHT) dir = UP;
else if (dir == UP) dir = LEFT;
else /* LEFT */ dir = DOWN;
}
}
if (dir == RIGHT)
{
if (++codex == maxline)
break;
}
else if (dir == LEFT)
{
if (codex-- == 0)
break;
}
else if (dir == DOWN)
{
if (++codey == lines)
break;
}
else
{
assert(dir == UP);
if (codey-- == 0)
break;
}
}
free(lengths);
free(mem);
}
static /*@null@*/ char *read_arbitrary_length_string(FILE *fp)
{
char *p = NULL, *p2, *q;
int size = 50;
if ((p = malloc((size_t) size)) == NULL)
return NULL;
if (fgets(p, size, fp) == NULL)
return NULL;
while ((q = strchr(p, '\n')) == NULL)
{
size *= 2;
if ((p2 = realloc(p, (size_t) size)) == NULL)
{
free(p);
return NULL;
}
p = p2;
if (fgets(&p[size/2-1], size/2+1, fp) == NULL)
return NULL; /* invalid; file must end with newline */
}
*q = '\0';
return p;
}