Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<climits>
#include<string>
#include<set>
#include<map>
using namespace std;
#define rep(i,n) for(int i=0;i<((int)(n));i++)
#define reg(i,a,b) for(int i=((int)(a));i<=((int)(b));i++)
#define irep(i,n) for(int i=((int)(n))-1;i>=0;i--)
#define ireg(i,a,b) for(int i=((int)(b));i>=((int)(a));i--)
typedef long long int lli;
typedef pair<int,int> mp;
#define fir first
#define sec second
#define IINF INT_MAX
#define LINF LLONG_MAX
struct list{
struct node{
char a;
node *bef,*nxt;
node(){
a=0;
bef=nxt=NULL;
}
};
node* no;
list(){
no=new node();
}
int read(){
return no->a;
}
void write(char p){
no->a=p;
}
void inc(){
(no->a)++;
}
void dec(){
(no->a)--;
}
void movenxt(){
if(no->nxt==NULL){
no->nxt=new node();
no->nxt->bef=no;
}
no=no->nxt;
}
void movebef(){
if(no->bef==NULL){
no->bef=new node();
no->bef->nxt=no;
}
no=no->bef;
}
string tostr(){
string res="[";
node* tn=no;
while(tn->bef!=NULL)tn=tn->bef;
while(tn!=NULL){
if(tn==no)res+="(";
char buf[8];
sprintf(buf,"%d",tn->a);
res+=string(buf);
if(tn==no)res+=")";
res+=",";
tn=tn->nxt;
}
res+="]";
return res;
}
};
struct interpriter{
string s; //ソースコード
vector<int> braket; //かぎかっこの対応をつけておく
int ip; //インストラクションポインタ
list* arr; //操作する配列
interpriter(string is){
s=is;
braket.resize(s.size());
vector<int> ps;
rep(i,s.size()){
char c=s[i];
if(c=='[')ps.push_back(i);
else if(c==']'){
if(ps.size()==0){
fprintf(stderr,"かっこ不足。\n");
exit(0);
}
int tk=ps.back();
ps.pop_back();
braket[tk]=i;
braket[i]=tk;
}
}
if(ps.size()!=0){
fprintf(stderr,"かっこ不足。\n");
exit(0);
}
fprintf(stderr,"inited\n");
}
void run(){
arr=new list();
int p=0,ls=s.size(),sn=0;
while(p!=ls){
sn++;
char c=s[p];
//printf("%d .. %d %c ",sn,p,c);
switch(c){
case '+':
arr->inc();
break;
case '-':
arr->dec();
break;
case '>':
arr->movenxt();
break;
case '<':
arr->movebef();
break;
case ',':
{
char r;
for(;;){
r=getchar();
if(r!='\n')break;
}
arr->write(r);
}
break;
case '.':
printf("%c",arr->read());
break;
case '[':
//printf("no .. %d\n",arr->read());
if(arr->read()==0){
p=braket[p];
}
break;
case ']':
p=braket[p]-1;
break;
case '#':
fprintf(stderr,"%s\n",arr->tostr().c_str());
break;
default:
break;
}
//getchar();
p++;
}
fprintf(stderr,"\nhalt\n");
fprintf(stderr,"%s\n",arr->tostr().c_str());
}
};
char s[100000];
#include<iostream>
#include<fstream>
#include <sys/time.h>
double getdtime(void){
timeval tv;
gettimeofday(&tv, NULL);
//clock_t nt = clock();
double res = ((double)(tv.tv_sec) + (double)(tv.tv_usec) * 0.001 * 0.001);
//printf("nt .. %f\n",res);
return res;
}
int main(int argc,char** argv){
if(argc<=1){
cerr << "usage: bf_interpriter.exe [sourcecode]" << endl;
return 0;
}
ifstream str(argv[1],ios::in);
if( !str ) {
cerr << "cannot open source code." << endl;
return 0;
}
string s="";
while(!str.eof()){
string ns;
str >> ns;
s+=ns;
}
interpriter p(s);
double start_t = getdtime();
p.run();
fprintf(stderr,"runtime .. %lf\n",getdtime() - start_t);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment