All pastes #338511 Raw Edit

Untitled

public text v1 · immutable
·published 2007-02-03 14:40 UTC
rendered paste body
#include<iostream>
#include<string>
#include<stack>
#include<windows.h>
#include<list>
using namespace std;

class bfentry
{
public:
	long oper,parama,paramb,dummy;
	bfentry(){}
	bfentry(long o,long pa,long pb=0){oper=o;parama=pa;paramb=pb;}
	bfentry(bfentry &other){oper=other.oper;parama=other.parama;paramb=other.paramb;}
	bfentry& operator=(bfentry &other){oper=other.oper;parama=other.parama;paramb=other.paramb;return *this;}
};
string operchars="+-><[],.?";
void bfinc(const char *&ptr)
{
	while(operchars.find(*ptr,0)==-1&&*ptr)*ptr++;
	if(*ptr!='[')ptr++;
	else
	{
		long opens=1;ptr++;
		while(opens)
		{
			if(*ptr=='[')opens++;
			if(*ptr==']')opens--;
			ptr++;
		}
	}
}
void bffastinc(const char *&ptr)
{
	while(operchars.find(*ptr,0)==-1&&*ptr)*ptr++;
	if(*ptr!='[')
	{
		if(*ptr=='+'||*ptr=='-')
			while(*ptr=='+'||*ptr=='-')
				ptr++;
		else if(*ptr=='>'||*ptr=='<')
			while(*ptr=='>'||*ptr=='<')
				ptr++;
		else if(*ptr=='.'||*ptr==','||*ptr=='?')ptr++;
	}
	else
	{
		long opens=1;ptr++;
		while(opens)
		{
			if(*ptr=='[')opens++;
			if(*ptr==']')opens--;
			ptr++;
		}
	}
}
enum opercodes{ADD,MOVE,IO,UPJUMP,DOWNJUMP,END};
enum iocodes{PRINT,INPUT,DEBUG};
bfentry* parse(const char *codearr,bfentry retaddr)
{
	// length of this level, must later on also take into account that ++++++++++=(+10)
	long length=0;
	const char *tmpcode=codearr;
	while(*tmpcode&&*tmpcode!=']')
	{
		length++;
		bffastinc(tmpcode);
	}
	// allocate memory for this level
	bfentry *levcode=new bfentry[length+1];
	long icode=0;
	tmpcode=codearr;
	while(icode<length)
	{
		if(*tmpcode=='+'||*tmpcode=='-')
		{
			levcode[icode].oper=ADD;
			levcode[icode].parama=0;
			while(*tmpcode=='+'||*tmpcode=='-')
			{
				if(*tmpcode=='+')levcode[icode].parama++;
				else if(*tmpcode=='-')levcode[icode].parama--;
				tmpcode++;
			}
		}
		else if(*tmpcode=='>'||*tmpcode=='<')
		{
			levcode[icode].oper=MOVE;
			levcode[icode].parama=0;
			while(*tmpcode=='>'||*tmpcode=='<')
			{
				if(*tmpcode=='>')levcode[icode].parama++;
				else if(*tmpcode=='<')levcode[icode].parama--;
				tmpcode++;
			}
		}
		else if(*tmpcode=='.')
		{
			levcode[icode].oper=IO;
			levcode[icode].parama=PRINT;
			bfinc(tmpcode);
		}
		else if(*tmpcode=='[')
		{
			levcode[icode].oper=UPJUMP;
			levcode[icode].parama=(long)parse(tmpcode+1,bfentry(DOWNJUMP,(long)&levcode[icode]));
			bfinc(tmpcode);
		}
		icode++;		
	}
	levcode[icode]=retaddr;
	levcode[icode].paramb=(long)levcode;
	return levcode;
}
void run3(string code)
{
	long runcount=0;
	char *mem=new char[30000];
	for(int i=0;i<30000;i++)mem[i]=0;
	bfentry *bfcode=parse(code.c_str(),bfentry(END,0));
	while(bfcode->oper!=END)
	{
		runcount++;
		switch(bfcode->oper)
		{
		case(ADD):*mem+=bfcode->parama;break;
		case(MOVE):mem+=bfcode->parama;break;
		case(IO):
			{
				if(bfcode->parama==PRINT)cout<<*(char*)mem;
			}break;
		case(UPJUMP):
			{
				if(*mem){bfcode=(bfentry*)bfcode->parama;continue;}
			}break;
		case(DOWNJUMP):
			{
				if(!*mem)bfcode=(bfentry*)bfcode->parama;
				else{bfcode=(bfentry*)bfcode->paramb;continue;}
			}break;
		}
		bfcode++;
	}
	cout<<"\n"<<runcount<<" cycles"<<"\n";
}

int main(int nargs,char **args)
{
	long /*start=GetTickCount();
	run2(
		// Test calculation, 0..10:0...10:0...10:0...10:0...10
		"++++++++++[>++++++++++[>++++++++++[>++++++++++[>++++++++++[>++++++++++[>++++++++++[-]<-]<-]<-]<-]<-]<-]"
		// End
		//"++++++++[->++++++++<]>[->+>+>+<<<]>[->>+<<]>+++++.>------------------.----------."
	);
	cout<<"\n"<<GetTickCount()-start<<"\n";*/
	start=GetTickCount();
	run3(
		
		//"++++++++++[>++++++++++<-]>."
		// Test calculation, 0..10:0...10:0...10:0...10:0...10
		"+++++++++++++[>+++++++++++++[>+++++++++++++[>+++++++++++++[>+++++++++++++[>+++++++++++++[>+++++++++++++[-]<-]<-]<-]<-]<-]<-]"
		// End
		//"++++++++[->++++++++<]>[->+>+>+<<<]>[->>+<<]>+++++.>------------------.----------."
		
		/*"->+>+++>>+>++>+>+++>>+>++>>>+>+>+>++>+>>>>+++>+>>++>+>+++>>++>++>>+>>+>++>++>+>>>>+++>"
		"+>>>>++>++>>>>+>>++>+>+++>>>++>>++++++>>+>>++>+>>>>+++>>+++++>>+>+++>>>++>>++>>+>>++>+"
		">+++>>>++>>+++++++++++++>>+>>++>+>+++>+>+++>>>++>>++++>>+>>++>+>>>>+++>>+++++>>>>++>>>"
		">+>+>++>>+++>+>>>>+++>+>>>>+++>+>>>>+++>>++>++>+>+++>+>++>++>>>>>>++>+>+++>>>>>+++>>>+"
		"+>+>+++>+>+>++>>>>>>++>>>+>>>++>+>>>>+++>+>>>+>>++>+>++++++++++++++++++>>>>+>+>>>+>>++"
		">+>+++>>>++>>++++++++>>+>>++>+>>>>+++>>++++++>>>+>++>>+++>+>+>++>+>+++>>>>>+++>>>+>+>>"
		"++>+>+++>>>++>>++++++++>>+>>++>+>>>>+++>>++++>>+>+++>>>>>>++>+>+++>>+>++>>>>+>+>++>+>>"
		">>+++>>+++>>>+[[->>+<<]<+]+++++[->+++++++++<]>.[+]>>[<<+++++++[->+++++++++<]>-.-------"
		"------------>-[-<.<+>>]<[+]<+>>>]<<<[-[-[-[>>+<++++++[->+++++<]]>++++++++++++++<]>+++<"
		"]++++++[->+++++++<]>+<<<-[->>>++<<<]>[->>.<<]<<]"*/

	);
	cout<<GetTickCount()-start<<"\n";
	return 0;
}