aboutsummaryrefslogtreecommitdiff
path: root/brainfuck.c
blob: de5f703b50a0d946ef796533c03626e3eef22c8a (plain)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 * gcc -ansi -pedantic -Wall -Wextra -Werror -o brainfuck brainfuck.c
 */

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <getopt.h>

#define DATATYPE int

FILE * prog;

char *   code = NULL;
DATATYPE *   data = NULL;
unsigned int  *  stack = NULL;

unsigned int data_size  = 30000;
unsigned int stack_size = 128;

char format = 'i';

unsigned int cp = 0;
unsigned int dp = 0;
unsigned int sp = 0;
char ignore [] = "\t\r\n ";


void read_code()
{
    int allocated, n, c, comment;

    allocated = 1000;
    n = 0;

    code = (char*) malloc(allocated * sizeof(char));
    comment = 0;
    while (EOF != (c = getc(prog)))
    {
        if (c == ';') /* end of code */
            break;
        else if (c == '#')
            comment = 1;
        else if (c == '\n' || c == '\r')
            comment = 0;

        if (comment || (NULL != strchr(ignore, c)))
            continue;

        if (n >= allocated)
        {
            allocated <<= 1;
            code = (char*) realloc(code, allocated * sizeof(char));
        }
        code[n++] = (char) c;
    }
    
    if (n >= allocated)
    {
        allocated <<= 1;
        code = (char*) realloc(code, allocated * sizeof(char));
    }
    code[n++] = '\0';
}

void init_data()
{
    data = (DATATYPE*) calloc(data_size, sizeof(DATATYPE));
}

void init_stack()
{
    stack = (unsigned int*) malloc(stack_size);
}


int inc_dp()
{
    if (dp < data_size)
    {
        ++dp;
        return 1;
    }
    else
    {
        fprintf(stderr, "Data overflow\n");
        return 0;
    }
}

void dec_dp()
{
    if (dp > 0)
        --dp;
    else
        fprintf(stderr, "Data underflow\n");
}


void push_cp()
{
    if (sp < stack_size)
        stack[sp++] = cp;
    else
        fprintf(stderr, "Stack overflow\n");
}

void pop_cp()
{
    if (sp > 0)
        cp = stack[--sp];
    else
        fprintf(stderr, "Stack underflow\n");
}

int skip()
{
    int ob = 0;
    while (code[cp] != '\0')
    {
        switch (code[cp])
        {
            case '[': ++ob; break;
            case ']': --ob; if (0 == ob) return 1;
        }
        ++cp;
    }
    return 0;
}


void print()
{
    switch (format)
    {
        case 'i': printf("%i\n",   data[dp]); break;
        case 'u': printf("%u\n",   data[dp]); break;
        case 'c': printf("%c",     data[dp]); break;
        case 'o': printf("0%o\n",  data[dp]); break;
        case 'x': printf("0x%X\n", data[dp]); break;
    }
}

void input()
{
    switch (format)
    {
        case 'i': scanf("%i\n",   (signed int*)   &(data[dp])); break;
        case 'u': scanf("%u\n",   (unsigned int*) &(data[dp])); break;
        case 'c': scanf("%c",     (char*)         &(data[dp])); break;
        case 'o': scanf("0%o\n",  (unsigned int*) &(data[dp])); break;
        case 'x': scanf("0x%X\n", (unsigned int*) &(data[dp])); break;
    }
}

void run_code()
{
    char cmd;
    while ('\0' != (cmd = code[cp]))
    {
        /* fprintf(stderr, "%c", cmd); */
        switch (cmd)
        {
            case '[': if (data[dp])
                      {
                          push_cp(); ++cp;
                      }
                      else
                      {
                          skip(); ++cp;
                      }
                      break;
            case ']': pop_cp(); break;
            case '+': ++(data[dp]); ++cp; break;
            case '-': --(data[dp]); ++cp; break;
            case '>': inc_dp();     ++cp; break;
            case '<': dec_dp();     ++cp; break;
            case '.': print();      ++cp; break;
            case ',': input();      ++cp; break;

            case 'i':
            case 'u':
            case 'c':
            case 'o':
            case 'x':
                      format = cmd; ++cp; break;
            default: ++cp;
        }
    }
}

void free_all()
{
    if (code  != NULL) free(code);
    if (data  != NULL) free(data);
    if (stack != NULL) free(stack);
}

void usage(char * self)
{
    printf("%s: Brainfuck programming language interpreter\n", self);
    printf("See http://en.wikipedia.org/wiki/Brainfuck for more details\n\n");

    printf("Usage: %s [options] [file]\n\n", self);

    printf("Size of each data cell is %u byte(s)\n", sizeof(DATATYPE));
    printf("All data cell are zeros initially\n\n");
    printf("Options (defaults are in brackets):\n");
    printf("   -s num         stack size (%u)\n", stack_size);
    printf("   -d num         data size (%u)\n", data_size);
    printf("\n");
    printf("Formats for operators '.' and ',' (output and input):\n");
    printf("   -c, -i, -u, -o, -x  char, signed int, unsigned int, octal, hexadecimal\n");
    printf("                       octal number must be prepended by '0' (zero),\n");
    printf("                       and hexadecimal - by '0x'\n");
    printf("Default i/o format: -%c\n", format);
    printf("\n");
    printf("   -h             this help message\n");
    printf("\n");
    printf("  file            file to execute,\n");
    printf("                  if omitted read stdin\n");
    printf("\n");
    printf("Standard operators: <>+-[].,\n");
    printf("Extensions:\n");
    printf("            ciuox - change i/o format (same as -c & others above)\n");
    printf("            ;     - end of code (useful when reading stdin)\n");
    printf("            #     - comment to the end of line (useful when reading files)\n");
    printf("\n");
    printf("Example:\n");
    printf(" echo '+++[.-]' | %s # count down from 3 to 0\n", self);
    printf(" echo ',+++.;5' | %s # shows 8\n", self);
    printf(" echo ',>,<[->+<]>.;4 5' | %s # shows 4+5=9\n", self);

    exit(EXIT_SUCCESS);
}

int main(int argc, char ** argv)
{
    char * filename;
    char * self = argv[0];
    int opt;

    while (-1 != (opt = getopt(argc, argv, "s:d:h?iucox")))
    {
       switch (opt)
       {
           case 's':
               sscanf(optarg, "%u", &stack_size);
               break;
           case 'd':
               sscanf(optarg, "%u", &data_size);
               break;
           case 'i':
           case 'u':
           case 'c':
           case 'o':
           case 'x':
               format = opt;
               break;
           default: usage(self);
       }
    }

    if (optind < argc)
    {
        filename = argv[optind];
        if (NULL == (prog = fopen(filename, "r")))
        {
            fprintf(stderr, "%s: open '%s': %s\n", self, filename, strerror(errno));
            exit(EXIT_FAILURE);
        }
    }
    else
        prog = stdin;

    read_code();

    if (prog != stdin)
        fclose(prog);

    init_data();
    init_stack();

    run_code();

    free_all();
    return 0;
}