#include <stdio.h>
#include <string.h>

/*
 *  CLAW Interpreter
 *  That Python-crazed mofo, David Hollis, forced me into commenting this
 *  shit out, so... I did.  As of... what's today...?  Oh, right... as of
 *  May 16th, 2003, CLAW's source code will be VERY commented... I know
 *  it's sloppy, but, fuck, it's like... almost 12... go to hell, you ass.
 *  Remember, with CLAW, an ASCII chart is your ONLY friend.
 */

int  p, r, q;
char a[5000], f[5000], b, o, *s=f, *d;

void interpret(char *c)
{
	char *d;

	r++;
	while( *c ) {
		//if(strchr("<>+-,.[]\n",*c))printf("%c",*c);
		switch(o=1,*c++) {
		case '<': p--;        break; // shift down one cell in the array
		case '>': p++;        break; // shift up one cell in the array
		case '+': a[p]++;     break; // increment the cell's value by 1
		case '-': a[p]--;     break; // decrememnt the cell's value by 1
		case '.': putchar(a[p]); fflush(stdout); break; // write the value at the cell to stdout
		case ',': a[p]=getchar();fflush(stdout); break; // input a value and write it to the cell
		case '[':
			for( b=1,d=c; b && *c; c++ ) // blah
				b+=*c=='[', b-=*c==']'; // blah
			if(!b) {
				c[-1]=0;
				while( a[p] )
					interpret(d); 
				c[-1]=']';
				break;
			}
		case ']':
			puts("UNBALANCED BRACKETS"), exit(0); // STOP YELLING!!  WAAAAAH!!!
		case '#': // if you find a hash mark (and option -d is on) then you get some...
			if(q>2 && strcmp(d,"-d")==1) // shitty "debugging"!
				printf("%2d %2d %2d %2d %2d %2d %2d %2d %2d %2d\n%*s\n",
				       *a,a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],3*p+2,"^");
			break;
		default: o=0;
		}
		if( p<0 || p>100)
			puts("RANGE ERROR"), exit(0); // STOP IT!!!
	}
	r--;
	//chkabort(); // don't ask, I don't know!
}

main(int argc,char *argv[]) {

	FILE *z;                             // oh, gee, what's a FILE?
	q=argc;                              // to make argc global, you fudgenugget
	d=argv[1];                           // to make argv[1] global, you dumbass
	
    if(q<2)                              // if the command line returns less than 2 arguments
        printf("usage: cint [input] [options]\noptions:\n -d ... for debugging\n\n"); // tell those bitches off
	else {                               // else!
	if( z = fopen(argv[1], "r") ) {      // open that fucking fuck of a file
		while( ( b = getc(z) ) > 0 )     // and while you can still get characters
			*s++=b;                      // add that damn character to the fucking buffer
		*s=0;                            // doo doo doo
		interpret(f);                    // interpret the fucking thing
	}
	else                                 // oh, yeah, if you can't open that file
        printf("error: file `%s' does not exist\n",argv[1]); // then bitch       
	}
}