/* (c) 2002 Jeyasankar Kottalam
 *
 * Jeyasankar Kottalam's BrainF*** interpreter
 */

#include <stdio.h>

/* Sets the size of the address space
 * Default:     65536                     
 * Traditional: 30000
 */
#define MEMORY_SIZE 65536

/* Sets the size of the largest loadable sourcefile
 * Default: 65536
 */ 
#define MAX_FILE_SIZE 65536

/* Sets the maximum nesting level of loops
 * Default: 4096
 */ 
#define MAX_LOOPS 4096

int main(int a, char *b[])
{
	FILE *c = a>1 ? fopen(1[b],"r") : stdin;
	unsigned char d[MAX_FILE_SIZE]={0},
		e[MEMORY_SIZE]={0},
		*f=d,
		*g=e+(MEMORY_SIZE/2),
		*h[MAX_LOOPS],
		**i=h;
	unsigned j;
	FILE *k=stdin;
	FILE *l=stdout;

	c && fread(d, 1, sizeof d, c);
	c && c != k && fclose(c);

	for(j=0, f=d; *f; ++f)
		switch(*f)
		{
		case '+':        ++*g; break;
		case '-':        --*g; break;
		case '>':         ++g; break;
		case '<':         --g; break;
		case ',': *g=fgetc(k); break;
		case '.': fputc(*g,l); fflush(l); break;

		case '[':
			if(*g) *i++ = f-1;
			else while(*++f != ']' || j)
				j += (*f == '[') - (*f == ']');
			break;

		case ']': f = *--i; break;
		}

	putchar('\n');
	return !c;
}

