aboutsummaryrefslogtreecommitdiff
path: root/cripp/cripp.cpp
blob: 0e193b0b2662c0be8a381fa60c18a30d0cf2cfa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <iostream>
#include <fstream>
#include <exception>
#include <cctype>
#include <gmpxx.h>

#undef RIPMODE
#undef DEBUGMODE

using namespace std;

typedef mpz_class ripint;

template <typename T>
int integral(T v){
	return (int)v;
}
template <>
int integral<mpz_class>(mpz_class v){
	return v.get_si();
}

template <typename T>
class Llist{
	struct Node{
		T value;
		Node *prev,*next;

		inline Node(void):prev(NULL),next(NULL){}
	};

	Node *first,*last;
	int sz;

public:
	inline Llist(void):first(NULL),last(NULL),sz(0){}
	inline ~Llist(void){
		Node *n;
		while(first){
			n=first->next;
			delete first;
			first=n;
		}
	}

	inline int size(void){return sz;}

	inline void push_back(T v){
		Node *n=new Node;
		swap(n->value,v);
		if(last==NULL)first=last=n;
		else {
			last->next=n;
			n->prev=last;
			last=n;
		}
		sz++;
	}
	inline void push_front(T v){
		Node *n=new Node;
		swap(n->value,v);
		if(first==NULL)first=last=n;
		else {
			first->prev=n;
			n->next=first;
			first=n;
		}
		sz++;
	}
	inline void pop_back(void){
		if(last==NULL)throw length_error("pop_back on empty Llist");
		Node *n=last;
		last=last->prev;
		if(last==NULL)first=NULL;
		else last->next=NULL;
		delete n;
		sz--;
	}
	inline void pop_front(void){
		if(first==NULL)throw length_error("pop_front on empty Llist");
		Node *n=first;
		first=first->next;
		if(first==NULL)last=NULL;
		else first->prev=NULL;
		delete n;
		sz--;
	}

	inline T& back(void){
		if(last==NULL)throw length_error("back on empty Llist");
		return last->value;
	}
	inline T& front(void){
		if(first==NULL)throw length_error("front on empty Llist");
		return first->value;
	}

	inline void* itemp(int i){
		if(i>=0){
			Node *n=first;
			while(i-->0)n=n->next;
			return n;
		} else {
			Node *n=last;
			while(++i<0)n=n->prev;
			return n;
		}
	}

	inline void* frontp(void){return first;}
	inline void* backp(void){return last;}

	inline void erase(void *p){
		Node *n=(Node*)p;
		if(n->prev)n->prev->next=n->next; else first=n->next;
		if(n->next)n->next->prev=n->prev; else last=n->prev;
		sz--;
		delete n;
	}
	inline void insertafter(void *after,T v){
		Node *aftern=(Node*)after;
		Node *n=new Node;
		swap(n->value,v);
		n->prev=aftern;
		if(aftern->next){
			n->next=aftern->next;
			aftern->next->prev=n;
			aftern->next=n;
		} else {
			aftern->next=n;
			last=n;
		}
		sz++;
	}

	static inline void* nextp(void *p){return ((Node*)p)->next;}
	static inline void* prevp(void *p){return ((Node*)p)->prev;}
	static inline T& valueof(void *p){return ((Node*)p)->value;}
};

#ifdef RIPMODE
void riperror(string){
	cerr<<"rip"<<endl;
	exit(1);
}
#else
void riperror(string s){
	throw runtime_error(s);
}
#endif

void buildjumpmap(char *code,int len,int *jumpmap){
	Llist<int> stack;
	int i;
	int iwidx=-1,fidx=-1,lastangleidx=-1;
	for(i=0;i<len;i++){
		if(isspace(code[i]))continue;
		if(code[i]=='['){
			stack.push_back(i);
			if(iwidx!=-1)jumpmap[iwidx]=i;
			if(lastangleidx!=-1)jumpmap[lastangleidx]=i;
		} else if(code[i]==']'){
			if(stack.size()==0)riperror("Unexpected closing ']'");
			const int start=stack.back();
			stack.pop_back();
			jumpmap[start]=i;
			jumpmap[i]=start;
		} else if(code[i]=='<'){
			if(fidx!=-1)jumpmap[fidx]=i;
			const int start=i;
			for(i++;i<len;i++)if(code[i]=='>')break;
			if(i==len)riperror("Unmatched open '<'");
			jumpmap[start]=i;
			lastangleidx=i; //the '>'
		} else if(code[i]=='>'){
			riperror("Unexpected closing '>'");
		} else if(code[i]=='W'||code[i]=='I'){
			iwidx=i;
		} else if(code[i]=='F'){
			fidx=i;
		}
		if(iwidx!=i)iwidx=-1;
		if(fidx!=i)fidx=-1;
		if(lastangleidx!=i)lastangleidx=-1;
	}
}

void runcode(const char *code,const int *jumpmap,Llist<ripint> &stack,const int startat=0){
	int i;
	for(i=startat;code[i];i++){
#ifdef DEBUGMODE
		cout<<'[';
		for(void *p=stack.frontp();p;p=Llist<ripint>::nextp(p)){
			cout<<Llist<ripint>::valueof(p)<<' ';
		}
		cout<<']'<<endl;
#endif
		if(code[i]>='0'&&code[i]<='9'){
			stack.push_back(code[i]-'0');
			continue;
		}
		switch(code[i]){
			case 'P':
				stack.pop_back();
				break;
			case 'S':{
				const ripint b=stack.back(); stack.pop_back();
				const ripint a=stack.back(); stack.pop_back();
				stack.push_back(b);
				stack.push_back(a);
				break;
			}
			case 'D':
				stack.push_back(stack.back());
				break;
			case 'i':
				stack.back()++;
				break;
			case 'd':
				stack.back()--;
				break;
			case 'r':{
				const int n=integral(stack.back()); stack.pop_back();
				const ripint value=stack.back(); stack.pop_back();
				if(n==stack.size()+1)stack.push_front(value);
				else {
					void *const before=stack.itemp(-n);
					stack.insertafter(before,value);
				}
				break;
			}
			case 'R':{
				const int n=integral(stack.back()); stack.pop_back();
				void *const p=stack.itemp(-n);
				stack.push_back(Llist<ripint>::valueof(p));
				stack.erase(p);
				break;
			}
			case 'l':
				stack.push_back(stack.size());
				break;
			case 'a': case 's': case 'm': case 'q': case 'G': case 'L': case 'E':{
				const ripint b=stack.back(); stack.pop_back();
				const ripint a=stack.back(); stack.pop_back();
				switch(code[i]){
					case 'a': stack.push_back(a+b); break;
					case 's': stack.push_back(a-b); break;
					case 'm': stack.push_back(a*b); break;
					case 'q': stack.push_back(a/b); break;
					case 'G': stack.push_back(a>b); break;
					case 'L': stack.push_back(a<b); break;
					case 'E': stack.push_back(a==b); break;
				}
				break;
			}
			case 'n':
				stack.back()=!integral(stack.back());
				break;
			case 'I': {
				if(jumpmap[i]==0)riperror("No code block after 'I'");
				const int cond=integral(stack.back()); stack.pop_back();
				if(cond){
					runcode(code,jumpmap,stack,jumpmap[i]+1);
				}
				i=jumpmap[jumpmap[i]];
				break;
			}
			case 'W': {
				if(jumpmap[i]==0)riperror("No code block after 'W'");
				int cond;
				while(true){
					cond=integral(stack.back()); stack.pop_back();
					//cerr<<"whiling on "<<string(code+i-2,5)<<"; cond="<<cond<<endl;
					if(!cond)break;
					runcode(code,jumpmap,stack,jumpmap[i]+1);
				}
				i=jumpmap[jumpmap[i]];
				break;
			}
			case 'o':
				cout<<(char)integral(stack.back());
				stack.pop_back();
				break;
			case 'O':
				cout<<stack.back();
				stack.pop_back();
				break;
			case 'g':{
				char c;
				cin>>c;
				stack.push_back((int)c);
				break;
			}
			case '$':
				for(void *p=stack.frontp();p;p=Llist<ripint>::nextp(p)){
					cout<<Llist<ripint>::valueof(p)<<' ';
				}
				cout<<endl;
				break;
			case ']':
				return;
		}
	}
}

int main(int argc,char **argv){
	if(argc!=2){
		cout<<"Pass a rip file as the command-line argument"<<endl;
		return 1;
	}
	ifstream codef(argv[1]);
	codef.seekg(0,ios::end);
	const size_t len=codef.tellg();
	codef.seekg(0);
	char *code=new char[len+1];
	codef.read(code,len);
	code[len]='\0';
	codef.close();

	int *jumpmap=new int[len]();
	buildjumpmap(code,len,jumpmap);

	Llist<ripint> stack;
	runcode(code,jumpmap,stack);

	delete[] jumpmap;
	delete[] code;
}