/* osdclang.c */
/* OSDClang interpreter */
/*
  Copyright (c) 2007, Stuart Coyle

  All rights reserved.

  Redistribution and use in source and binary forms, with or without modification,
  are permitted provided that the following conditions are met:

  * Redistributions of source code must retain the above copyright notice, 
  this list of conditions and the following disclaimer.
  * Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
  * Neither the name of Stuart Coyle nor the names of its contributors
  may be used to endorse or promote products derived from this software
  without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/
#include <stdio.h>
/* If this is undefined we will just treat invalid command codes as nop */
#define MAX_DATA_SIZE 4096
#define MAX_TEXT_SIZE 4096

/* The highly advanced memory model */
char text[MAX_TEXT_SIZE];
char data[MAX_DATA_SIZE];
int textsize;
char* pdata;
char* ptext;

/* Assign a number to a char - . = 0, ? = 1, ! = 2 */
int convert(char c){
	switch(c){
	case '.': return 0x00;
	case '?': return 0x01;
	case '!': return 0x02;
	default: 
		return(-1);
 	}
	return -1;
};

/* Get character but ignore spaces, tabs and newlines */
char osdngetc(FILE *f){
	char c;
	do{
		c=getc(f);
	}while(c == ' ' || c == '\t' || c == '\n');
	return c;
}

/* Slurp the program file into our expansive memory. */
int load(char *filename){

	FILE *f;
	char a,b;
	int p = 0;

	f = fopen(filename,"r");
	if(!f) {
		return(0);
	}
	while(((a = osdngetc(f))!= EOF)&& 
	      ((b = osdngetc(f))!= EOF)){
		text[p]=(convert(a)<<2)+convert(b);
		p++;
		if(p > MAX_TEXT_SIZE){
			return(0);
		}
	}
	return p;
};

/* Execute a single command. Oh! the POWER!! */
void docommand(void){
	switch (*ptext){		
	case 0:  *pdata += 1;
		break; 
	case 1:  pdata++;
		break; 
	case 2:  *pdata = getchar();
		break; 
	case 4:  pdata--;
		break; 
	case 6: if(*pdata){
		while(*(ptext++)!=6){};
	}
	        break; 
	case 8: putchar(*pdata);
		break; 
	case 9: if(!*pdata){
		while(*(ptext--)!=9){};
	}
		break; 
	case 10: *pdata -= 1;
		break;
 	default: 
		break;/* do nothing */
	};
};

int main(int argc, char **argv){
	char *filename;
	filename = argv[1];
      	textsize = load(filename);
	if(!textsize){
		return(1);
	}; 
	ptext = text;
	pdata = data;
	while(ptext < text+textsize){
		docommand();
		ptext++;
	}
	/* Being a Turing complete language this can't be guaranteed to 
	   ever return...ask Godel */
	return(0);
};

